blob: a7f08d1a8deef8fc46c88f6f92f2cdfac61291e7 [file] [log] [blame]
Andrey Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev2caf2892012-01-27 00:12:03 +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/**
29 * MySQL Database Adapter Class
30 *
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_mysql_driver extends CI_DB {
42
Andrey Andreev2caf2892012-01-27 00:12:03 +020043 public $dbdriver = 'mysql';
Derek Allard2067d1a2008-11-13 22:59:24 +000044
45 // The character used for escaping
Andrey Andreev2caf2892012-01-27 00:12:03 +020046 protected $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 protected $_like_escape_str = '';
50 protected $_like_escape_chr = '';
Barry Mienydd671972010-10-04 16:33:58 +020051
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 Andreev2caf2892012-01-27 00:12:03 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
Andrey Andreev2caf2892012-01-27 00:12:03 +020061 * 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 Andreev2caf2892012-01-27 00:12:03 +020065 public $delete_hack = TRUE;
66
67 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020069 parent::__construct($params);
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if ($this->port != '')
72 {
73 $this->hostname .= ':'.$this->port;
74 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020075 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev2caf2892012-01-27 00:12:03 +020077 /**
78 * Non-persistent database connection
79 *
80 * @return resource
81 */
82 public function db_connect()
83 {
Derek Allard2067d1a2008-11-13 22:59:24 +000084 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
90 * Persistent database connection
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020093 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020094 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return @mysql_pconnect($this->hostname, $this->username, $this->password);
97 }
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 Andreev2caf2892012-01-27 00:12:03 +0200109 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000110 {
111 if (mysql_ping($this->conn_id) === FALSE)
112 {
113 $this->conn_id = FALSE;
114 }
115 }
116
117 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 /**
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 Andreev2caf2892012-01-27 00:12:03 +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 (@mysql_select_db($database, $this->conn_id))
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 Andreev2caf2892012-01-27 00:12:03 +0200148 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200150 public 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('mysql_set_charset')
153 ? @mysql_set_charset($charset, $this->conn_id)
154 : @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
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 /**
160 * Version number query string
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @return string
163 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200164 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200166 return 'SELECT version() AS ver';
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 Andreev2caf2892012-01-27 00:12:03 +0200175 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200177 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200179 return @mysql_query($this->_prep_query($sql), $this->conn_id);
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 Andreev2caf2892012-01-27 00:12:03 +0200192 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200194 // mysql_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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200222 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
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 Andreev2caf2892012-01-27 00:12:03 +0200278 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200279 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if (is_array($str))
281 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500282 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500283 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000284 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Jones37f4b9c2011-07-01 17:56:50 -0500287 return $str;
288 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000289
Andrey Andreev2caf2892012-01-27 00:12:03 +0200290 if (function_exists('mysql_real_escape_string') && is_resource($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000292 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294 elseif (function_exists('mysql_escape_string'))
295 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000296 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298 else
299 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 // escape LIKE condition wildcards
304 if ($like === TRUE)
305 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200306 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 return $str;
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 * Affected Rows
316 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200317 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200319 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return @mysql_affected_rows($this->conn_id);
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
325
326 /**
327 * Insert ID
328 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200329 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200331 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 return @mysql_insert_id($this->conn_id);
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * "Count All" query
340 *
341 * Generates a platform-specific query string that counts all records in
342 * the specified database
343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @param string
345 * @return string
346 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200347 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000350 {
351 return 0;
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Andrey Andreev2caf2892012-01-27 00:12:03 +0200354 $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 +0000355 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000356 {
357 return 0;
358 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000359
Andrey Andreev2caf2892012-01-27 00:12:03 +0200360 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500361 $this->_reset_select();
Andrey Andreev2caf2892012-01-27 00:12:03 +0200362 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * List table query
369 *
370 * Generates a platform-specific query string so that the table names can be fetched
371 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200372 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @return string
374 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200375 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200377 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
Andrey Andreev2caf2892012-01-27 00:12:03 +0200379 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200381 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 return $sql;
385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // --------------------------------------------------------------------
388
389 /**
390 * Show column query
391 *
392 * Generates a platform-specific query string so that the column names can be fetched
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param string the table name
395 * @return string
396 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200397 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200399 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Field data query
406 *
407 * Generates a platform-specific query so that the column data can be retrieved
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200410 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200412 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200414 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200420 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200422 * Returns an array containing code and message of the last
423 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200425 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200427 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Escape the SQL Identifiers
436 *
437 * This function escapes column and table names
438 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @param string
440 * @return string
441 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200442 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 if ($this->_escape_char == '')
445 {
446 return $item;
447 }
448
449 foreach ($this->_reserved_identifiers as $id)
450 {
451 if (strpos($item, '.'.$id) !== FALSE)
452 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200453 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200456 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200457 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 if (strpos($item, '.') !== FALSE)
461 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200462 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200466 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
470
471 /**
472 * From Tables
473 *
474 * This function implicitly groups FROM tables so there is no confusion
475 * about operator precedence in harmony with SQL standards
476 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200477 * @param string table name
478 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200480 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
482 if ( ! is_array($tables))
483 {
484 $tables = array($tables);
485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 return '('.implode(', ', $tables).')';
488 }
489
490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 /**
493 * Insert statement
494 *
495 * Generates a platform-specific insert string from the supplied data
496 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @param string the table name
498 * @param array the insert keys
499 * @param array the insert values
500 * @return string
501 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200502 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200503 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200504 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 // --------------------------------------------------------------------
508
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Jones20aa2bd2010-03-02 17:28:07 -0600510 /**
511 * Replace statement
512 *
513 * Generates a platform-specific replace string from the supplied data
514 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600515 * @param string the table name
516 * @param array the insert keys
517 * @param array the insert values
518 * @return string
519 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200520 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200521 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200522 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600523 }
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Jones20aa2bd2010-03-02 17:28:07 -0600525 // --------------------------------------------------------------------
526
527 /**
528 * Insert_batch statement
529 *
530 * Generates a platform-specific insert string from the supplied data
531 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600532 * @param string the table name
533 * @param array the insert keys
534 * @param array the insert values
535 * @return string
536 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200537 protected function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200538 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200539 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Jones20aa2bd2010-03-02 17:28:07 -0600542 // --------------------------------------------------------------------
543
544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 /**
546 * Update statement
547 *
548 * Generates a platform-specific update string from the supplied data
549 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @param string the table name
551 * @param array the update data
552 * @param array the where clause
553 * @param array the orderby clause
554 * @param array the limit clause
555 * @return string
556 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200557 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500559 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200561 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 }
Barry Mienydd671972010-10-04 16:33:58 +0200563
Andrey Andreev2caf2892012-01-27 00:12:03 +0200564 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
565 if (count($like) > 0)
Mancy205d0292011-12-20 12:45:58 +0300566 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200567 $where .= ($where == '' ? ' WHERE ' : ' AND ').implode(' ', $like);
Mancyc4caf5a2011-12-20 11:47:51 +0300568 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000569
Andrey Andreev2caf2892012-01-27 00:12:03 +0200570 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
571 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
572 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 }
574
575 // --------------------------------------------------------------------
576
Derek Jones20aa2bd2010-03-02 17:28:07 -0600577
578 /**
579 * Update_Batch statement
580 *
581 * Generates a platform-specific batch update string from the supplied data
582 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600583 * @param string the table name
584 * @param array the update data
585 * @param array the where clause
586 * @return string
587 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200588 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600589 {
590 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500591 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600592 {
593 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200594
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500595 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200596 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600597 if ($field != $index)
598 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500599 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600600 }
601 }
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Jones20aa2bd2010-03-02 17:28:07 -0600604 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500605 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600606 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200607 $cases .= $k." = CASE \n"
608 .implode("\n", $v)."\n"
609 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Andrey Andreev2caf2892012-01-27 00:12:03 +0200612 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
613 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
614 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600615 }
616
617 // --------------------------------------------------------------------
618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 /**
620 * Truncate statement
621 *
622 * Generates a platform-specific truncate string from the supplied data
623 * If the database does not support the truncate() command
624 * This function maps to "DELETE FROM table"
625 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * @param string the table name
627 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200628 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200629 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200631 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
Barry Mienydd671972010-10-04 16:33:58 +0200633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // --------------------------------------------------------------------
635
636 /**
637 * Delete statement
638 *
639 * Generates a platform-specific delete string from the supplied data
640 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 * @param string the table name
642 * @param array the where clause
643 * @param string the limit clause
644 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200645 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200646 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 {
648 $conditions = '';
649
650 if (count($where) > 0 OR count($like) > 0)
651 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200652 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653
654 if (count($where) > 0 && count($like) > 0)
655 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200656 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658 $conditions .= implode("\n", $like);
659 }
660
Andrey Andreev2caf2892012-01-27 00:12:03 +0200661 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 }
663
664 // --------------------------------------------------------------------
665
666 /**
667 * Limit string
668 *
669 * Generates a platform-specific LIMIT clause
670 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @param string the sql query string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200672 * @param int the number of rows to limit the query to
673 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 * @return string
675 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200676 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200677 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200678 return $sql.' LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * Close DB Connection
685 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 * @param resource
687 * @return void
688 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200689 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 {
691 @mysql_close($conn_id);
692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694}
695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696/* End of file mysql_driver.php */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200697/* Location: ./system/database/drivers/mysql/mysql_driver.php */