blob: 99bf55942a27d8877bbbdcee268436528acb8d5e [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 *
Barry Mienydd671972010-10-04 16:33:58 +0200217 * @return bool
218 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200219 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200222 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 return TRUE;
225 }
226
227 // Reset the transaction failure flag.
228 // If the $test_mode flag is set to TRUE transactions will be rolled back
229 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200230 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $this->simple_query('SET AUTOCOMMIT=0');
233 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
234 return TRUE;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Commit Transaction
241 *
Barry Mienydd671972010-10-04 16:33:58 +0200242 * @return bool
243 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200244 public function trans_commit()
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('COMMIT');
253 $this->simple_query('SET AUTOCOMMIT=1');
254 return TRUE;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Rollback Transaction
261 *
Barry Mienydd671972010-10-04 16:33:58 +0200262 * @return bool
263 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200264 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200267 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 return TRUE;
270 }
271
272 $this->simple_query('ROLLBACK');
273 $this->simple_query('SET AUTOCOMMIT=1');
274 return TRUE;
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // --------------------------------------------------------------------
278
279 /**
280 * Escape String
281 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @return string
285 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200286 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200287 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if (is_array($str))
289 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500290 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500291 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000292 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 return $str;
296 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000297
Andrey Andreevf6a41142012-03-12 15:34:10 +0200298 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 // escape LIKE condition wildcards
301 if ($like === TRUE)
302 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300303 return str_replace(array($this->_like_escape_chr, '%', '_'),
304 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
305 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
312
313 /**
314 * Affected Rows
315 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200316 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200318 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 return @mysql_affected_rows($this->conn_id);
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 // --------------------------------------------------------------------
324
325 /**
326 * Insert ID
327 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200328 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200330 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 return @mysql_insert_id($this->conn_id);
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * List table query
339 *
340 * Generates a platform-specific query string so that the table names can be fetched
341 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200342 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @return string
344 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200345 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300347 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100349 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200351 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
354 return $sql;
355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
358
359 /**
360 * Show column query
361 *
362 * Generates a platform-specific query string so that the column names can be fetched
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string the table name
365 * @return string
366 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300367 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200369 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // --------------------------------------------------------------------
373
374 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200375 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200378 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200380 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100382 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200383 {
384 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
385 }
386
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200387 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200388 $query = $query->result_object();
389
390 $retval = array();
391 for ($i = 0, $c = count($query); $i < $c; $i++)
392 {
393 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
394
395 $retval[$i] = new stdClass();
396 $retval[$i]->name = $query[$i]->Field;
397 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
398 $retval[$i]->default = $query[$i]->Default;
399 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
400 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
401 }
402
403 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405
406 // --------------------------------------------------------------------
407
408 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200409 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200411 * Returns an array containing code and message of the last
412 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200414 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200418 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
421 // --------------------------------------------------------------------
422
423 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600424 * Update_Batch statement
425 *
426 * Generates a platform-specific batch update string from the supplied data
427 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600428 * @param string the table name
429 * @param array the update data
430 * @param array the where clause
431 * @return string
432 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200433 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600434 {
435 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500436 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600437 {
438 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200439
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500440 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200441 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100442 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600443 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500444 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600445 }
446 }
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Jones20aa2bd2010-03-02 17:28:07 -0600449 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500450 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600451 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200452 $cases .= $k." = CASE \n"
453 .implode("\n", $v)."\n"
454 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Andrey Andreev2caf2892012-01-27 00:12:03 +0200457 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100458 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev2caf2892012-01-27 00:12:03 +0200459 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600460 }
461
462 // --------------------------------------------------------------------
463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300465 * FROM tables
466 *
467 * Groups tables in FROM clauses if needed, so there is no confusion
468 * about operator precedence.
469 *
470 * @return string
471 */
472 protected function _from_tables()
473 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300474 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300475 {
476 return '('.implode(', ', $this->qb_from).')';
477 }
478
479 return implode(', ', $this->qb_from);
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * Close DB Connection
486 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return void
488 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300489 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300491 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494}
495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300497/* Location: ./system/database/drivers/mysql/mysql_driver.php */