blob: 87be7a54aca2eee3f4c6ab0d6783549572e2b3cf [file] [log] [blame]
Andrey Andreevd9038782012-01-26 12:38:49 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd9038782012-01-26 12:38:49 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd9038782012-01-26 12:38:49 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * SQLite Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_sqlite_driver extends CI_DB {
42
Andrey Andreevd9038782012-01-26 12:38:49 +020043 public $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // The character used to escape with - not needed for SQLite
Andrey Andreevd33e24c2012-02-12 03:23:07 +020046 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences
Andrey Andreev979b5282012-03-20 16:45:39 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreevd9038782012-01-26 12:38:49 +020050 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreevd9038782012-01-26 12:38:49 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000059
60 /**
61 * Non-persistent database connection
62 *
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreevd9038782012-01-26 12:38:49 +020065 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
67 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
68 {
69 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if ($this->db_debug)
72 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000073 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000074 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 return FALSE;
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 return $conn_id;
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 // --------------------------------------------------------------------
83
84 /**
85 * Persistent database connection
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Andrey Andreevd9038782012-01-26 12:38:49 +020089 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
91 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
92 {
93 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 if ($this->db_debug)
96 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000097 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 return FALSE;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 return $conn_id;
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200109 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @return string
112 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200113 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200115 return isset($this->data_cache['version'])
116 ? $this->data_cache['version']
117 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // --------------------------------------------------------------------
121
122 /**
123 * Execute the query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string an SQL query
126 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200128 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200130 return $this->is_write_type($sql)
131 ? @sqlite_exec($this->conn_id, $sql)
132 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // --------------------------------------------------------------------
136
137 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * Begin Transaction
139 *
Barry Mienydd671972010-10-04 16:33:58 +0200140 * @return bool
141 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200142 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200145 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
147 return TRUE;
148 }
149
150 // Reset the transaction failure flag.
151 // If the $test_mode flag is set to TRUE transactions will be rolled back
152 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200153 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154
155 $this->simple_query('BEGIN TRANSACTION');
156 return TRUE;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Commit Transaction
163 *
Barry Mienydd671972010-10-04 16:33:58 +0200164 * @return bool
165 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200166 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200169 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 return TRUE;
172 }
173
174 $this->simple_query('COMMIT');
175 return TRUE;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Rollback Transaction
182 *
Barry Mienydd671972010-10-04 16:33:58 +0200183 * @return bool
184 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200185 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200188 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 return TRUE;
191 }
192
193 $this->simple_query('ROLLBACK');
194 return TRUE;
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Escape String
201 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000203 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @return string
205 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200206 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000208 if (is_array($str))
209 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500210 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200211 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000212 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200213 }
214
215 return $str;
216 }
217
Derek Jonese4ed5832009-02-20 21:44:59 +0000218 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Jonese4ed5832009-02-20 21:44:59 +0000220 // escape LIKE condition wildcards
221 if ($like === TRUE)
222 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300223 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300224 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200225 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Jonese4ed5832009-02-20 21:44:59 +0000228 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // --------------------------------------------------------------------
232
233 /**
234 * Affected Rows
235 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200236 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200238 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
240 return sqlite_changes($this->conn_id);
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // --------------------------------------------------------------------
244
245 /**
246 * Insert ID
247 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200248 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200250 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 return @sqlite_last_insert_rowid($this->conn_id);
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 * List table query
259 *
260 * Generates a platform-specific query string so that the table names can be fetched
261 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200262 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @return string
264 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200265 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200267 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000268
Andrey Andreevd9038782012-01-26 12:38:49 +0200269 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200271 return $sql." AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 return $sql;
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Show column query
281 *
282 * Generates a platform-specific query string so that the column names can be fetched
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200285 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200287 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 // Not supported
290 return FALSE;
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Field data query
297 *
298 * Generates a platform-specific query so that the column data can be retrieved
299 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200301 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200303 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Andrey Andreev94115572012-06-07 21:34:56 +0300305 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307
308 // --------------------------------------------------------------------
309
310 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200311 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200313 * Returns an array containing code and message of the last
314 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200316 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200318 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200320 $error = array('code' => sqlite_last_error($this->conn_id));
321 $error['message'] = sqlite_error_string($error['code']);
322 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324
325 // --------------------------------------------------------------------
326
327 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300328 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300330 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @param string the table name
333 * @param array the insert keys
334 * @param array the insert values
335 * @return string
336 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300337 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200338 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300339 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
343
344 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 * Truncate statement
346 *
347 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300348 *
349 * If the database does not support the truncate() command,
350 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * @param string the table name
353 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200354 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200355 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300357 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
361
362 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * Close DB Connection
364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @return void
366 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300367 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300369 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372}
373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400375/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */