CI_DB_mysql_driver Class Reference

Inheritance diagram for CI_DB_mysql_driver:
Collaboration diagram for CI_DB_mysql_driver:

List of all members.


Public Member Functions

 db_connect ()
 Non-persistent database connection.
 db_pconnect ()
 Persistent database connection.
 db_select ()
 Select the database.
 db_set_charset ($charset, $collation)
 Set client character set.
 _version ()
 Version number query string.
 _execute ($sql)
 Execute the query.
 _prep_query ($sql)
 Prep the query.
 trans_begin ($test_mode=FALSE)
 Begin Transaction.
 trans_commit ()
 Commit Transaction.
 trans_rollback ()
 Rollback Transaction.
 escape_str ($str)
 Escape String.
 affected_rows ()
 Affected Rows.
 insert_id ()
 Insert ID.
 count_all ($table= '')
 "Count All" query
 _list_tables ($prefix_limit=FALSE)
 List table query.
 _list_columns ($table= '')
 Show column query.
 _field_data ($table)
 Field data query.
 _error_message ()
 The error message string.
 _error_number ()
 The error message number.
 _escape_table ($table)
 Escape Table Name.
 _protect_identifiers ($item, $first_word_only=FALSE)
 Protect Identifiers.
 _from_tables ($tables)
 From Tables.
 _insert ($table, $keys, $values)
 Insert statement.
 _update ($table, $values, $where, $orderby=array(), $limit=FALSE)
 Update statement.
 _truncate ($table)
 Truncate statement.
 _delete ($table, $where=array(), $like=array(), $limit=FALSE)
 Delete statement.
 _limit ($sql, $limit, $offset)
 Limit string.
 _close ($conn_id)
 Close DB Connection.

Public Attributes

 $delete_hack = TRUE
 Whether to use the MySQL "delete hack" which allows the number of affected rows to be shown.
 $_count_string = 'SELECT COUNT(*) AS '
 The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.
 $_random_keyword = ' RAND()'

Detailed Description

Definition at line 31 of file mysql_driver.php.


Member Function Documentation

CI_DB_mysql_driver::_close ( conn_id  ) 

Close DB Connection.

public

Parameters:
resource 
Returns:
void

Definition at line 645 of file mysql_driver.php.

00646         {
00647                 @mysql_close($conn_id);
00648         }

CI_DB_mysql_driver::_delete ( table,
where = array(),
like = array(),
limit = FALSE 
)

Delete statement.

Generates a platform-specific delete string from the supplied data

public

Parameters:
string the table name
array the where clause
string the limit clause
Returns:
string

Definition at line 588 of file mysql_driver.php.

00589         {
00590                 $conditions = '';
00591 
00592                 if (count($where) > 0 OR count($like) > 0)
00593                 {
00594                         $conditions = "\nWHERE ";
00595                         $conditions .= implode("\n", $this->ar_where);
00596 
00597                         if (count($where) > 0 && count($like) > 0)
00598                         {
00599                                 $conditions .= " AND ";
00600                         }
00601                         $conditions .= implode("\n", $like);
00602                 }
00603 
00604                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00605         
00606                 return "DELETE FROM ".$table.$conditions.$limit;
00607         }

CI_DB_mysql_driver::_error_message (  ) 

The error message string.

private

Returns:
string

Definition at line 384 of file mysql_driver.php.

00385         {
00386                 return mysql_error($this->conn_id);
00387         }

CI_DB_mysql_driver::_error_number (  ) 

The error message number.

private

Returns:
integer

Definition at line 397 of file mysql_driver.php.

00398         {
00399                 return mysql_errno($this->conn_id);
00400         }

CI_DB_mysql_driver::_escape_table ( table  ) 

Escape Table Name.

This function adds backticks if the table name has a period in it. Some DBs will get cranky unless periods are escaped

private

Parameters:
string the table name
Returns:
string

Definition at line 414 of file mysql_driver.php.

00415         {
00416                 if (strpos($table, '.') !== FALSE)
00417                 {
00418                         $table = '`' . str_replace('.', '`.`', $table) . '`';
00419                 }
00420                 
00421                 return $table;
00422         }

CI_DB_mysql_driver::_execute ( sql  ) 

Execute the query.

private called by the base class

Parameters:
string an SQL query
Returns:
resource

Definition at line 122 of file mysql_driver.php.

References _prep_query().

00123         {
00124                 $sql = $this->_prep_query($sql);
00125                 return @mysql_query($sql, $this->conn_id);
00126         }

Here is the call graph for this function:

CI_DB_mysql_driver::_field_data ( table  ) 

Field data query.

Generates a platform-specific query so that the column data can be retrieved

public

Parameters:
string the table name
Returns:
object

Definition at line 371 of file mysql_driver.php.

00372         {
00373                 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
00374         }

CI_DB_mysql_driver::_from_tables ( tables  ) 

From Tables.

This function implicitly groups FROM tables so there is no confusion about operator precedence in harmony with SQL standards

public

Parameters:
type 
Returns:
type

Definition at line 496 of file mysql_driver.php.

00497         {
00498                 if ( ! is_array($tables))
00499                 {
00500                         $tables = array($tables);
00501                 }
00502                 
00503                 return '('.implode(', ', $tables).')';
00504         }

CI_DB_mysql_driver::_insert ( table,
keys,
values 
)

Insert statement.

Generates a platform-specific insert string from the supplied data

public

Parameters:
string the table name
array the insert keys
array the insert values
Returns:
string

Definition at line 519 of file mysql_driver.php.

00520         {       
00521                 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
00522         }

CI_DB_mysql_driver::_limit ( sql,
limit,
offset 
)

Limit string.

Generates a platform-specific LIMIT clause

public

Parameters:
string the sql query string
integer the number of rows to limit the query to
integer the offset value
Returns:
string

Definition at line 622 of file mysql_driver.php.

00623         {       
00624                 if ($offset == 0)
00625                 {
00626                         $offset = '';
00627                 }
00628                 else
00629                 {
00630                         $offset .= ", ";
00631                 }
00632                 
00633                 return $sql."LIMIT ".$offset.$limit;
00634         }

CI_DB_mysql_driver::_list_columns ( table = ''  ) 

Show column query.

Generates a platform-specific query string so that the column names can be fetched

public

Parameters:
string the table name
Returns:
string

Definition at line 355 of file mysql_driver.php.

00356         {
00357                 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
00358         }

CI_DB_mysql_driver::_list_tables ( prefix_limit = FALSE  ) 

List table query.

Generates a platform-specific query string so that the table names can be fetched

private

Parameters:
boolean 
Returns:
string

Definition at line 332 of file mysql_driver.php.

00333         {
00334                 $sql = "SHOW TABLES FROM `".$this->database."`";        
00335 
00336                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
00337                 {
00338                         $sql .= " LIKE '".$this->dbprefix."%'";
00339                 }
00340 
00341                 return $sql;
00342         }

CI_DB_mysql_driver::_prep_query ( sql  ) 

Prep the query.

If needed, each database adapter can prep the query string

private called by execute()

Parameters:
string an SQL query
Returns:
string

Definition at line 139 of file mysql_driver.php.

Referenced by _execute().

00140         {
00141                 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
00142                 // the query so that it returns the number of affected rows
00143                 if ($this->delete_hack === TRUE)
00144                 {
00145                         if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
00146                         {
00147                                 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
00148                         }
00149                 }
00150                 
00151                 return $sql;
00152         }

Here is the caller graph for this function:

CI_DB_mysql_driver::_protect_identifiers ( item,
first_word_only = FALSE 
)

Protect Identifiers.

This function adds backticks if appropriate based on db type

private

Parameters:
mixed the item to escape
boolean only affect the first word
Returns:
mixed the item with backticks

Definition at line 436 of file mysql_driver.php.

Referenced by count_all().

