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