1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, 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 = '';
41 * The syntax to count rows is slightly different across different
42 * database engines, so this string appears in each driver and is
43 * used for the count_all() and count_all_results() functions.
45 var $_count_string = "SELECT COUNT(*) AS ";
46 var $_random_keyword = ' Random()'; // database specific random keyword
49 * Non-persistent database connection
51 * @access private called by the base class
56 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
58 log_message('error', $error);
62 $this->display_error($error, '', TRUE);
71 // --------------------------------------------------------------------
74 * Persistent database connection
76 * @access private called by the base class
79 function db_pconnect()
81 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
83 log_message('error', $error);
87 $this->display_error($error, '', TRUE);
96 // --------------------------------------------------------------------
101 * @access private called by the base class
109 // --------------------------------------------------------------------
112 * Set client character set
119 function db_set_charset($charset, $collation)
121 // @todo - add support if needed
125 // --------------------------------------------------------------------
128 * Version number query string
135 return sqlite_libversion();
138 // --------------------------------------------------------------------
143 * @access private called by the base class
144 * @param string an SQL query
147 function _execute($sql)
149 $sql = $this->_prep_query($sql);
150 return @sqlite_query($this->conn_id, $sql);
153 // --------------------------------------------------------------------
158 * If needed, each database adapter can prep the query string
160 * @access private called by execute()
161 * @param string an SQL query
164 function _prep_query($sql)
169 // --------------------------------------------------------------------
177 function trans_begin($test_mode = FALSE)
179 if ( ! $this->trans_enabled)
184 // When transactions are nested we only begin/commit/rollback the outermost ones
185 if ($this->_trans_depth > 0)
190 // Reset the transaction failure flag.
191 // If the $test_mode flag is set to TRUE transactions will be rolled back
192 // even if the queries produce a successful result.
193 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
195 $this->simple_query('BEGIN TRANSACTION');
199 // --------------------------------------------------------------------
207 function trans_commit()
209 if ( ! $this->trans_enabled)
214 // When transactions are nested we only begin/commit/rollback the outermost ones
215 if ($this->_trans_depth > 0)
220 $this->simple_query('COMMIT');
224 // --------------------------------------------------------------------
227 * Rollback Transaction
232 function trans_rollback()
234 if ( ! $this->trans_enabled)
239 // When transactions are nested we only begin/commit/rollback the outermost ones
240 if ($this->_trans_depth > 0)
245 $this->simple_query('ROLLBACK');
249 // --------------------------------------------------------------------
258 function escape_str($str)
260 return sqlite_escape_string($str);
263 // --------------------------------------------------------------------
271 function affected_rows()
273 return sqlite_changes($this->conn_id);
276 // --------------------------------------------------------------------
286 return @sqlite_last_insert_rowid($this->conn_id);
289 // --------------------------------------------------------------------
294 * Generates a platform-specific query string that counts all records in
295 * the specified database
301 function count_all($table = '')
306 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
308 if ($query->num_rows() == 0)
311 $row = $query->row();
312 return $row->numrows;
315 // --------------------------------------------------------------------
320 * Generates a platform-specific query string so that the table names can be fetched
326 function _list_tables($prefix_limit = FALSE)
328 $sql = "SELECT name from sqlite_master WHERE type='table'";
330 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
332 $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
337 // --------------------------------------------------------------------
342 * Generates a platform-specific query string so that the column names can be fetched
345 * @param string the table name
348 function _list_columns($table = '')
354 // --------------------------------------------------------------------
359 * Generates a platform-specific query so that the column data can be retrieved
362 * @param string the table name
365 function _field_data($table)
367 return "SELECT * FROM ".$table." LIMIT 1";
370 // --------------------------------------------------------------------
373 * The error message string
378 function _error_message()
380 return sqlite_error_string(sqlite_last_error($this->conn_id));
383 // --------------------------------------------------------------------
386 * The error message number
391 function _error_number()
393 return sqlite_last_error($this->conn_id);
396 // --------------------------------------------------------------------
399 * Escape the SQL Identifiers
401 * This function escapes column and table names
407 function _escape_identifiers($item)
409 if ($this->_escape_char == '')
414 if (strpos($item, '.') !== FALSE)
416 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
420 $str = $this->_escape_char.$item.$this->_escape_char;
423 // remove duplicates if the user already included the escape
424 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
427 // --------------------------------------------------------------------
432 * This function implicitly groups FROM tables so there is no confusion
433 * about operator precedence in harmony with SQL standards
439 function _from_tables($tables)
441 if ( ! is_array($tables))
443 $tables = array($tables);
446 return '('.implode(', ', $tables).')';
449 // --------------------------------------------------------------------
454 * Generates a platform-specific insert string from the supplied data
457 * @param string the table name
458 * @param array the insert keys
459 * @param array the insert values
462 function _insert($table, $keys, $values)
464 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
467 // --------------------------------------------------------------------
472 * Generates a platform-specific update string from the supplied data
475 * @param string the table name
476 * @param array the update data
477 * @param array the where clause
478 * @param array the orderby clause
479 * @param array the limit clause
482 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
484 foreach($values as $key => $val)
486 $valstr[] = $key." = ".$val;
489 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
491 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
493 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
495 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
497 $sql .= $orderby.$limit;
503 // --------------------------------------------------------------------
508 * Generates a platform-specific truncate string from the supplied data
509 * If the database does not support the truncate() command
510 * This function maps to "DELETE FROM table"
513 * @param string the table name
516 function _truncate($table)
518 return $this->_delete($table);
521 // --------------------------------------------------------------------
526 * Generates a platform-specific delete string from the supplied data
529 * @param string the table name
530 * @param array the where clause
531 * @param string the limit clause
534 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
538 if (count($where) > 0 OR count($like) > 0)
540 $conditions = "\nWHERE ";
541 $conditions .= implode("\n", $this->ar_where);
543 if (count($where) > 0 && count($like) > 0)
545 $conditions .= " AND ";
547 $conditions .= implode("\n", $like);
550 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
552 return "DELETE FROM ".$table.$conditions.$limit;
555 // --------------------------------------------------------------------
560 * Generates a platform-specific LIMIT clause
563 * @param string the sql query string
564 * @param integer the number of rows to limit the query to
565 * @param integer the offset value
568 function _limit($sql, $limit, $offset)
579 return $sql."LIMIT ".$offset.$limit;
582 // --------------------------------------------------------------------
585 * Close DB Connection
591 function _close($conn_id)
593 @sqlite_close($conn_id);
600 /* End of file sqlite_driver.php */
601 /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */