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