1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
\r
21 * Note: _DB is an extender class that the app controller
\r
22 * creates dynamically based on whether the active record
\r
23 * class is being used or not.
\r
25 * @package CodeIgniter
\r
26 * @subpackage Drivers
\r
27 * @category Database
\r
28 * @author ExpressionEngine Dev Team
\r
29 * @link http://codeigniter.com/user_guide/database/
\r
31 class CI_DB_mysqli_driver extends CI_DB {
\r
33 var $dbdriver = 'mysqli';
\r
35 // The character used for escaping
\r
36 var $_escape_char = '`';
\r
39 * The syntax to count rows is slightly different across different
\r
40 * database engines, so this string appears in each driver and is
\r
41 * used for the count_all() and count_all_results() functions.
\r
43 var $_count_string = "SELECT COUNT(*) AS ";
\r
44 var $_random_keyword = ' RAND()'; // database specific random keyword
\r
47 * Whether to use the MySQL "delete hack" which allows the number
\r
48 * of affected rows to be shown. Uses a preg_replace when enabled,
\r
49 * adding a bit more processing to all queries.
\r
51 var $delete_hack = TRUE;
\r
53 // --------------------------------------------------------------------
\r
56 * Non-persistent database connection
\r
58 * @access private called by the base class
\r
61 function db_connect()
\r
63 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
\r
66 // --------------------------------------------------------------------
\r
69 * Persistent database connection
\r
71 * @access private called by the base class
\r
74 function db_pconnect()
\r
76 return $this->db_connect();
\r
79 // --------------------------------------------------------------------
\r
82 * Select the database
\r
84 * @access private called by the base class
\r
87 function db_select()
\r
89 return @mysqli_select_db($this->conn_id, $this->database);
\r
92 // --------------------------------------------------------------------
\r
95 * Set client character set
\r
102 function _db_set_charset($charset, $collation)
\r
104 return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
\r
107 // --------------------------------------------------------------------
\r
110 * Version number query string
\r
115 function _version()
\r
117 return "SELECT version() AS ver";
\r
120 // --------------------------------------------------------------------
\r
123 * Execute the query
\r
125 * @access private called by the base class
\r
126 * @param string an SQL query
\r
129 function _execute($sql)
\r
131 $sql = $this->_prep_query($sql);
\r
132 $result = @mysqli_query($this->conn_id, $sql);
\r
136 // --------------------------------------------------------------------
\r
141 * If needed, each database adapter can prep the query string
\r
143 * @access private called by execute()
\r
144 * @param string an SQL query
\r
147 function _prep_query($sql)
\r
149 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
\r
150 // the query so that it returns the number of affected rows
\r
151 if ($this->delete_hack === TRUE)
\r
153 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
\r
155 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
\r
162 // --------------------------------------------------------------------
\r
165 * Begin Transaction
\r
170 function trans_begin($test_mode = FALSE)
\r
172 if ( ! $this->trans_enabled)
\r
177 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
178 if ($this->_trans_depth > 0)
\r
183 // Reset the transaction failure flag.
\r
184 // If the $test_mode flag is set to TRUE transactions will be rolled back
\r
185 // even if the queries produce a successful result.
\r
186 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
\r
188 $this->simple_query('SET AUTOCOMMIT=0');
\r
189 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
\r
193 // --------------------------------------------------------------------
\r
196 * Commit Transaction
\r
201 function trans_commit()
\r
203 if ( ! $this->trans_enabled)
\r
208 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
209 if ($this->_trans_depth > 0)
\r
214 $this->simple_query('COMMIT');
\r
215 $this->simple_query('SET AUTOCOMMIT=1');
\r
219 // --------------------------------------------------------------------
\r
222 * Rollback Transaction
\r
227 function trans_rollback()
\r
229 if ( ! $this->trans_enabled)
\r
234 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
235 if ($this->_trans_depth > 0)
\r
240 $this->simple_query('ROLLBACK');
\r
241 $this->simple_query('SET AUTOCOMMIT=1');
\r
245 // --------------------------------------------------------------------
\r
254 function escape_str($str)
\r
256 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
\r
258 return mysqli_real_escape_string($this->conn_id, $str);
\r
260 elseif (function_exists('mysql_escape_string'))
\r
262 return mysql_escape_string($str);
\r
266 return addslashes($str);
\r
270 // --------------------------------------------------------------------
\r
278 function affected_rows()
\r
280 return @mysqli_affected_rows($this->conn_id);
\r
283 // --------------------------------------------------------------------
\r
291 function insert_id()
\r
293 return @mysqli_insert_id($this->conn_id);
\r
296 // --------------------------------------------------------------------
\r
299 * "Count All" query
\r
301 * Generates a platform-specific query string that counts all records in
\r
302 * the specified database
\r
308 function count_all($table = '')
\r
313 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
\r
315 if ($query->num_rows() == 0)
\r
318 $row = $query->row();
\r
319 return $row->numrows;
\r
322 // --------------------------------------------------------------------
\r
327 * Generates a platform-specific query string so that the table names can be fetched
\r
333 function _list_tables($prefix_limit = FALSE)
\r
335 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
\r
337 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
\r
339 $sql .= " LIKE '".$this->dbprefix."%'";
\r
345 // --------------------------------------------------------------------
\r
348 * Show column query
\r
350 * Generates a platform-specific query string so that the column names can be fetched
\r
353 * @param string the table name
\r
356 function _list_columns($table = '')
\r
358 return "SHOW COLUMNS FROM ".$table;
\r
361 // --------------------------------------------------------------------
\r
366 * Generates a platform-specific query so that the column data can be retrieved
\r
369 * @param string the table name
\r
372 function _field_data($table)
\r
374 return "SELECT * FROM ".$table." LIMIT 1";
\r
377 // --------------------------------------------------------------------
\r
380 * The error message string
\r
385 function _error_message()
\r
387 return mysqli_error($this->conn_id);
\r
390 // --------------------------------------------------------------------
\r
393 * The error message number
\r
398 function _error_number()
\r
400 return mysqli_errno($this->conn_id);
\r
403 // --------------------------------------------------------------------
\r
406 * Escape the SQL Identifiers
\r
408 * This function escapes column and table names
\r
414 function _escape_identifiers($item)
\r
416 if ($this->_escape_char == '')
\r
421 if (strpos($item, '.') !== FALSE)
\r
423 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
\r
427 $str = $this->_escape_char.$item.$this->_escape_char;
\r
430 // remove duplicates if the user already included the escape
\r
431 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
\r
434 // --------------------------------------------------------------------
\r
439 * This function implicitly groups FROM tables so there is no confusion
\r
440 * about operator precedence in harmony with SQL standards
\r
446 function _from_tables($tables)
\r
448 if ( ! is_array($tables))
\r
450 $tables = array($tables);
\r
453 return '('.implode(', ', $tables).')';
\r
456 // --------------------------------------------------------------------
\r
461 * Generates a platform-specific insert string from the supplied data
\r
464 * @param string the table name
\r
465 * @param array the insert keys
\r
466 * @param array the insert values
\r
469 function _insert($table, $keys, $values)
\r
471 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
\r
474 // --------------------------------------------------------------------
\r
479 * Generates a platform-specific update string from the supplied data
\r
482 * @param string the table name
\r
483 * @param array the update data
\r
484 * @param array the where clause
\r
485 * @param array the orderby clause
\r
486 * @param array the limit clause
\r
489 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
\r
491 foreach($values as $key => $val)
\r
493 $valstr[] = $key." = ".$val;
\r
496 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
498 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
\r
500 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
\r
502 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
\r
504 $sql .= $orderby.$limit;
\r
510 // --------------------------------------------------------------------
\r
513 * Truncate statement
\r
515 * Generates a platform-specific truncate string from the supplied data
\r
516 * If the database does not support the truncate() command
\r
517 * This function maps to "DELETE FROM table"
\r
520 * @param string the table name
\r
523 function _truncate($table)
\r
525 return "TRUNCATE ".$table;
\r
528 // --------------------------------------------------------------------
\r
533 * Generates a platform-specific delete string from the supplied data
\r
536 * @param string the table name
\r
537 * @param array the where clause
\r
538 * @param string the limit clause
\r
541 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
\r
545 if (count($where) > 0 OR count($like) > 0)
\r
547 $conditions = "\nWHERE ";
\r
548 $conditions .= implode("\n", $this->ar_where);
\r
550 if (count($where) > 0 && count($like) > 0)
\r
552 $conditions .= " AND ";
\r
554 $conditions .= implode("\n", $like);
\r
557 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
559 return "DELETE FROM ".$table.$conditions.$limit;
\r
562 // --------------------------------------------------------------------
\r
567 * Generates a platform-specific LIMIT clause
\r
570 * @param string the sql query string
\r
571 * @param integer the number of rows to limit the query to
\r
572 * @param integer the offset value
\r
575 function _limit($sql, $limit, $offset)
\r
577 $sql .= "LIMIT ".$limit;
\r
581 $sql .= " OFFSET ".$offset;
\r
587 // --------------------------------------------------------------------
\r
590 * Close DB Connection
\r
596 function _close($conn_id)
\r
598 @mysqli_close($conn_id);
\r
605 /* End of file mysqli_driver.php */
\r
606 /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */