blob: e84b8346d656ac5ee2f0a14e43caabe4e8fce4ff [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 != '')
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 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200152 return @mysqli_set_charset($this->conn_id, $charset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200158 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @return string
161 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200162 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200164 return isset($this->data_cache['version'])
165 ? $this->data_cache['version']
166 : $this->data_cache['version'] = @mysqli_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Execute the query
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string an SQL query
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200175 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200177 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200179 return @mysqli_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // --------------------------------------------------------------------
183
184 /**
185 * Prep the query
186 *
187 * If needed, each database adapter can prep the query string
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string an SQL query
190 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200192 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200194 // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
195 // modifies the query so that it a proper number of affected rows is returned.
196 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200198 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 return $sql;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Begin Transaction
208 *
Barry Mienydd671972010-10-04 16:33:58 +0200209 * @return bool
210 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200211 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200214 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 return TRUE;
217 }
218
219 // Reset the transaction failure flag.
220 // If the $test_mode flag is set to TRUE transactions will be rolled back
221 // even if the queries produce a successful result.
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200222 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000223
224 $this->simple_query('SET AUTOCOMMIT=0');
225 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
226 return TRUE;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Commit Transaction
233 *
Barry Mienydd671972010-10-04 16:33:58 +0200234 * @return bool
235 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200236 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200239 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
241 return TRUE;
242 }
243
244 $this->simple_query('COMMIT');
245 $this->simple_query('SET AUTOCOMMIT=1');
246 return TRUE;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Rollback Transaction
253 *
Barry Mienydd671972010-10-04 16:33:58 +0200254 * @return bool
255 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200256 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200259 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 return TRUE;
262 }
263
264 $this->simple_query('ROLLBACK');
265 $this->simple_query('SET AUTOCOMMIT=1');
266 return TRUE;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Escape String
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000275 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @return string
277 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200278 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000280 if (is_array($str))
281 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500282 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200283 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000284 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200285 }
286
287 return $str;
288 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000289
Andrey Andreevf6a41142012-03-12 15:34:10 +0200290 $str = is_object($this->conn_id) ? mysqli_real_escape_string($this->conn_id, $str) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Jonese4ed5832009-02-20 21:44:59 +0000292 // escape LIKE condition wildcards
293 if ($like === TRUE)
294 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200295 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jonese4ed5832009-02-20 21:44:59 +0000298 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // --------------------------------------------------------------------
302
303 /**
304 * Affected Rows
305 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200306 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200308 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 return @mysqli_affected_rows($this->conn_id);
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
316 * Insert ID
317 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200318 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200320 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 return @mysqli_insert_id($this->conn_id);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * "Count All" query
329 *
330 * Generates a platform-specific query string that counts all records in
331 * the specified database
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @return string
335 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200336 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000339 {
340 return 0;
341 }
342
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200343 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000345 {
346 return 0;
347 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200349 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500350 $this->_reset_select();
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200351 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * List table query
358 *
359 * Generates a platform-specific query string so that the table names can be fetched
360 *
361 * @access private
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200362 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @return string
364 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200365 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200367 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Barry Mienydd671972010-10-04 16:33:58 +0200368
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200369 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200371 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 return $sql;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Show column query
381 *
382 * Generates a platform-specific query string so that the column names can be fetched
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string the table name
385 * @return string
386 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200387 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200389 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200395 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200398 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200400 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200402 if ($table == '')
403 {
404 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
405 }
406
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200407 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200408 $query = $query->result_object();
409
410 $retval = array();
411 for ($i = 0, $c = count($query); $i < $c; $i++)
412 {
413 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
414
415 $retval[$i] = new stdClass();
416 $retval[$i]->name = $query[$i]->Field;
417 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
418 $retval[$i]->default = $query[$i]->Default;
419 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
420 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
421 }
422
423 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 * Returns an array containing code and message of the last
432 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200436 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200438 return array('code' => mysqli_errno($this->conn_id), 'message' => mysqli_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Escape the SQL Identifiers
445 *
446 * This function escapes column and table names
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string
449 * @return string
450 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200451 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 if ($this->_escape_char == '')
454 {
455 return $item;
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 foreach ($this->_reserved_identifiers as $id)
459 {
460 if (strpos($item, '.'.$id) !== FALSE)
461 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200462 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200465 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 if (strpos($item, '.') !== FALSE)
470 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200471 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // remove duplicates if the user already included the escape
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200475 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * From Tables
482 *
483 * This function implicitly groups FROM tables so there is no confusion
484 * about operator precedence in harmony with SQL standards
485 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200486 * @param string
487 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200489 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 if ( ! is_array($tables))
492 {
493 $tables = array($tables);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return '('.implode(', ', $tables).')';
497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
502 * Insert statement
503 *
504 * Generates a platform-specific insert string from the supplied data
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string the table name
507 * @param array the insert keys
508 * @param array the insert values
509 * @return string
510 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200511 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200512 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200513 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500519 * Insert_batch statement
520 *
521 * Generates a platform-specific insert string from the supplied data
522 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500523 * @param string the table name
524 * @param array the insert keys
525 * @param array the insert values
526 * @return string
527 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200528 protected function _insert_batch($table, $keys, $values)
Pascal Kriete43ded232011-01-07 15:05:40 -0500529 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200530 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Pascal Kriete43ded232011-01-07 15:05:40 -0500531 }
RH Beckercfdb2322011-10-03 17:28:32 -0700532
Pascal Kriete43ded232011-01-07 15:05:40 -0500533 // --------------------------------------------------------------------
534
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000535
536 /**
537 * Replace statement
538 *
539 * Generates a platform-specific replace string from the supplied data
540 *
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000541 * @param string the table name
542 * @param array the insert keys
543 * @param array the insert values
544 * @return string
545 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200546 protected function _replace($table, $keys, $values)
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000547 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200548 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000549 }
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200550
Phil Sturgeon3b3782a2011-11-08 14:49:24 +0000551 // --------------------------------------------------------------------
552
Pascal Kriete43ded232011-01-07 15:05:40 -0500553 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * Update statement
555 *
556 * Generates a platform-specific update string from the supplied data
557 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @param string the table name
559 * @param array the update data
560 * @param array the where clause
561 * @param array the orderby clause
562 * @param array the limit clause
563 * @return string
564 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200565 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500567 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200569 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200572 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
573 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
574 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
575 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577
Pascal Kriete43ded232011-01-07 15:05:40 -0500578 // --------------------------------------------------------------------
579
580 /**
581 * Update_Batch statement
582 *
583 * Generates a platform-specific batch update string from the supplied data
584 *
Pascal Kriete43ded232011-01-07 15:05:40 -0500585 * @param string the table name
586 * @param array the update data
587 * @param array the where clause
588 * @return string
589 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200590 protected function _update_batch($table, $values, $index, $where = NULL)
Pascal Kriete43ded232011-01-07 15:05:40 -0500591 {
592 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500593 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500594 {
595 $ids[] = $val[$index];
596
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500597 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500598 {
599 if ($field != $index)
600 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500601 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500602 }
603 }
604 }
605
Pascal Kriete43ded232011-01-07 15:05:40 -0500606 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500607 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500608 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200609 $cases .= $k.' = CASE '."\n"
610 .implode("\n", $v)."\n"
611 .'ELSE '.$k.' END, ';
Pascal Kriete43ded232011-01-07 15:05:40 -0500612 }
613
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200614 $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '';
Pascal Kriete43ded232011-01-07 15:05:40 -0500615
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200616 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
617 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
618 .$index.' IN('.implode(',', $ids).')';
Pascal Kriete43ded232011-01-07 15:05:40 -0500619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Truncate statement
625 *
626 * Generates a platform-specific truncate string from the supplied data
627 * If the database does not support the truncate() command
628 * This function maps to "DELETE FROM table"
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @param string the table name
631 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200632 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200633 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200635 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 // --------------------------------------------------------------------
639
640 /**
641 * Delete statement
642 *
643 * Generates a platform-specific delete string from the supplied data
644 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 * @param string the table name
646 * @param array the where clause
647 * @param string the limit clause
648 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200649 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200650 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 {
652 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000653 if (count($where) > 0 OR count($like) > 0)
654 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200655 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
657 if (count($where) > 0 && count($like) > 0)
658 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200659 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661 $conditions .= implode("\n", $like);
662 }
663
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200664 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000665 }
666
667 // --------------------------------------------------------------------
668
669 /**
670 * Limit string
671 *
672 * Generates a platform-specific LIMIT clause
673 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @param string the sql query string
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200675 * @param int the number of rows to limit the query to
676 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 * @return string
678 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200679 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200680 {
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200681 return $sql.' LIMIT '.$limit
682 .($offset > 0 ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 }
684
685 // --------------------------------------------------------------------
686
687 /**
688 * Close DB Connection
689 *
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200690 * @param object
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 * @return void
692 */
Andrey Andreev1ff49e02012-01-27 11:30:41 +0200693 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 {
695 @mysqli_close($conn_id);
696 }
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 */