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