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