blob: c6ffb492994ad78f70336906749631e309aa5cc1 [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 Andreev1ff49e02012-01-27 11:30:41 +020088 return ($this->port != '')
89 ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
90 : @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 // --------------------------------------------------------------------
94
95 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000096 * Reconnect
97 *
98 * Keep / reestablish the db connection if no queries have been
99 * sent for a length of time exceeding the server's idle timeout
100 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000101 * @return void
102 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200103 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000104 {
105 if (mysqli_ping($this->conn_id) === FALSE)
106 {
107 $this->conn_id = FALSE;
108 }
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * Select the database
115 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200116 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200118 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
120 return @mysqli_select_db($this->conn_id, $this->database);
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Set client character set
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @param string
129 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200130 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200132 protected function _db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
RH Beckercfdb2322011-10-03 17:28:32 -0700134 return function_exists('mysqli_set_charset')
135 ? @mysqli_set_charset($this->conn_id, $charset)
136 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
138
139 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 /**
142 * Version number query string
143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return string
145 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200146 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200148 return @mysqli_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Execute the query
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200157 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200158 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200159 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200161 return @mysqli_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // --------------------------------------------------------------------
165
166 /**
167 * Prep the query
168 *
169 * If needed, each database adapter can prep the query string
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string an SQL query
172 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200173 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200174 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200176 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
177 // modifies the query so that it a proper number of affected rows is returned.
178 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200180 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return $sql;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Begin Transaction
190 *
Barry Mienydd671972010-10-04 16:33:58 +0200191 * @return bool
192 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200193 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200196 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 return TRUE;
199 }
200
201 // Reset the transaction failure flag.
202 // If the $test_mode flag is set to TRUE transactions will be rolled back
203 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200204 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000205
206 $this->simple_query('SET AUTOCOMMIT=0');
207 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
208 return TRUE;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Commit Transaction
215 *
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200218 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200221 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return TRUE;
224 }
225
226 $this->simple_query('COMMIT');
227 $this->simple_query('SET AUTOCOMMIT=1');
228 return TRUE;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Rollback Transaction
235 *
Barry Mienydd671972010-10-04 16:33:58 +0200236 * @return bool
237 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200238 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200241 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 return TRUE;
244 }
245
246 $this->simple_query('ROLLBACK');
247 $this->simple_query('SET AUTOCOMMIT=1');
248 return TRUE;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Escape String
255 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000257 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * @return string
259 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200260 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000262 if (is_array($str))
263 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500264 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200265 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000266 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200267 }
268
269 return $str;
270 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000271
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200272 if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276 elseif (function_exists('mysql_escape_string'))
277 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000278 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280 else
281 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 // escape LIKE condition wildcards
286 if ($like === TRUE)
287 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200288 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // --------------------------------------------------------------------
295
296 /**
297 * Affected Rows
298 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200299 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200301 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
303 return @mysqli_affected_rows($this->conn_id);
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
307
308 /**
309 * Insert ID
310 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200311 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200313 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 return @mysqli_insert_id($this->conn_id);
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * "Count All" query
322 *
323 * Generates a platform-specific query string that counts all records in
324 * the specified database
325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string
327 * @return string
328 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200329 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000332 {
333 return 0;
334 }
335
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200336 $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 +0000337 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000338 {
339 return 0;
340 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000341
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200342 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500343 $this->_reset_select();
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200344 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * List table query
351 *
352 * Generates a platform-specific query string so that the table names can be fetched
353 *
354 * @access private
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200355 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return string
357 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200358 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200360 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Barry Mienydd671972010-10-04 16:33:58 +0200361
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200362 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200364 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return $sql;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Show column query
374 *
375 * Generates a platform-specific query string so that the column names can be fetched
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string the table name
378 * @return string
379 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200380 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200382 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Field data query
389 *
390 * Generates a platform-specific query so that the column data can be retrieved
391 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @param string the table name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200393 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200395 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200397 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * The error message string
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return string
406 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200407 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 return mysqli_error($this->conn_id);
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
413
414 /**
415 * The error message number
416 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200417 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200419 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 return mysqli_errno($this->conn_id);
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Escape the SQL Identifiers
428 *
429 * This function escapes column and table names
430 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @param string
432 * @return string
433 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200434 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
436 if ($this->_escape_char == '')
437 {
438 return $item;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 foreach ($this->_reserved_identifiers as $id)
442 {
443 if (strpos($item, '.'.$id) !== FALSE)
444 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200445 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200448 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200449 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 if (strpos($item, '.') !== FALSE)
453 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200454 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200458 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * From Tables
465 *
466 * This function implicitly groups FROM tables so there is no confusion
467 * about operator precedence in harmony with SQL standards
468 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200469 * @param string
470 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200472 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
474 if ( ! is_array($tables))
475 {
476 $tables = array($tables);
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 return '('.implode(', ', $tables).')';
480 }
481
482 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 /**
485 * Insert statement
486 *
487 * Generates a platform-specific insert string from the supplied data
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string the table name
490 * @param array the insert keys
491 * @param array the insert values
492 * @return string
493 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200494 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200495 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200496 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // --------------------------------------------------------------------
500
501 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500502 * Insert_batch statement
503 *
504 * Generates a platform-specific insert string from the supplied data
505 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500506 * @param string the table name
507 * @param array the insert keys
508 * @param array the insert values
509 * @return string
510 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200511 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500512 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200513 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500514 }
RH Beckercfdb2322011-10-03 17:28:32 -0700515
Pascal Kriete43ded232011-01-07 15:05:40 -0500516 // --------------------------------------------------------------------
517
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000518
519 /**
520 * Replace statement
521 *
522 * Generates a platform-specific replace string from the supplied data
523 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000524 * @param string the table name
525 * @param array the insert keys
526 * @param array the insert values
527 * @return string
528 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200529 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000530 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200531 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000532 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200533
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000534 // --------------------------------------------------------------------
535
Pascal Kriete43ded232011-01-07 15:05:40 -0500536 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * Update statement
538 *
539 * Generates a platform-specific update string from the supplied data
540 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @param string the table name
542 * @param array the update data
543 * @param array the where clause
544 * @param array the orderby clause
545 * @param array the limit clause
546 * @return string
547 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200548 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500550 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200552 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200555 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
556 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
557 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
558 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560
Pascal Kriete43ded232011-01-07 15:05:40 -0500561 // --------------------------------------------------------------------
562
563 /**
564 * Update_Batch statement
565 *
566 * Generates a platform-specific batch update string from the supplied data
567 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500568 * @param string the table name
569 * @param array the update data
570 * @param array the where clause
571 * @return string
572 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200573 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500574 {
575 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500576 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500577 {
578 $ids[] = $val[$index];
579
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500580 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500581 {
582 if ($field != $index)
583 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500584 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500585 }
586 }
587 }
588
Pascal Kriete43ded232011-01-07 15:05:40 -0500589 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500590 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500591 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200592 $cases .= $k.' = CASE '."\n"
593 .implode("\n", $v)."\n"
594 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500595 }
596
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200597 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500598
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200599 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
600 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
601 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 // --------------------------------------------------------------------
605
606 /**
607 * Truncate statement
608 *
609 * Generates a platform-specific truncate string from the supplied data
610 * If the database does not support the truncate() command
611 * This function maps to "DELETE FROM table"
612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @param string the table name
614 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200615 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200616 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200618 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Delete statement
625 *
626 * Generates a platform-specific delete string from the supplied data
627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @param string the table name
629 * @param array the where clause
630 * @param string the limit clause
631 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200632 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200633 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
635 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 if (count($where) > 0 OR count($like) > 0)
637 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200638 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000639
640 if (count($where) > 0 && count($like) > 0)
641 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200642 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 }
644 $conditions .= implode("\n", $like);
645 }
646
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200647 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
649
650 // --------------------------------------------------------------------
651
652 /**
653 * Limit string
654 *
655 * Generates a platform-specific LIMIT clause
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200658 * @param int the number of rows to limit the query to
659 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 * @return string
661 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200662 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200663 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200664 return $sql.' LIMIT '.$limit
665 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 }
667
668 // --------------------------------------------------------------------
669
670 /**
671 * Close DB Connection
672 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200673 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @return void
675 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200676 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 {
678 @mysqli_close($conn_id);
679 }
680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681}
682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683/* End of file mysqli_driver.php */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200684/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */