1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
21
* SQLite Database Adapter Class
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.
27
* @package CodeIgniter
30
* @author ExpressionEngine Dev Team
31
* @link http://codeigniter.com/user_guide/database/
33
class CI_DB_sqlite_driver extends CI_DB {
35
var $dbdriver = 'sqlite';
37
// The character used to escape with - not needed for SQLite
38
var $_escape_char = '';
40
// clause and character used for LIKE escape sequences
41
var $_like_escape_str = " ESCAPE '%s' ";
42
var $_like_escape_chr = '!';
45
* The syntax to count rows is slightly different across different
46
* database engines, so this string appears in each driver and is
47
* used for the count_all() and count_all_results() functions.
49
var $_count_string = "SELECT COUNT(*) AS ";
50
var $_random_keyword = ' Random()'; // database specific random keyword
53
* Non-persistent database connection
55
* @access private called by the base class
60
if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
62
log_message('error', $error);
66
$this->display_error($error, '', TRUE);
75
// --------------------------------------------------------------------
78
* Persistent database connection
80
* @access private called by the base class
83
function db_pconnect()
85
if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
87
log_message('error', $error);
91
$this->display_error($error, '', TRUE);
100
// --------------------------------------------------------------------
105
* Keep / reestablish the db connection if no queries have been
106
* sent for a length of time exceeding the server's idle timeout
113
// not implemented in SQLite
116
// --------------------------------------------------------------------
119
* Select the database
121
* @access private called by the base class
129
// --------------------------------------------------------------------
132
* Set client character set
139
function db_set_charset($charset, $collation)
141
// @todo - add support if needed
145
// --------------------------------------------------------------------
148
* Version number query string
155
return sqlite_libversion();
158
// --------------------------------------------------------------------
163
* @access private called by the base class
164
* @param string an SQL query
167
function _execute($sql)
169
$sql = $this->_prep_query($sql);
170
return @sqlite_query($this->conn_id, $sql);
173
// --------------------------------------------------------------------
178
* If needed, each database adapter can prep the query string
180
* @access private called by execute()
181
* @param string an SQL query
184
function _prep_query($sql)
189
// --------------------------------------------------------------------
197
function trans_begin($test_mode = FALSE)
199
if ( ! $this->trans_enabled)
204
// When transactions are nested we only begin/commit/rollback the outermost ones
205
if ($this->_trans_depth > 0)
210
// Reset the transaction failure flag.
211
// If the $test_mode flag is set to TRUE transactions will be rolled back
212
// even if the queries produce a successful result.
213
$this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
215
$this->simple_query('BEGIN TRANSACTION');
219
// --------------------------------------------------------------------
227
function trans_commit()
229
if ( ! $this->trans_enabled)
234
// When transactions are nested we only begin/commit/rollback the outermost ones
235
if ($this->_trans_depth > 0)
240
$this->simple_query('COMMIT');
244
// --------------------------------------------------------------------
247
* Rollback Transaction
252
function trans_rollback()
254
if ( ! $this->trans_enabled)
259
// When transactions are nested we only begin/commit/rollback the outermost ones
260
if ($this->_trans_depth > 0)
265
$this->simple_query('ROLLBACK');
269
// --------------------------------------------------------------------
276
* @param bool whether or not the string will be used in a LIKE condition
279
function escape_str($str, $like = FALSE)
283
foreach ($str as $key => $val)
285
$str[$key] = $this->escape_str($val, $like);
291
$str = sqlite_escape_string($str);
293
// escape LIKE condition wildcards
296
$str = str_replace( array('%', '_', $this->_like_escape_chr),
297
array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
304
// --------------------------------------------------------------------
312
function affected_rows()
314
return sqlite_changes($this->conn_id);
317
// --------------------------------------------------------------------
327
return @sqlite_last_insert_rowid($this->conn_id);
330
// --------------------------------------------------------------------
335
* Generates a platform-specific query string that counts all records in
336
* the specified database
342
function count_all($table = '')
349
$query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
351
if ($query->num_rows() == 0)
356
$row = $query->row();
357
$this->_reset_select();
358
return (int) $row->numrows;
361
// --------------------------------------------------------------------
366
* Generates a platform-specific query string so that the table names can be fetched
372
function _list_tables($prefix_limit = FALSE)
374
$sql = "SELECT name from sqlite_master WHERE type='table'";
376
if ($prefix_limit !== FALSE AND $this->dbprefix != '')
378
$sql .= " AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
383
// --------------------------------------------------------------------
388
* Generates a platform-specific query string so that the column names can be fetched
391
* @param string the table name
394
function _list_columns($table = '')
400
// --------------------------------------------------------------------
405
* Generates a platform-specific query so that the column data can be retrieved
408
* @param string the table name
411
function _field_data($table)
413
return "SELECT * FROM ".$table." LIMIT 1";
416
// --------------------------------------------------------------------
419
* The error message string
424
function _error_message()
426
return sqlite_error_string(sqlite_last_error($this->conn_id));
429
// --------------------------------------------------------------------
432
* The error message number
437
function _error_number()
439
return sqlite_last_error($this->conn_id);
442
// --------------------------------------------------------------------
445
* Escape the SQL Identifiers
447
* This function escapes column and table names
453
function _escape_identifiers($item)
455
if ($this->_escape_char == '')
460
foreach ($this->_reserved_identifiers as $id)
462
if (strpos($item, '.'.$id) !== FALSE)
464
$str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
466
// remove duplicates if the user already included the escape
467
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
471
if (strpos($item, '.') !== FALSE)
473
$str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
477
$str = $this->_escape_char.$item.$this->_escape_char;
480
// remove duplicates if the user already included the escape
481
return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
484
// --------------------------------------------------------------------
489
* This function implicitly groups FROM tables so there is no confusion
490
* about operator precedence in harmony with SQL standards
496
function _from_tables($tables)
498
if ( ! is_array($tables))
500
$tables = array($tables);
503
return '('.implode(', ', $tables).')';
506
// --------------------------------------------------------------------
511
* Generates a platform-specific insert string from the supplied data
514
* @param string the table name
515
* @param array the insert keys
516
* @param array the insert values
519
function _insert($table, $keys, $values)
521
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
524
// --------------------------------------------------------------------
529
* Generates a platform-specific update string from the supplied data
532
* @param string the table name
533
* @param array the update data
534
* @param array the where clause
535
* @param array the orderby clause
536
* @param array the limit clause
539
function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
541
foreach ($values as $key => $val)
543
$valstr[] = $key." = ".$val;
546
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
548
$orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
550
$sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
552
$sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
554
$sql .= $orderby.$limit;
560
// --------------------------------------------------------------------
565
* Generates a platform-specific truncate string from the supplied data
566
* If the database does not support the truncate() command
567
* This function maps to "DELETE FROM table"
570
* @param string the table name
573
function _truncate($table)
575
return $this->_delete($table);
578
// --------------------------------------------------------------------
583
* Generates a platform-specific delete string from the supplied data
586
* @param string the table name
587
* @param array the where clause
588
* @param string the limit clause
591
function _delete($table, $where = array(), $like = array(), $limit = FALSE)
595
if (count($where) > 0 OR count($like) > 0)
597
$conditions = "\nWHERE ";
598
$conditions .= implode("\n", $this->ar_where);
600
if (count($where) > 0 && count($like) > 0)
602
$conditions .= " AND ";
604
$conditions .= implode("\n", $like);
607
$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
609
return "DELETE FROM ".$table.$conditions.$limit;
612
// --------------------------------------------------------------------
617
* Generates a platform-specific LIMIT clause
620
* @param string the sql query string
621
* @param integer the number of rows to limit the query to
622
* @param integer the offset value
625
function _limit($sql, $limit, $offset)
636
return $sql."LIMIT ".$offset.$limit;
639
// --------------------------------------------------------------------
642
* Close DB Connection
648
function _close($conn_id)
650
@sqlite_close($conn_id);
657
/* End of file sqlite_driver.php */
658
/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */
b'\\ No newline at end of file'