blob: 25b6ceca1bc7656185c52f7de6f18a8be44667f5 [file] [log] [blame]
Andrey Andreev1ff49e02012-01-27 11:30:41 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev1ff49e02012-01-27 11:30:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev1ff49e02012-01-27 11:30:41 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
Andrey Andreev1ff49e02012-01-27 11:30:41 +020029 * MySQLi Database Adapter Class
Derek Allard2067d1a2008-11-13 22:59:24 +000030 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_mysqli_driver extends CI_DB {
42
Andrey Andreev1ff49e02012-01-27 11:30:41 +020043 public $dbdriver = 'mysqli';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used for escaping
Andrey Andreev1ff49e02012-01-27 11:30:41 +020046 protected $_escape_char = '`';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev1ff49e02012-01-27 11:30:41 +020049 protected $_like_escape_str = '';
50 protected $_like_escape_chr = '';
Derek Jonese4ed5832009-02-20 21:44:59 +000051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000059
60 /**
61 * Whether to use the MySQL "delete hack" which allows the number
62 * of affected rows to be shown. Uses a preg_replace when enabled,
63 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020065 public $delete_hack = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000066
67 /**
68 * Non-persistent database connection
69 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020070 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020071 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020072 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +020074 return ($this->port != '')
75 ? @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port)
76 : @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Persistent database connection
83 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +020084 * @return object
Barry Mienydd671972010-10-04 16:33:58 +020085 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +020086 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreevf055fa92012-01-27 20:36:23 +020088 // Persistent connection support was added in PHP 5.3.0
89 if ( ! is_php('5.3'))
90 {
91 return $this->db_connect();
92 }
93
Andrey Andreev1ff49e02012-01-27 11:30:41 +020094 return ($this->port != '')
95 ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
96 : @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 // --------------------------------------------------------------------
100
101 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000102 * Reconnect
103 *
104 * Keep / reestablish the db connection if no queries have been
105 * sent for a length of time exceeding the server's idle timeout
106 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000107 * @return void
108 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200109 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000110 {
111 if (mysqli_ping($this->conn_id) === FALSE)
112 {
113 $this->conn_id = FALSE;
114 }
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 * Select the database
121 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200122 * @param string database name
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200123 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200127 if ($database === '')
128 {
129 $database = $this->database;
130 }
131
132 if (@mysqli_select_db($this->conn_id, $database))
133 {
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
147 * @param string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200148 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200150 protected function _db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
RH Beckercfdb2322011-10-03 17:28:32 -0700152 return function_exists('mysqli_set_charset')
153 ? @mysqli_set_charset($this->conn_id, $charset)
154 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200160 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @return string
163 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200164 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200166 return isset($this->data_cache['version'])
167 ? $this->data_cache['version']
168 : $this->data_cache['version'] = @mysqli_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Execute the query
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200177 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200179 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200181 return @mysqli_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Prep the query
188 *
189 * If needed, each database adapter can prep the query string
190 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 * @param string an SQL query
192 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200194 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200196 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
197 // modifies the query so that it a proper number of affected rows is returned.
198 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200200 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 return $sql;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Begin Transaction
210 *
Barry Mienydd671972010-10-04 16:33:58 +0200211 * @return bool
212 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200213 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200216 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
218 return TRUE;
219 }
220
221 // Reset the transaction failure flag.
222 // If the $test_mode flag is set to TRUE transactions will be rolled back
223 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200224 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000225
226 $this->simple_query('SET AUTOCOMMIT=0');
227 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
228 return TRUE;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Commit Transaction
235 *
Barry Mienydd671972010-10-04 16:33:58 +0200236 * @return bool
237 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200238 public function trans_commit()
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('COMMIT');
247 $this->simple_query('SET AUTOCOMMIT=1');
248 return TRUE;
249 }
250
251 // --------------------------------------------------------------------
252
253 /**
254 * Rollback Transaction
255 *
Barry Mienydd671972010-10-04 16:33:58 +0200256 * @return bool
257 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200258 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200261 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 return TRUE;
264 }
265
266 $this->simple_query('ROLLBACK');
267 $this->simple_query('SET AUTOCOMMIT=1');
268 return TRUE;
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Escape String
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @return string
279 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200280 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 if (is_array($str))
283 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500284 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200285 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200287 }
288
289 return $str;
290 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000291
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200292 if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 elseif (function_exists('mysql_escape_string'))
297 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000298 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300 else
301 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000302 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 // escape LIKE condition wildcards
306 if ($like === TRUE)
307 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200308 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Affected Rows
318 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200319 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200321 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 return @mysqli_affected_rows($this->conn_id);
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
327
328 /**
329 * Insert ID
330 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200331 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200333 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 return @mysqli_insert_id($this->conn_id);
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * "Count All" query
342 *
343 * Generates a platform-specific query string that counts all records in
344 * the specified database
345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @param string
347 * @return string
348 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200349 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000352 {
353 return 0;
354 }
355
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200356 $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 +0000357 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000358 {
359 return 0;
360 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000361
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200362 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500363 $this->_reset_select();
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200364 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * List table query
371 *
372 * Generates a platform-specific query string so that the table names can be fetched
373 *
374 * @access private
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200375 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @return string
377 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200378 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200380 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Barry Mienydd671972010-10-04 16:33:58 +0200381
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200382 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200384 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $sql;
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Show column query
394 *
395 * Generates a platform-specific query string so that the column names can be fetched
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string the table name
398 * @return string
399 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200400 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200402 return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200408 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200411 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200413 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200415 if ($table == '')
416 {
417 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
418 }
419
420 $query = $this->query('DESCRIBE '.$this->_protect_identifiers($table, TRUE, NULL, FALSE));
421 $query = $query->result_object();
422
423 $retval = array();
424 for ($i = 0, $c = count($query); $i < $c; $i++)
425 {
426 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
427
428 $retval[$i] = new stdClass();
429 $retval[$i]->name = $query[$i]->Field;
430 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
431 $retval[$i]->default = $query[$i]->Default;
432 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
433 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
434 }
435
436 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200442 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200444 * Returns an array containing code and message of the last
445 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 return array('code' => mysqli_errno($this->conn_id), 'message' => mysqli_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Escape the SQL Identifiers
458 *
459 * This function escapes column and table names
460 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @param string
462 * @return string
463 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200464 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 if ($this->_escape_char == '')
467 {
468 return $item;
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 foreach ($this->_reserved_identifiers as $id)
472 {
473 if (strpos($item, '.'.$id) !== FALSE)
474 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200475 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200478 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200479 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 if (strpos($item, '.') !== FALSE)
483 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200484 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200488 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 // --------------------------------------------------------------------
492
493 /**
494 * From Tables
495 *
496 * This function implicitly groups FROM tables so there is no confusion
497 * about operator precedence in harmony with SQL standards
498 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200499 * @param string
500 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200502 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 if ( ! is_array($tables))
505 {
506 $tables = array($tables);
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 return '('.implode(', ', $tables).')';
510 }
511
512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
515 * Insert statement
516 *
517 * Generates a platform-specific insert string from the supplied data
518 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 * @param string the table name
520 * @param array the insert keys
521 * @param array the insert values
522 * @return string
523 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200524 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200526 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // --------------------------------------------------------------------
530
531 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500532 * Insert_batch statement
533 *
534 * Generates a platform-specific insert string from the supplied data
535 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500536 * @param string the table name
537 * @param array the insert keys
538 * @param array the insert values
539 * @return string
540 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200541 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500542 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200543 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500544 }
RH Beckercfdb2322011-10-03 17:28:32 -0700545
Pascal Kriete43ded232011-01-07 15:05:40 -0500546 // --------------------------------------------------------------------
547
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000548
549 /**
550 * Replace statement
551 *
552 * Generates a platform-specific replace string from the supplied data
553 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000554 * @param string the table name
555 * @param array the insert keys
556 * @param array the insert values
557 * @return string
558 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200559 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000560 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200561 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000562 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200563
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000564 // --------------------------------------------------------------------
565
Pascal Kriete43ded232011-01-07 15:05:40 -0500566 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * Update statement
568 *
569 * Generates a platform-specific update string from the supplied data
570 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * @param string the table name
572 * @param array the update data
573 * @param array the where clause
574 * @param array the orderby clause
575 * @param array the limit clause
576 * @return string
577 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200578 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500580 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200582 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200585 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
586 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
587 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
588 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 }
590
Pascal Kriete43ded232011-01-07 15:05:40 -0500591 // --------------------------------------------------------------------
592
593 /**
594 * Update_Batch statement
595 *
596 * Generates a platform-specific batch update string from the supplied data
597 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500598 * @param string the table name
599 * @param array the update data
600 * @param array the where clause
601 * @return string
602 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200603 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500604 {
605 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500606 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500607 {
608 $ids[] = $val[$index];
609
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500610 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500611 {
612 if ($field != $index)
613 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500614 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500615 }
616 }
617 }
618
Pascal Kriete43ded232011-01-07 15:05:40 -0500619 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500620 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500621 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200622 $cases .= $k.' = CASE '."\n"
623 .implode("\n", $v)."\n"
624 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500625 }
626
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200627 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500628
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200629 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
630 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
631 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // --------------------------------------------------------------------
635
636 /**
637 * Truncate statement
638 *
639 * Generates a platform-specific truncate string from the supplied data
640 * If the database does not support the truncate() command
641 * This function maps to "DELETE FROM table"
642 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @param string the table name
644 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200646 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200648 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 }
Barry Mienydd671972010-10-04 16:33:58 +0200650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // --------------------------------------------------------------------
652
653 /**
654 * Delete statement
655 *
656 * Generates a platform-specific delete string from the supplied data
657 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000658 * @param string the table name
659 * @param array the where clause
660 * @param string the limit clause
661 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200663 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 {
665 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 if (count($where) > 0 OR count($like) > 0)
667 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200668 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000669
670 if (count($where) > 0 && count($like) > 0)
671 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200672 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 }
674 $conditions .= implode("\n", $like);
675 }
676
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200677 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 }
679
680 // --------------------------------------------------------------------
681
682 /**
683 * Limit string
684 *
685 * Generates a platform-specific LIMIT clause
686 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200688 * @param int the number of rows to limit the query to
689 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 * @return string
691 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200692 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200693 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200694 return $sql.' LIMIT '.$limit
695 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000696 }
697
698 // --------------------------------------------------------------------
699
700 /**
701 * Close DB Connection
702 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200703 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 * @return void
705 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200706 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000707 {
708 @mysqli_close($conn_id);
709 }
710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711}
712
Derek Allard2067d1a2008-11-13 22:59:24 +0000713/* End of file mysqli_driver.php */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200714/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */