blob: 23f287b7ed592bb8499378f850e8b94275e1b188 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * SQLite Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_sqlite_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreevd9038782012-01-26 12:38:49 +020049 public $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000052
Andrey Andreeva24e52e2012-11-02 03:54:12 +020053 /**
54 * ORDER BY random keyword
55 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020056 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020057 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020058 protected $_random_keyword = array('RANDOM()', 'RANDOM()');
Derek Allard2067d1a2008-11-13 22:59:24 +000059
Andrey Andreeva24e52e2012-11-02 03:54:12 +020060 // --------------------------------------------------------------------
61
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * Non-persistent database connection
64 *
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Andrey Andreevd9038782012-01-26 12:38:49 +020067 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
70 {
71 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 if ($this->db_debug)
74 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000075 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 return FALSE;
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 return $conn_id;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // --------------------------------------------------------------------
85
86 /**
87 * Persistent database connection
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020090 */
Andrey Andreevd9038782012-01-26 12:38:49 +020091 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
94 {
95 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 if ($this->db_debug)
98 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000099 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 return FALSE;
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 return $conn_id;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200111 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return string
114 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200115 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200117 return isset($this->data_cache['version'])
118 ? $this->data_cache['version']
119 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // --------------------------------------------------------------------
123
124 /**
125 * Execute the query
126 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200127 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200130 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200132 return $this->is_write_type($sql)
133 ? @sqlite_exec($this->conn_id, $sql)
134 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // --------------------------------------------------------------------
138
139 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * Begin Transaction
141 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200142 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200143 * @return bool
144 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200145 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200148 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 return TRUE;
151 }
152
153 // Reset the transaction failure flag.
154 // If the $test_mode flag is set to TRUE transactions will be rolled back
155 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200156 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000157
158 $this->simple_query('BEGIN TRANSACTION');
159 return TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Commit Transaction
166 *
Barry Mienydd671972010-10-04 16:33:58 +0200167 * @return bool
168 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200169 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200172 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 return TRUE;
175 }
176
177 $this->simple_query('COMMIT');
178 return TRUE;
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Rollback Transaction
185 *
Barry Mienydd671972010-10-04 16:33:58 +0200186 * @return bool
187 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200188 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200191 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 return TRUE;
194 }
195
196 $this->simple_query('ROLLBACK');
197 return TRUE;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // --------------------------------------------------------------------
201
202 /**
203 * Escape String
204 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200205 * @param string $str
206 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 * @return string
208 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200209 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000211 if (is_array($str))
212 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500213 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200214 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000215 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200216 }
217
218 return $str;
219 }
220
Derek Jonese4ed5832009-02-20 21:44:59 +0000221 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Jonese4ed5832009-02-20 21:44:59 +0000223 // escape LIKE condition wildcards
224 if ($like === TRUE)
225 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300226 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300227 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200228 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jonese4ed5832009-02-20 21:44:59 +0000231 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
235
236 /**
237 * Affected Rows
238 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200239 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200241 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 return sqlite_changes($this->conn_id);
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
247
248 /**
249 * Insert ID
250 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200251 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200253 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return @sqlite_last_insert_rowid($this->conn_id);
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * List table query
262 *
263 * Generates a platform-specific query string so that the table names can be fetched
264 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200265 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @return string
267 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200268 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200270 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
Andrey Andreevd9038782012-01-26 12:38:49 +0200272 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200274 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 +0000275 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 return $sql;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Show column query
284 *
285 * Generates a platform-specific query string so that the column names can be fetched
286 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200287 * @param string $table
Andrey Andreevd9038782012-01-26 12:38:49 +0200288 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200290 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 // Not supported
293 return FALSE;
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Field data query
300 *
301 * Generates a platform-specific query so that the column data can be retrieved
302 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200303 * @param string $table
Andrey Andreevd9038782012-01-26 12:38:49 +0200304 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200306 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
Andrey Andreev94115572012-06-07 21:34:56 +0300308 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200314 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200316 * Returns an array containing code and message of the last
317 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200319 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200321 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200323 $error = array('code' => sqlite_last_error($this->conn_id));
324 $error['message'] = sqlite_error_string($error['code']);
325 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300331 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300333 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335 * @param string $table Table name
336 * @param array $keys INSERT keys
337 * @param array $values INSERT values
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return string
339 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300340 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200341 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300342 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // --------------------------------------------------------------------
346
347 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 * Truncate statement
349 *
350 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300351 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200352 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300353 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200355 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200357 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200358 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300360 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
364
365 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * Close DB Connection
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @return void
369 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300370 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300372 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375}
376
Derek Allard2067d1a2008-11-13 22:59:24 +0000377/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400378/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */