blob: 78b1d703a1848b01cb45bda1e7ec30497737811c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev2caf2892012-01-27 00:12:03 +02008 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
Andrey Andreev2caf2892012-01-27 00:12:03 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * MySQL Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
53class CI_DB_mysql_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020060 public $dbdriver = 'mysql';
RH Beckercfdb2322011-10-03 17:28:32 -070061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020063 * Compression flag
64 *
65 * @var bool
66 */
67 public $compress = FALSE;
68
69 /**
70 * DELETE hack flag
71 *
Andrey Andreev2caf2892012-01-27 00:12:03 +020072 * Whether to use the MySQL "delete hack" which allows the number
73 * of affected rows to be shown. Uses a preg_replace when enabled,
74 * adding a bit more processing to all queries.
Andrey Andreeva24e52e2012-11-02 03:54:12 +020075 *
76 * @var bool
Barry Mienydd671972010-10-04 16:33:58 +020077 */
Andrey Andreev2caf2892012-01-27 00:12:03 +020078 public $delete_hack = TRUE;
79
Andrey Andreevf8f14f32013-12-10 11:32:32 +020080 /**
81 * Strict ON flag
82 *
83 * Whether we're running in strict SQL mode.
84 *
85 * @var bool
86 */
Andrey Andreevc83e8942016-01-07 17:27:39 +020087 public $stricton;
Andrey Andreevf8f14f32013-12-10 11:32:32 +020088
Andrey Andreeva24e52e2012-11-02 03:54:12 +020089 // --------------------------------------------------------------------
90
Andrey Andreev473130a2012-06-24 02:51:18 +030091 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020092 * Identifier escape character
Andrey Andreev473130a2012-06-24 02:51:18 +030093 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020094 * @var string
95 */
96 protected $_escape_char = '`';
97
98 // --------------------------------------------------------------------
99
100 /**
101 * Class constructor
102 *
103 * @param array $params
Andrey Andreev473130a2012-06-24 02:51:18 +0300104 * @return void
105 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200106 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200108 parent::__construct($params);
109
Andrey Andreeve4c30192012-06-04 15:08:24 +0300110 if ( ! empty($this->port))
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 $this->hostname .= ':'.$this->port;
113 }
Andrey Andreev2caf2892012-01-27 00:12:03 +0200114 }
Barry Mienydd671972010-10-04 16:33:58 +0200115
Andrey Andreev473130a2012-06-24 02:51:18 +0300116 // --------------------------------------------------------------------
117
Andrey Andreev2caf2892012-01-27 00:12:03 +0200118 /**
119 * Non-persistent database connection
120 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200121 * @param bool $persistent
Andrey Andreev2caf2892012-01-27 00:12:03 +0200122 * @return resource
123 */
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300124 public function db_connect($persistent = FALSE)
Andrey Andreev2caf2892012-01-27 00:12:03 +0200125 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300126 $client_flags = ($this->compress === FALSE) ? 0 : MYSQL_CLIENT_COMPRESS;
127
128 if ($this->encrypt === TRUE)
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200129 {
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300130 $client_flags = $client_flags | MYSQL_CLIENT_SSL;
Michiel Vugteveen49f7b722012-08-20 18:52:21 +0200131 }
Andrey Andreev2f8bf9b2012-10-12 20:37:52 +0300132
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300133 // Error suppression is necessary mostly due to PHP 5.5+ issuing E_DEPRECATED messages
Andrey Andreev1a001492013-01-24 11:32:29 +0200134 $this->conn_id = ($persistent === TRUE)
Andrey Andreev1dc43aa2015-01-20 11:09:17 +0200135 ? mysql_pconnect($this->hostname, $this->username, $this->password, $client_flags)
136 : mysql_connect($this->hostname, $this->username, $this->password, TRUE, $client_flags);
Andrey Andreev1a001492013-01-24 11:32:29 +0200137
138 // ----------------------------------------------------------------
139
140 // Select the DB... assuming a database name is specified in the config file
141 if ($this->database !== '' && ! $this->db_select())
142 {
143 log_message('error', 'Unable to select database: '.$this->database);
144
145 return ($this->db_debug === TRUE)
146 ? $this->display_error('db_unable_to_select', $this->database)
147 : FALSE;
148 }
149
Andrey Andreevb98c6572016-01-07 16:10:10 +0200150 if (isset($this->stricton) && is_resource($this->conn_id))
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200151 {
Andrey Andreevb98c6572016-01-07 16:10:10 +0200152 if ($this->stricton)
153 {
154 $this->simple_query('SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")');
155 }
Andrey Andreev0a9cc832016-01-07 17:52:20 +0200156 else
Andrey Andreevb98c6572016-01-07 16:10:10 +0200157 {
158 $this->simple_query(
159 'SET SESSION sql_mode =
Andrey Andreevc83e8942016-01-07 17:27:39 +0200160 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
161 @@sql_mode,
162 "STRICT_ALL_TABLES,", ""),
163 ",STRICT_ALL_TABLES", ""),
164 "STRICT_ALL_TABLES", ""),
165 "STRICT_TRANS_TABLES,", ""),
166 ",STRICT_TRANS_TABLES", ""),
167 "STRICT_TRANS_TABLES", "")'
Andrey Andreevb98c6572016-01-07 16:10:10 +0200168 );
169 }
Andrey Andreevf8f14f32013-12-10 11:32:32 +0200170 }
171
Andrey Andreev1a001492013-01-24 11:32:29 +0200172 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 // --------------------------------------------------------------------
176
177 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000178 * Reconnect
179 *
180 * Keep / reestablish the db connection if no queries have been
181 * sent for a length of time exceeding the server's idle timeout
182 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000183 * @return void
184 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200185 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000186 {
187 if (mysql_ping($this->conn_id) === FALSE)
188 {
189 $this->conn_id = FALSE;
190 }
191 }
192
193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Jones87cbafc2009-02-27 16:29:59 +0000195 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * Select the database
197 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200198 * @param string $database
Andrey Andreev2caf2892012-01-27 00:12:03 +0200199 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200200 */
Andrey Andreev11454e02012-02-22 16:05:47 +0200201 public function db_select($database = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200203 if ($database === '')
204 {
205 $database = $this->database;
206 }
207
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300208 if (mysql_select_db($database, $this->conn_id))
Andrey Andreev024ba2d2012-02-24 11:40:36 +0200209 {
210 $this->database = $database;
211 return TRUE;
212 }
213
214 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Set client character set
221 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200222 * @param string $charset
Andrey Andreev2caf2892012-01-27 00:12:03 +0200223 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 */
Andrey Andreev95bd1d12012-03-12 16:22:28 +0200225 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300227 return mysql_set_charset($charset, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200233 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 * @return string
236 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200237 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Andrey Andreev2b730372012-11-05 17:01:11 +0200239 if (isset($this->data_cache['version']))
240 {
241 return $this->data_cache['version'];
242 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200243
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300244 if ( ! $this->conn_id OR ($version = mysql_get_server_info($this->conn_id)) === FALSE)
Andrey Andreev2b730372012-11-05 17:01:11 +0200245 {
246 return FALSE;
247 }
248
249 return $this->data_cache['version'] = $version;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Execute the query
256 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200257 * @param string $sql an SQL query
Andrey Andreev2caf2892012-01-27 00:12:03 +0200258 * @return mixed
Barry Mienydd671972010-10-04 16:33:58 +0200259 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200260 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300262 return mysql_query($this->_prep_query($sql), $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * Prep the query
269 *
270 * If needed, each database adapter can prep the query string
271 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200272 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200274 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200275 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200277 // mysql_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack
278 // modifies the query so that it a proper number of affected rows is returned.
279 if ($this->delete_hack === TRUE && preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreevbe999662013-01-10 11:40:09 +0200281 return trim($sql).' WHERE 1=1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 return $sql;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Begin Transaction
291 *
Barry Mienydd671972010-10-04 16:33:58 +0200292 * @return bool
293 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300294 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 $this->simple_query('SET AUTOCOMMIT=0');
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300297 return $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Commit Transaction
304 *
Barry Mienydd671972010-10-04 16:33:58 +0200305 * @return bool
306 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300307 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300309 if ($this->simple_query('COMMIT'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300311 $this->simple_query('SET AUTOCOMMIT=1');
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 return TRUE;
313 }
314
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300315 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Rollback Transaction
322 *
Barry Mienydd671972010-10-04 16:33:58 +0200323 * @return bool
324 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300325 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300327 if ($this->simple_query('ROLLBACK'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300329 $this->simple_query('SET AUTOCOMMIT=1');
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 return TRUE;
331 }
332
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300333 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200339 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200341 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return string
343 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200344 protected function _escape_str($str)
Barry Mienydd671972010-10-04 16:33:58 +0200345 {
Andrey Andreev62fad282014-06-19 15:25:40 +0300346 return mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
350
351 /**
352 * Affected Rows
353 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200354 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200356 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300358 return mysql_affected_rows($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Insert ID
365 *
Andrey Andreev2caf2892012-01-27 00:12:03 +0200366 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200368 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300370 return mysql_insert_id($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * List table query
377 *
378 * Generates a platform-specific query string so that the table names can be fetched
379 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200380 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @return string
382 */
Andrey Andreev2caf2892012-01-27 00:12:03 +0200383 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
Andrey Andreev473130a2012-06-24 02:51:18 +0300385 $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database);
Derek Allard2067d1a2008-11-13 22:59:24 +0000386
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100387 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200389 return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 return $sql;
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // --------------------------------------------------------------------
396
397 /**
398 * Show column query
399 *
400 * Generates a platform-specific query string so that the column names can be fetched
401 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200402 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return string
404 */
Andrey Andreev473130a2012-06-24 02:51:18 +0300405 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Andrey Andreev2caf2892012-01-27 00:12:03 +0200407 return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 // --------------------------------------------------------------------
411
412 /**
Andrey Andreev3722e502012-03-03 15:04:38 +0200413 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200415 * @param string $table
Andrey Andreev10ecc842012-11-16 01:06:20 +0200416 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200418 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200420 if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE)
421 {
422 return FALSE;
423 }
Andrey Andreev3722e502012-03-03 15:04:38 +0200424 $query = $query->result_object();
425
426 $retval = array();
427 for ($i = 0, $c = count($query); $i < $c; $i++)
428 {
Andrey Andreev3722e502012-03-03 15:04:38 +0200429 $retval[$i] = new stdClass();
430 $retval[$i]->name = $query[$i]->Field;
Andrey Andreev10ecc842012-11-16 01:06:20 +0200431
432 sscanf($query[$i]->Type, '%[a-z](%d)',
433 $retval[$i]->type,
434 $retval[$i]->max_length
435 );
436
Andrey Andreev3722e502012-03-03 15:04:38 +0200437 $retval[$i]->default = $query[$i]->Default;
Andrey Andreev3722e502012-03-03 15:04:38 +0200438 $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
439 }
440
441 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 * Returns an array containing code and message of the last
450 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200452 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 return array('code' => mysql_errno($this->conn_id), 'message' => mysql_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 // --------------------------------------------------------------------
460
461 /**
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300462 * FROM tables
463 *
464 * Groups tables in FROM clauses if needed, so there is no confusion
465 * about operator precedence.
466 *
467 * @return string
468 */
469 protected function _from_tables()
470 {
Andrey Andreevfce9abe2012-10-09 11:37:00 +0300471 if ( ! empty($this->qb_join) && count($this->qb_from) > 1)
Andrey Andreev7eaa14f2012-10-09 11:34:01 +0300472 {
473 return '('.implode(', ', $this->qb_from).')';
474 }
475
476 return implode(', ', $this->qb_from);
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * Close DB Connection
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return void
485 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300486 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300488 // Error suppression to avoid annoying E_WARNINGs in cases
489 // where the connection has already been closed for some reason.
Andrey Andreev79922c02012-05-23 12:27:17 +0300490 @mysql_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493}