blob: 2457e558ccfb50736da96a18b24a5cad3deca7f1 [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 Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 public $dbdriver = 'mysql';
RH Beckercfdb2322011-10-03 17:28:32 -070050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020052 * Compression flag
53 *
54 * @var bool
55 */
56 public $compress = FALSE;
57
58 /**
59 * DELETE hack flag
60 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020061 * Whether to use the MySQL "delete hack" which allows the number
62 * of affected rows to be shown. Uses a preg_replace when enabled,
63 * adding a bit more processing to all queries.
Andrey Andreeva24e52e2012-11-02 03:54:12 +020064 *
65 * @var bool
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020067 public $delete_hack = TRUE;
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev473130a2012-06-24 02:51:18 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * Identifier escape character
Andrey Andreev473130a2012-06-24 02:51:18 +030073 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020074 * @var string
75 */
76 protected $_escape_char = '`';
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Class constructor
82 *
83 * @param array $params
Andrey Andreev473130a2012-06-24 02:51:18 +030084 * @return void
85 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020086 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020088 parent::__construct($params);
89
Andrey Andreeve4c30192012-06-04 15:08:24 +030090 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
92 $this->hostname .= ':'.$this->port;
93 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev473130a2012-06-24 02:51:18 +030096 // --------------------------------------------------------------------
97
Andrey Andreev2caf2892012-01-27 00:12:03 +020098 /**
99 * Non-persistent database connection
100 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200101 * @param bool $persistent
Andrey Andreev2caf2892012-01-27 00:12:03 +0200102 * @return resource
103 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300104 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +0200105 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300106 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
107
108 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200109 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300110 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200111 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300112
Andrey Andreev98ebf432012-10-12 20:44:03 +0300113 return ($persistent === TRUE)
114 ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
115 : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Barry Mienydd671972010-10-04 16:33:58 +0200117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
119
120 /**
121 * Persistent database connection
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200125 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300127 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // --------------------------------------------------------------------
131
132 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000133 * Reconnect
134 *
135 * Keep / reestablish the db connection if no queries have been
136 * sent for a length of time exceeding the server's idle timeout
137 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000138 * @return void
139 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200140 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000141 {
142 if (mysql_ping($this->conn_id) === FALSE)
143 {
144 $this->conn_id = FALSE;
145 }
146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jones87cbafc2009-02-27 16:29:59 +0000150 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * Select the database
152 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200153 * @param string $database
Andrey Andreev2caf2892012-01-27 00:12:03 +0200154 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200156 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200158 if ($database === '')
159 {
160 $database = $this->database;
161 }
162
163 if (@mysql_select_db($database, $this->conn_id))
164 {
165 $this->database = $database;
166 return TRUE;
167 }
168
169 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Set client character set
176 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200177 * @param string $charset
Andrey Andreev2caf2892012-01-27 00:12:03 +0200178 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200180 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200182 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
184
185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200188 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @return string
191 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200192 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200194 return isset($this->data_cache['version'])
195 ? $this->data_cache['version']
196 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Execute the query
203 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200204 * @param string $sql an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200205 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200206 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200207 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200209 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // --------------------------------------------------------------------
213
214 /**
215 * Prep the query
216 *
217 * If needed, each database adapter can prep the query string
218 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200219 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200221 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200222 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200224 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
225 // modifies the query so that it a proper number of affected rows is returned.
226 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200228 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 return $sql;
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Begin Transaction
238 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200239 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200240 * @return bool
241 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200242 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200245 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 return TRUE;
248 }
249
250 // Reset the transaction failure flag.
251 // If the $test_mode flag is set to TRUE transactions will be rolled back
252 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200253 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 $this->simple_query('SET AUTOCOMMIT=0');
256 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
257 return TRUE;
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Commit Transaction
264 *
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return bool
266 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200267 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200270 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 return TRUE;
273 }
274
275 $this->simple_query('COMMIT');
276 $this->simple_query('SET AUTOCOMMIT=1');
277 return TRUE;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Rollback Transaction
284 *
Barry Mienydd671972010-10-04 16:33:58 +0200285 * @return bool
286 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200287 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200290 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 return TRUE;
293 }
294
295 $this->simple_query('ROLLBACK');
296 $this->simple_query('SET AUTOCOMMIT=1');
297 return TRUE;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Escape String
304 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200305 * @param string $str
306 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return string
308 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200309 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200310 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 if (is_array($str))
312 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500313 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500314 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Jones37f4b9c2011-07-01 17:56:50 -0500318 return $str;
319 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
Andrey Andreevf6a41142012-03-12 15:34:10 +0200321 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Jonese4ed5832009-02-20 21:44:59 +0000323 // escape LIKE condition wildcards
324 if ($like === TRUE)
325 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300326 return str_replace(array($this->_like_escape_chr, '%', '_'),
327 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
328 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Jonese4ed5832009-02-20 21:44:59 +0000331 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
337 * Affected Rows
338 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200339 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200341 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 return @mysql_affected_rows($this->conn_id);
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Insert ID
350 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200351 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200353 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 return @mysql_insert_id($this->conn_id);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * List table query
362 *
363 * Generates a platform-specific query string so that the table names can be fetched
364 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200365 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @return string
367 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200368 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300370 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100372 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200374 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 return $sql;
378 }
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // --------------------------------------------------------------------
381
382 /**
383 * Show column query
384 *
385 * Generates a platform-specific query string so that the column names can be fetched
386 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200387 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return string
389 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300390 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200392 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394
395 // --------------------------------------------------------------------
396
397 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200398 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200400 * @param string $table
Andrey Andreev3722e502012-03-03 15:04:38 +0200401 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200403 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100405 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200406 {
407 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
408 }
409
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200410 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200411 $query = $query->result_object();
412
413 $retval = array();
414 for ($i = 0, $c = count($query); $i < $c; $i++)
415 {
416 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
417
418 $retval[$i] = new stdClass();
419 $retval[$i]->name = $query[$i]->Field;
420 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
421 $retval[$i]->default = $query[$i]->Default;
422 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
423 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
424 }
425
426 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200432 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 * Returns an array containing code and message of the last
435 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200437 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200439 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200441 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600447 * Update_Batch statement
448 *
449 * Generates a platform-specific batch update string from the supplied data
450 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200451 * @param string $table Table name
452 * @param array $values Update data
453 * @param string $index WHERE key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600454 * @return string
455 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300456 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600457 {
458 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500459 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600460 {
461 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200462
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500463 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200464 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100465 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600466 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500467 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600468 }
469 }
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Jones20aa2bd2010-03-02 17:28:07 -0600472 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500473 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600474 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200475 $cases .= $k." = CASE \n"
476 .implode("\n", $v)."\n"
477 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Andrey Andreevb0478652012-07-18 15:34:46 +0300480 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
481
Andrey Andreevd40459d2012-07-18 16:46:39 +0300482 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600483 }
484
485 // --------------------------------------------------------------------
486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300488 * FROM tables
489 *
490 * Groups tables in FROM clauses if needed, so there is no confusion
491 * about operator precedence.
492 *
493 * @return string
494 */
495 protected function _from_tables()
496 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300497 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300498 {
499 return '('.implode(', ', $this->qb_from).')';
500 }
501
502 return implode(', ', $this->qb_from);
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * Close DB Connection
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @return void
511 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300512 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300514 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517}
518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300520/* Location: ./system/database/drivers/mysql/mysql_driver.php */