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. |
| 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 | |
| 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 | { |
| 43 | if ( ! $conn_id = sqlite_open($this->database, 0666, $error)) |
| 44 | { |
| 45 | log_message('error', $error); |
| 46 | |
| 47 | if ($this->db_debug) |
| 48 | { |
| 49 | $this->display_error($error, '', TRUE); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return $conn_id; |
| 54 | } |
| 55 | |
| 56 | // -------------------------------------------------------------------- |
| 57 | |
| 58 | /** |
| 59 | * Persistent database connection |
| 60 | * |
| 61 | * @access private called by the base class |
| 62 | * @return resource |
| 63 | */ |
| 64 | function db_pconnect() |
| 65 | { |
| 66 | if ( ! $conn_id = sqlite_popen($this->database, 0666, $error)) |
| 67 | { |
| 68 | log_message('error', $error); |
| 69 | |
| 70 | if ($this->db_debug) |
| 71 | { |
| 72 | $this->display_error($error, '', TRUE); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $conn_id; |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * Select the database |
| 83 | * |
| 84 | * @access private called by the base class |
| 85 | * @return resource |
| 86 | */ |
| 87 | function db_select() |
| 88 | { |
| 89 | return TRUE; |
| 90 | } |
| 91 | |
| 92 | // -------------------------------------------------------------------- |
| 93 | |
| 94 | /** |
| 95 | * Execute the query |
| 96 | * |
| 97 | * @access private called by the base class |
| 98 | * @param string an SQL query |
| 99 | * @return resource |
| 100 | */ |
| 101 | function _execute($sql) |
| 102 | { |
| 103 | $sql = $this->_prep_query($sql); |
| 104 | return @sqlite_query($this->conn_id, $sql); |
| 105 | } |
| 106 | |
| 107 | // -------------------------------------------------------------------- |
| 108 | |
| 109 | /** |
| 110 | * Prep the query |
| 111 | * |
| 112 | * If needed, each database adapter can prep the query string |
| 113 | * |
| 114 | * @access private called by execute() |
| 115 | * @param string an SQL query |
| 116 | * @return string |
| 117 | */ |
| 118 | function _prep_query($sql) |
| 119 | { |
| 120 | return $sql; |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Begin Transaction |
| 127 | * |
| 128 | * @access public |
| 129 | * @return bool |
| 130 | */ |
| 131 | function trans_begin($test_mode = FALSE) |
| 132 | { |
| 133 | if ( ! $this->trans_enabled) |
| 134 | { |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 139 | if ($this->_trans_depth > 0) |
| 140 | { |
| 141 | return TRUE; |
| 142 | } |
| 143 | |
| 144 | // Reset the transaction failure flag. |
| 145 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 146 | // even if the queries produce a successful result. |
| 147 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 148 | |
| 149 | $this->simple_query('BEGIN TRANSACTION'); |
| 150 | return TRUE; |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
| 156 | * Commit Transaction |
| 157 | * |
| 158 | * @access public |
| 159 | * @return bool |
| 160 | */ |
| 161 | function trans_commit() |
| 162 | { |
| 163 | if ( ! $this->trans_enabled) |
| 164 | { |
| 165 | return TRUE; |
| 166 | } |
| 167 | |
| 168 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 169 | if ($this->_trans_depth > 0) |
| 170 | { |
| 171 | return TRUE; |
| 172 | } |
| 173 | |
| 174 | $this->simple_query('COMMIT'); |
| 175 | return TRUE; |
| 176 | } |
| 177 | |
| 178 | // -------------------------------------------------------------------- |
| 179 | |
| 180 | /** |
| 181 | * Rollback Transaction |
| 182 | * |
| 183 | * @access public |
| 184 | * @return bool |
| 185 | */ |
| 186 | function trans_rollback() |
| 187 | { |
| 188 | if ( ! $this->trans_enabled) |
| 189 | { |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 194 | if ($this->_trans_depth > 0) |
| 195 | { |
| 196 | return TRUE; |
| 197 | } |
| 198 | |
| 199 | $this->simple_query('ROLLBACK'); |
| 200 | return TRUE; |
| 201 | } |
| 202 | |
| 203 | // -------------------------------------------------------------------- |
| 204 | |
| 205 | /** |
| 206 | * Escape String |
| 207 | * |
| 208 | * @access public |
| 209 | * @param string |
| 210 | * @return string |
| 211 | */ |
| 212 | function escape_str($str) |
| 213 | { |
| 214 | return sqlite_escape_string($str); |
| 215 | } |
| 216 | |
| 217 | // -------------------------------------------------------------------- |
| 218 | |
| 219 | /** |
| 220 | * Affected Rows |
| 221 | * |
| 222 | * @access public |
| 223 | * @return integer |
| 224 | */ |
| 225 | function affected_rows() |
| 226 | { |
| 227 | return sqlite_changes($this->conn_id); |
| 228 | } |
| 229 | |
| 230 | // -------------------------------------------------------------------- |
| 231 | |
| 232 | /** |
| 233 | * Insert ID |
| 234 | * |
| 235 | * @access public |
| 236 | * @return integer |
| 237 | */ |
| 238 | function insert_id() |
| 239 | { |
| 240 | return @sqlite_last_insert_rowid($this->conn_id); |
| 241 | } |
| 242 | |
| 243 | // -------------------------------------------------------------------- |
| 244 | |
| 245 | /** |
| 246 | * "Count All" query |
| 247 | * |
| 248 | * Generates a platform-specific query string that counts all records in |
| 249 | * the specified database |
| 250 | * |
| 251 | * @access public |
| 252 | * @param string |
| 253 | * @return string |
| 254 | */ |
| 255 | function count_all($table = '') |
| 256 | { |
| 257 | if ($table == '') |
| 258 | return '0'; |
| 259 | |
| 260 | $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`"); |
| 261 | |
| 262 | if ($query->num_rows() == 0) |
| 263 | return '0'; |
| 264 | |
| 265 | $row = $query->row(); |
| 266 | return $row->numrows; |
| 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * The error message string |
| 273 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 274 | * @access private |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 275 | * @return string |
| 276 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 277 | function _error_message() |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 278 | { |
| 279 | return sqlite_error_string(sqlite_last_error($this->conn_id)); |
| 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
| 285 | * The error message number |
| 286 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 287 | * @access private |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 288 | * @return integer |
| 289 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 290 | function _error_number() |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 291 | { |
| 292 | return sqlite_last_error($this->conn_id); |
| 293 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 294 | |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 295 | // -------------------------------------------------------------------- |
| 296 | |
| 297 | /** |
| 298 | * Escape Table Name |
| 299 | * |
| 300 | * This function adds backticks if the table name has a period |
| 301 | * in it. Some DBs will get cranky unless periods are escaped |
| 302 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 303 | * @access private |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 304 | * @param string the table name |
| 305 | * @return string |
| 306 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 307 | function _escape_table($table) |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 308 | { |
| 309 | if (stristr($table, '.')) |
| 310 | { |
| 311 | $table = preg_replace("/\./", "`.`", $table); |
| 312 | } |
| 313 | |
| 314 | return $table; |
| 315 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 316 | |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
| 320 | * Insert statement |
| 321 | * |
| 322 | * Generates a platform-specific insert string from the supplied data |
| 323 | * |
| 324 | * @access public |
| 325 | * @param string the table name |
| 326 | * @param array the insert keys |
| 327 | * @param array the insert values |
| 328 | * @return string |
| 329 | */ |
| 330 | function _insert($table, $keys, $values) |
| 331 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 332 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------- |
| 336 | |
| 337 | /** |
| 338 | * Update statement |
| 339 | * |
| 340 | * Generates a platform-specific update string from the supplied data |
| 341 | * |
| 342 | * @access public |
| 343 | * @param string the table name |
| 344 | * @param array the update data |
| 345 | * @param array the where clause |
| 346 | * @return string |
| 347 | */ |
| 348 | function _update($table, $values, $where) |
| 349 | { |
| 350 | foreach($values as $key => $val) |
| 351 | { |
| 352 | $valstr[] = $key." = ".$val; |
| 353 | } |
| 354 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 355 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Delete statement |
| 362 | * |
| 363 | * Generates a platform-specific delete string from the supplied data |
| 364 | * |
| 365 | * @access public |
| 366 | * @param string the table name |
| 367 | * @param array the where clause |
| 368 | * @return string |
| 369 | */ |
| 370 | function _delete($table, $where) |
| 371 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 372 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 373 | } |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 374 | |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 375 | // -------------------------------------------------------------------- |
| 376 | |
| 377 | /** |
| 378 | * Limit string |
| 379 | * |
| 380 | * Generates a platform-specific LIMIT clause |
| 381 | * |
| 382 | * @access public |
| 383 | * @param string the sql query string |
| 384 | * @param integer the number of rows to limit the query to |
| 385 | * @param integer the offset value |
| 386 | * @return string |
| 387 | */ |
| 388 | function _limit($sql, $limit, $offset) |
| 389 | { |
| 390 | if ($offset == 0) |
| 391 | { |
| 392 | $offset = ''; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | $offset .= ", "; |
| 397 | } |
| 398 | |
| 399 | return $sql."LIMIT ".$offset.$limit; |
| 400 | } |
| 401 | |
| 402 | // -------------------------------------------------------------------- |
| 403 | |
| 404 | /** |
| 405 | * Close DB Connection |
| 406 | * |
| 407 | * @access public |
| 408 | * @param resource |
| 409 | * @return void |
| 410 | */ |
| 411 | function _close($conn_id) |
| 412 | { |
| 413 | sqlite_close($conn_id); |
| 414 | } |
| 415 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 416 | |
admin | 4653762 | 2006-09-24 18:13:49 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | ?> |