00437         {
00438                 if (is_array($item))
00439                 {
00440                         $escaped_array = array();
00441 
00442                         foreach($item as $k=>$v)
00443                         {
00444                                 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
00445                         }
00446 
00447                         return $escaped_array;
00448                 }       
00449 
00450                 // This function may get "item1 item2" as a string, and so
00451                 // we may need "`item1` `item2`" and not "`item1 item2`"
00452                 if (ctype_alnum($item) === FALSE)
00453                 {
00454                         if (strpos($item, '.') !== FALSE)
00455                         {
00456                                 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
00457                                 $table_name =  substr($item, 0, strpos($item, '.')+1);
00458                                 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
00459                         }
00460 
00461                         // This function may get "field >= 1", and need it to return "`field` >= 1"
00462                         $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
00463 
00464                         $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
00465                 }
00466                 else
00467                 {
00468                         return "`{$item}`";
00469                 }
00470 
00471                 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
00472                 
00473                 foreach ($exceptions as $exception)
00474                 {
00475                 
00476                         if (stristr($item, " `{$exception}` ") !== FALSE)
00477                         {
00478                                 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
00479                         }
00480                 }
00481                 return $item;
00482         }

Here is the caller graph for this function:

CI_DB_mysql_driver::_truncate ( table  ) 

Truncate statement.

Generates a platform-specific truncate string from the supplied data If the database does not support the truncate() command This function maps to "DELETE FROM table"

public

Parameters:
string the table name
Returns:
string

Definition at line 570 of file mysql_driver.php.

00571         {
00572                 return "TRUNCATE ".$this->_escape_table($table);
00573         }

CI_DB_mysql_driver::_update ( table,
values,
where,
orderby = array(),
limit = FALSE 
)

Update statement.

Generates a platform-specific update string from the supplied data

public

Parameters:
string the table name
array the update data
array the where clause
array the orderby clause
array the limit clause
Returns:
string

Definition at line 539 of file mysql_driver.php.

00540         {
00541                 foreach($values as $key => $val)
00542                 {
00543                         $valstr[] = $key." = ".$val;
00544                 }
00545                 
00546                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00547                 
00548                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
00549         
00550                 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
00551                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
00552                 $sql .= $orderby.$limit;
00553                 
00554                 return $sql;
00555         }

CI_DB_mysql_driver::_version (  ) 

Version number query string.

public

Returns:
string

Definition at line 108 of file mysql_driver.php.

00109         {
00110                 return "SELECT version() AS ver";
00111         }

CI_DB_mysql_driver::affected_rows (  ) 

Affected Rows.

public

Returns:
integer

Definition at line 280 of file mysql_driver.php.

00281         {
00282                 return @mysql_affected_rows($this->conn_id);
00283         }

CI_DB_mysql_driver::count_all ( table = ''  ) 

"Count All" query

Generates a platform-specific query string that counts all records in the specified database

public

Parameters:
string 
Returns:
string

Definition at line 310 of file mysql_driver.php.

References _protect_identifiers().

00311         {
00312                 if ($table == '')
00313                         return '0';
00314         
00315                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
00316                 
00317                 $row = $query->row();
00318                 return (int)$row->numrows;
00319         }

Here is the call graph for this function:

CI_DB_mysql_driver::db_connect (  ) 

Non-persistent database connection.

private called by the base class

Returns:
resource

Definition at line 54 of file mysql_driver.php.

00055         {
00056                 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
00057         }

CI_DB_mysql_driver::db_pconnect (  ) 

Persistent database connection.

private called by the base class

Returns:
resource

Definition at line 67 of file mysql_driver.php.

00068         {
00069                 return @mysql_pconnect($this->hostname, $this->username, $this->password);
00070         }

CI_DB_mysql_driver::db_select (  ) 

Select the database.

private called by the base class

Returns:
resource

Definition at line 80 of file mysql_driver.php.

00081         {
00082                 return @mysql_select_db($this->database, $this->conn_id);
00083         }

CI_DB_mysql_driver::db_set_charset ( charset,
collation 
)

Set client character set.

