blob: f82e775e6bf0eadca8ff2398d8537ccd8567d073 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey 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';
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030044 public $compress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000045
46 // The character used for escaping
Andrey Andreev2caf2892012-01-27 00:12:03 +020047 protected $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000048
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
Andrey Andreev2caf2892012-01-27 00:12:03 +020052 * Whether to use the MySQL "delete hack" which allows the number
53 * of affected rows to be shown. Uses a preg_replace when enabled,
54 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020056 public $delete_hack = TRUE;
57
Andrey Andreev473130a2012-06-24 02:51:18 +030058 /**
59 * Constructor
60 *
61 * @param array
62 * @return void
63 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020064 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020066 parent::__construct($params);
67
Andrey Andreeve4c30192012-06-04 15:08:24 +030068 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 $this->hostname .= ':'.$this->port;
71 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020072 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Andrey Andreev473130a2012-06-24 02:51:18 +030074 // --------------------------------------------------------------------
75
Andrey Andreev2caf2892012-01-27 00:12:03 +020076 /**
77 * Non-persistent database connection
78 *
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030079 * @param bool
Andrey Andreev2caf2892012-01-27 00:12:03 +020080 * @return resource
81 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030082 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +020083 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030084 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
85
86 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020087 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030088 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020089 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030090
Andrey Andreev98ebf432012-10-12 20:44:03 +030091 return ($persistent === TRUE)
92 ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
93 : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
97
98 /**
99 * Persistent database connection
100 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200103 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300105 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * Reconnect
112 *
113 * Keep / reestablish the db connection if no queries have been
114 * sent for a length of time exceeding the server's idle timeout
115 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000116 * @return void
117 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200118 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 {
120 if (mysql_ping($this->conn_id) === FALSE)
121 {
122 $this->conn_id = FALSE;
123 }
124 }
125
126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Jones87cbafc2009-02-27 16:29:59 +0000128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Select the database
130 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200131 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200132 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200134 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200136 if ($database === '')
137 {
138 $database = $this->database;
139 }
140
141 if (@mysql_select_db($database, $this->conn_id))
142 {
143 $this->database = $database;
144 return TRUE;
145 }
146
147 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Set client character set
154 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200156 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200158 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200160 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
162
163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200166 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @return string
169 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200170 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200172 return isset($this->data_cache['version'])
173 ? $this->data_cache['version']
174 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Execute the query
181 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200183 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200184 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200185 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200187 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // --------------------------------------------------------------------
191
192 /**
193 * Prep the query
194 *
195 * If needed, each database adapter can prep the query string
196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @param string an SQL query
198 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200199 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200200 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200202 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
203 // modifies the query so that it a proper number of affected rows is returned.
204 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200206 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 return $sql;
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Begin Transaction
216 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300217 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200218 * @return bool
219 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200220 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200223 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 return TRUE;
226 }
227
228 // Reset the transaction failure flag.
229 // If the $test_mode flag is set to TRUE transactions will be rolled back
230 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200231 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $this->simple_query('SET AUTOCOMMIT=0');
234 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Commit Transaction
242 *
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return bool
244 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200245 public function trans_commit()
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('COMMIT');
254 $this->simple_query('SET AUTOCOMMIT=1');
255 return TRUE;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Rollback Transaction
262 *
Barry Mienydd671972010-10-04 16:33:58 +0200263 * @return bool
264 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200265 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200268 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 return TRUE;
271 }
272
273 $this->simple_query('ROLLBACK');
274 $this->simple_query('SET AUTOCOMMIT=1');
275 return TRUE;
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // --------------------------------------------------------------------
279
280 /**
281 * Escape String
282 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000284 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 * @return string
286 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200287 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 if (is_array($str))
290 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500291 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500292 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000293 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Jones37f4b9c2011-07-01 17:56:50 -0500296 return $str;
297 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
Andrey Andreevf6a41142012-03-12 15:34:10 +0200299 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Jonese4ed5832009-02-20 21:44:59 +0000301 // escape LIKE condition wildcards
302 if ($like === TRUE)
303 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300304 return str_replace(array($this->_like_escape_chr, '%', '_'),
305 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
306 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Affected Rows
316 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200317 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200319 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return @mysql_affected_rows($this->conn_id);
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
325
326 /**
327 * Insert ID
328 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200329 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200331 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 return @mysql_insert_id($this->conn_id);
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * List table query
340 *
341 * Generates a platform-specific query string so that the table names can be fetched
342 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200343 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @return string
345 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200346 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300348 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100350 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200352 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 return $sql;
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Show column query
362 *
363 * Generates a platform-specific query string so that the column names can be fetched
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @param string the table name
366 * @return string
367 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300368 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200370 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200376 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200379 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200381 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100383 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200384 {
385 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
386 }
387
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200388 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200389 $query = $query->result_object();
390
391 $retval = array();
392 for ($i = 0, $c = count($query); $i < $c; $i++)
393 {
394 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
395
396 $retval[$i] = new stdClass();
397 $retval[$i]->name = $query[$i]->Field;
398 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
399 $retval[$i]->default = $query[$i]->Default;
400 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
401 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
402 }
403
404 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200412 * Returns an array containing code and message of the last
413 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200415 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200419 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600425 * Update_Batch statement
426 *
427 * Generates a platform-specific batch update string from the supplied data
428 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600429 * @param string the table name
430 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300431 * @param string the where key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600432 * @return string
433 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300434 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600435 {
436 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500437 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600438 {
439 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200440
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500441 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200442 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100443 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600444 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500445 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600446 }
447 }
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Jones20aa2bd2010-03-02 17:28:07 -0600450 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500451 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600452 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200453 $cases .= $k." = CASE \n"
454 .implode("\n", $v)."\n"
455 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreevb0478652012-07-18 15:34:46 +0300458 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
459
Andrey Andreevd40459d2012-07-18 16:46:39 +0300460 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600461 }
462
463 // --------------------------------------------------------------------
464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300466 * FROM tables
467 *
468 * Groups tables in FROM clauses if needed, so there is no confusion
469 * about operator precedence.
470 *
471 * @return string
472 */
473 protected function _from_tables()
474 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300475 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300476 {
477 return '('.implode(', ', $this->qb_from).')';
478 }
479
480 return implode(', ', $this->qb_from);
481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * Close DB Connection
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @return void
489 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300490 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300492 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495}
496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300498/* Location: ./system/database/drivers/mysql/mysql_driver.php */