blob: a79b2a4adb81ba2c9959ae314182ce2b3de9421c [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 Andreev1ff49e02012-01-27 11:30:41 +0200122 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200123 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200124 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 return @mysqli_select_db($this->conn_id, $this->database);
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Set client character set
133 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @param string
135 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200136 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200138 protected function _db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
RH Beckercfdb2322011-10-03 17:28:32 -0700140 return function_exists('mysqli_set_charset')
141 ? @mysqli_set_charset($this->conn_id, $charset)
142 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
148 * Version number query string
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @return string
151 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200152 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200154 return @mysqli_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Execute the query
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200163 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200165 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200167 return @mysqli_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
171
172 /**
173 * Prep the query
174 *
175 * If needed, each database adapter can prep the query string
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string an SQL query
178 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200180 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200182 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
183 // modifies the query so that it a proper number of affected rows is returned.
184 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200186 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 return $sql;
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Begin Transaction
196 *
Barry Mienydd671972010-10-04 16:33:58 +0200197 * @return bool
198 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200199 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200202 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 return TRUE;
205 }
206
207 // Reset the transaction failure flag.
208 // If the $test_mode flag is set to TRUE transactions will be rolled back
209 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200210 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
212 $this->simple_query('SET AUTOCOMMIT=0');
213 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
214 return TRUE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Commit Transaction
221 *
Barry Mienydd671972010-10-04 16:33:58 +0200222 * @return bool
223 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200224 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return TRUE;
230 }
231
232 $this->simple_query('COMMIT');
233 $this->simple_query('SET AUTOCOMMIT=1');
234 return TRUE;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rollback Transaction
241 *
Barry Mienydd671972010-10-04 16:33:58 +0200242 * @return bool
243 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200244 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200247 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 return TRUE;
250 }
251
252 $this->simple_query('ROLLBACK');
253 $this->simple_query('SET AUTOCOMMIT=1');
254 return TRUE;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Escape String
261 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000263 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return string
265 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200266 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000268 if (is_array($str))
269 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500270 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200271 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000272 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200273 }
274
275 return $str;
276 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000277
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200278 if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000280 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282 elseif (function_exists('mysql_escape_string'))
283 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000284 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286 else
287 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000288 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 // escape LIKE condition wildcards
292 if ($like === TRUE)
293 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200294 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Affected Rows
304 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200305 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200307 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 return @mysqli_affected_rows($this->conn_id);
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Insert ID
316 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200317 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200319 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return @mysqli_insert_id($this->conn_id);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * "Count All" query
328 *
329 * Generates a platform-specific query string that counts all records in
330 * the specified database
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string
333 * @return string
334 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200335 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000338 {
339 return 0;
340 }
341
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200342 $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 +0000343 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000344 {
345 return 0;
346 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200348 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500349 $this->_reset_select();
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200350 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * List table query
357 *
358 * Generates a platform-specific query string so that the table names can be fetched
359 *
360 * @access private
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200361 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @return string
363 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200364 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200366 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Barry Mienydd671972010-10-04 16:33:58 +0200367
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200368 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200370 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 return $sql;
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Show column query
380 *
381 * Generates a platform-specific query string so that the column names can be fetched
382 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @param string the table name
384 * @return string
385 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200386 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200388 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Field data query
395 *
396 * Generates a platform-specific query so that the column data can be retrieved
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @param string the table name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200399 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200401 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200403 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * The error message string
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return string
412 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200413 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return mysqli_error($this->conn_id);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * The error message number
422 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200423 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200425 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 return mysqli_errno($this->conn_id);
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Escape the SQL Identifiers
434 *
435 * This function escapes column and table names
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string
438 * @return string
439 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200440 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 if ($this->_escape_char == '')
443 {
444 return $item;
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 foreach ($this->_reserved_identifiers as $id)
448 {
449 if (strpos($item, '.'.$id) !== FALSE)
450 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200451 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200454 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200455 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if (strpos($item, '.') !== FALSE)
459 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200460 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200464 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
468
469 /**
470 * From Tables
471 *
472 * This function implicitly groups FROM tables so there is no confusion
473 * about operator precedence in harmony with SQL standards
474 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200475 * @param string
476 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200478 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 if ( ! is_array($tables))
481 {
482 $tables = array($tables);
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 return '('.implode(', ', $tables).')';
486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Insert statement
492 *
493 * Generates a platform-specific insert string from the supplied data
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string the table name
496 * @param array the insert keys
497 * @param array the insert values
498 * @return string
499 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200500 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200501 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200502 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500508 * Insert_batch statement
509 *
510 * Generates a platform-specific insert string from the supplied data
511 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200517 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500518 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200519 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500520 }
RH Beckercfdb2322011-10-03 17:28:32 -0700521
Pascal Kriete43ded232011-01-07 15:05:40 -0500522 // --------------------------------------------------------------------
523
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000524
525 /**
526 * Replace statement
527 *
528 * Generates a platform-specific replace string from the supplied data
529 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000530 * @param string the table name
531 * @param array the insert keys
532 * @param array the insert values
533 * @return string
534 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200535 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000536 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200537 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000538 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200539
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000540 // --------------------------------------------------------------------
541
Pascal Kriete43ded232011-01-07 15:05:40 -0500542 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 * Update statement
544 *
545 * Generates a platform-specific update string from the supplied data
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string the table name
548 * @param array the update data
549 * @param array the where clause
550 * @param array the orderby clause
551 * @param array the limit clause
552 * @return string
553 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200554 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500556 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200558 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200561 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
562 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
563 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
564 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566
Pascal Kriete43ded232011-01-07 15:05:40 -0500567 // --------------------------------------------------------------------
568
569 /**
570 * Update_Batch statement
571 *
572 * Generates a platform-specific batch update string from the supplied data
573 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500574 * @param string the table name
575 * @param array the update data
576 * @param array the where clause
577 * @return string
578 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200579 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500580 {
581 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500582 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500583 {
584 $ids[] = $val[$index];
585
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500586 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500587 {
588 if ($field != $index)
589 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500590 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500591 }
592 }
593 }
594
Pascal Kriete43ded232011-01-07 15:05:40 -0500595 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500596 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500597 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200598 $cases .= $k.' = CASE '."\n"
599 .implode("\n", $v)."\n"
600 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500601 }
602
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200603 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500604
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200605 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
606 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
607 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // --------------------------------------------------------------------
611
612 /**
613 * Truncate statement
614 *
615 * Generates a platform-specific truncate string from the supplied data
616 * If the database does not support the truncate() command
617 * This function maps to "DELETE FROM table"
618 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @param string the table name
620 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200621 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200622 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200624 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 // --------------------------------------------------------------------
628
629 /**
630 * Delete statement
631 *
632 * Generates a platform-specific delete string from the supplied data
633 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 * @param string the table name
635 * @param array the where clause
636 * @param string the limit clause
637 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200638 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200639 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 {
641 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 if (count($where) > 0 OR count($like) > 0)
643 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200644 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645
646 if (count($where) > 0 && count($like) > 0)
647 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200648 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
650 $conditions .= implode("\n", $like);
651 }
652
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200653 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655
656 // --------------------------------------------------------------------
657
658 /**
659 * Limit string
660 *
661 * Generates a platform-specific LIMIT clause
662 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200664 * @param int the number of rows to limit the query to
665 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return string
667 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200668 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200669 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200670 return $sql.' LIMIT '.$limit
671 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 }
673
674 // --------------------------------------------------------------------
675
676 /**
677 * Close DB Connection
678 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200679 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 * @return void
681 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200682 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 {
684 @mysqli_close($conn_id);
685 }
686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687}
688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689/* End of file mysqli_driver.php */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200690/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */