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 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 52 | protected $_random_keyword = ' RAND()'; // database specific random keyword |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Whether to use the MySQL "delete hack" which allows the number |
| 56 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 57 | * adding a bit more processing to all queries. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 59 | public $delete_hack = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Non-persistent database connection |
| 63 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 64 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 66 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | { |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 68 | return empty($this->port) |
| 69 | ? @new mysqli($this->hostname, $this->username, $this->password, $this->database) |
| 70 | : @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Persistent database connection |
| 77 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 78 | * @return object |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 80 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | { |
Andrey Andreev | f055fa9 | 2012-01-27 20:36:23 +0200 | [diff] [blame] | 82 | // Persistent connection support was added in PHP 5.3.0 |
| 83 | if ( ! is_php('5.3')) |
| 84 | { |
| 85 | return $this->db_connect(); |
| 86 | } |
| 87 | |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 88 | return empty($this->port) |
| 89 | ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database) |
| 90 | : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | // -------------------------------------------------------------------- |
| 94 | |
| 95 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 96 | * Reconnect |
| 97 | * |
| 98 | * Keep / reestablish the db connection if no queries have been |
| 99 | * sent for a length of time exceeding the server's idle timeout |
| 100 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 101 | * @return void |
| 102 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 103 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 104 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 105 | if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE) |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 106 | { |
| 107 | $this->conn_id = FALSE; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | * Select the database |
| 115 | * |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 116 | * @param string database name |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 117 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | */ |
Andrey Andreev | 11454e0 | 2012-02-22 16:05:47 +0200 | [diff] [blame] | 119 | public function db_select($database = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | { |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 121 | if ($database === '') |
| 122 | { |
| 123 | $database = $this->database; |
| 124 | } |
| 125 | |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 126 | if (@$this->conn_id->select_db($database)) |
Andrey Andreev | 024ba2d | 2012-02-24 11:40:36 +0200 | [diff] [blame] | 127 | { |
| 128 | $this->database = $database; |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
| 137 | /** |
| 138 | * Set client character set |
| 139 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | * @param string |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 141 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | */ |
Andrey Andreev | 95bd1d1 | 2012-03-12 16:22:28 +0200 | [diff] [blame] | 143 | protected function _db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 145 | return @$this->conn_id->set_charset($charset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 151 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | * @return string |
| 154 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 155 | public function version() |
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 | return isset($this->data_cache['version']) |
| 158 | ? $this->data_cache['version'] |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 159 | : $this->data_cache['version'] = $this->conn_id->server_info; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Execute the query |
| 166 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | * @param string an SQL query |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 168 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 170 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 172 | return @$this->conn_id->query($this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
| 178 | * Prep the query |
| 179 | * |
| 180 | * If needed, each database adapter can prep the query string |
| 181 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | * @param string an SQL query |
| 183 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 185 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 187 | // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack |
| 188 | // modifies the query so that it a proper number of affected rows is returned. |
| 189 | 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] | 190 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 191 | 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] | 192 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | return $sql; |
| 195 | } |
| 196 | |
| 197 | // -------------------------------------------------------------------- |
| 198 | |
| 199 | /** |
| 200 | * Begin Transaction |
| 201 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | * @return bool |
| 203 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 204 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 207 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
| 209 | return TRUE; |
| 210 | } |
| 211 | |
| 212 | // Reset the transaction failure flag. |
| 213 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 214 | // even if the queries produce a successful result. |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 215 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | |
| 217 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 218 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 219 | return TRUE; |
| 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
| 225 | * Commit Transaction |
| 226 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | * @return bool |
| 228 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 229 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 232 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | { |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | $this->simple_query('COMMIT'); |
| 238 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 239 | return TRUE; |
| 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Rollback Transaction |
| 246 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | * @return bool |
| 248 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 249 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 252 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | { |
| 254 | return TRUE; |
| 255 | } |
| 256 | |
| 257 | $this->simple_query('ROLLBACK'); |
| 258 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 259 | return TRUE; |
| 260 | } |
| 261 | |
| 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
| 265 | * Escape String |
| 266 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 268 | * @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] | 269 | * @return string |
| 270 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 271 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 273 | if (is_array($str)) |
| 274 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 275 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 276 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 277 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return $str; |
| 281 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 282 | |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 283 | $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] | 284 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 285 | // escape LIKE condition wildcards |
| 286 | if ($like === TRUE) |
| 287 | { |
Andrey Andreev | 21cb2d3 | 2012-05-25 01:01:06 +0300 | [diff] [blame] | 288 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 289 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 290 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 291 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 293 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | // -------------------------------------------------------------------- |
| 297 | |
| 298 | /** |
| 299 | * Affected Rows |
| 300 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 301 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 303 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 305 | return $this->conn_id->affected_rows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | // -------------------------------------------------------------------- |
| 309 | |
| 310 | /** |
| 311 | * Insert ID |
| 312 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 313 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 315 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 317 | return $this->conn_id->insert_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | * List table query |
| 324 | * |
| 325 | * Generates a platform-specific query string so that the table names can be fetched |
| 326 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 327 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | * @return string |
| 329 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 330 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | { |
Andrey Andreev | 473130a | 2012-06-24 02:51:18 +0300 | [diff] [blame] | 332 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 334 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 336 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | return $sql; |
| 340 | } |
| 341 | |
| 342 | // -------------------------------------------------------------------- |
| 343 | |
| 344 | /** |
| 345 | * Show column query |
| 346 | * |
| 347 | * Generates a platform-specific query string so that the column names can be fetched |
| 348 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | * @param string the table name |
| 350 | * @return string |
| 351 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 352 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 354 | return 'SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 360 | * Returns an object with field data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | * @param string the table name |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 363 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | */ |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 365 | public function field_data($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 367 | if ($table === '') |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 368 | { |
| 369 | return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; |
| 370 | } |
| 371 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 372 | $query = $this->query('DESCRIBE '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Andrey Andreev | 3722e50 | 2012-03-03 15:04:38 +0200 | [diff] [blame] | 373 | $query = $query->result_object(); |
| 374 | |
| 375 | $retval = array(); |
| 376 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 377 | { |
| 378 | preg_match('/([a-z]+)(\(\d+\))?/', $query[$i]->Type, $matches); |
| 379 | |
| 380 | $retval[$i] = new stdClass(); |
| 381 | $retval[$i]->name = $query[$i]->Field; |
| 382 | $retval[$i]->type = empty($matches[1]) ? NULL : $matches[1]; |
| 383 | $retval[$i]->default = $query[$i]->Default; |
| 384 | $retval[$i]->max_length = empty($matches[2]) ? NULL : preg_replace('/[^\d]/', '', $matches[2]); |
| 385 | $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); |
| 386 | } |
| 387 | |
| 388 | return $retval; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // -------------------------------------------------------------------- |
| 392 | |
| 393 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 394 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 396 | * Returns an array containing code and message of the last |
| 397 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 399 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 401 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 403 | return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | // -------------------------------------------------------------------- |
| 407 | |
| 408 | /** |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 409 | * Update_Batch statement |
| 410 | * |
| 411 | * Generates a platform-specific batch update string from the supplied data |
| 412 | * |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 413 | * @param string the table name |
| 414 | * @param array the update data |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 415 | * @param string the where key |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 416 | * @return string |
| 417 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 418 | protected function _update_batch($table, $values, $index) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 419 | { |
| 420 | $ids = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 421 | foreach ($values as $key => $val) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 422 | { |
| 423 | $ids[] = $val[$index]; |
| 424 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 425 | foreach (array_keys($val) as $field) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 426 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 427 | if ($field !== $index) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 428 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 429 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 434 | $cases = ''; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 435 | foreach ($final as $k => $v) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 436 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 437 | $cases .= $k.' = CASE '."\n" |
| 438 | .implode("\n", $v)."\n" |
| 439 | .'ELSE '.$k.' END, '; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 442 | $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 443 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame^] | 444 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 445 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | // -------------------------------------------------------------------- |
| 448 | |
| 449 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | * Close DB Connection |
| 451 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | * @return void |
| 453 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 454 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | { |
Andrey Andreev | 992f175 | 2012-03-19 16:58:43 +0200 | [diff] [blame] | 456 | $this->conn_id->close(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | /* End of file mysqli_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 462 | /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ |