Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 29 | * MySQLi Database Adapter Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_mysqli_driver extends CI_DB { |
| 42 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 43 | public $dbdriver = 'mysqli'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | // The character used for escaping |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 46 | protected $_escape_char = '`'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 48 | // clause and character used for LIKE escape sequences - not used in MySQL |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 49 | protected $_like_escape_str = ''; |
Andrey Andreev | 21cb2d3 | 2012-05-25 01:01:06 +0300 | [diff] [blame] | 50 | protected $_like_escape_chr = '\\'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 51 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | /** |
| 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 Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 57 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 58 | protected $_random_keyword = ' RAND()'; // database specific random keyword |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 65 | public $delete_hack = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Non-persistent database connection |
| 69 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 70 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 72 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 74 | return ($this->port !== '') |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 75 | ? @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port) |
| 76 | : @new mysqli($this->hostname, $this->username, $this->password, $this->database); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Persistent database connection |
| 83 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 84 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 86 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | { |
Andrey Andreev | f055fa9 | 2012-01-27 20:36:23 +0200 | [diff] [blame] | 88 | // Persistent connection support was added in PHP 5.3.0 |
| 89 | if ( ! is_php('5.3')) |
| 90 | { |
| 91 | return $this->db_connect(); |
| 92 | } |
| 93 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 94 | return ($this->port !== '') |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 95 | ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port) |
| 96 | : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 102 | * 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 Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 107 | * @return void |
| 108 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 109 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 111 | if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE) |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 112 | { |
| 113 | $this->conn_id = FALSE; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // -------------------------------------------------------------------- |
| 118 | |
| 119 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | * Select the database |
| 121 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 122 | * @param string database name |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 123 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 124 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 125 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 127 | if ($database === '') |
| 128 | { |
| 129 | $database = $this->database; |
| 130 | } |
| 131 | |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 132 | if (@$this->conn_id->select_db($database)) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 133 | { |
| 134 | $this->database = $database; |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Set client character set |
| 145 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * @param string |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 147 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 149 | protected function _db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 151 | return @$this->conn_id->set_charset($charset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 157 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | * @return string |
| 160 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 161 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 163 | return isset($this->data_cache['version']) |
| 164 | ? $this->data_cache['version'] |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 165 | : $this->data_cache['version'] = $this->conn_id->server_info; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Execute the query |
| 172 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * @param string an SQL query |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 174 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 176 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 178 | return @$this->conn_id->query($this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Prep the query |
| 185 | * |
| 186 | * If needed, each database adapter can prep the query string |
| 187 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | * @param string an SQL query |
| 189 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 190 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 191 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 193 | // mysqli_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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 197 | return preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \\1 WHERE 1=1', $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | return $sql; |
| 201 | } |
| 202 | |
| 203 | // -------------------------------------------------------------------- |
| 204 | |
| 205 | /** |
| 206 | * Begin Transaction |
| 207 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 208 | * @return bool |
| 209 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 210 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 213 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
| 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 Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 221 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | |
| 223 | $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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | * @return bool |
| 234 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 235 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 238 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | * @return bool |
| 254 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 255 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 258 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
| 260 | return TRUE; |
| 261 | } |
| 262 | |
| 263 | $this->simple_query('ROLLBACK'); |
| 264 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 265 | return TRUE; |
| 266 | } |
| 267 | |
| 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Escape String |
| 272 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 274 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | * @return string |
| 276 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 277 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 279 | if (is_array($str)) |
| 280 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 281 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 283 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return $str; |
| 287 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 288 | |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 289 | $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 291 | // escape LIKE condition wildcards |
| 292 | if ($like === TRUE) |
| 293 | { |
Andrey Andreev | 21cb2d3 | 2012-05-25 01:01:06 +0300 | [diff] [blame] | 294 | 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 Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 297 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 299 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
| 305 | * Affected Rows |
| 306 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 307 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 309 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 311 | return $this->conn_id->affected_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 313 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | // -------------------------------------------------------------------- |
| 315 | |
| 316 | /** |
| 317 | * Insert ID |
| 318 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 319 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 321 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 323 | return $this->conn_id->insert_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | * List table query |
| 330 | * |
| 331 | * Generates a platform-specific query string so that the table names can be fetched |
| 332 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 333 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | * @return string |
| 335 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 336 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 338 | $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 340 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 342 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | return $sql; |
| 346 | } |
| 347 | |
| 348 | // -------------------------------------------------------------------- |
| 349 | |
| 350 | /** |
| 351 | * Show column query |
| 352 | * |
| 353 | * Generates a platform-specific query string so that the column names can be fetched |
| 354 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | * @param string the table name |
| 356 | * @return string |
| 357 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 358 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 360 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 366 | * Returns an object with field data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | * @param string the table name |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 369 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | */ |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 371 | public function field_data($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 373 | if ($table === '') |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 374 | { |
| 375 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
| 376 | } |
| 377 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 378 | $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 379 | $query = $query->result_object(); |
| 380 | |
| 381 | $retval = array(); |
| 382 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 383 | { |
| 384 | preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches); |
| 385 | |
| 386 | $retval[$i] = new stdClass(); |
| 387 | $retval[$i]->name = $query[$i]->Field; |
| 388 | $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1]; |
| 389 | $retval[$i]->default = $query[$i]->Default; |
| 390 | $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]); |
| 391 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
| 392 | } |
| 393 | |
| 394 | return $retval; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 400 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 402 | * Returns an array containing code and message of the last |
| 403 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 405 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 407 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 409 | return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * From Tables |
| 416 | * |
| 417 | * This function implicitly groups FROM tables so there is no confusion |
| 418 | * about operator precedence in harmony with SQL standards |
| 419 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 420 | * @param string |
| 421 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 423 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | { |
| 425 | if ( ! is_array($tables)) |
| 426 | { |
| 427 | $tables = array($tables); |
| 428 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 429 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | return '('.implode(', ', $tables).')'; |
| 431 | } |
| 432 | |
| 433 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 434 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | /** |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 436 | * Update_Batch statement |
| 437 | * |
| 438 | * Generates a platform-specific batch update string from the supplied data |
| 439 | * |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 440 | * @param string the table name |
| 441 | * @param array the update data |
| 442 | * @param array the where clause |
| 443 | * @return string |
| 444 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 445 | protected function _update_batch($table, $values, $index, $where = NULL) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 446 | { |
| 447 | $ids = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 448 | foreach ($values as $key => $val) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 449 | { |
| 450 | $ids[] = $val[$index]; |
| 451 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 452 | foreach (array_keys($val) as $field) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 453 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 454 | if ($field !== $index) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 455 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 456 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 461 | $cases = ''; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 462 | foreach ($final as $k => $v) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 463 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 464 | $cases .= $k.' = CASE '."\n" |
| 465 | .implode("\n", $v)."\n" |
| 466 | .'ELSE '.$k.' END, '; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 467 | } |
| 468 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 469 | $where = ($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 470 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 471 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame^] | 472 | .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 473 | .$index.' IN('.implode(',', $ids).')'; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 474 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | // -------------------------------------------------------------------- |
| 477 | |
| 478 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | * Limit string |
| 480 | * |
| 481 | * Generates a platform-specific LIMIT clause |
| 482 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | * @param string the sql query string |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 484 | * @param int the number of rows to limit the query to |
| 485 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | * @return string |
| 487 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 488 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 490 | return $sql.' LIMIT '.$limit |
| 491 | .($offset > 0 ? ' OFFSET '.$offset : ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // -------------------------------------------------------------------- |
| 495 | |
| 496 | /** |
| 497 | * Close DB Connection |
| 498 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | * @return void |
| 500 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 501 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 503 | $this->conn_id->close(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | /* End of file mysqli_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 509 | /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ |