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