blob: f0fc4ab3e8f882a21c0022255d629debb8dd47dd [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * MySQL Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_mysql_driver extends CI_DB {
43
Andrey Andreev2caf2892012-01-27 00:12:03 +020044 public $dbdriver = 'mysql';
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030045 public $compress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000046
47 // The character used for escaping
Andrey Andreev2caf2892012-01-27 00:12:03 +020048 protected $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000049
Andrey Andreev2caf2892012-01-27 00:12:03 +020050 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
Andrey Andreev2caf2892012-01-27 00:12:03 +020053 * Whether to use the MySQL "delete hack" which allows the number
54 * of affected rows to be shown. Uses a preg_replace when enabled,
55 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020056 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020057 public $delete_hack = TRUE;
58
Andrey Andreev473130a2012-06-24 02:51:18 +030059 /**
60 * Constructor
61 *
62 * @param array
63 * @return void
64 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020065 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020067 parent::__construct($params);
68
Andrey Andreeve4c30192012-06-04 15:08:24 +030069 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
71 $this->hostname .= ':'.$this->port;
72 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020073 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Andrey Andreev473130a2012-06-24 02:51:18 +030075 // --------------------------------------------------------------------
76
Andrey Andreev2caf2892012-01-27 00:12:03 +020077 /**
78 * Non-persistent database connection
79 *
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030080 * @param bool
Andrey Andreev2caf2892012-01-27 00:12:03 +020081 * @return resource
82 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030083 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +020084 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +030085 $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
Andrey Andreev98ebf432012-10-12 20:44:03 +030092 return ($persistent === TRUE)
93 ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
94 : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Derek Allard2067d1a2008-11-13 22:59:24 +000095 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 // --------------------------------------------------------------------
98
99 /**
100 * Persistent database connection
101 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200103 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200104 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300106 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
110
111 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 * Reconnect
113 *
114 * Keep / reestablish the db connection if no queries have been
115 * sent for a length of time exceeding the server's idle timeout
116 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 * @return void
118 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200119 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 {
121 if (mysql_ping($this->conn_id) === FALSE)
122 {
123 $this->conn_id = FALSE;
124 }
125 }
126
127 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Jones87cbafc2009-02-27 16:29:59 +0000129 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * Select the database
131 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200132 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200133 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200134 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200135 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200137 if ($database === '')
138 {
139 $database = $this->database;
140 }
141
142 if (@mysql_select_db($database, $this->conn_id))
143 {
144 $this->database = $database;
145 return TRUE;
146 }
147
148 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Set client character set
155 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200157 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200159 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200161 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 }
163
164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200167 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @return string
170 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200171 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200173 return isset($this->data_cache['version'])
174 ? $this->data_cache['version']
175 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Execute the query
182 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200184 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200186 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200188 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Prep the query
195 *
196 * If needed, each database adapter can prep the query string
197 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @param string an SQL query
199 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200200 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200201 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200203 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
204 // modifies the query so that it a proper number of affected rows is returned.
205 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200207 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 return $sql;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Begin Transaction
217 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300218 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return bool
220 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200221 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200224 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 return TRUE;
227 }
228
229 // Reset the transaction failure flag.
230 // If the $test_mode flag is set to TRUE transactions will be rolled back
231 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200232 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $this->simple_query('SET AUTOCOMMIT=0');
235 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
236 return TRUE;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Commit Transaction
243 *
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return bool
245 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200246 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 return TRUE;
252 }
253
254 $this->simple_query('COMMIT');
255 $this->simple_query('SET AUTOCOMMIT=1');
256 return TRUE;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Rollback Transaction
263 *
Barry Mienydd671972010-10-04 16:33:58 +0200264 * @return bool
265 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200266 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200269 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 return TRUE;
272 }
273
274 $this->simple_query('ROLLBACK');
275 $this->simple_query('SET AUTOCOMMIT=1');
276 return TRUE;
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // --------------------------------------------------------------------
280
281 /**
282 * Escape String
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @return string
287 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200288 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200289 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if (is_array($str))
291 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500292 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500293 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jones37f4b9c2011-07-01 17:56:50 -0500297 return $str;
298 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000299
Andrey Andreevf6a41142012-03-12 15:34:10 +0200300 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jonese4ed5832009-02-20 21:44:59 +0000302 // escape LIKE condition wildcards
303 if ($like === TRUE)
304 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300305 return str_replace(array($this->_like_escape_chr, '%', '_'),
306 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
307 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Jonese4ed5832009-02-20 21:44:59 +0000310 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
316 * Affected Rows
317 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200318 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200320 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 return @mysql_affected_rows($this->conn_id);
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Insert ID
329 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200330 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200332 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return @mysql_insert_id($this->conn_id);
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 * List table query
341 *
342 * Generates a platform-specific query string so that the table names can be fetched
343 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200344 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * @return string
346 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200347 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300349 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100351 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200353 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
355
356 return $sql;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
360
361 /**
362 * Show column query
363 *
364 * Generates a platform-specific query string so that the column names can be fetched
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string the table name
367 * @return string
368 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300369 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200371 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373
374 // --------------------------------------------------------------------
375
376 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200377 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200380 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200382 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100384 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200385 {
386 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
387 }
388
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200389 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200390 $query = $query->result_object();
391
392 $retval = array();
393 for ($i = 0, $c = count($query); $i < $c; $i++)
394 {
395 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
396
397 $retval[$i] = new stdClass();
398 $retval[$i]->name = $query[$i]->Field;
399 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
400 $retval[$i]->default = $query[$i]->Default;
401 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
402 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
403 }
404
405 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200411 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200413 * Returns an array containing code and message of the last
414 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200416 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200418 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200420 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600426 * Update_Batch statement
427 *
428 * Generates a platform-specific batch update string from the supplied data
429 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600430 * @param string the table name
431 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300432 * @param string the where key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600433 * @return string
434 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300435 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600436 {
437 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500438 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600439 {
440 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200441
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500442 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200443 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100444 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600445 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500446 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600447 }
448 }
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Jones20aa2bd2010-03-02 17:28:07 -0600451 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500452 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600453 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200454 $cases .= $k." = CASE \n"
455 .implode("\n", $v)."\n"
456 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Andrey Andreevb0478652012-07-18 15:34:46 +0300459 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
460
Andrey Andreevd40459d2012-07-18 16:46:39 +0300461 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600462 }
463
464 // --------------------------------------------------------------------
465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300467 * FROM tables
468 *
469 * Groups tables in FROM clauses if needed, so there is no confusion
470 * about operator precedence.
471 *
472 * @return string
473 */
474 protected function _from_tables()
475 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300476 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300477 {
478 return '('.implode(', ', $this->qb_from).')';
479 }
480
481 return implode(', ', $this->qb_from);
482 }
483
484 // --------------------------------------------------------------------
485
486 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * Close DB Connection
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @return void
490 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300491 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300493 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496}
497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300499/* Location: ./system/database/drivers/mysql/mysql_driver.php */