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