blob: b1edc2925e6dc7e2af1b64b3947b1318f3e50082 [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 Andreev2b730372012-11-05 17:01:11 +0200194 if (isset($this->data_cache['version']))
195 {
196 return $this->data_cache['version'];
197 }
198 elseif ( ! $this->conn_id)
199 {
200 $this->initialize();
201 }
202
203 if ( ! $this->conn_id OR ($version = @mysql_get_server_info($this->conn_id)) === FALSE)
204 {
205 return FALSE;
206 }
207
208 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Execute the query
215 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200216 * @param string $sql an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200217 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200219 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200221 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
227 * Prep the query
228 *
229 * If needed, each database adapter can prep the query string
230 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200231 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200234 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200236 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
237 // modifies the query so that it a proper number of affected rows is returned.
238 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200240 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 return $sql;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Begin Transaction
250 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200251 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return bool
253 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200254 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200257 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 return TRUE;
260 }
261
262 // Reset the transaction failure flag.
263 // If the $test_mode flag is set to TRUE transactions will be rolled back
264 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200265 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->simple_query('SET AUTOCOMMIT=0');
268 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
269 return TRUE;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Commit Transaction
276 *
Barry Mienydd671972010-10-04 16:33:58 +0200277 * @return bool
278 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200279 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200282 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
284 return TRUE;
285 }
286
287 $this->simple_query('COMMIT');
288 $this->simple_query('SET AUTOCOMMIT=1');
289 return TRUE;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Rollback Transaction
296 *
Barry Mienydd671972010-10-04 16:33:58 +0200297 * @return bool
298 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200299 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200302 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 return TRUE;
305 }
306
307 $this->simple_query('ROLLBACK');
308 $this->simple_query('SET AUTOCOMMIT=1');
309 return TRUE;
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
315 * Escape String
316 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200317 * @param string $str
318 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 * @return string
320 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200321 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200322 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 if (is_array($str))
324 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500325 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500326 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Jones37f4b9c2011-07-01 17:56:50 -0500330 return $str;
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
Andrey Andreevf6a41142012-03-12 15:34:10 +0200333 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Jonese4ed5832009-02-20 21:44:59 +0000335 // escape LIKE condition wildcards
336 if ($like === TRUE)
337 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300338 return str_replace(array($this->_like_escape_chr, '%', '_'),
339 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
340 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Jonese4ed5832009-02-20 21:44:59 +0000343 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Affected Rows
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 affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 return @mysql_affected_rows($this->conn_id);
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Insert ID
362 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200363 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200365 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 return @mysql_insert_id($this->conn_id);
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * List table query
374 *
375 * Generates a platform-specific query string so that the table names can be fetched
376 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200377 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * @return string
379 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200380 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300382 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100384 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200386 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 return $sql;
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // --------------------------------------------------------------------
393
394 /**
395 * Show column query
396 *
397 * Generates a platform-specific query string so that the column names can be fetched
398 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200399 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @return string
401 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300402 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200404 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200410 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200412 * @param string $table
Andrey Andreev3722e502012-03-03 15:04:38 +0200413 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200415 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100417 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200418 {
419 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
420 }
421
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200422 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200423 $query = $query->result_object();
424
425 $retval = array();
426 for ($i = 0, $c = count($query); $i < $c; $i++)
427 {
428 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
429
430 $retval[$i] = new stdClass();
431 $retval[$i]->name = $query[$i]->Field;
432 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
433 $retval[$i]->default = $query[$i]->Default;
434 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
435 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
436 }
437
438 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200444 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200446 * Returns an array containing code and message of the last
447 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600459 * Update_Batch statement
460 *
461 * Generates a platform-specific batch update string from the supplied data
462 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200463 * @param string $table Table name
464 * @param array $values Update data
465 * @param string $index WHERE key
Derek Jones20aa2bd2010-03-02 17:28:07 -0600466 * @return string
467 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300468 protected function _update_batch($table, $values, $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600469 {
470 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500471 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600472 {
473 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200474
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500475 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200476 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100477 if ($field !== $index)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600478 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500479 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600480 }
481 }
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Jones20aa2bd2010-03-02 17:28:07 -0600484 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500485 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600486 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200487 $cases .= $k." = CASE \n"
488 .implode("\n", $v)."\n"
489 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Andrey Andreevb0478652012-07-18 15:34:46 +0300492 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
493
Andrey Andreevd40459d2012-07-18 16:46:39 +0300494 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Derek Jones20aa2bd2010-03-02 17:28:07 -0600495 }
496
497 // --------------------------------------------------------------------
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300500 * FROM tables
501 *
502 * Groups tables in FROM clauses if needed, so there is no confusion
503 * about operator precedence.
504 *
505 * @return string
506 */
507 protected function _from_tables()
508 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300509 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300510 {
511 return '('.implode(', ', $this->qb_from).')';
512 }
513
514 return implode(', ', $this->qb_from);
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * Close DB Connection
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @return void
523 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300524 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300526 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529}
530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300532/* Location: ./system/database/drivers/mysql/mysql_driver.php */