blob: 207a68f1402aa9b50bd5d478d872a07189251bce [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 Andreev11454e02012-02-22 16:05:47 +0200127 return @mysql_select_db(($database == '' ? $this->database : $database), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Set client character set
134 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @param string
136 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200137 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200139 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
RH Beckercfdb2322011-10-03 17:28:32 -0700141 return function_exists('mysql_set_charset')
142 ? @mysql_set_charset($charset, $this->conn_id)
143 : @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145
146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
149 * Version number query string
150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @return string
152 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200153 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200155 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Execute the query
162 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200164 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200165 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200166 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200168 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
172
173 /**
174 * Prep the query
175 *
176 * If needed, each database adapter can prep the query string
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @param string an SQL query
179 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200180 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200181 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200183 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
184 // modifies the query so that it a proper number of affected rows is returned.
185 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200187 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 return $sql;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Begin Transaction
197 *
Barry Mienydd671972010-10-04 16:33:58 +0200198 * @return bool
199 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200200 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200203 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
205 return TRUE;
206 }
207
208 // Reset the transaction failure flag.
209 // If the $test_mode flag is set to TRUE transactions will be rolled back
210 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200211 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 $this->simple_query('SET AUTOCOMMIT=0');
214 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
215 return TRUE;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Barry Mienydd671972010-10-04 16:33:58 +0200223 * @return bool
224 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200225 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200228 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return TRUE;
231 }
232
233 $this->simple_query('COMMIT');
234 $this->simple_query('SET AUTOCOMMIT=1');
235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rollback Transaction
242 *
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return bool
244 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200245 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return TRUE;
251 }
252
253 $this->simple_query('ROLLBACK');
254 $this->simple_query('SET AUTOCOMMIT=1');
255 return TRUE;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Escape String
262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @return string
266 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200267 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 if (is_array($str))
270 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500271 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500272 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000273 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Jones37f4b9c2011-07-01 17:56:50 -0500276 return $str;
277 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
Andrey Andreev2caf2892012-01-27 00:12:03 +0200279 if (function_exists('mysql_real_escape_string') && is_resource($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283 elseif (function_exists('mysql_escape_string'))
284 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287 else
288 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
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 Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200306 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200308 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 return @mysql_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 Andreev2caf2892012-01-27 00:12:03 +0200318 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200320 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 return @mysql_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 Andreev2caf2892012-01-27 00:12:03 +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 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Andrey Andreev2caf2892012-01-27 00:12:03 +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 Andreev2caf2892012-01-27 00:12:03 +0200349 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500350 $this->_reset_select();
Andrey Andreev2caf2892012-01-27 00:12:03 +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 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200361 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 * @return string
363 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200364 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200366 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000367
Andrey Andreev2caf2892012-01-27 00:12:03 +0200368 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200370 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 return $sql;
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Show column query
380 *
381 * Generates a platform-specific query string so that the column names can be fetched
382 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 * @param string the table name
384 * @return string
385 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200386 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200388 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Field data query
395 *
396 * Generates a platform-specific query so that the column data can be retrieved
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200399 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200401 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200403 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * The error message string
410 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 * @return string
412 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200413 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return mysql_error($this->conn_id);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * The error message number
422 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200423 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200425 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 return mysql_errno($this->conn_id);
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Escape the SQL Identifiers
434 *
435 * This function escapes column and table names
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string
438 * @return string
439 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200440 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
442 if ($this->_escape_char == '')
443 {
444 return $item;
445 }
446
447 foreach ($this->_reserved_identifiers as $id)
448 {
449 if (strpos($item, '.'.$id) !== FALSE)
450 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200451 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200454 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200455 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if (strpos($item, '.') !== FALSE)
459 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200460 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200464 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
468
469 /**
470 * From Tables
471 *
472 * This function implicitly groups FROM tables so there is no confusion
473 * about operator precedence in harmony with SQL standards
474 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200475 * @param string table name
476 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200478 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 if ( ! is_array($tables))
481 {
482 $tables = array($tables);
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 return '('.implode(', ', $tables).')';
486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Insert statement
492 *
493 * Generates a platform-specific insert string from the supplied data
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string the table name
496 * @param array the insert keys
497 * @param array the insert values
498 * @return string
499 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200500 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200501 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200502 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Jones20aa2bd2010-03-02 17:28:07 -0600508 /**
509 * Replace statement
510 *
511 * Generates a platform-specific replace string from the supplied data
512 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600513 * @param string the table name
514 * @param array the insert keys
515 * @param array the insert values
516 * @return string
517 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200518 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200519 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200520 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Jones20aa2bd2010-03-02 17:28:07 -0600523 // --------------------------------------------------------------------
524
525 /**
526 * Insert_batch statement
527 *
528 * Generates a platform-specific insert string from the supplied data
529 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600530 * @param string the table name
531 * @param array the insert keys
532 * @param array the insert values
533 * @return string
534 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200535 protected function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200536 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200537 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Jones20aa2bd2010-03-02 17:28:07 -0600540 // --------------------------------------------------------------------
541
542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 /**
544 * Update statement
545 *
546 * Generates a platform-specific update string from the supplied data
547 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @param string the table name
549 * @param array the update data
550 * @param array the where clause
551 * @param array the orderby clause
552 * @param array the limit clause
553 * @return string
554 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200555 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500557 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200559 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Andrey Andreev2caf2892012-01-27 00:12:03 +0200562 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
563 if (count($like) > 0)
Mancy205d0292011-12-20 12:45:58 +0300564 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200565 $where .= ($where == '' ? ' WHERE ' : ' AND ').implode(' ', $like);
Mancyc4caf5a2011-12-20 11:47:51 +0300566 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000567
Andrey Andreev2caf2892012-01-27 00:12:03 +0200568 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
569 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
570 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
573 // --------------------------------------------------------------------
574
Derek Jones20aa2bd2010-03-02 17:28:07 -0600575
576 /**
577 * Update_Batch statement
578 *
579 * Generates a platform-specific batch update string from the supplied data
580 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600581 * @param string the table name
582 * @param array the update data
583 * @param array the where clause
584 * @return string
585 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200586 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600587 {
588 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500589 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600590 {
591 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200592
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500593 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200594 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600595 if ($field != $index)
596 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500597 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600598 }
599 }
600 }
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Jones20aa2bd2010-03-02 17:28:07 -0600602 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500603 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600604 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200605 $cases .= $k." = CASE \n"
606 .implode("\n", $v)."\n"
607 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600608 }
Barry Mienydd671972010-10-04 16:33:58 +0200609
Andrey Andreev2caf2892012-01-27 00:12:03 +0200610 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
611 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
612 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600613 }
614
615 // --------------------------------------------------------------------
616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 /**
618 * Truncate statement
619 *
620 * Generates a platform-specific truncate string from the supplied data
621 * If the database does not support the truncate() command
622 * This function maps to "DELETE FROM table"
623 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param string the table name
625 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200626 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200627 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200629 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // --------------------------------------------------------------------
633
634 /**
635 * Delete statement
636 *
637 * Generates a platform-specific delete string from the supplied data
638 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @param string the table name
640 * @param array the where clause
641 * @param string the limit clause
642 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200644 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 $conditions = '';
647
648 if (count($where) > 0 OR count($like) > 0)
649 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200650 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000651
652 if (count($where) > 0 && count($like) > 0)
653 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200654 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656 $conditions .= implode("\n", $like);
657 }
658
Andrey Andreev2caf2892012-01-27 00:12:03 +0200659 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661
662 // --------------------------------------------------------------------
663
664 /**
665 * Limit string
666 *
667 * Generates a platform-specific LIMIT clause
668 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 * @param string the sql query string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200670 * @param int the number of rows to limit the query to
671 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * @return string
673 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200674 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200675 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200676 return $sql.' LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 }
678
679 // --------------------------------------------------------------------
680
681 /**
682 * Close DB Connection
683 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 * @param resource
685 * @return void
686 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200687 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 {
689 @mysql_close($conn_id);
690 }
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692}
693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694/* End of file mysql_driver.php */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200695/* Location: ./system/database/drivers/mysql/mysql_driver.php */