admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.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 Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class CI_DB_mysqli_driver extends CI_DB { |
| 32 | |
| 33 | /** |
| 34 | * Whether to use the MySQL "delete hack" which allows the number |
| 35 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 36 | * adding a bit more processing to all queries. |
| 37 | */ |
| 38 | var $delete_hack = TRUE; |
| 39 | |
| 40 | // -------------------------------------------------------------------- |
| 41 | |
| 42 | /** |
| 43 | * Non-persistent database connection |
| 44 | * |
| 45 | * @access private called by the base class |
| 46 | * @return resource |
| 47 | */ |
| 48 | function db_connect() |
| 49 | { |
| 50 | return mysqli_connect($this->hostname, $this->username, $this->password); |
| 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Persistent database connection |
| 57 | * |
| 58 | * @access private called by the base class |
| 59 | * @return resource |
| 60 | */ |
| 61 | function db_pconnect() |
| 62 | { |
| 63 | return $this->db_connect(); |
| 64 | } |
| 65 | |
| 66 | // -------------------------------------------------------------------- |
| 67 | |
| 68 | /** |
| 69 | * Select the database |
| 70 | * |
| 71 | * @access private called by the base class |
| 72 | * @return resource |
| 73 | */ |
| 74 | function db_select() |
| 75 | { |
| 76 | return @mysqli_select_db($this->conn_id, $this->database); |
| 77 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Version number query string |
| 83 | * |
| 84 | * @access public |
| 85 | * @return string |
| 86 | */ |
| 87 | function _version() |
| 88 | { |
| 89 | return "SELECT version() AS ver"; |
| 90 | } |
| 91 | |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Execute the query |
| 96 | * |
| 97 | * @access private called by the base class |
| 98 | * @param string an SQL query |
| 99 | * @return resource |
| 100 | */ |
| 101 | function _execute($sql) |
| 102 | { |
| 103 | $sql = $this->_prep_query($sql); |
| 104 | $result = @mysqli_query($this->conn_id, $sql); |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | // -------------------------------------------------------------------- |
| 109 | |
| 110 | /** |
| 111 | * Prep the query |
| 112 | * |
| 113 | * If needed, each database adapter can prep the query string |
| 114 | * |
| 115 | * @access private called by execute() |
| 116 | * @param string an SQL query |
| 117 | * @return string |
| 118 | */ |
| 119 | function _prep_query($sql) |
| 120 | { |
| 121 | // "DELETE FROM TABLE" returns 0 affected rows This hack modifies |
| 122 | // the query so that it returns the number of affected rows |
| 123 | if ($this->delete_hack === TRUE) |
| 124 | { |
| 125 | if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) |
| 126 | { |
| 127 | $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return $sql; |
| 132 | } |
| 133 | |
| 134 | // -------------------------------------------------------------------- |
| 135 | |
| 136 | /** |
| 137 | * Begin Transaction |
| 138 | * |
| 139 | * @access public |
| 140 | * @return bool |
| 141 | */ |
| 142 | function trans_begin($test_mode = FALSE) |
| 143 | { |
| 144 | if ( ! $this->trans_enabled) |
| 145 | { |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 150 | if ($this->_trans_depth > 0) |
| 151 | { |
| 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | // Reset the transaction failure flag. |
| 156 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 157 | // even if the queries produce a successful result. |
| 158 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 159 | |
| 160 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 161 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 162 | return TRUE; |
| 163 | } |
| 164 | |
| 165 | // -------------------------------------------------------------------- |
| 166 | |
| 167 | /** |
| 168 | * Commit Transaction |
| 169 | * |
| 170 | * @access public |
| 171 | * @return bool |
| 172 | */ |
| 173 | function trans_commit() |
| 174 | { |
| 175 | if ( ! $this->trans_enabled) |
| 176 | { |
| 177 | return TRUE; |
| 178 | } |
| 179 | |
| 180 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 181 | if ($this->_trans_depth > 0) |
| 182 | { |
| 183 | return TRUE; |
| 184 | } |
| 185 | |
| 186 | $this->simple_query('COMMIT'); |
| 187 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 188 | return TRUE; |
| 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
| 192 | |
| 193 | /** |
| 194 | * Rollback Transaction |
| 195 | * |
| 196 | * @access public |
| 197 | * @return bool |
| 198 | */ |
| 199 | function trans_rollback() |
| 200 | { |
| 201 | if ( ! $this->trans_enabled) |
| 202 | { |
| 203 | return TRUE; |
| 204 | } |
| 205 | |
| 206 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 207 | if ($this->_trans_depth > 0) |
| 208 | { |
| 209 | return TRUE; |
| 210 | } |
| 211 | |
| 212 | $this->simple_query('ROLLBACK'); |
| 213 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 214 | return TRUE; |
| 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Escape String |
| 221 | * |
| 222 | * @access public |
| 223 | * @param string |
| 224 | * @return string |
| 225 | */ |
| 226 | function escape_str($str) |
| 227 | { |
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame^] | 228 | if (get_magic_quotes_gpc()) |
| 229 | { |
| 230 | return $str; |
| 231 | } |
| 232 | |
| 233 | if (function_exists('mysql_escape_string')) |
| 234 | { |
| 235 | return mysqli_real_escape_string($this->conn_id, $str); |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | return addslashes($str); |
| 240 | } |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // -------------------------------------------------------------------- |
| 244 | |
| 245 | /** |
| 246 | * Affected Rows |
| 247 | * |
| 248 | * @access public |
| 249 | * @return integer |
| 250 | */ |
| 251 | function affected_rows() |
| 252 | { |
| 253 | return @mysqli_affected_rows($this->conn_id); |
| 254 | } |
| 255 | |
| 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Insert ID |
| 260 | * |
| 261 | * @access public |
| 262 | * @return integer |
| 263 | */ |
| 264 | function insert_id() |
| 265 | { |
| 266 | return @mysqli_insert_id($this->conn_id); |
| 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * "Count All" query |
| 273 | * |
| 274 | * Generates a platform-specific query string that counts all records in |
| 275 | * the specified database |
| 276 | * |
| 277 | * @access public |
| 278 | * @param string |
| 279 | * @return string |
| 280 | */ |
| 281 | function count_all($table = '') |
| 282 | { |
| 283 | if ($table == '') |
| 284 | return '0'; |
| 285 | |
| 286 | $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`"); |
| 287 | |
| 288 | if ($query->num_rows() == 0) |
| 289 | return '0'; |
| 290 | |
| 291 | $row = $query->row(); |
| 292 | return $row->numrows; |
| 293 | } |
| 294 | |
| 295 | // -------------------------------------------------------------------- |
| 296 | |
| 297 | /** |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 298 | * Show columnn query |
| 299 | * |
| 300 | * Generates a platform-specific query string so that the column names can be fetched |
| 301 | * |
| 302 | * @access public |
| 303 | * @param string the table name |
| 304 | * @return string |
| 305 | */ |
| 306 | function _list_columns($table = '') |
| 307 | { |
| 308 | return "SHOW COLUMNS FROM ".$this->_escape_table($table); |
| 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
| 314 | * Field data query |
| 315 | * |
| 316 | * Generates a platform-specific query so that the column data can be retrieved |
| 317 | * |
| 318 | * @access public |
| 319 | * @param string the table name |
| 320 | * @return object |
| 321 | */ |
| 322 | function _field_data($table) |
| 323 | { |
| 324 | return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1"; |
| 325 | } |
| 326 | |
| 327 | // -------------------------------------------------------------------- |
| 328 | |
| 329 | /** |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 330 | * The error message string |
| 331 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 332 | * @access private |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 333 | * @return string |
| 334 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 335 | function _error_message() |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 336 | { |
| 337 | return mysqli_error($this->conn_id); |
| 338 | } |
| 339 | |
| 340 | // -------------------------------------------------------------------- |
| 341 | |
| 342 | /** |
| 343 | * The error message number |
| 344 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 345 | * @access private |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 346 | * @return integer |
| 347 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 348 | function _error_number() |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 349 | { |
| 350 | return mysqli_errno($this->conn_id); |
| 351 | } |
| 352 | |
| 353 | // -------------------------------------------------------------------- |
| 354 | |
| 355 | /** |
| 356 | * Escape Table Name |
| 357 | * |
| 358 | * This function adds backticks if the table name has a period |
| 359 | * in it. Some DBs will get cranky unless periods are escaped |
| 360 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 361 | * @access private |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 362 | * @param string the table name |
| 363 | * @return string |
| 364 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 365 | function _escape_table($table) |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 366 | { |
| 367 | if (stristr($table, '.')) |
| 368 | { |
| 369 | $table = preg_replace("/\./", "`.`", $table); |
| 370 | } |
| 371 | |
| 372 | return $table; |
| 373 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 374 | |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 375 | // -------------------------------------------------------------------- |
| 376 | |
| 377 | /** |
| 378 | * Insert statement |
| 379 | * |
| 380 | * Generates a platform-specific insert string from the supplied data |
| 381 | * |
| 382 | * @access public |
| 383 | * @param string the table name |
| 384 | * @param array the insert keys |
| 385 | * @param array the insert values |
| 386 | * @return string |
| 387 | */ |
| 388 | function _insert($table, $keys, $values) |
| 389 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 390 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // -------------------------------------------------------------------- |
| 394 | |
| 395 | /** |
| 396 | * Update statement |
| 397 | * |
| 398 | * Generates a platform-specific update string from the supplied data |
| 399 | * |
| 400 | * @access public |
| 401 | * @param string the table name |
| 402 | * @param array the update data |
| 403 | * @param array the where clause |
| 404 | * @return string |
| 405 | */ |
| 406 | function _update($table, $values, $where) |
| 407 | { |
| 408 | foreach($values as $key => $val) |
| 409 | { |
| 410 | $valstr[] = $key." = ".$val; |
| 411 | } |
| 412 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 413 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // -------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
| 419 | * Delete statement |
| 420 | * |
| 421 | * Generates a platform-specific delete string from the supplied data |
| 422 | * |
| 423 | * @access public |
| 424 | * @param string the table name |
| 425 | * @param array the where clause |
| 426 | * @return string |
| 427 | */ |
| 428 | function _delete($table, $where) |
| 429 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 430 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 431 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 432 | |
| 433 | // -------------------------------------------------------------------- |
| 434 | |
| 435 | /** |
| 436 | * Limit string |
| 437 | * |
| 438 | * Generates a platform-specific LIMIT clause |
| 439 | * |
| 440 | * @access public |
| 441 | * @param string the sql query string |
| 442 | * @param integer the number of rows to limit the query to |
| 443 | * @param integer the offset value |
| 444 | * @return string |
| 445 | */ |
| 446 | function _limit($sql, $limit, $offset) |
| 447 | { |
| 448 | $sql .= "LIMIT ".$limit; |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 449 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 450 | if ($offset > 0) |
| 451 | { |
| 452 | $sql .= " OFFSET ".$offset; |
| 453 | } |
| 454 | |
| 455 | return $sql; |
| 456 | } |
| 457 | |
| 458 | // -------------------------------------------------------------------- |
| 459 | |
| 460 | /** |
| 461 | * Close DB Connection |
| 462 | * |
| 463 | * @access public |
| 464 | * @param resource |
| 465 | * @return void |
| 466 | */ |
| 467 | function _close($conn_id) |
| 468 | { |
| 469 | mysqli_close($conn_id); |
| 470 | } |
| 471 | |
admin | ff2d251 | 2006-09-24 18:12:18 +0000 | [diff] [blame] | 472 | |
| 473 | } |
| 474 | |
| 475 | ?> |