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