blob: 43e8ac7565ae4ead2ed140a381be7f337dc4319b [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 != '')
Andrey Andreev992f1752012-03-19 16:58:43 +020075 ? @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port)
76 : @new mysqli($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 != '')
Andrey Andreev992f1752012-03-19 16:58:43 +020095 ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
96 : @new mysqli('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 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200111 if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 {
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 Andreev024ba2d2012-02-24 11:40:36 +0200127 if ($database === '')
128 {
129 $database = $this->database;
130 }
131
Andrey Andreev992f1752012-03-19 16:58:43 +0200132 if (@$this->conn_id->select_db($database))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200133 {
134 $this->database = $database;
135 return TRUE;
136 }
137
138 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Set client character set
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200147 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200149 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200151 return @$this->conn_id->set_charset($charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200157 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @return string
160 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200161 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200163 return isset($this->data_cache['version'])
164 ? $this->data_cache['version']
Andrey Andreev992f1752012-03-19 16:58:43 +0200165 : $this->data_cache['version'] = $this->conn_id->server_info;
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200174 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200176 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200178 return @$this->conn_id->query($this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Prep the query
185 *
186 * If needed, each database adapter can prep the query string
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string an SQL query
189 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200191 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200193 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
194 // modifies the query so that it a proper number of affected rows is returned.
195 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200197 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 return $sql;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Begin Transaction
207 *
Barry Mienydd671972010-10-04 16:33:58 +0200208 * @return bool
209 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200210 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200213 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 return TRUE;
216 }
217
218 // Reset the transaction failure flag.
219 // If the $test_mode flag is set to TRUE transactions will be rolled back
220 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200221 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
223 $this->simple_query('SET AUTOCOMMIT=0');
224 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
225 return TRUE;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Commit Transaction
232 *
Barry Mienydd671972010-10-04 16:33:58 +0200233 * @return bool
234 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200235 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200238 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 return TRUE;
241 }
242
243 $this->simple_query('COMMIT');
244 $this->simple_query('SET AUTOCOMMIT=1');
245 return TRUE;
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Rollback Transaction
252 *
Barry Mienydd671972010-10-04 16:33:58 +0200253 * @return bool
254 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200255 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200258 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return TRUE;
261 }
262
263 $this->simple_query('ROLLBACK');
264 $this->simple_query('SET AUTOCOMMIT=1');
265 return TRUE;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Escape String
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
276 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200277 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000279 if (is_array($str))
280 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500281 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200282 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200284 }
285
286 return $str;
287 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000288
Andrey Andreev992f1752012-03-19 16:58:43 +0200289 $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
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 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200309 return $this->conn_id->affected_rows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
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 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200321 return $this->conn_id->insert_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
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 Andreev032e7ea2012-03-06 19:48:35 +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 Andreev032e7ea2012-03-06 19:48:35 +0200388 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390
391 // --------------------------------------------------------------------
392
393 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200394 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200397 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200399 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200401 if ($table == '')
402 {
403 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
404 }
405
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200406 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200407 $query = $query->result_object();
408
409 $retval = array();
410 for ($i = 0, $c = count($query); $i < $c; $i++)
411 {
412 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
413
414 $retval[$i] = new stdClass();
415 $retval[$i]->name = $query[$i]->Field;
416 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
417 $retval[$i]->default = $query[$i]->Default;
418 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
419 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
420 }
421
422 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
424
425 // --------------------------------------------------------------------
426
427 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200428 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200430 * Returns an array containing code and message of the last
431 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200433 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200435 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200437 return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * Escape the SQL Identifiers
444 *
445 * This function escapes column and table names
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param string
448 * @return string
449 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200450 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 if ($this->_escape_char == '')
453 {
454 return $item;
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 foreach ($this->_reserved_identifiers as $id)
458 {
459 if (strpos($item, '.'.$id) !== FALSE)
460 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200461 $item = str_replace('.', $this->_escape_char.'.', $item);
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);
Barry Mienydd671972010-10-04 16:33:58 +0200465 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 if (strpos($item, '.') !== FALSE)
469 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200470 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200474 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
478
479 /**
480 * From Tables
481 *
482 * This function implicitly groups FROM tables so there is no confusion
483 * about operator precedence in harmony with SQL standards
484 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200485 * @param string
486 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200488 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
490 if ( ! is_array($tables))
491 {
492 $tables = array($tables);
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 return '('.implode(', ', $tables).')';
496 }
497
498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 /**
501 * Insert statement
502 *
503 * Generates a platform-specific insert string from the supplied data
504 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @param string the table name
506 * @param array the insert keys
507 * @param array the insert values
508 * @return string
509 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200510 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200511 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200512 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // --------------------------------------------------------------------
516
517 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500518 * Insert_batch statement
519 *
520 * Generates a platform-specific insert string from the supplied data
521 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500522 * @param string the table name
523 * @param array the insert keys
524 * @param array the insert values
525 * @return string
526 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200527 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500528 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200529 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500530 }
RH Beckercfdb2322011-10-03 17:28:32 -0700531
Pascal Kriete43ded232011-01-07 15:05:40 -0500532 // --------------------------------------------------------------------
533
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000534
535 /**
536 * Replace statement
537 *
538 * Generates a platform-specific replace string from the supplied data
539 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000540 * @param string the table name
541 * @param array the insert keys
542 * @param array the insert values
543 * @return string
544 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200545 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000546 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200547 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000548 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200549
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000550 // --------------------------------------------------------------------
551
Pascal Kriete43ded232011-01-07 15:05:40 -0500552 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * Update statement
554 *
555 * Generates a platform-specific update string from the supplied data
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @param string the table name
558 * @param array the update data
559 * @param array the where clause
560 * @param array the orderby clause
561 * @param array the limit clause
562 * @return string
563 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200564 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500566 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200568 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200571 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
572 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
573 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
574 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576
Pascal Kriete43ded232011-01-07 15:05:40 -0500577 // --------------------------------------------------------------------
578
579 /**
580 * Update_Batch statement
581 *
582 * Generates a platform-specific batch update string from the supplied data
583 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500584 * @param string the table name
585 * @param array the update data
586 * @param array the where clause
587 * @return string
588 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200589 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500590 {
591 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500592 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500593 {
594 $ids[] = $val[$index];
595
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500596 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500597 {
598 if ($field != $index)
599 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500600 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500601 }
602 }
603 }
604
Pascal Kriete43ded232011-01-07 15:05:40 -0500605 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500606 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500607 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200608 $cases .= $k.' = CASE '."\n"
609 .implode("\n", $v)."\n"
610 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500611 }
612
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200613 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500614
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200615 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
616 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
617 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500618 }
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 // --------------------------------------------------------------------
621
622 /**
623 * Truncate statement
624 *
625 * Generates a platform-specific truncate string from the supplied data
626 * If the database does not support the truncate() command
627 * This function maps to "DELETE FROM table"
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param string the table name
630 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200631 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200632 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200634 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
Barry Mienydd671972010-10-04 16:33:58 +0200636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 // --------------------------------------------------------------------
638
639 /**
640 * Delete statement
641 *
642 * Generates a platform-specific delete string from the supplied data
643 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 * @param string the table name
645 * @param array the where clause
646 * @param string the limit clause
647 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200648 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200649 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 if (count($where) > 0 OR count($like) > 0)
653 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200654 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
656 if (count($where) > 0 && count($like) > 0)
657 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200658 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660 $conditions .= implode("\n", $like);
661 }
662
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200663 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Limit string
670 *
671 * Generates a platform-specific LIMIT clause
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200674 * @param int the number of rows to limit the query to
675 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * @return string
677 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200678 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200679 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200680 return $sql.' LIMIT '.$limit
681 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 }
683
684 // --------------------------------------------------------------------
685
686 /**
687 * Close DB Connection
688 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200689 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @return void
691 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200692 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
Andrey Andreev992f1752012-03-19 16:58:43 +0200694 $this->conn_id->close();
695 $this->conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698}
699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700/* End of file mysqli_driver.php */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200701/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */