
Public Member Functions | |
| CI_DB_driver ($params) | |
| Constructor. | |
| initialize () | |
| Initialize Database Settings. | |
| db_set_charset ($charset, $collation) | |
| Set client character set. | |
| platform () | |
| The name of the platform in use (mysql, mssql, etc. | |
| version () | |
| Database Version Number. | |
| query ($sql, $binds=FALSE, $return_object=TRUE) | |
| Execute the query. | |
| load_rdriver () | |
| Load the result drivers. | |
| simple_query ($sql) | |
| Simple Query This is a simplified version of the query() function. | |
| trans_off () | |
| Disable Transactions This permits transactions to be disabled at run-time. | |
| trans_strict ($mode=TRUE) | |
| Enable/disable Transaction Strict Mode When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back. | |
| trans_start ($test_mode=FALSE) | |
| Start Transaction. | |
| trans_complete () | |
| Complete Transaction. | |
| trans_status () | |
| Lets you retrieve the transaction flag to determine if it has failed. | |
| compile_binds ($sql, $binds) | |
| Compile Bindings. | |
| is_write_type ($sql) | |
| Determines if a query is a "write" type. | |
| elapsed_time ($decimals=6) | |
| Calculate the aggregate query elapsed time. | |
| total_queries () | |
| Returns the total number of queries. | |
| last_query () | |
| Returns the last query that was executed. | |
| escape ($str) | |
| "Smart" Escape String | |
| primary ($table= '') | |
| Primary. | |
| list_tables ($constrain_by_prefix=FALSE) | |
| Returns an array of table names. | |
| table_exists ($table_name) | |
| Determine if a particular table exists public. | |
| list_fields ($table= '') | |
| Fetch MySQL Field Names. | |
| field_exists ($field_name, $table_name) | |
| Determine if a particular field exists public. | |
| field_data ($table= '') | |
| Returns an object with field data. | |
| insert_string ($table, $data) | |
| Generate an insert string. | |
| update_string ($table, $data, $where) | |
| Generate an update string. | |
| _has_operator ($str) | |
| Tests whether the string has an SQL operator. | |
| call_function ($function) | |
| Enables a native PHP function to be run, using a platform agnostic wrapper. | |
| cache_set_path ($path= '') | |
| Set Cache Directory Path. | |
| cache_on () | |
| Enable Query Caching. | |
| cache_off () | |
| Disable Query Caching. | |
| cache_delete ($segment_one= '', $segment_two= '') | |
| Delete the cache files associated with a particular URI. | |
| cache_delete_all () | |
| Delete All cache files. | |
| _cache_init () | |
| Initialize the Cache Class. | |
| close () | |
| Close DB Connection. | |
| display_error ($error= '', $swap= '', $native=FALSE) | |
| Display an error message. | |
| protect_identifiers ($item, $prefix_single=FALSE) | |
| Protect Identifiers. | |
| _protect_identifiers ($item, $prefix_single=FALSE, $protect_identifiers=NULL, $field_exists=TRUE) | |
| Protect Identifiers. | |
Public Attributes | |
| $username | |
| $password | |
| $hostname | |
| $database | |
| $dbdriver = 'mysql' | |
| $dbprefix = '' | |
| $char_set = 'utf8' | |
| $dbcollat = 'utf8_general_ci' | |
| $autoinit = TRUE | |
| $swap_pre = '' | |
| $port = '' | |
| $pconnect = FALSE | |
| $conn_id = FALSE | |
| $result_id = FALSE | |
| $db_debug = FALSE | |
| $benchmark = 0 | |
| $query_count = 0 | |
| $bind_marker = '?' | |
| $save_queries = TRUE | |
| $queries = array() | |
| $query_times = array() | |
| $data_cache = array() | |
| $trans_enabled = TRUE | |
| $trans_strict = TRUE | |
| $_trans_depth = 0 | |
| $_trans_status = TRUE | |
| $cache_on = FALSE | |
| $cachedir = '' | |
| $cache_autodel = FALSE | |
| $CACHE | |
| $_protect_identifiers = TRUE | |
| $_reserved_identifiers = array('*') | |
| $stmt_id | |
| $curs_id | |
| $limit_used | |
Definition at line 31 of file DB_driver.php.
| CI_DB_driver::_cache_init | ( | ) |
Initialize the Cache Class.
private
Definition at line 1083 of file DB_driver.php.
References cache_off().
Referenced by cache_delete(), cache_delete_all(), and query().
01084 { 01085 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache')) 01086 { 01087 return TRUE; 01088 } 01089 01090 if ( ! @include(BASEPATH.'database/DB_cache'.EXT)) 01091 { 01092 return $this->cache_off(); 01093 } 01094 01095 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects 01096 return TRUE; 01097 }


| CI_DB_driver::_has_operator | ( | $ | str | ) |
Tests whether the string has an SQL operator.
private
| string |
Definition at line 951 of file DB_driver.php.
Referenced by CI_DB_active_record::_having(), CI_DB_active_record::_where(), and update_string().
00952 { 00953 $str = trim($str); 00954 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str)) 00955 { 00956 return FALSE; 00957 } 00958 00959 return TRUE; 00960 }

| CI_DB_driver::_protect_identifiers | ( | $ | item, | |
| $ | prefix_single = FALSE, |
|||
| $ | protect_identifiers = NULL, |
|||
| $ | field_exists = TRUE | |||
| ) |
Protect Identifiers.
This function is used extensively by the Active Record class, and by a couple functions in this class. It takes a column or table name (optionally with an alias) and inserts the table prefix onto it. Some logic is necessary in order to deal with column names that include the path. Consider a query like this:
SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
Or a query with aliasing:
SELECT m.member_id, m.member_name FROM members AS m
Since the column name can include up to four segments (host, DB, table, column) or also have an alias prefix, we need to do a bit of work to figure this out and insert the table prefix (if it exists) in the proper position, and escape only the correct identifiers.
private
| string | ||
| bool | ||
| mixed | ||
| bool |
Definition at line 1193 of file DB_driver.php.
Referenced by CI_DB_active_record::_compile_select(), CI_DB_active_record::_having(), CI_DB_active_record::_like(), CI_DB_active_record::_max_min_avg_sum(), CI_DB_active_record::_merge_cache(), CI_DB_active_record::_where(), CI_DB_active_record::_where_in(), CI_DB_active_record::count_all_results(), CI_DB_active_record::delete(), CI_DB_active_record::empty_table(), field_data(), CI_DB_active_record::from(), CI_DB_active_record::group_by(), CI_DB_active_record::insert(), insert_string(), CI_DB_active_record::join(), list_fields(), CI_DB_active_record::order_by(), protect_identifiers(), CI_DB_active_record::select(), CI_DB_active_record::set(), table_exists(), CI_DB_active_record::truncate(), CI_DB_active_record::update(), and update_string().
01194 { 01195 if ( ! is_bool($protect_identifiers)) 01196 { 01197 $protect_identifiers = $this->_protect_identifiers; 01198 } 01199 01200 // Convert tabs or multiple spaces into single spaces 01201 $item = preg_replace('/[\t| ]+/', ' ', $item); 01202 01203 // If the item has an alias declaration we remove it and set it aside. 01204 // Basically we remove everything to the right of the first space 01205 $alias = ''; 01206 if (strpos($item, ' ') !== FALSE) 01207 { 01208 $alias = strstr($item, " "); 01209 $item = substr($item, 0, - strlen($alias)); 01210 } 01211 01212 // Break the string apart if it contains periods, then insert the table prefix 01213 // in the correct location, assuming the period doesn't indicate that we're dealing 01214 // with an alias. While we're at it, we will escape the components 01215 if (strpos($item, '.') !== FALSE) 01216 { 01217 $parts = explode('.', $item); 01218 01219 // Does the first segment of the exploded item match 01220 // one of the aliases previously identified? If so, 01221 // we have nothing more to do other then escape the item 01222 if (in_array($parts[0], $this->ar_aliased_tables)) 01223 { 01224 if ($protect_identifiers === TRUE) 01225 { 01226 foreach ($parts as $key => $val) 01227 { 01228 if ( ! in_array($val, $this->_reserved_identifiers)) 01229 { 01230 $parts[$key] = $this->_escape_identifiers($val); 01231 } 01232 } 01233 01234 $item = implode('.', $parts); 01235 } 01236 return $item.$alias; 01237 } 01238 01239 // Is there a table prefix defined in the config file? If not, no need to do anything 01240 if ($this->dbprefix != '') 01241 { 01242 // We now add the table prefix based on some logic. 01243 // Do we have 4 segments (hostname.database.table.column)? 01244 // If so, we add the table prefix to the column name in the 3rd segment. 01245 if (isset($parts[3])) 01246 { 01247 $i = 2; 01248 } 01249 // Do we have 3 segments (database.table.column)? 01250 // If so, we add the table prefix to the column name in 2nd position 01251 elseif (isset($parts[2])) 01252 { 01253 $i = 1; 01254 } 01255 // Do we have 2 segments (table.column)? 01256 // If so, we add the table prefix to the column name in 1st segment 01257 else 01258 { 01259 $i = 0; 01260 } 01261 01262 // This flag is set when the supplied $item does not contain a field name. 01263 // This can happen when this function is being called from a JOIN. 01264 if ($field_exists == FALSE) 01265 { 01266 $i++; 01267 } 01268 01269 // We only add the table prefix if it does not already exist 01270 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix) 01271 { 01272 $parts[$i] = $this->dbprefix.$parts[$i]; 01273 } 01274 01275 // Put the parts back together 01276 $item = implode('.', $parts); 01277 } 01278 01279 if ($protect_identifiers === TRUE) 01280 { 01281 $item = $this->_escape_identifiers($item); 01282 } 01283 01284 return $item.$alias; 01285 } 01286 01287 // This is basically a bug fix for queries that use MAX, MIN, etc. 01288 // If a parenthesis is found we know that we do not need to 01289 // escape the data or add a prefix. There's probably a more graceful 01290 // way to deal with this, but I'm not thinking of it -- Rick 01291 if (strpos($item, '(') !== FALSE) 01292 { 01293 return $item.$alias; 01294 } 01295 01296 // Is there a table prefix? If not, no need to insert it 01297 if ($this->dbprefix != '') 01298 { 01299 // Do we prefix an item with no segments? 01300 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix) 01301 { 01302 $item = $this->dbprefix.$item; 01303 } 01304 } 01305 01306 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers)) 01307 { 01308 $item = $this->_escape_identifiers($item); 01309 } 01310 01311 return $item.$alias; 01312 }

| CI_DB_driver::cache_delete | ( | $ | segment_one = '', |
|
| $ | segment_two = '' | |||
| ) |
Delete the cache files associated with a particular URI.
public
Definition at line 1048 of file DB_driver.php.
References _cache_init().
01049 { 01050 if ( ! $this->_cache_init()) 01051 { 01052 return FALSE; 01053 } 01054 return $this->CACHE->delete($segment_one, $segment_two); 01055 }

| CI_DB_driver::cache_delete_all | ( | ) |
Delete All cache files.
public
Definition at line 1065 of file DB_driver.php.
References _cache_init().
01066 { 01067 if ( ! $this->_cache_init()) 01068 { 01069 return FALSE; 01070 } 01071 01072 return $this->CACHE->delete_all(); 01073 }

| CI_DB_driver::cache_off | ( | ) |
Disable Query Caching.
public
Definition at line 1033 of file DB_driver.php.
References cache_on().
Referenced by _cache_init().
01034 { 01035 $this->cache_on = FALSE; 01036 return FALSE; 01037 }


| CI_DB_driver::cache_on | ( | ) |
Enable Query Caching.
public
Definition at line 1019 of file DB_driver.php.
Referenced by cache_off(), and query().
01020 { 01021 $this->cache_on = TRUE; 01022 return TRUE; 01023 }

| CI_DB_driver::cache_set_path | ( | $ | path = '' |
) |
Set Cache Directory Path.
public
| string | the path to the cache directory |
Definition at line 1006 of file DB_driver.php.
| CI_DB_driver::call_function | ( | $ | function | ) |
Enables a native PHP function to be run, using a platform agnostic wrapper.
public
| string | the function name | |
| mixed | any parameters needed by the function |
Definition at line 972 of file DB_driver.php.
References display_error(), and if.
00973 { 00974 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_'; 00975 00976 if (FALSE === strpos($driver, $function)) 00977 { 00978 $function = $driver.$function; 00979 } 00980 00981 if ( ! function_exists($function)) 00982 { 00983 if ($this->db_debug) 00984 { 00985 return $this->display_error('db_unsupported_function'); 00986 } 00987 return FALSE; 00988 } 00989 else 00990 { 00991 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null; 00992 00993 return call_user_func_array($function, $args); 00994 } 00995 }

| CI_DB_driver::CI_DB_driver | ( | $ | params | ) |
Constructor.
Accepts one parameter containing the database connection settings.
| array |
Definition at line 81 of file DB_driver.php.
References log_message().
00082 { 00083 if (is_array($params)) 00084 { 00085 foreach ($params as $key => $val) 00086 { 00087 $this->$key = $val; 00088 } 00089 } 00090 00091 log_message('debug', 'Database Driver Class Initialized'); 00092 }

| CI_DB_driver::close | ( | ) |
Close DB Connection.
public
Definition at line 1107 of file DB_driver.php.
01108 { 01109 if (is_resource($this->conn_id) OR is_object($this->conn_id)) 01110 { 01111 $this->_close($this->conn_id); 01112 } 01113 $this->conn_id = FALSE; 01114 }
| CI_DB_driver::compile_binds | ( | $ | sql, | |
| $ | binds | |||
| ) |
Compile Bindings.
public
| string | the sql statement | |
| array | an array of bind data |
Definition at line 572 of file DB_driver.php.
References escape().
Referenced by query().
00573 { 00574 if (strpos($sql, $this->bind_marker) === FALSE) 00575 { 00576 return $sql; 00577 } 00578 00579 if ( ! is_array($binds)) 00580 { 00581 $binds = array($binds); 00582 } 00583 00584 // Get the sql segments around the bind markers 00585 $segments = explode($this->bind_marker, $sql); 00586 00587 // The count of bind should be 1 less then the count of segments 00588 // If there are more bind arguments trim it down 00589 if (count($binds) >= count($segments)) { 00590 $binds = array_slice($binds, 0, count($segments)-1); 00591 } 00592 00593 // Construct the binded query 00594 $result = $segments[0]; 00595 $i = 0; 00596 foreach ($binds as $bind) 00597 { 00598 $result .= $this->escape($bind); 00599 $result .= $segments[++$i]; 00600 } 00601 00602 return $result; 00603 }


| CI_DB_driver::db_set_charset | ( | $ | charset, | |
| $ | collation | |||
| ) |
Set client character set.
public
| string | ||
| string |
Definition at line 169 of file DB_driver.php.
References display_error(), and log_message().
Referenced by initialize().
00170 { 00171 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat)) 00172 { 00173 log_message('error', 'Unable to set database connection charset: '.$this->char_set); 00174 00175 if ($this->db_debug) 00176 { 00177 $this->display_error('db_unable_to_set_charset', $this->char_set); 00178 } 00179 00180 return FALSE; 00181 } 00182 00183 return TRUE; 00184 }


| CI_DB_driver::display_error | ( | $ | error = '', |
|
| $ | swap = '', |
|||
| $ | native = FALSE | |||
| ) |
Display an error message.
public
| string | the error message | |
| string | any "swap" values | |
| boolean | whether to localize the message |
Definition at line 1127 of file DB_driver.php.
References $LANG, and load_class().
Referenced by CI_DB_active_record::_max_min_avg_sum(), call_function(), db_set_charset(), CI_DB_active_record::dbprefix(), CI_DB_active_record::delete(), CI_DB_active_record::empty_table(), field_data(), initialize(), CI_DB_active_record::insert(), list_fields(), list_tables(), query(), CI_DB_active_record::truncate(), CI_DB_active_record::update(), and version().
01128 { 01129 $LANG =& load_class('Language'); 01130 $LANG->load('db'); 01131 01132 $heading = $LANG->line('db_error_heading'); 01133 01134 if ($native == TRUE) 01135 { 01136 $message = $error; 01137 } 01138 else 01139 { 01140 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error; 01141 } 01142 01143 $error =& load_class('Exceptions'); 01144 echo $error->show_error($heading, $message, 'error_db'); 01145 exit; 01146 }


| CI_DB_driver::elapsed_time | ( | $ | decimals = 6 |
) |
Calculate the aggregate query elapsed time.
public
| integer | The number of decimal places |
Definition at line 632 of file DB_driver.php.
| CI_DB_driver::escape | ( | $ | str | ) |
"Smart" Escape String
Escapes data based on type Sets boolean and null types
public
| string |
Definition at line 675 of file DB_driver.php.
Referenced by CI_DB_active_record::_where_in(), compile_binds(), insert_string(), CI_DB_active_record::set(), and update_string().
00676 { 00677 switch (gettype($str)) 00678 { 00679 case 'string' : $str = "'".$this->escape_str($str)."'"; 00680 break; 00681 case 'boolean' : $str = ($str === FALSE) ? 0 : 1; 00682 break; 00683 default : $str = ($str === NULL) ? 'NULL' : $str; 00684 break; 00685 } 00686 00687 return $str; 00688 }

| CI_DB_driver::field_data | ( | $ | table = '' |
) |
Returns an object with field data.
public
| string | the table name |
Definition at line 850 of file DB_driver.php.
References _protect_identifiers(), display_error(), and query().
00851 { 00852 if ($table == '') 00853 { 00854 if ($this->db_debug) 00855 { 00856 return $this->display_error('db_field_param_missing'); 00857 } 00858 return FALSE; 00859 } 00860 00861 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE))); 00862 00863 return $query->field_data(); 00864 }

| CI_DB_driver::field_exists | ( | $ | field_name, | |
| $ | table_name | |||
| ) |
Determine if a particular field exists public.
| string | ||
| string |
Definition at line 836 of file DB_driver.php.
References list_fields().
00837 { 00838 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; 00839 }

| CI_DB_driver::initialize | ( | ) |
Initialize Database Settings.
private Called by the constructor
| mixed |
Definition at line 103 of file DB_driver.php.
References db_set_charset(), display_error(), and log_message().
Referenced by simple_query().
00104 { 00105 // If an existing connection resource is available 00106 // there is no need to connect and select the database 00107 if (is_resource($this->conn_id) OR is_object($this->conn_id)) 00108 { 00109 return TRUE; 00110 } 00111 00112 // ---------------------------------------------------------------- 00113 00114 // Connect to the database and set the connection ID 00115 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect(); 00116 00117 // No connection resource? Throw an error 00118 if ( ! $this->conn_id) 00119 { 00120 log_message('error', 'Unable to connect to the database'); 00121 00122 if ($this->db_debug) 00123 { 00124 $this->display_error('db_unable_to_connect'); 00125 } 00126 return FALSE; 00127 } 00128 00129 // ---------------------------------------------------------------- 00130 00131 // Select the DB... assuming a database name is specified in the config file 00132 if ($this->database != '') 00133 { 00134 if ( ! $this->db_select()) 00135 { 00136 log_message('error', 'Unable to select database: '.$this->database); 00137 00138 if ($this->db_debug) 00139 { 00140 $this->display_error('db_unable_to_select', $this->database); 00141 } 00142 return FALSE; 00143 } 00144 else 00145 { 00146 // We've selected the DB. Now we set the character set 00147 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat)) 00148 { 00149 return FALSE; 00150 } 00151 00152 return TRUE; 00153 } 00154 } 00155 00156 return TRUE; 00157 }


| CI_DB_driver::insert_string | ( | $ | table, | |
| $ | data | |||
| ) |
Generate an insert string.
public
| string | the table upon which the query will be performed | |
| array | an associative array data of key/values |
Definition at line 876 of file DB_driver.php.
References _protect_identifiers(), and escape().
00877 { 00878 $fields = array(); 00879 $values = array(); 00880 00881 foreach($data as $key => $val) 00882 { 00883 $fields[] = $this->_escape_identifiers($key); 00884 $values[] = $this->escape($val); 00885 } 00886 00887 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values); 00888 }

| CI_DB_driver::is_write_type | ( | $ | sql | ) |
Determines if a query is a "write" type.
public
| string | An SQL query string |
Definition at line 614 of file DB_driver.php.
Referenced by query().
00615 { 00616 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql)) 00617 { 00618 return FALSE; 00619 } 00620 return TRUE; 00621 }

| CI_DB_driver::last_query | ( | ) |
Returns the last query that was executed.
public
Definition at line 658 of file DB_driver.php.
| CI_DB_driver::list_fields | ( | $ | table = '' |
) |
Fetch MySQL Field Names.
public
| string | the table name |
Definition at line 782 of file DB_driver.php.
References _protect_identifiers(), display_error(), and query().
Referenced by field_exists(), and primary().
00783 { 00784 // Is there a cached result? 00785 if (isset($this->data_cache['field_names'][$table])) 00786 { 00787 return $this->data_cache['field_names'][$table]; 00788 } 00789 00790 if ($table == '') 00791 { 00792 if ($this->db_debug) 00793 { 00794 return $this->display_error('db_field_param_missing'); 00795 } 00796 return FALSE; 00797 } 00798 00799 if (FALSE === ($sql = $this->_list_columns($this->_protect_identifiers($table, TRUE, NULL, FALSE)))) 00800 { 00801 if ($this->db_debug) 00802 { 00803 return $this->display_error('db_unsupported_function'); 00804 } 00805 return FALSE; 00806 } 00807 00808 $query = $this->query($sql); 00809 00810 $retval = array(); 00811 foreach($query->result_array() as $row) 00812 { 00813 if (isset($row['COLUMN_NAME'])) 00814 { 00815 $retval[] = $row['COLUMN_NAME']; 00816 } 00817 else 00818 { 00819 $retval[] = current($row); 00820 } 00821 } 00822 00823 $this->data_cache['field_names'][$table] = $retval; 00824 return $this->data_cache['field_names'][$table]; 00825 }


| CI_DB_driver::list_tables | ( | $ | constrain_by_prefix = FALSE |
) |
Returns an array of table names.
public
Definition at line 722 of file DB_driver.php.
References display_error(), and query().
Referenced by table_exists().
00723 { 00724 // Is there a cached result? 00725 if (isset($this->data_cache['table_names'])) 00726 { 00727 return $this->data_cache['table_names']; 00728 } 00729 00730 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix))) 00731 { 00732 if ($this->db_debug) 00733 { 00734 return $this->display_error('db_unsupported_function'); 00735 } 00736 return FALSE; 00737 } 00738 00739 $retval = array(); 00740 $query = $this->query($sql); 00741 00742 if ($query->num_rows() > 0) 00743 { 00744 foreach($query->result_array() as $row) 00745 { 00746 if (isset($row['TABLE_NAME'])) 00747 { 00748 $retval[] = $row['TABLE_NAME']; 00749 } 00750 else 00751 { 00752 $retval[] = array_shift($row); 00753 } 00754 } 00755 } 00756 00757 $this->data_cache['table_names'] = $retval; 00758 return $this->data_cache['table_names']; 00759 }


| CI_DB_driver::load_rdriver | ( | ) |
Load the result drivers.
public
Definition at line 415 of file DB_driver.php.
Referenced by query().
00416 { 00417 $driver = 'CI_DB_'.$this->dbdriver.'_result'; 00418 00419 if ( ! class_exists($driver)) 00420 { 00421 include_once(BASEPATH.'database/DB_result'.EXT); 00422 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT); 00423 } 00424 00425 return $driver; 00426 }

| CI_DB_driver::platform | ( | ) |
The name of the platform in use (mysql, mssql, etc.
..)
public
Definition at line 194 of file DB_driver.php.
| CI_DB_driver::primary | ( | $ | table = '' |
) |
Primary.
Retrieves the primary key. It assumes that the row in the first position is the primary key
public
| string | the table name |
Definition at line 702 of file DB_driver.php.
References list_fields().
00703 { 00704 $fields = $this->list_fields($table); 00705 00706 if ( ! is_array($fields)) 00707 { 00708 return FALSE; 00709 } 00710 00711 return current($fields); 00712 }

| CI_DB_driver::protect_identifiers | ( | $ | item, | |
| $ | prefix_single = FALSE | |||
| ) |
Protect Identifiers.
This function adds backticks if appropriate based on db type
private
| mixed | the item to escape |
Definition at line 1159 of file DB_driver.php.
References _protect_identifiers().
01160 { 01161 return $this->_protect_identifiers($item, $prefix_single); 01162 }

| CI_DB_driver::query | ( | $ | sql, | |
| $ | binds = FALSE, |
|||
| $ | return_object = TRUE | |||
| ) |
Execute the query.
Accepts an SQL string as input and returns a result object upon successful execution of a "read" type query. Returns boolean TRUE upon successful execution of a "write" type query. Returns boolean FALSE upon failure, and if the $db_debug variable is set to TRUE will raise an error.
public
| string | An SQL query string | |
| array | An array of binding data |
Definition at line 244 of file DB_driver.php.
References _cache_init(), cache_on(), compile_binds(), display_error(), is_write_type(), load_rdriver(), log_message(), simple_query(), and trans_complete().
Referenced by CI_DB_active_record::count_all_results(), CI_DB_active_record::delete(), CI_DB_active_record::empty_table(), field_data(), CI_DB_active_record::get(), CI_DB_active_record::get_where(), CI_DB_active_record::insert(), list_fields(), list_tables(), CI_DB_active_record::truncate(), CI_DB_active_record::update(), and version().
00245 { 00246 if ($sql == '') 00247 { 00248 if ($this->db_debug) 00249 { 00250 log_message('error', 'Invalid query: '.$sql); 00251 return $this->display_error('db_invalid_query'); 00252 } 00253 return FALSE; 00254 } 00255 00256 // Verify table prefix and replace if necessary 00257 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) ) 00258 { 00259 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql); 00260 } 00261 00262 // Is query caching enabled? If the query is a "read type" 00263 // we will load the caching class and return the previously 00264 // cached query if it exists 00265 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT')) 00266 { 00267 if ($this->_cache_init()) 00268 { 00269 $this->load_rdriver(); 00270 if (FALSE !== ($cache = $this->CACHE->read($sql))) 00271 { 00272 return $cache; 00273 } 00274 } 00275 } 00276 00277 // Compile binds if needed 00278 if ($binds !== FALSE) 00279 { 00280 $sql = $this->compile_binds($sql, $binds); 00281 } 00282 00283 // Save the query for debugging 00284 if ($this->save_queries == TRUE) 00285 { 00286 $this->queries[] = $sql; 00287 } 00288 00289 // Start the Query Timer 00290 $time_start = list($sm, $ss) = explode(' ', microtime()); 00291 00292 // Run the Query 00293 if (FALSE === ($this->result_id = $this->simple_query($sql))) 00294 { 00295 if ($this->save_queries == TRUE) 00296 { 00297 $this->query_times[] = 0; 00298 } 00299 00300 // This will trigger a rollback if transactions are being used 00301 $this->_trans_status = FALSE; 00302 00303 if ($this->db_debug) 00304 { 00305 // grab the error number and message now, as we might run some 00306 // additional queries before displaying the error 00307 $error_no = $this->_error_number(); 00308 $error_msg = $this->_error_message(); 00309 00310 // We call this function in order to roll-back queries 00311 // if transactions are enabled. If we don't call this here 00312 // the error message will trigger an exit, causing the 00313 // transactions to remain in limbo. 00314 $this->trans_complete(); 00315 00316 // Log and display errors 00317 log_message('error', 'Query error: '.$error_msg); 00318 return $this->display_error( 00319 array( 00320 'Error Number: '.$error_no, 00321 $error_msg, 00322 $sql 00323 ) 00324 ); 00325 } 00326 00327 return FALSE; 00328 } 00329 00330 // Stop and aggregate the query time results 00331 $time_end = list($em, $es) = explode(' ', microtime()); 00332 $this->benchmark += ($em + $es) - ($sm + $ss); 00333 00334 if ($this->save_queries == TRUE) 00335 { 00336 $this->query_times[] = ($em + $es) - ($sm + $ss); 00337 } 00338 00339 // Increment the query counter 00340 $this->query_count++; 00341 00342 // Was the query a "write" type? 00343 // If so we'll simply return true 00344 if ($this->is_write_type($sql) === TRUE) 00345 { 00346 // If caching is enabled we'll auto-cleanup any 00347 // existing files related to this particular URI 00348 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init()) 00349 { 00350 $this->CACHE->delete(); 00351 } 00352 00353 return TRUE; 00354 } 00355 00356 // Return TRUE if we don't need to create a result object 00357 // Currently only the Oracle driver uses this when stored 00358 // procedures are used 00359 if ($return_object !== TRUE) 00360 { 00361 return TRUE; 00362 } 00363 00364 // Load and instantiate the result driver 00365 00366 $driver = $this->load_rdriver(); 00367 $RES = new $driver(); 00368 $RES->conn_id = $this->conn_id; 00369 $RES->result_id = $this->result_id; 00370 00371 if ($this->dbdriver == 'oci8') 00372 { 00373 $RES->stmt_id = $this->stmt_id; 00374 $RES->curs_id = NULL; 00375 $RES->limit_used = $this->limit_used; 00376 $this->stmt_id = FALSE; 00377 } 00378 00379 // oci8 vars must be set before calling this 00380 $RES->num_rows = $RES->num_rows(); 00381 00382 // Is query caching enabled? If so, we'll serialize the 00383 // result object and save it to a cache file. 00384 if ($this->cache_on == TRUE AND $this->_cache_init()) 00385 { 00386 // We'll create a new instance of the result object 00387 // only without the platform specific driver since 00388 // we can't use it with cached data (the query result 00389 // resource ID won't be any good once we've cached the 00390 // result object, so we'll have to compile the data 00391 // and save it) 00392 $CR = new CI_DB_result(); 00393 $CR->num_rows = $RES->num_rows(); 00394 $CR->result_object = $RES->result_object(); 00395 $CR->result_array = $RES->result_array(); 00396 00397 // Reset these since cached objects can not utilize resource IDs. 00398 $CR->conn_id = NULL; 00399 $CR->result_id = NULL; 00400 00401 $this->CACHE->write($sql, $CR); 00402 } 00403 00404 return $RES; 00405 }


| CI_DB_driver::simple_query | ( | $ | sql | ) |
Simple Query This is a simplified version of the query() function.
Internally we only use it when running transaction commands since they do not require all the features of the main query() function.
public
| string | the sql query |
Definition at line 440 of file DB_driver.php.
References initialize().
Referenced by query().
00441 { 00442 if ( ! $this->conn_id) 00443 { 00444 $this->initialize(); 00445 } 00446 00447 return $this->_execute($sql); 00448 }


| CI_DB_driver::table_exists | ( | $ | table_name | ) |
Determine if a particular table exists public.
Definition at line 768 of file DB_driver.php.
References _protect_identifiers(), and list_tables().
00769 { 00770 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE; 00771 }

| CI_DB_driver::total_queries | ( | ) |
Returns the total number of queries.
public
Definition at line 645 of file DB_driver.php.
| CI_DB_driver::trans_complete | ( | ) |
Complete Transaction.
public
Definition at line 514 of file DB_driver.php.
References log_message(), and trans_strict().
Referenced by query().
00515 { 00516 if ( ! $this->trans_enabled) 00517 { 00518 return FALSE; 00519 } 00520 00521 // When transactions are nested we only begin/commit/rollback the outermost ones 00522 if ($this->_trans_depth > 1) 00523 { 00524 $this->_trans_depth -= 1; 00525 return TRUE; 00526 } 00527 00528 // The query() function will set this flag to FALSE in the event that a query failed 00529 if ($this->_trans_status === FALSE) 00530 { 00531 $this->trans_rollback(); 00532 00533 // If we are NOT running in strict mode, we will reset 00534 // the _trans_status flag so that subsequent groups of transactions 00535 // will be permitted. 00536 if ($this->trans_strict === FALSE) 00537 { 00538 $this->_trans_status = TRUE; 00539 } 00540 00541 log_message('debug', 'DB Transaction Failure'); 00542 return FALSE; 00543 } 00544 00545 $this->trans_commit(); 00546 return TRUE; 00547 }


| CI_DB_driver::trans_off | ( | ) |
Disable Transactions This permits transactions to be disabled at run-time.
public
Definition at line 459 of file DB_driver.php.
| CI_DB_driver::trans_start | ( | $ | test_mode = FALSE |
) |
Start Transaction.
public
Definition at line 489 of file DB_driver.php.
00490 { 00491 if ( ! $this->trans_enabled) 00492 { 00493 return FALSE; 00494 } 00495 00496 // When transactions are nested we only begin/commit/rollback the outermost ones 00497 if ($this->_trans_depth > 0) 00498 { 00499 $this->_trans_depth += 1; 00500 return; 00501 } 00502 00503 $this->trans_begin($test_mode); 00504 }
| CI_DB_driver::trans_status | ( | ) |
Lets you retrieve the transaction flag to determine if it has failed.
public
Definition at line 557 of file DB_driver.php.
| CI_DB_driver::trans_strict | ( | $ | mode = TRUE |
) |
Enable/disable Transaction Strict Mode When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back.
If strict mode is disabled, each group is treated autonomously, meaning a failure of one group will not affect any others
public
Definition at line 476 of file DB_driver.php.
Referenced by trans_complete().
00477 { 00478 $this->trans_strict = is_bool($mode) ? $mode : TRUE; 00479 }

| CI_DB_driver::update_string | ( | $ | table, | |
| $ | data, | |||
| $ | where | |||
| ) |
Generate an update string.
public
| string | the table upon which the query will be performed | |
| array | an associative array data of key/values | |
| mixed | the "where" statement |
Definition at line 901 of file DB_driver.php.
References _has_operator(), _protect_identifiers(), and escape().
00902 { 00903 if ($where == '') 00904 { 00905 return false; 00906 } 00907 00908 $fields = array(); 00909 foreach($data as $key => $val) 00910 { 00911 $fields[$this->_protect_identifiers($key)] = $this->escape($val); 00912 } 00913 00914 if ( ! is_array($where)) 00915 { 00916 $dest = array($where); 00917 } 00918 else 00919 { 00920 $dest = array(); 00921 foreach ($where as $key => $val) 00922 { 00923 $prefix = (count($dest) == 0) ? '' : ' AND '; 00924 00925 if ($val !== '') 00926 { 00927 if ( ! $this->_has_operator($key)) 00928 { 00929 $key .= ' ='; 00930 } 00931 00932 $val = ' '.$this->escape($val); 00933 } 00934 00935 $dest[] = $prefix.$key.$val; 00936 } 00937 } 00938 00939 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest); 00940 }

| CI_DB_driver::version | ( | ) |
Database Version Number.
Returns a string containing the version of the database being used
public
Definition at line 208 of file DB_driver.php.
References display_error(), and query().
00209 { 00210 if (FALSE === ($sql = $this->_version())) 00211 { 00212 if ($this->db_debug) 00213 { 00214 return $this->display_error('db_unsupported_function'); 00215 } 00216 return FALSE; 00217 } 00218 00219 if ($this->dbdriver == 'oci8') 00220 { 00221 return $sql; 00222 } 00223 00224 $query = $this->query($sql); 00225 return $query->row('ver'); 00226 }

| CI_DB_driver::$_protect_identifiers = TRUE |
Definition at line 65 of file DB_driver.php.
| CI_DB_driver::$_reserved_identifiers = array('*') |
Definition at line 66 of file DB_driver.php.
| CI_DB_driver::$_trans_depth = 0 |
Definition at line 57 of file DB_driver.php.
| CI_DB_driver::$_trans_status = TRUE |
Definition at line 58 of file DB_driver.php.
| CI_DB_driver::$autoinit = TRUE |
Definition at line 41 of file DB_driver.php.
| CI_DB_driver::$benchmark = 0 |
Definition at line 48 of file DB_driver.php.
| CI_DB_driver::$bind_marker = '?' |
Definition at line 50 of file DB_driver.php.
| CI_DB_driver::$CACHE |
Definition at line 62 of file DB_driver.php.
| CI_DB_driver::$cache_autodel = FALSE |
Definition at line 61 of file DB_driver.php.
| CI_DB_driver::$cache_on = FALSE |
Definition at line 59 of file DB_driver.php.
| CI_DB_driver::$cachedir = '' |
Definition at line 60 of file DB_driver.php.
| CI_DB_driver::$char_set = 'utf8' |
Definition at line 39 of file DB_driver.php.
| CI_DB_driver::$conn_id = FALSE |
Definition at line 45 of file DB_driver.php.
| CI_DB_driver::$curs_id |
Definition at line 70 of file DB_driver.php.
| CI_DB_driver::$data_cache = array() |
Definition at line 54 of file DB_driver.php.
| CI_DB_driver::$database |
Definition at line 36 of file DB_driver.php.
| CI_DB_driver::$db_debug = FALSE |
Definition at line 47 of file DB_driver.php.
| CI_DB_driver::$dbcollat = 'utf8_general_ci' |
Definition at line 40 of file DB_driver.php.
| CI_DB_driver::$dbdriver = 'mysql' |
Definition at line 37 of file DB_driver.php.
| CI_DB_driver::$dbprefix = '' |
Definition at line 38 of file DB_driver.php.
| CI_DB_driver::$hostname |
Definition at line 35 of file DB_driver.php.
| CI_DB_driver::$limit_used |
Definition at line 71 of file DB_driver.php.
| CI_DB_driver::$password |
Definition at line 34 of file DB_driver.php.
| CI_DB_driver::$pconnect = FALSE |
Definition at line 44 of file DB_driver.php.
| CI_DB_driver::$port = '' |
Definition at line 43 of file DB_driver.php.
| CI_DB_driver::$queries = array() |
Definition at line 52 of file DB_driver.php.
| CI_DB_driver::$query_count = 0 |
Definition at line 49 of file DB_driver.php.
| CI_DB_driver::$query_times = array() |
Definition at line 53 of file DB_driver.php.
| CI_DB_driver::$result_id = FALSE |
Definition at line 46 of file DB_driver.php.
| CI_DB_driver::$save_queries = TRUE |
Definition at line 51 of file DB_driver.php.
| CI_DB_driver::$stmt_id |
Definition at line 69 of file DB_driver.php.
| CI_DB_driver::$swap_pre = '' |
Definition at line 42 of file DB_driver.php.
| CI_DB_driver::$trans_enabled = TRUE |
Definition at line 55 of file DB_driver.php.
| CI_DB_driver::$trans_strict = TRUE |
Definition at line 56 of file DB_driver.php.
| CI_DB_driver::$username |
Definition at line 33 of file DB_driver.php.