blob: 0b80f55be94875660253b44f9eed71cf7416796d [file] [log] [blame]
Andrey Andreev1ff49e02012-01-27 11:30:41 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreev1ff49e02012-01-27 11:30:41 +020029 * MySQLi Database Adapter Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mysqli_driver extends CI_DB {
42
Andrey Andreev1ff49e02012-01-27 11:30:41 +020043 public $dbdriver = 'mysqli';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev1ff49e02012-01-27 11:30:41 +020046 protected $_escape_char = '`';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev1ff49e02012-01-27 11:30:41 +020049 protected $_like_escape_str = '';
50 protected $_like_escape_chr = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000059
60 /**
61 * Whether to use the MySQL "delete hack" which allows the number
62 * of affected rows to be shown. Uses a preg_replace when enabled,
63 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020065 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000066
67 /**
68 * Non-persistent database connection
69 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020070 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020072 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +020074 return ($this->port != '')
75 ? @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port)
76 : @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Persistent database connection
83 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020084 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020086 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreevf055fa92012-01-27 20:36:23 +020088 // Persistent connection support was added in PHP 5.3.0
89 if ( ! is_php('5.3'))
90 {
91 return $this->db_connect();
92 }
93
Andrey Andreev1ff49e02012-01-27 11:30:41 +020094 return ($this->port != '')
95 ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
96 : @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 // --------------------------------------------------------------------
100
101 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000102 * Reconnect
103 *
104 * Keep / reestablish the db connection if no queries have been
105 * sent for a length of time exceeding the server's idle timeout
106 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000107 * @return void
108 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200109 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000110 {
111 if (mysqli_ping($this->conn_id) === FALSE)
112 {
113 $this->conn_id = FALSE;
114 }
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 * Select the database
121 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200122 * @param string database name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200123 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev11454e02012-02-22 16:05:47 +0200127 return @mysqli_select_db($this->conn_id, ($database == '' ? $this->database : $database));
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Set client character set
134 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @param string
136 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200137 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200139 protected function _db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
RH Beckercfdb2322011-10-03 17:28:32 -0700141 return function_exists('mysqli_set_charset')
142 ? @mysqli_set_charset($this->conn_id, $charset)
143 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Version number query string
150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @return string
152 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200153 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200155 return @mysqli_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Execute the query
162 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200164 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200165 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200166 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200168 return @mysqli_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
174 * Prep the query
175 *
176 * If needed, each database adapter can prep the query string
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @param string an SQL query
179 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200180 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200181 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200183 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
184 // modifies the query so that it a proper number of affected rows is returned.
185 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200187 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return $sql;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Begin Transaction
197 *
Barry Mienydd671972010-10-04 16:33:58 +0200198 * @return bool
199 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200200 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200203 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 return TRUE;
206 }
207
208 // Reset the transaction failure flag.
209 // If the $test_mode flag is set to TRUE transactions will be rolled back
210 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200211 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213 $this->simple_query('SET AUTOCOMMIT=0');
214 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
215 return TRUE;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Barry Mienydd671972010-10-04 16:33:58 +0200223 * @return bool
224 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200225 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200228 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return TRUE;
231 }
232
233 $this->simple_query('COMMIT');
234 $this->simple_query('SET AUTOCOMMIT=1');
235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rollback Transaction
242 *
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return bool
244 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200245 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return TRUE;
251 }
252
253 $this->simple_query('ROLLBACK');
254 $this->simple_query('SET AUTOCOMMIT=1');
255 return TRUE;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Escape String
262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @return string
266 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200267 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 if (is_array($str))
270 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500271 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200272 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000273 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200274 }
275
276 return $str;
277 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000278
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200279 if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283 elseif (function_exists('mysql_escape_string'))
284 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287 else
288 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Jonese4ed5832009-02-20 21:44:59 +0000292 // escape LIKE condition wildcards
293 if ($like === TRUE)
294 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200295 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jonese4ed5832009-02-20 21:44:59 +0000298 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // --------------------------------------------------------------------
302
303 /**
304 * Affected Rows
305 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200306 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200308 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 return @mysqli_affected_rows($this->conn_id);
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
316 * Insert ID
317 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200318 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200320 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 return @mysqli_insert_id($this->conn_id);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * "Count All" query
329 *
330 * Generates a platform-specific query string that counts all records in
331 * the specified database
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @return string
335 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200336 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000339 {
340 return 0;
341 }
342
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200343 $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000345 {
346 return 0;
347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200349 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500350 $this->_reset_select();
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200351 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * List table query
358 *
359 * Generates a platform-specific query string so that the table names can be fetched
360 *
361 * @access private
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200362 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @return string
364 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200365 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200367 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Barry Mienydd671972010-10-04 16:33:58 +0200368
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200369 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200371 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 return $sql;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Show column query
381 *
382 * Generates a platform-specific query string so that the column names can be fetched
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string the table name
385 * @return string
386 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200387 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200389 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * Field data query
396 *
397 * Generates a platform-specific query so that the column data can be retrieved
398 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @param string the table name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200400 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200402 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200404 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * The error message string
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @return string
413 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200414 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 return mysqli_error($this->conn_id);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
420
421 /**
422 * The error message number
423 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200424 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200426 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
428 return mysqli_errno($this->conn_id);
429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Escape the SQL Identifiers
435 *
436 * This function escapes column and table names
437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param string
439 * @return string
440 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200441 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 if ($this->_escape_char == '')
444 {
445 return $item;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 foreach ($this->_reserved_identifiers as $id)
449 {
450 if (strpos($item, '.'.$id) !== FALSE)
451 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200452 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200455 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200456 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 if (strpos($item, '.') !== FALSE)
460 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200461 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200465 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 // --------------------------------------------------------------------
469
470 /**
471 * From Tables
472 *
473 * This function implicitly groups FROM tables so there is no confusion
474 * about operator precedence in harmony with SQL standards
475 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200476 * @param string
477 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200479 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 {
481 if ( ! is_array($tables))
482 {
483 $tables = array($tables);
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 return '('.implode(', ', $tables).')';
487 }
488
489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Insert statement
493 *
494 * Generates a platform-specific insert string from the supplied data
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param string the table name
497 * @param array the insert keys
498 * @param array the insert values
499 * @return string
500 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200501 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200502 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200503 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500509 * Insert_batch statement
510 *
511 * Generates a platform-specific insert string from the supplied data
512 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500513 * @param string the table name
514 * @param array the insert keys
515 * @param array the insert values
516 * @return string
517 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200518 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500519 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200520 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500521 }
RH Beckercfdb2322011-10-03 17:28:32 -0700522
Pascal Kriete43ded232011-01-07 15:05:40 -0500523 // --------------------------------------------------------------------
524
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000525
526 /**
527 * Replace statement
528 *
529 * Generates a platform-specific replace string from the supplied data
530 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000531 * @param string the table name
532 * @param array the insert keys
533 * @param array the insert values
534 * @return string
535 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200536 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000537 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200538 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000539 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200540
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000541 // --------------------------------------------------------------------
542
Pascal Kriete43ded232011-01-07 15:05:40 -0500543 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * Update statement
545 *
546 * Generates a platform-specific update string from the supplied data
547 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @param string the table name
549 * @param array the update data
550 * @param array the where clause
551 * @param array the orderby clause
552 * @param array the limit clause
553 * @return string
554 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200555 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500557 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200559 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200562 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
563 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
564 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
565 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567
Pascal Kriete43ded232011-01-07 15:05:40 -0500568 // --------------------------------------------------------------------
569
570 /**
571 * Update_Batch statement
572 *
573 * Generates a platform-specific batch update string from the supplied data
574 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500575 * @param string the table name
576 * @param array the update data
577 * @param array the where clause
578 * @return string
579 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200580 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500581 {
582 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500583 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500584 {
585 $ids[] = $val[$index];
586
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500587 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500588 {
589 if ($field != $index)
590 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500591 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500592 }
593 }
594 }
595
Pascal Kriete43ded232011-01-07 15:05:40 -0500596 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500597 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500598 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200599 $cases .= $k.' = CASE '."\n"
600 .implode("\n", $v)."\n"
601 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500602 }
603
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200604 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500605
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200606 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
607 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
608 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // --------------------------------------------------------------------
612
613 /**
614 * Truncate statement
615 *
616 * Generates a platform-specific truncate string from the supplied data
617 * If the database does not support the truncate() command
618 * This function maps to "DELETE FROM table"
619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @param string the table name
621 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200622 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200623 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200625 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 }
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 // --------------------------------------------------------------------
629
630 /**
631 * Delete statement
632 *
633 * Generates a platform-specific delete string from the supplied data
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @param string the table name
636 * @param array the where clause
637 * @param string the limit clause
638 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200639 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200640 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 if (count($where) > 0 OR count($like) > 0)
644 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200645 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000646
647 if (count($where) > 0 && count($like) > 0)
648 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200649 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 }
651 $conditions .= implode("\n", $like);
652 }
653
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200654 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656
657 // --------------------------------------------------------------------
658
659 /**
660 * Limit string
661 *
662 * Generates a platform-specific LIMIT clause
663 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200665 * @param int the number of rows to limit the query to
666 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @return string
668 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200669 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200670 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200671 return $sql.' LIMIT '.$limit
672 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
674
675 // --------------------------------------------------------------------
676
677 /**
678 * Close DB Connection
679 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200680 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 * @return void
682 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200683 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
685 @mysqli_close($conn_id);
686 }
687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688}
689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690/* End of file mysqli_driver.php */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200691/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */