admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * 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 | * Execute the query |
| 81 | * |
| 82 | * @access private called by the base class |
| 83 | * @param string an SQL query |
| 84 | * @return resource |
| 85 | */ |
| 86 | function _execute($sql) |
| 87 | { |
| 88 | $sql = $this->_prep_query($sql); |
| 89 | return @mysql_query($sql, $this->conn_id); |
| 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Prep the query |
| 96 | * |
| 97 | * If needed, each database adapter can prep the query string |
| 98 | * |
| 99 | * @access private called by execute() |
| 100 | * @param string an SQL query |
| 101 | * @return string |
| 102 | */ |
| 103 | function _prep_query($sql) |
| 104 | { |
| 105 | // "DELETE FROM TABLE" returns 0 affected rows This hack modifies |
| 106 | // the query so that it returns the number of affected rows |
| 107 | if ($this->delete_hack === TRUE) |
| 108 | { |
| 109 | if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) |
| 110 | { |
| 111 | $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $sql; |
| 116 | } |
| 117 | |
| 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
| 121 | * Begin Transaction |
| 122 | * |
| 123 | * @access public |
| 124 | * @return bool |
| 125 | */ |
| 126 | function trans_begin($test_mode = FALSE) |
| 127 | { |
| 128 | if ( ! $this->trans_enabled) |
| 129 | { |
| 130 | return TRUE; |
| 131 | } |
| 132 | |
| 133 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 134 | if ($this->_trans_depth > 0) |
| 135 | { |
| 136 | return TRUE; |
| 137 | } |
| 138 | |
| 139 | // Reset the transaction failure flag. |
| 140 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 141 | // even if the queries produce a successful result. |
| 142 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 143 | |
| 144 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 145 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
| 152 | * Commit Transaction |
| 153 | * |
| 154 | * @access public |
| 155 | * @return bool |
| 156 | */ |
| 157 | function trans_commit() |
| 158 | { |
| 159 | if ( ! $this->trans_enabled) |
| 160 | { |
| 161 | return TRUE; |
| 162 | } |
| 163 | |
| 164 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 165 | if ($this->_trans_depth > 0) |
| 166 | { |
| 167 | return TRUE; |
| 168 | } |
| 169 | |
| 170 | $this->simple_query('COMMIT'); |
| 171 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 172 | return TRUE; |
| 173 | } |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
| 178 | * Rollback Transaction |
| 179 | * |
| 180 | * @access public |
| 181 | * @return bool |
| 182 | */ |
| 183 | function trans_rollback() |
| 184 | { |
| 185 | if ( ! $this->trans_enabled) |
| 186 | { |
| 187 | return TRUE; |
| 188 | } |
| 189 | |
| 190 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 191 | if ($this->_trans_depth > 0) |
| 192 | { |
| 193 | return TRUE; |
| 194 | } |
| 195 | |
| 196 | $this->simple_query('ROLLBACK'); |
| 197 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 198 | return TRUE; |
| 199 | } |
| 200 | |
| 201 | // -------------------------------------------------------------------- |
| 202 | |
| 203 | /** |
| 204 | * Escape String |
| 205 | * |
| 206 | * @access public |
| 207 | * @param string |
| 208 | * @return string |
| 209 | */ |
| 210 | function escape_str($str) |
| 211 | { |
| 212 | return mysql_real_escape_string($str); |
| 213 | } |
| 214 | |
| 215 | // -------------------------------------------------------------------- |
| 216 | |
| 217 | /** |
| 218 | * Affected Rows |
| 219 | * |
| 220 | * @access public |
| 221 | * @return integer |
| 222 | */ |
| 223 | function affected_rows() |
| 224 | { |
| 225 | return @mysql_affected_rows($this->conn_id); |
| 226 | } |
| 227 | |
| 228 | // -------------------------------------------------------------------- |
| 229 | |
| 230 | /** |
| 231 | * Insert ID |
| 232 | * |
| 233 | * @access public |
| 234 | * @return integer |
| 235 | */ |
| 236 | function insert_id() |
| 237 | { |
| 238 | return @mysql_insert_id($this->conn_id); |
| 239 | } |
| 240 | |
| 241 | // -------------------------------------------------------------------- |
| 242 | |
| 243 | /** |
| 244 | * "Count All" query |
| 245 | * |
| 246 | * Generates a platform-specific query string that counts all records in |
| 247 | * the specified database |
| 248 | * |
| 249 | * @access public |
| 250 | * @param string |
| 251 | * @return string |
| 252 | */ |
| 253 | function count_all($table = '') |
| 254 | { |
| 255 | if ($table == '') |
| 256 | return '0'; |
| 257 | |
| 258 | $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`"); |
| 259 | |
| 260 | if ($query->num_rows() == 0) |
| 261 | return '0'; |
| 262 | |
| 263 | $row = $query->row(); |
| 264 | return $row->numrows; |
| 265 | } |
| 266 | |
| 267 | // -------------------------------------------------------------------- |
| 268 | |
| 269 | /** |
| 270 | * The error message string |
| 271 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 272 | * @access private |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 273 | * @return string |
| 274 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 275 | function _error_message() |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 276 | { |
| 277 | return mysql_error($this->conn_id); |
| 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
| 283 | * The error message number |
| 284 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 285 | * @access private |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 286 | * @return integer |
| 287 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 288 | function _error_number() |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 289 | { |
| 290 | return mysql_errno($this->conn_id); |
| 291 | } |
| 292 | |
| 293 | // -------------------------------------------------------------------- |
| 294 | |
| 295 | /** |
| 296 | * Escape Table Name |
| 297 | * |
| 298 | * This function adds backticks if the table name has a period |
| 299 | * in it. Some DBs will get cranky unless periods are escaped |
| 300 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 301 | * @access private |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 302 | * @param string the table name |
| 303 | * @return string |
| 304 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 305 | function _escape_table($table) |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 306 | { |
| 307 | if (stristr($table, '.')) |
| 308 | { |
| 309 | $table = preg_replace("/\./", "`.`", $table); |
| 310 | } |
| 311 | |
| 312 | return $table; |
| 313 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 314 | |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 315 | // -------------------------------------------------------------------- |
| 316 | |
| 317 | /** |
| 318 | * Insert statement |
| 319 | * |
| 320 | * Generates a platform-specific insert string from the supplied data |
| 321 | * |
| 322 | * @access public |
| 323 | * @param string the table name |
| 324 | * @param array the insert keys |
| 325 | * @param array the insert values |
| 326 | * @return string |
| 327 | */ |
| 328 | function _insert($table, $keys, $values) |
| 329 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 330 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // -------------------------------------------------------------------- |
| 334 | |
| 335 | /** |
| 336 | * Update statement |
| 337 | * |
| 338 | * Generates a platform-specific update string from the supplied data |
| 339 | * |
| 340 | * @access public |
| 341 | * @param string the table name |
| 342 | * @param array the update data |
| 343 | * @param array the where clause |
| 344 | * @return string |
| 345 | */ |
| 346 | function _update($table, $values, $where) |
| 347 | { |
| 348 | foreach($values as $key => $val) |
| 349 | { |
| 350 | $valstr[] = $key." = ".$val; |
| 351 | } |
| 352 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 353 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // -------------------------------------------------------------------- |
| 357 | |
| 358 | /** |
| 359 | * Delete statement |
| 360 | * |
| 361 | * Generates a platform-specific delete string from the supplied data |
| 362 | * |
| 363 | * @access public |
| 364 | * @param string the table name |
| 365 | * @param array the where clause |
| 366 | * @return string |
| 367 | */ |
| 368 | function _delete($table, $where) |
| 369 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 370 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 371 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 372 | |
| 373 | // -------------------------------------------------------------------- |
| 374 | |
| 375 | /** |
| 376 | * Limit string |
| 377 | * |
| 378 | * Generates a platform-specific LIMIT clause |
| 379 | * |
| 380 | * @access public |
| 381 | * @param string the sql query string |
| 382 | * @param integer the number of rows to limit the query to |
| 383 | * @param integer the offset value |
| 384 | * @return string |
| 385 | */ |
| 386 | function _limit($sql, $limit, $offset) |
| 387 | { |
| 388 | if ($offset == 0) |
| 389 | { |
| 390 | $offset = ''; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | $offset .= ", "; |
| 395 | } |
| 396 | |
| 397 | return $sql."LIMIT ".$offset.$limit; |
| 398 | } |
| 399 | |
| 400 | // -------------------------------------------------------------------- |
| 401 | |
| 402 | /** |
| 403 | * Close DB Connection |
| 404 | * |
| 405 | * @access public |
| 406 | * @param resource |
| 407 | * @return void |
| 408 | */ |
| 409 | function _close($conn_id) |
| 410 | { |
| 411 | mysql_close($conn_id); |
| 412 | } |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 413 | |
admin | 046000b | 2006-09-24 18:12:07 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | ?> |