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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 |
| 32 | * creates dynamically based on whether the active record |
| 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 = ''; |
| 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 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 74 | return ($this->port != '') |
| 75 | ? @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port) |
| 76 | : @mysqli_connect($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 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 94 | return ($this->port != '') |
| 95 | ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port) |
| 96 | : @mysqli_connect('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 | { |
| 111 | if (mysqli_ping($this->conn_id) === FALSE) |
| 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 | |
| 132 | if (@mysqli_select_db($this->conn_id, $database)) |
| 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 |
| 147 | * @param string |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 148 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 150 | protected function _db_set_charset($charset, $collation) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | { |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 152 | return function_exists('mysqli_set_charset') |
| 153 | ? @mysqli_set_charset($this->conn_id, $charset) |
| 154 | : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'"); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 160 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | * @return string |
| 163 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 164 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 166 | return isset($this->data_cache['version']) |
| 167 | ? $this->data_cache['version'] |
| 168 | : $this->data_cache['version'] = @mysqli_get_server_info($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * Execute the query |
| 175 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | * @param string an SQL query |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 177 | * @return mixed |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 179 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 181 | return @mysqli_query($this->conn_id, $this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Prep the query |
| 188 | * |
| 189 | * If needed, each database adapter can prep the query string |
| 190 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | * @param string an SQL query |
| 192 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 194 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 196 | // mysqli_affected_rows() returns 0 for "DELETE FROM TABLE" queries. This hack |
| 197 | // modifies the query so that it a proper number of affected rows is returned. |
| 198 | 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] | 199 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 200 | 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] | 201 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | return $sql; |
| 204 | } |
| 205 | |
| 206 | // -------------------------------------------------------------------- |
| 207 | |
| 208 | /** |
| 209 | * Begin Transaction |
| 210 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | * @return bool |
| 212 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 213 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 216 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | { |
| 218 | return TRUE; |
| 219 | } |
| 220 | |
| 221 | // Reset the transaction failure flag. |
| 222 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 223 | // even if the queries produce a successful result. |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 224 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | |
| 226 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 227 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | // -------------------------------------------------------------------- |
| 232 | |
| 233 | /** |
| 234 | * Commit Transaction |
| 235 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 236 | * @return bool |
| 237 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 238 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 241 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | { |
| 243 | return TRUE; |
| 244 | } |
| 245 | |
| 246 | $this->simple_query('COMMIT'); |
| 247 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 248 | return TRUE; |
| 249 | } |
| 250 | |
| 251 | // -------------------------------------------------------------------- |
| 252 | |
| 253 | /** |
| 254 | * Rollback Transaction |
| 255 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | * @return bool |
| 257 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 258 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 261 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | { |
| 263 | return TRUE; |
| 264 | } |
| 265 | |
| 266 | $this->simple_query('ROLLBACK'); |
| 267 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 268 | return TRUE; |
| 269 | } |
| 270 | |
| 271 | // -------------------------------------------------------------------- |
| 272 | |
| 273 | /** |
| 274 | * Escape String |
| 275 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 277 | * @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] | 278 | * @return string |
| 279 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 280 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 282 | if (is_array($str)) |
| 283 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 284 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 286 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return $str; |
| 290 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 291 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 292 | if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 294 | $str = mysqli_real_escape_string($this->conn_id, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | } |
| 296 | elseif (function_exists('mysql_escape_string')) |
| 297 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 298 | $str = mysql_escape_string($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 302 | $str = addslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 305 | // escape LIKE condition wildcards |
| 306 | if ($like === TRUE) |
| 307 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 308 | return str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 309 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 310 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 311 | return $str; |
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 | * Affected Rows |
| 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 affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | { |
| 323 | return @mysqli_affected_rows($this->conn_id); |
| 324 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
| 329 | * Insert ID |
| 330 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 331 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 333 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | { |
| 335 | return @mysqli_insert_id($this->conn_id); |
| 336 | } |
| 337 | |
| 338 | // -------------------------------------------------------------------- |
| 339 | |
| 340 | /** |
| 341 | * "Count All" query |
| 342 | * |
| 343 | * Generates a platform-specific query string that counts all records in |
| 344 | * the specified database |
| 345 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | * @param string |
| 347 | * @return string |
| 348 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 349 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | { |
| 351 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 352 | { |
| 353 | return 0; |
| 354 | } |
| 355 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 356 | $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 358 | { |
| 359 | return 0; |
| 360 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 362 | $query = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 363 | $this->_reset_select(); |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 364 | return (int) $query->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * List table query |
| 371 | * |
| 372 | * Generates a platform-specific query string so that the table names can be fetched |
| 373 | * |
| 374 | * @access private |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 375 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | * @return string |
| 377 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 378 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 380 | $sql = 'SHOW TABLES FROM '.$this->_escape_char.$this->database.$this->_escape_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 382 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 384 | return $sql." LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | return $sql; |
| 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * Show column query |
| 394 | * |
| 395 | * Generates a platform-specific query string so that the column names can be fetched |
| 396 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | * @param string the table name |
| 398 | * @return string |
| 399 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 400 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 402 | return 'SHOW COLUMNS FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // -------------------------------------------------------------------- |
| 406 | |
| 407 | /** |
| 408 | * Field data query |
| 409 | * |
| 410 | * Generates a platform-specific query so that the column data can be retrieved |
| 411 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | * @param string the table name |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 413 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 415 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 417 | return 'DESCRIBE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // -------------------------------------------------------------------- |
| 421 | |
| 422 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 423 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 425 | * Returns an array containing code and message of the last |
| 426 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 428 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 430 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 432 | return array('code' => mysqli_errno($this->conn_id), 'message' => mysqli_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
| 438 | * Escape the SQL Identifiers |
| 439 | * |
| 440 | * This function escapes column and table names |
| 441 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | * @param string |
| 443 | * @return string |
| 444 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 445 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | { |
| 447 | if ($this->_escape_char == '') |
| 448 | { |
| 449 | return $item; |
| 450 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | foreach ($this->_reserved_identifiers as $id) |
| 453 | { |
| 454 | if (strpos($item, '.'.$id) !== FALSE) |
| 455 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 456 | $item = str_replace('.', $this->_escape_char.'.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | // remove duplicates if the user already included the escape |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 459 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 460 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | if (strpos($item, '.') !== FALSE) |
| 464 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 465 | $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | // remove duplicates if the user already included the escape |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 469 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | // -------------------------------------------------------------------- |
| 473 | |
| 474 | /** |
| 475 | * From Tables |
| 476 | * |
| 477 | * This function implicitly groups FROM tables so there is no confusion |
| 478 | * about operator precedence in harmony with SQL standards |
| 479 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 480 | * @param string |
| 481 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 483 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | { |
| 485 | if ( ! is_array($tables)) |
| 486 | { |
| 487 | $tables = array($tables); |
| 488 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | return '('.implode(', ', $tables).')'; |
| 491 | } |
| 492 | |
| 493 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | /** |
| 496 | * Insert statement |
| 497 | * |
| 498 | * Generates a platform-specific insert string from the supplied data |
| 499 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | * @param string the table name |
| 501 | * @param array the insert keys |
| 502 | * @param array the insert values |
| 503 | * @return string |
| 504 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 505 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 507 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | // -------------------------------------------------------------------- |
| 511 | |
| 512 | /** |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 513 | * Insert_batch statement |
| 514 | * |
| 515 | * Generates a platform-specific insert string from the supplied data |
| 516 | * |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 517 | * @param string the table name |
| 518 | * @param array the insert keys |
| 519 | * @param array the insert values |
| 520 | * @return string |
| 521 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 522 | protected function _insert_batch($table, $keys, $values) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 523 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 524 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 525 | } |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 526 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 527 | // -------------------------------------------------------------------- |
| 528 | |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * Replace statement |
| 532 | * |
| 533 | * Generates a platform-specific replace string from the supplied data |
| 534 | * |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 535 | * @param string the table name |
| 536 | * @param array the insert keys |
| 537 | * @param array the insert values |
| 538 | * @return string |
| 539 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 540 | protected function _replace($table, $keys, $values) |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 541 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 542 | return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 543 | } |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 544 | |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 545 | // -------------------------------------------------------------------- |
| 546 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 547 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | * Update statement |
| 549 | * |
| 550 | * Generates a platform-specific update string from the supplied data |
| 551 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | * @param string the table name |
| 553 | * @param array the update data |
| 554 | * @param array the where clause |
| 555 | * @param array the orderby clause |
| 556 | * @param array the limit clause |
| 557 | * @return string |
| 558 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 559 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 561 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 563 | $valstr[] = $key.' = '.$val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 565 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 566 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
| 567 | .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') |
| 568 | .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') |
| 569 | .( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 572 | // -------------------------------------------------------------------- |
| 573 | |
| 574 | /** |
| 575 | * Update_Batch statement |
| 576 | * |
| 577 | * Generates a platform-specific batch update string from the supplied data |
| 578 | * |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 579 | * @param string the table name |
| 580 | * @param array the update data |
| 581 | * @param array the where clause |
| 582 | * @return string |
| 583 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 584 | protected function _update_batch($table, $values, $index, $where = NULL) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 585 | { |
| 586 | $ids = array(); |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 587 | foreach ($values as $key => $val) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 588 | { |
| 589 | $ids[] = $val[$index]; |
| 590 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 591 | foreach (array_keys($val) as $field) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 592 | { |
| 593 | if ($field != $index) |
| 594 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 595 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 600 | $cases = ''; |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 601 | foreach ($final as $k => $v) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 602 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 603 | $cases .= $k.' = CASE '."\n" |
| 604 | .implode("\n", $v)."\n" |
| 605 | .'ELSE '.$k.' END, '; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 606 | } |
| 607 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 608 | $where = ($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : ''; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 609 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 610 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2) |
| 611 | .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '') |
| 612 | .$index.' IN('.implode(',', $ids).')'; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 613 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 614 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | // -------------------------------------------------------------------- |
| 616 | |
| 617 | /** |
| 618 | * Truncate statement |
| 619 | * |
| 620 | * Generates a platform-specific truncate string from the supplied data |
| 621 | * If the database does not support the truncate() command |
| 622 | * This function maps to "DELETE FROM table" |
| 623 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | * @param string the table name |
| 625 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 627 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 629 | return 'TRUNCATE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | // -------------------------------------------------------------------- |
| 633 | |
| 634 | /** |
| 635 | * Delete statement |
| 636 | * |
| 637 | * Generates a platform-specific delete string from the supplied data |
| 638 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | * @param string the table name |
| 640 | * @param array the where clause |
| 641 | * @param string the limit clause |
| 642 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 644 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | { |
| 646 | $conditions = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | if (count($where) > 0 OR count($like) > 0) |
| 648 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 649 | $conditions = "\nWHERE ".implode("\n", $this->ar_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | |
| 651 | if (count($where) > 0 && count($like) > 0) |
| 652 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 653 | $conditions .= ' AND '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | } |
| 655 | $conditions .= implode("\n", $like); |
| 656 | } |
| 657 | |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 658 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // -------------------------------------------------------------------- |
| 662 | |
| 663 | /** |
| 664 | * Limit string |
| 665 | * |
| 666 | * Generates a platform-specific LIMIT clause |
| 667 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 668 | * @param string the sql query string |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 669 | * @param int the number of rows to limit the query to |
| 670 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 671 | * @return string |
| 672 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 673 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 674 | { |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 675 | return $sql.' LIMIT '.$limit |
| 676 | .($offset > 0 ? ' OFFSET '.$offset : ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | // -------------------------------------------------------------------- |
| 680 | |
| 681 | /** |
| 682 | * Close DB Connection |
| 683 | * |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 684 | * @param object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 685 | * @return void |
| 686 | */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 687 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | { |
| 689 | @mysqli_close($conn_id); |
| 690 | } |
| 691 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | /* End of file mysqli_driver.php */ |
Andrey Andreev | 1ff49e0 | 2012-01-27 11:30:41 +0200 | [diff] [blame] | 695 | /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ |