blob: d801a9aaf813f58470aab1df0f64facd8f6b33a9 [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';
Derek Allard2067d1a2008-11-13 22:59:24 +000044
45 // The character used for escaping
Andrey Andreev2caf2892012-01-27 00:12:03 +020046 protected $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000047
48 // clause and character used for LIKE escape sequences - not used in MySQL
Andrey Andreev2caf2892012-01-27 00:12:03 +020049 protected $_like_escape_str = '';
Andrey Andreev21cb2d32012-05-25 01:01:06 +030050 protected $_like_escape_chr = '\\';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RAND()'; // database specific random keyword
RH Beckercfdb2322011-10-03 17:28:32 -070059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
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.
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020065 public $delete_hack = TRUE;
66
67 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020069 parent::__construct($params);
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if ($this->port != '')
72 {
73 $this->hostname .= ':'.$this->port;
74 }
Andrey Andreev2caf2892012-01-27 00:12:03 +020075 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Andrey Andreev2caf2892012-01-27 00:12:03 +020077 /**
78 * Non-persistent database connection
79 *
80 * @return resource
81 */
82 public function db_connect()
83 {
Derek Allard2067d1a2008-11-13 22:59:24 +000084 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
90 * Persistent database connection
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020093 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020094 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000095 {
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return @mysql_pconnect($this->hostname, $this->username, $this->password);
97 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 // --------------------------------------------------------------------
100
101 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000102 * Reconnect
103 *
104 * Keep / reestablish the db connection if no queries have been
105 * sent for a length of time exceeding the server's idle timeout
106 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000107 * @return void
108 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200109 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000110 {
111 if (mysql_ping($this->conn_id) === FALSE)
112 {
113 $this->conn_id = FALSE;
114 }
115 }
116
117 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 * Select the database
121 *
Andrey Andreev11454e02012-02-22 16:05:47 +0200122 * @param string database name
Andrey Andreev2caf2892012-01-27 00:12:03 +0200123 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200125 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200127 if ($database === '')
128 {
129 $database = $this->database;
130 }
131
132 if (@mysql_select_db($database, $this->conn_id))
133 {
134 $this->database = $database;
135 return TRUE;
136 }
137
138 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Set client character set
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200147 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200149 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreevb3442a12012-03-12 15:35:40 +0200151 return @mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200157 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @return string
160 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200161 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200163 return isset($this->data_cache['version'])
164 ? $this->data_cache['version']
165 : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200174 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200176 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200178 return @mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Prep the query
185 *
186 * If needed, each database adapter can prep the query string
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string an SQL query
189 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200191 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200193 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
194 // modifies the query so that it a proper number of affected rows is returned.
195 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200197 return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 return $sql;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Begin Transaction
207 *
Barry Mienydd671972010-10-04 16:33:58 +0200208 * @return bool
209 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200210 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200213 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 return TRUE;
216 }
217
218 // Reset the transaction failure flag.
219 // If the $test_mode flag is set to TRUE transactions will be rolled back
220 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200221 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $this->simple_query('SET AUTOCOMMIT=0');
224 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
225 return TRUE;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Commit Transaction
232 *
Barry Mienydd671972010-10-04 16:33:58 +0200233 * @return bool
234 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200235 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200238 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 return TRUE;
241 }
242
243 $this->simple_query('COMMIT');
244 $this->simple_query('SET AUTOCOMMIT=1');
245 return TRUE;
246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Rollback Transaction
252 *
Barry Mienydd671972010-10-04 16:33:58 +0200253 * @return bool
254 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200255 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200258 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return TRUE;
261 }
262
263 $this->simple_query('ROLLBACK');
264 $this->simple_query('SET AUTOCOMMIT=1');
265 return TRUE;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
271 * Escape String
272 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @return string
276 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200277 public function escape_str($str, $like = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if (is_array($str))
280 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500281 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500282 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500284 }
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Jones37f4b9c2011-07-01 17:56:50 -0500286 return $str;
287 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
Andrey Andreevf6a41142012-03-12 15:34:10 +0200289 $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 // escape LIKE condition wildcards
292 if ($like === TRUE)
293 {
Andrey Andreev21cb2d32012-05-25 01:01:06 +0300294 return str_replace(array($this->_like_escape_chr, '%', '_'),
295 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
296 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Affected Rows
306 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200307 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200309 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
311 return @mysql_affected_rows($this->conn_id);
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Insert ID
318 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200319 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200321 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 return @mysql_insert_id($this->conn_id);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * "Count All" query
330 *
331 * Generates a platform-specific query string that counts all records in
332 * the specified database
333 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @param string
335 * @return string
336 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200337 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000340 {
341 return 0;
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Andrey Andreev2caf2892012-01-27 00:12:03 +0200344 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000346 {
347 return 0;
348 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000349
Andrey Andreev2caf2892012-01-27 00:12:03 +0200350 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500351 $this->_reset_select();
Andrey Andreev2caf2892012-01-27 00:12:03 +0200352 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * List table query
359 *
360 * Generates a platform-specific query string so that the table names can be fetched
361 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200362 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @return string
364 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200365 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200367 $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000368
Andrey Andreev2caf2892012-01-27 00:12:03 +0200369 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200371 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373
374 return $sql;
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // --------------------------------------------------------------------
378
379 /**
380 * Show column query
381 *
382 * Generates a platform-specific query string so that the column names can be fetched
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string the table name
385 * @return string
386 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200387 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200389 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200395 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string the table name
Andrey Andreev3722e502012-03-03 15:04:38 +0200398 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200400 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200402 if ($table == '')
403 {
404 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
405 }
406
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200407 $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev3722e502012-03-03 15:04:38 +0200408 $query = $query->result_object();
409
410 $retval = array();
411 for ($i = 0, $c = count($query); $i < $c; $i++)
412 {
413 preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches);
414
415 $retval[$i] = new stdClass();
416 $retval[$i]->name = $query[$i]->Field;
417 $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1];
418 $retval[$i]->default = $query[$i]->Default;
419 $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]);
420 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
421 }
422
423 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200429 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200431 * Returns an array containing code and message of the last
432 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200436 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200438 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * From Tables
445 *
446 * This function implicitly groups FROM tables so there is no confusion
447 * about operator precedence in harmony with SQL standards
448 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200449 * @param string table name
450 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200452 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 if ( ! is_array($tables))
455 {
456 $tables = array($tables);
457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 return '('.implode(', ', $tables).')';
460 }
461
462 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 /**
Derek Jones20aa2bd2010-03-02 17:28:07 -0600465 * Update_Batch statement
466 *
467 * Generates a platform-specific batch update string from the supplied data
468 *
Derek Jones20aa2bd2010-03-02 17:28:07 -0600469 * @param string the table name
470 * @param array the update data
471 * @param array the where clause
472 * @return string
473 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200474 protected function _update_batch($table, $values, $index, $where = NULL)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600475 {
476 $ids = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500477 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600478 {
479 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200480
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500481 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200482 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600483 if ($field != $index)
484 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500485 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600486 }
487 }
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Jones20aa2bd2010-03-02 17:28:07 -0600490 $cases = '';
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500491 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600492 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200493 $cases .= $k." = CASE \n"
494 .implode("\n", $v)."\n"
495 .'ELSE '.$k.' END, ';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Andrey Andreev2caf2892012-01-27 00:12:03 +0200498 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
499 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
500 .$index.' IN('.implode(',', $ids).')';
Derek Jones20aa2bd2010-03-02 17:28:07 -0600501 }
502
503 // --------------------------------------------------------------------
504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * Limit string
507 *
508 * Generates a platform-specific LIMIT clause
509 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * @param string the sql query string
Andrey Andreev2caf2892012-01-27 00:12:03 +0200511 * @param int the number of rows to limit the query to
512 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @return string
514 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200515 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200517 return $sql.' LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Close DB Connection
524 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @return void
526 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300527 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300529 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532}
533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300535/* Location: ./system/database/drivers/mysql/mysql_driver.php */