Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | 9ba2bf2 | 2008-09-12 23:34:39 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 28 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 29 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 30 | */
|
| 31 | class CI_DB_mysqli_driver extends CI_DB {
|
| 32 |
|
Rick Ellis | 5aa8c60 | 2008-10-07 01:24:07 +0000 | [diff] [blame] | 33 | var $dbdriver = 'mysqli';
|
| 34 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 35 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 36 | * The syntax to count rows is slightly different across different
|
| 37 | * database engines, so this string appears in each driver and is
|
| 38 | * used for the count_all() and count_all_results() functions.
|
| 39 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 40 | var $_count_string = "SELECT COUNT(*) AS ";
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 41 | var $_random_keyword = ' RAND()'; // database specific random keyword
|
| 42 |
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 43 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 44 | * Whether to use the MySQL "delete hack" which allows the number
|
| 45 | * of affected rows to be shown. Uses a preg_replace when enabled,
|
| 46 | * adding a bit more processing to all queries.
|
| 47 | */
|
| 48 | var $delete_hack = TRUE;
|
| 49 |
|
| 50 | // --------------------------------------------------------------------
|
| 51 |
|
| 52 | /**
|
| 53 | * Non-persistent database connection
|
| 54 | *
|
| 55 | * @access private called by the base class
|
| 56 | * @return resource
|
| 57 | */
|
| 58 | function db_connect()
|
| 59 | {
|
| 60 | return @mysqli_connect($this->hostname, $this->username, $this->password);
|
| 61 | }
|
| 62 |
|
| 63 | // --------------------------------------------------------------------
|
| 64 |
|
| 65 | /**
|
| 66 | * Persistent database connection
|
| 67 | *
|
| 68 | * @access private called by the base class
|
| 69 | * @return resource
|
| 70 | */
|
| 71 | function db_pconnect()
|
| 72 | {
|
| 73 | return $this->db_connect();
|
| 74 | }
|
| 75 |
|
| 76 | // --------------------------------------------------------------------
|
| 77 |
|
| 78 | /**
|
| 79 | * Select the database
|
| 80 | *
|
| 81 | * @access private called by the base class
|
| 82 | * @return resource
|
| 83 | */
|
| 84 | function db_select()
|
| 85 | {
|
| 86 | return @mysqli_select_db($this->conn_id, $this->database);
|
| 87 | }
|
| 88 |
|
| 89 | // --------------------------------------------------------------------
|
| 90 |
|
| 91 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 92 | * Set client character set
|
| 93 | *
|
| 94 | * @access public
|
| 95 | * @param string
|
| 96 | * @param string
|
| 97 | * @return resource
|
| 98 | */
|
| 99 | function db_set_charset($charset, $collation)
|
| 100 | {
|
Derek Jones | b1f90db | 2008-02-13 05:22:38 +0000 | [diff] [blame] | 101 | return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 102 | }
|
| 103 |
|
| 104 | // --------------------------------------------------------------------
|
| 105 |
|
| 106 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 107 | * Version number query string
|
| 108 | *
|
| 109 | * @access public
|
| 110 | * @return string
|
| 111 | */
|
| 112 | function _version()
|
| 113 | {
|
| 114 | return "SELECT version() AS ver";
|
| 115 | }
|
| 116 |
|
| 117 | // --------------------------------------------------------------------
|
| 118 |
|
| 119 | /**
|
| 120 | * Execute the query
|
| 121 | *
|
| 122 | * @access private called by the base class
|
| 123 | * @param string an SQL query
|
| 124 | * @return resource
|
| 125 | */
|
| 126 | function _execute($sql)
|
| 127 | {
|
| 128 | $sql = $this->_prep_query($sql);
|
| 129 | $result = @mysqli_query($this->conn_id, $sql);
|
| 130 | return $result;
|
| 131 | }
|
| 132 |
|
| 133 | // --------------------------------------------------------------------
|
| 134 |
|
| 135 | /**
|
| 136 | * Prep the query
|
| 137 | *
|
| 138 | * If needed, each database adapter can prep the query string
|
| 139 | *
|
| 140 | * @access private called by execute()
|
| 141 | * @param string an SQL query
|
| 142 | * @return string
|
| 143 | */
|
| 144 | function _prep_query($sql)
|
| 145 | {
|
| 146 | // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
|
| 147 | // the query so that it returns the number of affected rows
|
| 148 | if ($this->delete_hack === TRUE)
|
| 149 | {
|
| 150 | if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
|
| 151 | {
|
| 152 | $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return $sql;
|
| 157 | }
|
| 158 |
|
| 159 | // --------------------------------------------------------------------
|
| 160 |
|
| 161 | /**
|
| 162 | * Begin Transaction
|
| 163 | *
|
| 164 | * @access public
|
| 165 | * @return bool
|
| 166 | */
|
| 167 | function trans_begin($test_mode = FALSE)
|
| 168 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 169 | if ( ! $this->trans_enabled)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 170 | {
|
| 171 | return TRUE;
|
| 172 | }
|
| 173 |
|
| 174 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 175 | if ($this->_trans_depth > 0)
|
| 176 | {
|
| 177 | return TRUE;
|
| 178 | }
|
| 179 |
|
| 180 | // Reset the transaction failure flag.
|
| 181 | // If the $test_mode flag is set to TRUE transactions will be rolled back
|
| 182 | // even if the queries produce a successful result.
|
| 183 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
|
| 184 |
|
| 185 | $this->simple_query('SET AUTOCOMMIT=0');
|
| 186 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
|
| 187 | return TRUE;
|
| 188 | }
|
| 189 |
|
| 190 | // --------------------------------------------------------------------
|
| 191 |
|
| 192 | /**
|
| 193 | * Commit Transaction
|
| 194 | *
|
| 195 | * @access public
|
| 196 | * @return bool
|
| 197 | */
|
| 198 | function trans_commit()
|
| 199 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 200 | if ( ! $this->trans_enabled)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 201 | {
|
| 202 | return TRUE;
|
| 203 | }
|
| 204 |
|
| 205 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 206 | if ($this->_trans_depth > 0)
|
| 207 | {
|
| 208 | return TRUE;
|
| 209 | }
|
| 210 |
|
| 211 | $this->simple_query('COMMIT');
|
| 212 | $this->simple_query('SET AUTOCOMMIT=1');
|
| 213 | return TRUE;
|
| 214 | }
|
| 215 |
|
| 216 | // --------------------------------------------------------------------
|
| 217 |
|
| 218 | /**
|
| 219 | * Rollback Transaction
|
| 220 | *
|
| 221 | * @access public
|
| 222 | * @return bool
|
| 223 | */
|
| 224 | function trans_rollback()
|
| 225 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 226 | if ( ! $this->trans_enabled)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 227 | {
|
| 228 | return TRUE;
|
| 229 | }
|
| 230 |
|
| 231 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 232 | if ($this->_trans_depth > 0)
|
| 233 | {
|
| 234 | return TRUE;
|
| 235 | }
|
| 236 |
|
| 237 | $this->simple_query('ROLLBACK');
|
| 238 | $this->simple_query('SET AUTOCOMMIT=1');
|
| 239 | return TRUE;
|
| 240 | }
|
| 241 |
|
| 242 | // --------------------------------------------------------------------
|
| 243 |
|
| 244 | /**
|
| 245 | * Escape String
|
| 246 | *
|
| 247 | * @access public
|
| 248 | * @param string
|
| 249 | * @return string
|
| 250 | */
|
| 251 | function escape_str($str)
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 252 | {
|
Derek Jones | d33972b | 2008-02-13 04:16:27 +0000 | [diff] [blame] | 253 | if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 254 | {
|
| 255 | return mysqli_real_escape_string($this->conn_id, $str);
|
| 256 | }
|
| 257 | elseif (function_exists('mysql_escape_string'))
|
| 258 | {
|
| 259 | return mysql_escape_string($str);
|
| 260 | }
|
| 261 | else
|
| 262 | {
|
| 263 | return addslashes($str);
|
| 264 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 265 | }
|
| 266 |
|
| 267 | // --------------------------------------------------------------------
|
| 268 |
|
| 269 | /**
|
| 270 | * Affected Rows
|
| 271 | *
|
| 272 | * @access public
|
| 273 | * @return integer
|
| 274 | */
|
| 275 | function affected_rows()
|
| 276 | {
|
| 277 | return @mysqli_affected_rows($this->conn_id);
|
| 278 | }
|
| 279 |
|
| 280 | // --------------------------------------------------------------------
|
| 281 |
|
| 282 | /**
|
| 283 | * Insert ID
|
| 284 | *
|
| 285 | * @access public
|
| 286 | * @return integer
|
| 287 | */
|
| 288 | function insert_id()
|
| 289 | {
|
| 290 | return @mysqli_insert_id($this->conn_id);
|
| 291 | }
|
| 292 |
|
| 293 | // --------------------------------------------------------------------
|
| 294 |
|
| 295 | /**
|
| 296 | * "Count All" query
|
| 297 | *
|
| 298 | * Generates a platform-specific query string that counts all records in
|
| 299 | * the specified database
|
| 300 | *
|
| 301 | * @access public
|
| 302 | * @param string
|
| 303 | * @return string
|
| 304 | */
|
| 305 | function count_all($table = '')
|
| 306 | {
|
| 307 | if ($table == '')
|
| 308 | return '0';
|
| 309 |
|
Derek Allard | f6cd45c | 2008-01-18 14:31:51 +0000 | [diff] [blame] | 310 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 311 |
|
| 312 | if ($query->num_rows() == 0)
|
| 313 | return '0';
|
| 314 |
|
| 315 | $row = $query->row();
|
| 316 | return $row->numrows;
|
| 317 | }
|
| 318 |
|
| 319 | // --------------------------------------------------------------------
|
| 320 |
|
| 321 | /**
|
| 322 | * List table query
|
| 323 | *
|
| 324 | * Generates a platform-specific query string so that the table names can be fetched
|
| 325 | *
|
| 326 | * @access private
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 327 | * @param boolean
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 328 | * @return string
|
| 329 | */
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 330 | function _list_tables($prefix_limit = FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 331 | {
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 332 | $sql = "SHOW TABLES FROM `".$this->database."`";
|
| 333 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 334 | if ($prefix_limit !== FALSE AND $this->dbprefix != '')
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 335 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 336 | $sql .= " LIKE '".$this->dbprefix."%'";
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 337 | }
|
| 338 |
|
| 339 | return $sql;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 340 | }
|
| 341 |
|
| 342 | // --------------------------------------------------------------------
|
| 343 |
|
| 344 | /**
|
| 345 | * Show column query
|
| 346 | *
|
| 347 | * Generates a platform-specific query string so that the column names can be fetched
|
| 348 | *
|
| 349 | * @access public
|
| 350 | * @param string the table name
|
| 351 | * @return string
|
| 352 | */
|
| 353 | function _list_columns($table = '')
|
| 354 | {
|
| 355 | return "SHOW COLUMNS FROM ".$this->_escape_table($table);
|
| 356 | }
|
| 357 |
|
| 358 | // --------------------------------------------------------------------
|
| 359 |
|
| 360 | /**
|
| 361 | * Field data query
|
| 362 | *
|
| 363 | * Generates a platform-specific query so that the column data can be retrieved
|
| 364 | *
|
| 365 | * @access public
|
| 366 | * @param string the table name
|
| 367 | * @return object
|
| 368 | */
|
| 369 | function _field_data($table)
|
| 370 | {
|
| 371 | return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
|
| 372 | }
|
| 373 |
|
| 374 | // --------------------------------------------------------------------
|
| 375 |
|
| 376 | /**
|
| 377 | * The error message string
|
| 378 | *
|
| 379 | * @access private
|
| 380 | * @return string
|
| 381 | */
|
| 382 | function _error_message()
|
| 383 | {
|
| 384 | return mysqli_error($this->conn_id);
|
| 385 | }
|
| 386 |
|
| 387 | // --------------------------------------------------------------------
|
| 388 |
|
| 389 | /**
|
| 390 | * The error message number
|
| 391 | *
|
| 392 | * @access private
|
| 393 | * @return integer
|
| 394 | */
|
| 395 | function _error_number()
|
| 396 | {
|
| 397 | return mysqli_errno($this->conn_id);
|
| 398 | }
|
Rick Ellis | 52dc8ca | 2008-09-30 19:53:52 +0000 | [diff] [blame] | 399 |
|
| 400 | // --------------------------------------------------------------------
|
| 401 |
|
| 402 | /**
|
| 403 | * Escape Column Name
|
| 404 | *
|
| 405 | * This function adds backticks around supplied column name
|
| 406 | *
|
| 407 | * @access private
|
| 408 | * @param string the column name
|
| 409 | * @return string
|
| 410 | */
|
| 411 | function _escape_column($column)
|
| 412 | {
|
| 413 | return '`' .$column. '`';
|
| 414 | }
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 415 |
|
| 416 | // --------------------------------------------------------------------
|
| 417 |
|
| 418 | /**
|
| 419 | * Escape Table Name
|
| 420 | *
|
| 421 | * This function adds backticks if the table name has a period
|
| 422 | * in it. Some DBs will get cranky unless periods are escaped
|
| 423 | *
|
| 424 | * @access private
|
| 425 | * @param string the table name
|
| 426 | * @return string
|
| 427 | */
|
| 428 | function _escape_table($table)
|
| 429 | {
|
Derek Allard | c074338 | 2008-02-11 05:54:44 +0000 | [diff] [blame] | 430 | if (strpos($table, '.') !== FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 431 | {
|
Derek Allard | c074338 | 2008-02-11 05:54:44 +0000 | [diff] [blame] | 432 | $table = '`' . str_replace('.', '`.`', $table) . '`';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 433 | }
|
| 434 |
|
| 435 | return $table;
|
| 436 | }
|
| 437 |
|
| 438 | // --------------------------------------------------------------------
|
| 439 |
|
| 440 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 441 | * Protect Identifiers
|
| 442 | *
|
| 443 | * This function adds backticks if appropriate based on db type
|
| 444 | *
|
| 445 | * @access private
|
| 446 | * @param mixed the item to escape
|
| 447 | * @param boolean only affect the first word
|
| 448 | * @return mixed the item with backticks
|
| 449 | */
|
| 450 | function _protect_identifiers($item, $first_word_only = FALSE)
|
| 451 | {
|
| 452 | if (is_array($item))
|
| 453 | {
|
| 454 | $escaped_array = array();
|
| 455 |
|
| 456 | foreach($item as $k=>$v)
|
| 457 | {
|
| 458 | $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
|
| 459 | }
|
| 460 |
|
| 461 | return $escaped_array;
|
| 462 | }
|
| 463 |
|
| 464 | // This function may get "item1 item2" as a string, and so
|
| 465 | // we may need "`item1` `item2`" and not "`item1 item2`"
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 466 | if (ctype_alnum($item) === FALSE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 467 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 468 | if (strpos($item, '.') !== FALSE)
|
| 469 | {
|
| 470 | $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
|
| 471 | $table_name = substr($item, 0, strpos($item, '.')+1);
|
| 472 | $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
|
| 473 | }
|
| 474 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 475 | // This function may get "field >= 1", and need it to return "`field` >= 1"
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 476 | $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 477 |
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 478 | $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
|
| 479 | }
|
| 480 | else
|
| 481 | {
|
| 482 | return "`{$item}`";
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 483 | }
|
| 484 |
|
Derek Allard | 9a4d1da | 2008-02-25 14:18:38 +0000 | [diff] [blame] | 485 | $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 486 |
|
| 487 | foreach ($exceptions as $exception)
|
| 488 | {
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 489 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 490 | if (stristr($item, " `{$exception}` ") !== FALSE)
|
| 491 | {
|
| 492 | $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
|
| 493 | }
|
| 494 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 495 | return $item;
|
| 496 | }
|
| 497 |
|
| 498 | // --------------------------------------------------------------------
|
| 499 |
|
| 500 | /**
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 501 | * From Tables
|
| 502 | *
|
| 503 | * This function implicitly groups FROM tables so there is no confusion
|
| 504 | * about operator precedence in harmony with SQL standards
|
| 505 | *
|
| 506 | * @access public
|
| 507 | * @param type
|
| 508 | * @return type
|
| 509 | */
|
| 510 | function _from_tables($tables)
|
| 511 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 512 | if ( ! is_array($tables))
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 513 | {
|
| 514 | $tables = array($tables);
|
| 515 | }
|
| 516 |
|
| 517 | return '('.implode(', ', $tables).')';
|
| 518 | }
|
| 519 |
|
| 520 | // --------------------------------------------------------------------
|
| 521 |
|
| 522 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 523 | * Insert statement
|
| 524 | *
|
| 525 | * Generates a platform-specific insert string from the supplied data
|
| 526 | *
|
| 527 | * @access public
|
| 528 | * @param string the table name
|
| 529 | * @param array the insert keys
|
| 530 | * @param array the insert values
|
| 531 | * @return string
|
| 532 | */
|
| 533 | function _insert($table, $keys, $values)
|
| 534 | {
|
| 535 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
|
| 536 | }
|
| 537 |
|
| 538 | // --------------------------------------------------------------------
|
| 539 |
|
| 540 | /**
|
| 541 | * Update statement
|
| 542 | *
|
| 543 | * Generates a platform-specific update string from the supplied data
|
| 544 | *
|
| 545 | * @access public
|
| 546 | * @param string the table name
|
| 547 | * @param array the update data
|
| 548 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 549 | * @param array the orderby clause
|
| 550 | * @param array the limit clause
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 551 | * @return string
|
| 552 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 553 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 554 | {
|
| 555 | foreach($values as $key => $val)
|
| 556 | {
|
| 557 | $valstr[] = $key." = ".$val;
|
| 558 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 559 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 560 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 561 |
|
| 562 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 563 |
|
Derek Allard | 32cf7eb | 2008-02-05 16:03:50 +0000 | [diff] [blame] | 564 | $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
|
| 565 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
|
| 566 | $sql .= $orderby.$limit;
|
| 567 |
|
| 568 | return $sql;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 569 | }
|
| 570 |
|
| 571 |
|
| 572 | // --------------------------------------------------------------------
|
| 573 |
|
| 574 | /**
|
| 575 | * Truncate statement
|
| 576 | *
|
| 577 | * Generates a platform-specific truncate string from the supplied data
|
| 578 | * If the database does not support the truncate() command
|
| 579 | * This function maps to "DELETE FROM table"
|
| 580 | *
|
| 581 | * @access public
|
| 582 | * @param string the table name
|
| 583 | * @return string
|
| 584 | */
|
| 585 | function _truncate($table)
|
| 586 | {
|
| 587 | return "TRUNCATE ".$this->_escape_table($table);
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 588 | }
|
| 589 |
|
| 590 | // --------------------------------------------------------------------
|
| 591 |
|
| 592 | /**
|
| 593 | * Delete statement
|
| 594 | *
|
| 595 | * Generates a platform-specific delete string from the supplied data
|
| 596 | *
|
| 597 | * @access public
|
| 598 | * @param string the table name
|
| 599 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 600 | * @param string the limit clause
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 601 | * @return string
|
| 602 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 603 | function _delete($table, $where = array(), $like = array(), $limit = FALSE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 604 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 605 | $conditions = '';
|
| 606 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 607 | if (count($where) > 0 OR count($like) > 0)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 608 | {
|
| 609 | $conditions = "\nWHERE ";
|
| 610 | $conditions .= implode("\n", $this->ar_where);
|
| 611 |
|
| 612 | if (count($where) > 0 && count($like) > 0)
|
| 613 | {
|
| 614 | $conditions .= " AND ";
|
| 615 | }
|
| 616 | $conditions .= implode("\n", $like);
|
| 617 | }
|
| 618 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 619 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 620 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 621 | return "DELETE FROM ".$table.$conditions.$limit;
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 622 | }
|
| 623 |
|
| 624 | // --------------------------------------------------------------------
|
| 625 |
|
| 626 | /**
|
| 627 | * Limit string
|
| 628 | *
|
| 629 | * Generates a platform-specific LIMIT clause
|
| 630 | *
|
| 631 | * @access public
|
| 632 | * @param string the sql query string
|
| 633 | * @param integer the number of rows to limit the query to
|
| 634 | * @param integer the offset value
|
| 635 | * @return string
|
| 636 | */
|
| 637 | function _limit($sql, $limit, $offset)
|
| 638 | {
|
| 639 | $sql .= "LIMIT ".$limit;
|
| 640 |
|
| 641 | if ($offset > 0)
|
| 642 | {
|
| 643 | $sql .= " OFFSET ".$offset;
|
| 644 | }
|
| 645 |
|
| 646 | return $sql;
|
| 647 | }
|
| 648 |
|
| 649 | // --------------------------------------------------------------------
|
| 650 |
|
| 651 | /**
|
| 652 | * Close DB Connection
|
| 653 | *
|
| 654 | * @access public
|
| 655 | * @param resource
|
| 656 | * @return void
|
| 657 | */
|
| 658 | function _close($conn_id)
|
| 659 | {
|
| 660 | @mysqli_close($conn_id);
|
| 661 | }
|
| 662 |
|
| 663 |
|
| 664 | }
|
| 665 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 666 |
|
| 667 | /* End of file mysqli_driver.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 668 | /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */ |