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