blob: 7108a6db18a1581be060eec0d5f68eac2bf2fff0 [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 /**
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'] = @mysql_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 Andreev2caf2892012-01-27 00:12:03 +0200177 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200179 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200181 return @mysql_query($this->_prep_query($sql), $this->conn_id);
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 Andreev2caf2892012-01-27 00:12:03 +0200194 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200196 // mysql_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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200224 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +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 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
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 Andreev2caf2892012-01-27 00:12:03 +0200280 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200281 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 if (is_array($str))
283 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500284 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500285 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jones37f4b9c2011-07-01 17:56:50 -0500289 return $str;
290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
Andrey Andreev2caf2892012-01-27 00:12:03 +0200292 if (function_exists('mysql_real_escape_string') && is_resource($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 $str = mysql_real_escape_string($str, $this->conn_id);
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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200319 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200321 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 return @mysql_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 Andreev2caf2892012-01-27 00:12:03 +0200331 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200333 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 return @mysql_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 Andreev2caf2892012-01-27 00:12:03 +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 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200362 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500363 $this->_reset_select();
Andrey Andreev2caf2892012-01-27 00:12:03 +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 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200374 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @return string
376 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200377 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200379 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380
Andrey Andreev2caf2892012-01-27 00:12:03 +0200381 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200383 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
385
386 return $sql;
387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // --------------------------------------------------------------------
390
391 /**
392 * Show column query
393 *
394 * Generates a platform-specific query string so that the column names can be fetched
395 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param string the table name
397 * @return string
398 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200399 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200401 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403
404 // --------------------------------------------------------------------
405
406 /**
407 * Field data query
408 *
409 * Generates a platform-specific query so that the column data can be retrieved
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200412 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200414 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200416 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200422 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200424 * Returns an array containing code and message of the last
425 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200427 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433
434 // --------------------------------------------------------------------
435
436 /**
437 * Escape the SQL Identifiers
438 *
439 * This function escapes column and table names
440 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @param string
442 * @return string
443 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200444 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 {
446 if ($this->_escape_char == '')
447 {
448 return $item;
449 }
450
451 foreach ($this->_reserved_identifiers as $id)
452 {
453 if (strpos($item, '.'.$id) !== FALSE)
454 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200455 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200458 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200459 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 if (strpos($item, '.') !== FALSE)
463 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200464 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200468 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
472
473 /**
474 * From Tables
475 *
476 * This function implicitly groups FROM tables so there is no confusion
477 * about operator precedence in harmony with SQL standards
478 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200479 * @param string table name
480 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200482 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 if ( ! is_array($tables))
485 {
486 $tables = array($tables);
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 return '('.implode(', ', $tables).')';
490 }
491
492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 /**
495 * Insert statement
496 *
497 * Generates a platform-specific insert string from the supplied data
498 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @param string the table name
500 * @param array the insert keys
501 * @param array the insert values
502 * @return string
503 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200504 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200505 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200506 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Jones20aa2bd2010-03-02 17:28:07 -0600512 /**
513 * Replace statement
514 *
515 * Generates a platform-specific replace string from the supplied data
516 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600517 * @param string the table name
518 * @param array the insert keys
519 * @param array the insert values
520 * @return string
521 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200522 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200523 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200524 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Jones20aa2bd2010-03-02 17:28:07 -0600527 // --------------------------------------------------------------------
528
529 /**
530 * Insert_batch statement
531 *
532 * Generates a platform-specific insert string from the supplied data
533 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600534 * @param string the table name
535 * @param array the insert keys
536 * @param array the insert values
537 * @return string
538 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200539 protected function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200540 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200541 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Jones20aa2bd2010-03-02 17:28:07 -0600544 // --------------------------------------------------------------------
545
546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 /**
548 * Update statement
549 *
550 * Generates a platform-specific update string from the supplied data
551 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 * @param string the table name
553 * @param array the update data
554 * @param array the where clause
555 * @param array the orderby clause
556 * @param array the limit clause
557 * @return string
558 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200559 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500561 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200563 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
Barry Mienydd671972010-10-04 16:33:58 +0200565
Andrey Andreev2caf2892012-01-27 00:12:03 +0200566 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
567 if (count($like) > 0)
Mancy205d0292011-12-20 12:45:58 +0300568 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200569 $where .= ($where == '' ? ' WHERE ' : ' AND ').implode(' ', $like);
Mancyc4caf5a2011-12-20 11:47:51 +0300570 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000571
Andrey Andreev2caf2892012-01-27 00:12:03 +0200572 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
573 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
574 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 }
576
577 // --------------------------------------------------------------------
578
Derek Jones20aa2bd2010-03-02 17:28:07 -0600579
580 /**
581 * Update_Batch statement
582 *
583 * Generates a platform-specific batch update string from the supplied data
584 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600585 * @param string the table name
586 * @param array the update data
587 * @param array the where clause
588 * @return string
589 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200590 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600591 {
592 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500593 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600594 {
595 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200596
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500597 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200598 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600599 if ($field != $index)
600 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500601 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600602 }
603 }
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Jones20aa2bd2010-03-02 17:28:07 -0600606 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500607 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600608 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200609 $cases .= $k." = CASE \n"
610 .implode("\n", $v)."\n"
611 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600612 }
Barry Mienydd671972010-10-04 16:33:58 +0200613
Andrey Andreev2caf2892012-01-27 00:12:03 +0200614 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
615 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
616 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600617 }
618
619 // --------------------------------------------------------------------
620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 /**
622 * Truncate statement
623 *
624 * Generates a platform-specific truncate string from the supplied data
625 * If the database does not support the truncate() command
626 * This function maps to "DELETE FROM table"
627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @param string the table name
629 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200630 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200631 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200633 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 }
Barry Mienydd671972010-10-04 16:33:58 +0200635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 // --------------------------------------------------------------------
637
638 /**
639 * Delete statement
640 *
641 * Generates a platform-specific delete string from the supplied data
642 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 * @param string the table name
644 * @param array the where clause
645 * @param string the limit clause
646 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200647 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200648 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 {
650 $conditions = '';
651
652 if (count($where) > 0 OR count($like) > 0)
653 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200654 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
656 if (count($where) > 0 && count($like) > 0)
657 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200658 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660 $conditions .= implode("\n", $like);
661 }
662
Andrey Andreev2caf2892012-01-27 00:12:03 +0200663 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Limit string
670 *
671 * Generates a platform-specific LIMIT clause
672 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 * @param string the sql query string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200674 * @param int the number of rows to limit the query to
675 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 * @return string
677 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200678 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200679 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200680 return $sql.' LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 }
682
683 // --------------------------------------------------------------------
684
685 /**
686 * Close DB Connection
687 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 * @param resource
689 * @return void
690 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200691 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 {
693 @mysql_close($conn_id);
694 }
Barry Mienydd671972010-10-04 16:33:58 +0200695
Derek Allard2067d1a2008-11-13 22:59:24 +0000696}
697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698/* End of file mysql_driver.php */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200699/* Location: ./system/database/drivers/mysql/mysql_driver.php */