public

Parameters:
string 
string 
Returns:
resource

Definition at line 95 of file mysql_driver.php.

References escape_str().

00096         {
00097                 return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
00098         }

Here is the call graph for this function:

CI_DB_mysql_driver::escape_str ( str  ) 

Escape String.

public

Parameters:
string 
Returns:
string

Definition at line 246 of file mysql_driver.php.

Referenced by db_set_charset().

00247         {       
00248                 if (is_array($str))
00249         {
00250                 foreach($str as $key => $val)
00251                 {
00252                         $str[$key] = $this->escape_str($val);
00253                 }
00254                 
00255                 return $str;
00256         }
00257         
00258                 if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
00259                 {
00260                         return mysql_real_escape_string($str, $this->conn_id);
00261                 }
00262                 elseif (function_exists('mysql_escape_string'))
00263                 {
00264                         return mysql_escape_string($str);
00265                 }
00266                 else
00267                 {
00268                         return addslashes($str);
00269                 }
00270         }

Here is the caller graph for this function:

CI_DB_mysql_driver::insert_id (  ) 

Insert ID.

public

Returns:
integer

Definition at line 293 of file mysql_driver.php.

00294         {
00295                 return @mysql_insert_id($this->conn_id);
00296         }

CI_DB_mysql_driver::trans_begin ( test_mode = FALSE  ) 

Begin Transaction.

public

Returns:
bool

Definition at line 162 of file mysql_driver.php.

00163         {
00164                 if ( ! $this->trans_enabled)
00165                 {
00166                         return TRUE;
00167                 }
00168                 
00169                 // When transactions are nested we only begin/commit/rollback the outermost ones
00170                 if ($this->_trans_depth > 0)
00171                 {
00172                         return TRUE;
00173                 }
00174 
00175                 // Reset the transaction failure flag.
00176                 // If the $test_mode flag is set to TRUE transactions will be rolled back
00177                 // even if the queries produce a successful result.
00178                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
00179                 
00180                 $this->simple_query('SET AUTOCOMMIT=0');
00181                 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
00182                 return TRUE;
00183         }

CI_DB_mysql_driver::trans_commit (  ) 

Commit Transaction.

public

Returns:
bool

Definition at line 193 of file mysql_driver.php.

00194         {
00195                 if ( ! $this->trans_enabled)
00196                 {
00197                         return TRUE;
00198                 }
00199 
00200                 // When transactions are nested we only begin/commit/rollback the outermost ones
00201                 if ($this->_trans_depth > 0)
00202                 {
00203                         return TRUE;
00204                 }
00205 
00206                 $this->simple_query('COMMIT');
00207                 $this->simple_query('SET AUTOCOMMIT=1');
00208                 return TRUE;
00209         }

CI_DB_mysql_driver::trans_rollback (  ) 

Rollback Transaction.

public

Returns:
bool

Definition at line 219 of file mysql_driver.php.

00220         {
00221                 if ( ! $this->trans_enabled)
00222                 {
00223                         return TRUE;
00224                 }
00225 
00226                 // When transactions are nested we only begin/commit/rollback the outermost ones
00227                 if ($this->_trans_depth > 0)
00228                 {
00229                         return TRUE;
00230                 }
00231 
00232                 $this->simple_query('ROLLBACK');
00233                 $this->simple_query('SET AUTOCOMMIT=1');
00234                 return TRUE;
00235         }


Member Data Documentation

CI_DB_mysql_driver::$_count_string = 'SELECT COUNT(*) AS '

The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.

Definition at line 45 of file mysql_driver.php.

CI_DB_mysql_driver::$_random_keyword = ' RAND()'

Definition at line 46 of file mysql_driver.php.

CI_DB_mysql_driver::$delete_hack = TRUE

Whether to use the MySQL "delete hack" which allows the number of affected rows to be shown.

Uses a preg_replace when enabled, adding a bit more processing to all queries.

Definition at line 38 of file mysql_driver.php.


The documentation for this class was generated from the following file: