blob: a827a6ed4073c8b549c4b3fbce4e064915ebef9b [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 Andreevf8f14f32013-12-10 11:32:32 +020069 /**
70 * Strict ON flag
71 *
72 * Whether we're running in strict SQL mode.
73 *
74 * @var bool
75 */
76 public $stricton = FALSE;
77
Andrey Andreeva24e52e2012-11-02 03:54:12 +020078 // --------------------------------------------------------------------
79
Andrey Andreev473130a2012-06-24 02:51:18 +030080 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020081 * Identifier escape character
Andrey Andreev473130a2012-06-24 02:51:18 +030082 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020083 * @var string
84 */
85 protected $_escape_char = '`';
86
87 // --------------------------------------------------------------------
88
89 /**
90 * Class constructor
91 *
92 * @param array $params
Andrey Andreev473130a2012-06-24 02:51:18 +030093 * @return void
94 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020095 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Andrey Andreev2caf2892012-01-27 00:12:03 +020097 parent::__construct($params);
98
Andrey Andreeve4c30192012-06-04 15:08:24 +030099 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 $this->hostname .= ':'.$this->port;
102 }
Andrey Andreev2caf2892012-01-27 00:12:03 +0200103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Andrey Andreev473130a2012-06-24 02:51:18 +0300105 // --------------------------------------------------------------------
106
Andrey Andreev2caf2892012-01-27 00:12:03 +0200107 /**
108 * Non-persistent database connection
109 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200110 * @param bool $persistent
Andrey Andreev2caf2892012-01-27 00:12:03 +0200111 * @return resource
112 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300113 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +0200114 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300115 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
116
117 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200118 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300119 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200120 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300121
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300122 // Error suppression is necessary mostly due to PHP 5.5+ issuing E_DEPRECATED messages
Andrey Andreev1a001492013-01-24 11:32:29 +0200123 $this->conn_id = ($persistent === TRUE)
Andrey Andreev4247ed12014-02-25 16:03:16 +0200124 ? @mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
125 : @mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Andrey Andreev1a001492013-01-24 11:32:29 +0200126
127 // ----------------------------------------------------------------
128
129 // Select the DB... assuming a database name is specified in the config file
130 if ($this->database !== '' && ! $this->db_select())
131 {
132 log_message('error', 'Unable to select database: '.$this->database);
133
134 return ($this->db_debug === TRUE)
135 ? $this->display_error('db_unable_to_select', $this->database)
136 : FALSE;
137 }
138
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200139 if ($this->stricton && is_resource($this->conn_id))
140 {
141 $this->simple_query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
142 }
143
Andrey Andreev1a001492013-01-24 11:32:29 +0200144 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
148
149 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000150 * Reconnect
151 *
152 * Keep / reestablish the db connection if no queries have been
153 * sent for a length of time exceeding the server's idle timeout
154 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000155 * @return void
156 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200157 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000158 {
159 if (mysql_ping($this->conn_id) === FALSE)
160 {
161 $this->conn_id = FALSE;
162 }
163 }
164
165 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Jones87cbafc2009-02-27 16:29:59 +0000167 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * Select the database
169 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200170 * @param string $database
Andrey Andreev2caf2892012-01-27 00:12:03 +0200171 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200172 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200173 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200175 if ($database === '')
176 {
177 $database = $this->database;
178 }
179
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300180 if (mysql_select_db($database, $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200181 {
182 $this->database = $database;
183 return TRUE;
184 }
185
186 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Set client character set
193 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200194 * @param string $charset
Andrey Andreev2caf2892012-01-27 00:12:03 +0200195 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200197 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300199 return mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
202 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200205 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * @return string
208 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200209 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200211 if (isset($this->data_cache['version']))
212 {
213 return $this->data_cache['version'];
214 }
215 elseif ( ! $this->conn_id)
216 {
217 $this->initialize();
218 }
219
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300220 if ( ! $this->conn_id OR ($version = mysql_get_server_info($this->conn_id)) === FALSE)
Andrey Andreev2b730372012-11-05 17:01:11 +0200221 {
222 return FALSE;
223 }
224
225 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Execute the query
232 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200233 * @param string $sql an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200234 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200235 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200236 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300238 return mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // --------------------------------------------------------------------
242
243 /**
244 * Prep the query
245 *
246 * If needed, each database adapter can prep the query string
247 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200248 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200250 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200251 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200253 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
254 // modifies the query so that it a proper number of affected rows is returned.
255 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200257 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 return $sql;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Begin Transaction
267 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200268 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @return bool
270 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200271 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200274 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 return TRUE;
277 }
278
279 // Reset the transaction failure flag.
280 // If the $test_mode flag is set to TRUE transactions will be rolled back
281 // even if the queries produce a successful result.
Andrey Andreev2caf2892012-01-27 00:12:03 +0200282 $this->_trans_failure = ($test_mode === TRUE);
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 $this->simple_query('SET AUTOCOMMIT=0');
285 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
286 return TRUE;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Commit Transaction
293 *
Barry Mienydd671972010-10-04 16:33:58 +0200294 * @return bool
295 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200296 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200299 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return TRUE;
302 }
303
304 $this->simple_query('COMMIT');
305 $this->simple_query('SET AUTOCOMMIT=1');
306 return TRUE;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Rollback Transaction
313 *
Barry Mienydd671972010-10-04 16:33:58 +0200314 * @return bool
315 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200316 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev2caf2892012-01-27 00:12:03 +0200319 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 return TRUE;
322 }
323
324 $this->simple_query('ROLLBACK');
325 $this->simple_query('SET AUTOCOMMIT=1');
326 return TRUE;
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // --------------------------------------------------------------------
330
331 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200332 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200334 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * @return string
336 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200337 protected function _escape_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200338 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300339 return mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
345 * Affected Rows
346 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200347 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200349 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300351 return mysql_affected_rows($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // --------------------------------------------------------------------
355
356 /**
357 * Insert ID
358 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200359 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200361 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300363 return mysql_insert_id($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * List table query
370 *
371 * Generates a platform-specific query string so that the table names can be fetched
372 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200373 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @return string
375 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200376 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300378 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000379
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100380 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200382 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384
385 return $sql;
386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
389
390 /**
391 * Show column query
392 *
393 * Generates a platform-specific query string so that the column names can be fetched
394 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200395 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @return string
397 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300398 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200400 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 // --------------------------------------------------------------------
404
405 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200406 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200408 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200409 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 */
Andrey Andreev3722e502012-03-03 15:04:38 +0200411 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100413 if ($table === '')
Andrey Andreev3722e502012-03-03 15:04:38 +0200414 {
415 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
416 }
417
Andrey Andreev10ecc842012-11-16 01:06:20 +0200418 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
419 {
420 return FALSE;
421 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200422 $query = $query->result_object();
423
424 $retval = array();
425 for ($i = 0, $c = count($query); $i < $c; $i++)
426 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200427 $retval[$i] = new stdClass();
428 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200429
430 sscanf($query[$i]->Type, '%[a-z](%d)',
431 $retval[$i]->type,
432 $retval[$i]->max_length
433 );
434
Andrey Andreev3722e502012-03-03 15:04:38 +0200435 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200436 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
437 }
438
439 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200445 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 * Returns an array containing code and message of the last
448 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200450 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200452 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300460 * FROM tables
461 *
462 * Groups tables in FROM clauses if needed, so there is no confusion
463 * about operator precedence.
464 *
465 * @return string
466 */
467 protected function _from_tables()
468 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300469 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300470 {
471 return '('.implode(', ', $this->qb_from).')';
472 }
473
474 return implode(', ', $this->qb_from);
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * Close DB Connection
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @return void
483 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300484 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300486 // Error suppression to avoid annoying E_WARNINGs in cases
487 // where the connection has already been closed for some reason.
Andrey Andreev79922c02012-05-23 12:27:17 +0300488 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491}
492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493/* End of file mysql_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300494/* Location: ./system/database/drivers/mysql/mysql_driver.php */