blob: 336db971d997a9fc04385ba602e612c652259676 [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 $connect_func = ($persistent === TRUE) ? 'mysql_pconnect' : 'mysql_connect';
85 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
86
87 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020088 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030089 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +020090 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030091
92 return @$connect_func($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // --------------------------------------------------------------------
96
97 /**
98 * Persistent database connection
99 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200101 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200102 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300104 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // --------------------------------------------------------------------
108
109 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000110 * Reconnect
111 *
112 * Keep / reestablish the db connection if no queries have been
113 * sent for a length of time exceeding the server's idle timeout
114 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000115 * @return void
116 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200117 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000118 {
119 if (mysql_ping($this->conn_id) === FALSE)
120 {
121 $this->conn_id = FALSE;
122 }
123 }
124
125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Jones87cbafc2009-02-27 16:29:59 +0000127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Select the database
129 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200130 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200131 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200133 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200135 if ($database === '')
136 {
137 $database = $this->database;
138 }
139
140 if (@mysql_select_db($database, $this->conn_id))
141 {
142 $this->database = $database;
143 return TRUE;
144 }
145
146 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Set client character set
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200155 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200157 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200159 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200165 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @return string
168 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200169 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200171 return isset($this->data_cache['version'])
172 ? $this->data_cache['version']
173 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Execute the query
180 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200182 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200183 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200184 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200186 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
190
191 /**
192 * Prep the query
193 *
194 * If needed, each database adapter can prep the query string
195 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * @param string an SQL query
197 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200198 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200199 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200201 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
202 // modifies the query so that it a proper number of affected rows is returned.
203 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200205 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return $sql;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Begin Transaction
215 *
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200218 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200221 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return TRUE;
224 }
225
226 // Reset the transaction failure flag.
227 // If the $test_mode flag is set to TRUE transactions will be rolled back
228 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200229 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->simple_query('SET AUTOCOMMIT=0');
232 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
233 return TRUE;
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Commit Transaction
240 *
Barry Mienydd671972010-10-04 16:33:58 +0200241 * @return bool
242 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200243 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200246 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 return TRUE;
249 }
250
251 $this->simple_query('COMMIT');
252 $this->simple_query('SET AUTOCOMMIT=1');
253 return TRUE;
254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Rollback Transaction
260 *
Barry Mienydd671972010-10-04 16:33:58 +0200261 * @return bool
262 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200263 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200266 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 return TRUE;
269 }
270
271 $this->simple_query('ROLLBACK');
272 $this->simple_query('SET AUTOCOMMIT=1');
273 return TRUE;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // --------------------------------------------------------------------
277
278 /**
279 * Escape String
280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @return string
284 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200285 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200286 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if (is_array($str))
288 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500289 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500290 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Jones37f4b9c2011-07-01 17:56:50 -0500294 return $str;
295 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000296
Andrey Andreevf6a41142012-03-12 15:34:10 +0200297 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 // escape LIKE condition wildcards
300 if ($like === TRUE)
301 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300302 return str_replace(array($this->_like_escape_chr, '%', '_'),
303 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
304 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
311
312 /**
313 * Affected Rows
314 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200315 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200317 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 return @mysql_affected_rows($this->conn_id);
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
323
324 /**
325 * Insert ID
326 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200327 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200329 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
331 return @mysql_insert_id($this->conn_id);
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * List table query
338 *
339 * Generates a platform-specific query string so that the table names can be fetched
340 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200341 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return string
343 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200344 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300346 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100348 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200350 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
352
353 return $sql;
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // --------------------------------------------------------------------
357
358 /**
359 * Show column query
360 *
361 * Generates a platform-specific query string so that the column names can be fetched
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string the table name
364 * @return string
365 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300366 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200368 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370
371 // --------------------------------------------------------------------
372
373 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200374 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200377 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200379 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100381 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200382 {
383 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
384 }
385
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200386 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200387 $query = $query->result_object();
388
389 $retval = array();
390 for ($i = 0, $c = count($query); $i < $c; $i++)
391 {
392 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
393
394 $retval[$i] = new stdClass();
395 $retval[$i]->name = $query[$i]->Field;
396 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
397 $retval[$i]->default = $query[$i]->Default;
398 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
399 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
400 }
401
402 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
404
405 // --------------------------------------------------------------------
406
407 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200408 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 * Returns an array containing code and message of the last
411 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200413 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200415 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200417 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600423 * Update_Batch statement
424 *
425 * Generates a platform-specific batch update string from the supplied data
426 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600427 * @param string the table name
428 * @param array the update data
429 * @param array the where clause
430 * @return string
431 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200432 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600433 {
434 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500435 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600436 {
437 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200438
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500439 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200440 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100441 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600442 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500443 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600444 }
445 }
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Jones20aa2bd2010-03-02 17:28:07 -0600448 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500449 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600450 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200451 $cases .= $k." = CASE \n"
452 .implode("\n", $v)."\n"
453 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Andrey Andreev2caf2892012-01-27 00:12:03 +0200456 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100457 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreev2caf2892012-01-27 00:12:03 +0200458 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600459 }
460
461 // --------------------------------------------------------------------
462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300464 * FROM tables
465 *
466 * Groups tables in FROM clauses if needed, so there is no confusion
467 * about operator precedence.
468 *
469 * @return string
470 */
471 protected function _from_tables()
472 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300473 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300474 {
475 return '('.implode(', ', $this->qb_from).')';
476 }
477
478 return implode(', ', $this->qb_from);
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * Close DB Connection
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @return void
487 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300488 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300490 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493}
494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300496/* Location: ./system/database/drivers/mysql/mysql_driver.php */