blob: 9b5e331b8e9e725c434da56778ffc8b6dad3d7c0 [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
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * 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 Andreev2caf2892012-01-27 00:12:03 +0200122 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200123 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200124 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 return @mysql_select_db($this->database, $this->conn_id);
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Set client character set
133 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @param string
135 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200136 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200138 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
RH Beckercfdb2322011-10-03 17:28:32 -0700140 return function_exists('mysql_set_charset')
141 ? @mysql_set_charset($charset, $this->conn_id)
142 : @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144
145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
148 * Version number query string
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @return string
151 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200152 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200154 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Execute the query
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200163 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200165 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200167 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
171
172 /**
173 * Prep the query
174 *
175 * If needed, each database adapter can prep the query string
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string an SQL query
178 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200180 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200182 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
183 // modifies the query so that it a proper number of affected rows is returned.
184 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200186 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 return $sql;
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Begin Transaction
196 *
Barry Mienydd671972010-10-04 16:33:58 +0200197 * @return bool
198 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200199 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200202 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
204 return TRUE;
205 }
206
207 // Reset the transaction failure flag.
208 // If the $test_mode flag is set to TRUE transactions will be rolled back
209 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200210 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 $this->simple_query('SET AUTOCOMMIT=0');
213 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
214 return TRUE;
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Commit Transaction
221 *
Barry Mienydd671972010-10-04 16:33:58 +0200222 * @return bool
223 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200224 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return TRUE;
230 }
231
232 $this->simple_query('COMMIT');
233 $this->simple_query('SET AUTOCOMMIT=1');
234 return TRUE;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Rollback Transaction
241 *
Barry Mienydd671972010-10-04 16:33:58 +0200242 * @return bool
243 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200244 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200247 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 return TRUE;
250 }
251
252 $this->simple_query('ROLLBACK');
253 $this->simple_query('SET AUTOCOMMIT=1');
254 return TRUE;
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // --------------------------------------------------------------------
258
259 /**
260 * Escape String
261 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000263 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 * @return string
265 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200266 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200267 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 if (is_array($str))
269 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500270 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500271 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000272 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Jones37f4b9c2011-07-01 17:56:50 -0500275 return $str;
276 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000277
Andrey Andreev2caf2892012-01-27 00:12:03 +0200278 if (function_exists('mysql_real_escape_string') && is_resource($this->conn_id))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000280 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282 elseif (function_exists('mysql_escape_string'))
283 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000284 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286 else
287 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000288 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 // escape LIKE condition wildcards
292 if ($like === TRUE)
293 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200294 return str_replace(array('%', '_'), array('\\%', '\\_'), $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Affected Rows
304 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200305 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200307 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 return @mysql_affected_rows($this->conn_id);
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Insert ID
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 insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return @mysql_insert_id($this->conn_id);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * "Count All" query
328 *
329 * Generates a platform-specific query string that counts all records in
330 * the specified database
331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string
333 * @return string
334 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200335 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000338 {
339 return 0;
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Andrey Andreev2caf2892012-01-27 00:12:03 +0200342 $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 +0000343 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000344 {
345 return 0;
346 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Andrey Andreev2caf2892012-01-27 00:12:03 +0200348 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500349 $this->_reset_select();
Andrey Andreev2caf2892012-01-27 00:12:03 +0200350 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 // --------------------------------------------------------------------
354
355 /**
356 * List table query
357 *
358 * Generates a platform-specific query string so that the table names can be fetched
359 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200360 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return string
362 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200363 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200365 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366
Andrey Andreev2caf2892012-01-27 00:12:03 +0200367 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200369 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 return $sql;
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // --------------------------------------------------------------------
376
377 /**
378 * Show column query
379 *
380 * Generates a platform-specific query string so that the column names can be fetched
381 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 * @param string the table name
383 * @return string
384 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200385 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200387 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Field data query
394 *
395 * Generates a platform-specific query so that the column data can be retrieved
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string the table name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200398 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200400 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200402 return 'DESCRIBE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * The error message string
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @return string
411 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200412 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return mysql_error($this->conn_id);
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // --------------------------------------------------------------------
418
419 /**
420 * The error message number
421 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200422 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200424 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 return mysql_errno($this->conn_id);
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Escape the SQL Identifiers
433 *
434 * This function escapes column and table names
435 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @param string
437 * @return string
438 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200439 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 if ($this->_escape_char == '')
442 {
443 return $item;
444 }
445
446 foreach ($this->_reserved_identifiers as $id)
447 {
448 if (strpos($item, '.'.$id) !== FALSE)
449 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200450 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200453 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200454 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 if (strpos($item, '.') !== FALSE)
458 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200459 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // remove duplicates if the user already included the escape
Andrey Andreev2caf2892012-01-27 00:12:03 +0200463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * From Tables
470 *
471 * This function implicitly groups FROM tables so there is no confusion
472 * about operator precedence in harmony with SQL standards
473 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200474 * @param string table name
475 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200477 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 if ( ! is_array($tables))
480 {
481 $tables = array($tables);
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 return '('.implode(', ', $tables).')';
485 }
486
487 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 /**
490 * Insert statement
491 *
492 * Generates a platform-specific insert string from the supplied data
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string the table name
495 * @param array the insert keys
496 * @param array the insert values
497 * @return string
498 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200499 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200500 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200501 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
505
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Jones20aa2bd2010-03-02 17:28:07 -0600507 /**
508 * Replace statement
509 *
510 * Generates a platform-specific replace string from the supplied data
511 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200517 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200518 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200519 return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jones20aa2bd2010-03-02 17:28:07 -0600522 // --------------------------------------------------------------------
523
524 /**
525 * Insert_batch statement
526 *
527 * Generates a platform-specific insert string from the supplied data
528 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600529 * @param string the table name
530 * @param array the insert keys
531 * @param array the insert values
532 * @return string
533 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200534 protected function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200535 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200536 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Jones20aa2bd2010-03-02 17:28:07 -0600539 // --------------------------------------------------------------------
540
541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 /**
543 * Update statement
544 *
545 * Generates a platform-specific update string from the supplied data
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string the table name
548 * @param array the update data
549 * @param array the where clause
550 * @param array the orderby clause
551 * @param array the limit clause
552 * @return string
553 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200554 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500556 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200558 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Andrey Andreev2caf2892012-01-27 00:12:03 +0200561 $where = ($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '';
562 if (count($like) > 0)
Mancy205d0292011-12-20 12:45:58 +0300563 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200564 $where .= ($where == '' ? ' WHERE ' : ' AND ').implode(' ', $like);
Mancyc4caf5a2011-12-20 11:47:51 +0300565 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
Andrey Andreev2caf2892012-01-27 00:12:03 +0200567 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where
568 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
569 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571
572 // --------------------------------------------------------------------
573
Derek Jones20aa2bd2010-03-02 17:28:07 -0600574
575 /**
576 * Update_Batch statement
577 *
578 * Generates a platform-specific batch update string from the supplied data
579 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600580 * @param string the table name
581 * @param array the update data
582 * @param array the where clause
583 * @return string
584 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200585 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600586 {
587 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500588 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600589 {
590 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200591
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500592 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200593 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600594 if ($field != $index)
595 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500596 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600597 }
598 }
599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Jones20aa2bd2010-03-02 17:28:07 -0600601 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500602 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600603 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200604 $cases .= $k." = CASE \n"
605 .implode("\n", $v)."\n"
606 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Andrey Andreev2caf2892012-01-27 00:12:03 +0200609 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
610 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
611 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600612 }
613
614 // --------------------------------------------------------------------
615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 /**
617 * Truncate statement
618 *
619 * Generates a platform-specific truncate string from the supplied data
620 * If the database does not support the truncate() command
621 * This function maps to "DELETE FROM table"
622 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 * @param string the table name
624 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200625 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200626 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200628 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
632
633 /**
634 * Delete statement
635 *
636 * Generates a platform-specific delete string from the supplied data
637 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @param string the table name
639 * @param array the where clause
640 * @param string the limit clause
641 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200643 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 $conditions = '';
646
647 if (count($where) > 0 OR count($like) > 0)
648 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000649 $conditions = "\nWHERE ".implode("\n", $this->qb_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000650
651 if (count($where) > 0 && count($like) > 0)
652 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200653 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655 $conditions .= implode("\n", $like);
656 }
657
Andrey Andreev2caf2892012-01-27 00:12:03 +0200658 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660
661 // --------------------------------------------------------------------
662
663 /**
664 * Limit string
665 *
666 * Generates a platform-specific LIMIT clause
667 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @param string the sql query string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200669 * @param int the number of rows to limit the query to
670 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return string
672 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200673 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200674 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200675 return $sql.' LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * Close DB Connection
682 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 * @param resource
684 * @return void
685 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200686 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 @mysql_close($conn_id);
689 }
Barry Mienydd671972010-10-04 16:33:58 +0200690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691}
692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693/* End of file mysql_driver.php */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200694/* Location: ./system/database/drivers/mysql/mysql_driver.php */