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 return (int) $row->numrows;
360 // --------------------------------------------------------------------
365 * Generates a platform-specific query string so that the table names can be fetched
371 function _list_tables($prefix_limit = FALSE)
373 $sql = "SELECT name from sqlite_master WHERE type='table'";
375 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
377 $sql .= " AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
382 // --------------------------------------------------------------------
387 * Generates a platform-specific query string so that the column names can be fetched
390 * @param string the table name
393 function _list_columns($table = '')
399 // --------------------------------------------------------------------
404 * Generates a platform-specific query so that the column data can be retrieved
407 * @param string the table name
410 function _field_data($table)
412 return "SELECT * FROM ".$table." LIMIT 1";
415 // --------------------------------------------------------------------
418 * The error message string
423 function _error_message()
425 return sqlite_error_string(sqlite_last_error($this->conn_id));
428 // --------------------------------------------------------------------
431 * The error message number
436 function _error_number()
438 return sqlite_last_error($this->conn_id);
441 // --------------------------------------------------------------------
444 * Escape the SQL Identifiers
446 * This function escapes column and table names
452 function _escape_identifiers($item)
454 if ($this->_escape_char == '')
459 foreach ($this->_reserved_identifiers as $id)
461 if (strpos($item, '.'.$id) !== FALSE)
463 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
465 // remove duplicates if the user already included the escape
466 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
470 if (strpos($item, '.') !== FALSE)
472 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
476 $str = $this->_escape_char.$item.$this->_escape_char;
479 // remove duplicates if the user already included the escape
480 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
483 // --------------------------------------------------------------------
488 * This function implicitly groups FROM tables so there is no confusion
489 * about operator precedence in harmony with SQL standards
495 function _from_tables($tables)
497 if ( ! is_array($tables))
499 $tables = array($tables);
502 return '('.implode(', ', $tables).')';
505 // --------------------------------------------------------------------
510 * Generates a platform-specific insert string from the supplied data
513 * @param string the table name
514 * @param array the insert keys
515 * @param array the insert values
518 function _insert($table, $keys, $values)
520 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
523 // --------------------------------------------------------------------
528 * Generates a platform-specific update string from the supplied data
531 * @param string the table name
532 * @param array the update data
533 * @param array the where clause
534 * @param array the orderby clause
535 * @param array the limit clause
538 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
540 foreach ($values as $key => $val)
542 $valstr[] = $key." = ".$val;
545 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
547 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
549 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
551 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
553 $sql .= $orderby.$limit;
559 // --------------------------------------------------------------------
564 * Generates a platform-specific truncate string from the supplied data
565 * If the database does not support the truncate() command
566 * This function maps to "DELETE FROM table"
569 * @param string the table name
572 function _truncate($table)
574 return $this->_delete($table);
577 // --------------------------------------------------------------------
582 * Generates a platform-specific delete string from the supplied data
585 * @param string the table name
586 * @param array the where clause
587 * @param string the limit clause
590 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
594 if (count($where) > 0 OR count($like) > 0)
596 $conditions = "\nWHERE ";
597 $conditions .= implode("\n", $this->ar_where);
599 if (count($where) > 0 && count($like) > 0)
601 $conditions .= " AND ";
603 $conditions .= implode("\n", $like);
606 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
608 return "DELETE FROM ".$table.$conditions.$limit;
611 // --------------------------------------------------------------------
616 * Generates a platform-specific LIMIT clause
619 * @param string the sql query string
620 * @param integer the number of rows to limit the query to
621 * @param integer the offset value
624 function _limit($sql, $limit, $offset)
635 return $sql."LIMIT ".$offset.$limit;
638 // --------------------------------------------------------------------
641 * Close DB Connection
647 function _close($conn_id)
649 @sqlite_close($conn_id);
656 /* End of file sqlite_driver.php */
657 /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */