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