Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * MySQLi Database Adapter Class - MySQLi only works with PHP 5 |
| 32 | * |
| 33 | * Note: _DB is an extender class that the app controller |
| 34 | * creates dynamically based on whether the active record |
| 35 | * class is being used or not. |
| 36 | * |
| 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 39 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_mysqli_driver extends CI_DB { |
| 44 | |
| 45 | var $dbdriver = 'mysqli'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | // The character used for escaping |
| 48 | var $_escape_char = '`'; |
| 49 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 50 | // clause and character used for LIKE escape sequences - not used in MySQL |
| 51 | var $_like_escape_str = ''; |
| 52 | var $_like_escape_chr = ''; |
| 53 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
| 57 | * used for the count_all() and count_all_results() functions. |
| 58 | */ |
| 59 | var $_count_string = "SELECT COUNT(*) AS "; |
| 60 | var $_random_keyword = ' RAND()'; // database specific random keyword |
| 61 | |
| 62 | /** |
| 63 | * Whether to use the MySQL "delete hack" which allows the number |
| 64 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 65 | * adding a bit more processing to all queries. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | var $delete_hack = TRUE; |
| 68 | |
Derek Jones | 3b9f88d | 2011-05-20 10:25:13 -0500 | [diff] [blame] | 69 | // whether SET NAMES must be used to set the character set |
| 70 | var $use_set_names; |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 71 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | // -------------------------------------------------------------------- |
| 73 | |
| 74 | /** |
| 75 | * Non-persistent database connection |
| 76 | * |
| 77 | * @access private called by the base class |
| 78 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | function db_connect() |
| 81 | { |
| 82 | if ($this->port != '') |
| 83 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | } |
| 86 | else |
| 87 | { |
| 88 | return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | // -------------------------------------------------------------------- |
| 94 | |
| 95 | /** |
| 96 | * Persistent database connection |
| 97 | * |
| 98 | * @access private called by the base class |
| 99 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 100 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | function db_pconnect() |
| 102 | { |
| 103 | return $this->db_connect(); |
| 104 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | // -------------------------------------------------------------------- |
| 107 | |
| 108 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 109 | * Reconnect |
| 110 | * |
| 111 | * Keep / reestablish the db connection if no queries have been |
| 112 | * sent for a length of time exceeding the server's idle timeout |
| 113 | * |
| 114 | * @access public |
| 115 | * @return void |
| 116 | */ |
| 117 | function reconnect() |
| 118 | { |
| 119 | if (mysqli_ping($this->conn_id) === FALSE) |
| 120 | { |
| 121 | $this->conn_id = FALSE; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // -------------------------------------------------------------------- |
| 126 | |
| 127 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 128 | * Select the database |
| 129 | * |
| 130 | * @access private called by the base class |
| 131 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | function db_select() |
| 134 | { |
| 135 | return @mysqli_select_db($this->conn_id, $this->database); |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | /** |
| 141 | * Set client character set |
| 142 | * |
| 143 | * @access private |
| 144 | * @param string |
| 145 | * @param string |
| 146 | * @return resource |
| 147 | */ |
| 148 | function _db_set_charset($charset, $collation) |
| 149 | { |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 150 | return function_exists('mysqli_set_charset') |
| 151 | ? @mysqli_set_charset($this->conn_id, $charset) |
| 152 | : @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] | 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Version number query string |
| 159 | * |
| 160 | * @access public |
| 161 | * @return string |
| 162 | */ |
| 163 | function _version() |
| 164 | { |
| 165 | return "SELECT version() AS ver"; |
| 166 | } |
| 167 | |
| 168 | // -------------------------------------------------------------------- |
| 169 | |
| 170 | /** |
| 171 | * Execute the query |
| 172 | * |
| 173 | * @access private called by the base class |
| 174 | * @param string an SQL query |
| 175 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | function _execute($sql) |
| 178 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | $sql = $this->_prep_query($sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | $result = @mysqli_query($this->conn_id, $sql); |
| 181 | return $result; |
| 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 | * |
| 191 | * @access private called by execute() |
| 192 | * @param string an SQL query |
| 193 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | function _prep_query($sql) |
| 196 | { |
| 197 | // "DELETE FROM TABLE" returns 0 affected rows This hack modifies |
| 198 | // the query so that it returns the number of affected rows |
| 199 | if ($this->delete_hack === TRUE) |
| 200 | { |
| 201 | if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) |
| 202 | { |
| 203 | $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); |
| 204 | } |
| 205 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | return $sql; |
| 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Begin Transaction |
| 214 | * |
| 215 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | * @return bool |
| 217 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | function trans_begin($test_mode = FALSE) |
| 219 | { |
| 220 | if ( ! $this->trans_enabled) |
| 221 | { |
| 222 | return TRUE; |
| 223 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 226 | if ($this->_trans_depth > 0) |
| 227 | { |
| 228 | return TRUE; |
| 229 | } |
| 230 | |
| 231 | // Reset the transaction failure flag. |
| 232 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 233 | // even if the queries produce a successful result. |
| 234 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 235 | |
| 236 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 237 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * Commit Transaction |
| 245 | * |
| 246 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | * @return bool |
| 248 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | function trans_commit() |
| 250 | { |
| 251 | if ( ! $this->trans_enabled) |
| 252 | { |
| 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 257 | if ($this->_trans_depth > 0) |
| 258 | { |
| 259 | return TRUE; |
| 260 | } |
| 261 | |
| 262 | $this->simple_query('COMMIT'); |
| 263 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
| 270 | * Rollback Transaction |
| 271 | * |
| 272 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 273 | * @return bool |
| 274 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | function trans_rollback() |
| 276 | { |
| 277 | if ( ! $this->trans_enabled) |
| 278 | { |
| 279 | return TRUE; |
| 280 | } |
| 281 | |
| 282 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 283 | if ($this->_trans_depth > 0) |
| 284 | { |
| 285 | return TRUE; |
| 286 | } |
| 287 | |
| 288 | $this->simple_query('ROLLBACK'); |
| 289 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 290 | return TRUE; |
| 291 | } |
| 292 | |
| 293 | // -------------------------------------------------------------------- |
| 294 | |
| 295 | /** |
| 296 | * Escape String |
| 297 | * |
| 298 | * @access public |
| 299 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 300 | * @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] | 301 | * @return string |
| 302 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 305 | if (is_array($str)) |
| 306 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 307 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 309 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return $str; |
| 313 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 314 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id)) |
| 316 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 317 | $str = mysqli_real_escape_string($this->conn_id, $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | } |
| 319 | elseif (function_exists('mysql_escape_string')) |
| 320 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 321 | $str = mysql_escape_string($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | } |
| 323 | else |
| 324 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 325 | $str = addslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 326 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 328 | // escape LIKE condition wildcards |
| 329 | if ($like === TRUE) |
| 330 | { |
| 331 | $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
| 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 334 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 336 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * Affected Rows |
| 341 | * |
| 342 | * @access public |
| 343 | * @return integer |
| 344 | */ |
| 345 | function affected_rows() |
| 346 | { |
| 347 | return @mysqli_affected_rows($this->conn_id); |
| 348 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | // -------------------------------------------------------------------- |
| 351 | |
| 352 | /** |
| 353 | * Insert ID |
| 354 | * |
| 355 | * @access public |
| 356 | * @return integer |
| 357 | */ |
| 358 | function insert_id() |
| 359 | { |
| 360 | return @mysqli_insert_id($this->conn_id); |
| 361 | } |
| 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * "Count All" query |
| 367 | * |
| 368 | * Generates a platform-specific query string that counts all records in |
| 369 | * the specified database |
| 370 | * |
| 371 | * @access public |
| 372 | * @param string |
| 373 | * @return string |
| 374 | */ |
| 375 | function count_all($table = '') |
| 376 | { |
| 377 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 378 | { |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 383 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 385 | { |
| 386 | return 0; |
| 387 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | |
| 389 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 390 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 391 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * List table query |
| 398 | * |
| 399 | * Generates a platform-specific query string so that the table names can be fetched |
| 400 | * |
| 401 | * @access private |
| 402 | * @param boolean |
| 403 | * @return string |
| 404 | */ |
| 405 | function _list_tables($prefix_limit = FALSE) |
| 406 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char; |
| 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 410 | { |
Derek Jones | 3c11b6f | 2009-02-20 22:36:27 +0000 | [diff] [blame] | 411 | $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | return $sql; |
| 415 | } |
| 416 | |
| 417 | // -------------------------------------------------------------------- |
| 418 | |
| 419 | /** |
| 420 | * Show column query |
| 421 | * |
| 422 | * Generates a platform-specific query string so that the column names can be fetched |
| 423 | * |
| 424 | * @access public |
| 425 | * @param string the table name |
| 426 | * @return string |
| 427 | */ |
| 428 | function _list_columns($table = '') |
| 429 | { |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 430 | return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | // -------------------------------------------------------------------- |
| 434 | |
| 435 | /** |
| 436 | * Field data query |
| 437 | * |
| 438 | * Generates a platform-specific query so that the column data can be retrieved |
| 439 | * |
| 440 | * @access public |
| 441 | * @param string the table name |
| 442 | * @return object |
| 443 | */ |
| 444 | function _field_data($table) |
| 445 | { |
danmontgomery | fc75645 | 2011-08-21 15:31:22 -0400 | [diff] [blame] | 446 | return "DESCRIBE ".$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // -------------------------------------------------------------------- |
| 450 | |
| 451 | /** |
| 452 | * The error message string |
| 453 | * |
| 454 | * @access private |
| 455 | * @return string |
| 456 | */ |
| 457 | function _error_message() |
| 458 | { |
| 459 | return mysqli_error($this->conn_id); |
| 460 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | // -------------------------------------------------------------------- |
| 463 | |
| 464 | /** |
| 465 | * The error message number |
| 466 | * |
| 467 | * @access private |
| 468 | * @return integer |
| 469 | */ |
| 470 | function _error_number() |
| 471 | { |
| 472 | return mysqli_errno($this->conn_id); |
| 473 | } |
| 474 | |
| 475 | // -------------------------------------------------------------------- |
| 476 | |
| 477 | /** |
| 478 | * Escape the SQL Identifiers |
| 479 | * |
| 480 | * This function escapes column and table names |
| 481 | * |
| 482 | * @access private |
| 483 | * @param string |
| 484 | * @return string |
| 485 | */ |
| 486 | function _escape_identifiers($item) |
| 487 | { |
| 488 | if ($this->_escape_char == '') |
| 489 | { |
| 490 | return $item; |
| 491 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 492 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | foreach ($this->_reserved_identifiers as $id) |
| 494 | { |
| 495 | if (strpos($item, '.'.$id) !== FALSE) |
| 496 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 498 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | // remove duplicates if the user already included the escape |
| 500 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 501 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | if (strpos($item, '.') !== FALSE) |
| 505 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | } |
| 508 | else |
| 509 | { |
| 510 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | // remove duplicates if the user already included the escape |
| 514 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 515 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | // -------------------------------------------------------------------- |
| 518 | |
| 519 | /** |
| 520 | * From Tables |
| 521 | * |
| 522 | * This function implicitly groups FROM tables so there is no confusion |
| 523 | * about operator precedence in harmony with SQL standards |
| 524 | * |
| 525 | * @access public |
| 526 | * @param type |
| 527 | * @return type |
| 528 | */ |
| 529 | function _from_tables($tables) |
| 530 | { |
| 531 | if ( ! is_array($tables)) |
| 532 | { |
| 533 | $tables = array($tables); |
| 534 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 535 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | return '('.implode(', ', $tables).')'; |
| 537 | } |
| 538 | |
| 539 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 540 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | /** |
| 542 | * Insert statement |
| 543 | * |
| 544 | * Generates a platform-specific insert string from the supplied data |
| 545 | * |
| 546 | * @access public |
| 547 | * @param string the table name |
| 548 | * @param array the insert keys |
| 549 | * @param array the insert values |
| 550 | * @return string |
| 551 | */ |
| 552 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 555 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 556 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | // -------------------------------------------------------------------- |
| 558 | |
| 559 | /** |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 560 | * Insert_batch statement |
| 561 | * |
| 562 | * Generates a platform-specific insert string from the supplied data |
| 563 | * |
| 564 | * @access public |
| 565 | * @param string the table name |
| 566 | * @param array the insert keys |
| 567 | * @param array the insert values |
| 568 | * @return string |
| 569 | */ |
| 570 | function _insert_batch($table, $keys, $values) |
| 571 | { |
| 572 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 573 | } |
RH Becker | cfdb232 | 2011-10-03 17:28:32 -0700 | [diff] [blame] | 574 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 575 | // -------------------------------------------------------------------- |
| 576 | |
Phil Sturgeon | 3b3782a | 2011-11-08 14:49:24 +0000 | [diff] [blame] | 577 | |
| 578 | /** |
| 579 | * Replace statement |
| 580 | * |
| 581 | * Generates a platform-specific replace string from the supplied data |
| 582 | * |
| 583 | * @access public |
| 584 | * @param string the table name |
| 585 | * @param array the insert keys |
| 586 | * @param array the insert values |
| 587 | * @return string |
| 588 | */ |
| 589 | function _replace($table, $keys, $values) |
| 590 | { |
| 591 | return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 592 | } |
| 593 | |
| 594 | // -------------------------------------------------------------------- |
| 595 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 596 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 597 | * Update statement |
| 598 | * |
| 599 | * Generates a platform-specific update string from the supplied data |
| 600 | * |
| 601 | * @access public |
| 602 | * @param string the table name |
| 603 | * @param array the update data |
| 604 | * @param array the where clause |
| 605 | * @param array the orderby clause |
| 606 | * @param array the limit clause |
| 607 | * @return string |
| 608 | */ |
| 609 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 610 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 611 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | { |
| 613 | $valstr[] = $key." = ".$val; |
| 614 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 615 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 617 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 619 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 620 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 621 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 623 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 625 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 626 | return $sql; |
| 627 | } |
| 628 | |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 629 | // -------------------------------------------------------------------- |
| 630 | |
| 631 | /** |
| 632 | * Update_Batch statement |
| 633 | * |
| 634 | * Generates a platform-specific batch update string from the supplied data |
| 635 | * |
| 636 | * @access public |
| 637 | * @param string the table name |
| 638 | * @param array the update data |
| 639 | * @param array the where clause |
| 640 | * @return string |
| 641 | */ |
| 642 | function _update_batch($table, $values, $index, $where = NULL) |
| 643 | { |
| 644 | $ids = array(); |
| 645 | $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
| 646 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 647 | foreach ($values as $key => $val) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 648 | { |
| 649 | $ids[] = $val[$index]; |
| 650 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 651 | foreach (array_keys($val) as $field) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 652 | { |
| 653 | if ($field != $index) |
| 654 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 655 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | $sql = "UPDATE ".$table." SET "; |
| 661 | $cases = ''; |
| 662 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 663 | foreach ($final as $k => $v) |
Pascal Kriete | 43ded23 | 2011-01-07 15:05:40 -0500 | [diff] [blame] | 664 | { |
| 665 | $cases .= $k.' = CASE '."\n"; |
| 666 | foreach ($v as $row) |
| 667 | { |
| 668 | $cases .= $row."\n"; |
| 669 | } |
| 670 | |
| 671 | $cases .= 'ELSE '.$k.' END, '; |
| 672 | } |
| 673 | |
| 674 | $sql .= substr($cases, 0, -2); |
| 675 | |
| 676 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 677 | |
| 678 | return $sql; |
| 679 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 680 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 681 | // -------------------------------------------------------------------- |
| 682 | |
| 683 | /** |
| 684 | * Truncate statement |
| 685 | * |
| 686 | * Generates a platform-specific truncate string from the supplied data |
| 687 | * If the database does not support the truncate() command |
| 688 | * This function maps to "DELETE FROM table" |
| 689 | * |
| 690 | * @access public |
| 691 | * @param string the table name |
| 692 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 693 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 694 | function _truncate($table) |
| 695 | { |
| 696 | return "TRUNCATE ".$table; |
| 697 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 698 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 699 | // -------------------------------------------------------------------- |
| 700 | |
| 701 | /** |
| 702 | * Delete statement |
| 703 | * |
| 704 | * Generates a platform-specific delete string from the supplied data |
| 705 | * |
| 706 | * @access public |
| 707 | * @param string the table name |
| 708 | * @param array the where clause |
| 709 | * @param string the limit clause |
| 710 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 711 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 712 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 713 | { |
| 714 | $conditions = ''; |
| 715 | |
| 716 | if (count($where) > 0 OR count($like) > 0) |
| 717 | { |
| 718 | $conditions = "\nWHERE "; |
| 719 | $conditions .= implode("\n", $this->ar_where); |
| 720 | |
| 721 | if (count($where) > 0 && count($like) > 0) |
| 722 | { |
| 723 | $conditions .= " AND "; |
| 724 | } |
| 725 | $conditions .= implode("\n", $like); |
| 726 | } |
| 727 | |
| 728 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 729 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 730 | return "DELETE FROM ".$table.$conditions.$limit; |
| 731 | } |
| 732 | |
| 733 | // -------------------------------------------------------------------- |
| 734 | |
| 735 | /** |
| 736 | * Limit string |
| 737 | * |
| 738 | * Generates a platform-specific LIMIT clause |
| 739 | * |
| 740 | * @access public |
| 741 | * @param string the sql query string |
| 742 | * @param integer the number of rows to limit the query to |
| 743 | * @param integer the offset value |
| 744 | * @return string |
| 745 | */ |
| 746 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 747 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 748 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 749 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 750 | if ($offset > 0) |
| 751 | { |
| 752 | $sql .= " OFFSET ".$offset; |
| 753 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 754 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 755 | return $sql; |
| 756 | } |
| 757 | |
| 758 | // -------------------------------------------------------------------- |
| 759 | |
| 760 | /** |
| 761 | * Close DB Connection |
| 762 | * |
| 763 | * @access public |
| 764 | * @param resource |
| 765 | * @return void |
| 766 | */ |
| 767 | function _close($conn_id) |
| 768 | { |
| 769 | @mysqli_close($conn_id); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | } |
| 774 | |
| 775 | |
| 776 | /* End of file mysqli_driver.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 777 | /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ |