blob: 19824dbbfa3d0b9eff13c1fadf29ab165e9681bb [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
Andrey Andreevd9038782012-01-26 12:38:49 +020052 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000053
54 /**
55 * Non-persistent database connection
56 *
Derek Allard2067d1a2008-11-13 22:59:24 +000057 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020058 */
Andrey Andreevd9038782012-01-26 12:38:49 +020059 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
61 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
62 {
63 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 if ($this->db_debug)
66 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000067 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000068 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 return FALSE;
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 return $conn_id;
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 // --------------------------------------------------------------------
77
78 /**
79 * Persistent database connection
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Andrey Andreevd9038782012-01-26 12:38:49 +020083 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000084 {
85 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
86 {
87 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if ($this->db_debug)
90 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000091 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 return FALSE;
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 return $conn_id;
98 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 // --------------------------------------------------------------------
101
102 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200103 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @return string
106 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200107 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200109 return isset($this->data_cache['version'])
110 ? $this->data_cache['version']
111 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // --------------------------------------------------------------------
115
116 /**
117 * Execute the query
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @param string an SQL query
120 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200121 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200122 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200124 return $this->is_write_type($sql)
125 ? @sqlite_exec($this->conn_id, $sql)
126 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 // --------------------------------------------------------------------
130
131 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * Begin Transaction
133 *
Barry Mienydd671972010-10-04 16:33:58 +0200134 * @return bool
135 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200136 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200139 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
141 return TRUE;
142 }
143
144 // Reset the transaction failure flag.
145 // If the $test_mode flag is set to TRUE transactions will be rolled back
146 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200147 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148
149 $this->simple_query('BEGIN TRANSACTION');
150 return TRUE;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Commit Transaction
157 *
Barry Mienydd671972010-10-04 16:33:58 +0200158 * @return bool
159 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200160 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200163 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 return TRUE;
166 }
167
168 $this->simple_query('COMMIT');
169 return TRUE;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Rollback Transaction
176 *
Barry Mienydd671972010-10-04 16:33:58 +0200177 * @return bool
178 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200179 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200182 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 return TRUE;
185 }
186
187 $this->simple_query('ROLLBACK');
188 return TRUE;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // --------------------------------------------------------------------
192
193 /**
194 * Escape String
195 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000197 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @return string
199 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200200 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000202 if (is_array($str))
203 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500204 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200205 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000206 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200207 }
208
209 return $str;
210 }
211
Derek Jonese4ed5832009-02-20 21:44:59 +0000212 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonese4ed5832009-02-20 21:44:59 +0000214 // escape LIKE condition wildcards
215 if ($like === TRUE)
216 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300217 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300218 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200219 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Jonese4ed5832009-02-20 21:44:59 +0000222 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Affected Rows
229 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200230 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200232 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return sqlite_changes($this->conn_id);
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // --------------------------------------------------------------------
238
239 /**
240 * Insert ID
241 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200242 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200244 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 return @sqlite_last_insert_rowid($this->conn_id);
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * List table query
253 *
254 * Generates a platform-specific query string so that the table names can be fetched
255 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200256 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @return string
258 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200259 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200261 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000262
Andrey Andreevd9038782012-01-26 12:38:49 +0200263 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200265 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 +0000266 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 return $sql;
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Show column query
275 *
276 * Generates a platform-specific query string so that the column names can be fetched
277 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200279 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200281 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
283 // Not supported
284 return FALSE;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Field data query
291 *
292 * Generates a platform-specific query so that the column data can be retrieved
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200295 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200297 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreev94115572012-06-07 21:34:56 +0300299 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 }
301
302 // --------------------------------------------------------------------
303
304 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200305 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200307 * Returns an array containing code and message of the last
308 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200310 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200312 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200314 $error = array('code' => sqlite_last_error($this->conn_id));
315 $error['message'] = sqlite_error_string($error['code']);
316 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318
319 // --------------------------------------------------------------------
320
321 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300322 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300324 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 * @param string the table name
327 * @param array the insert keys
328 * @param array the insert values
329 * @return string
330 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300331 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200332 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300333 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * Truncate statement
340 *
341 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300342 *
343 * If the database does not support the truncate() command,
344 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @param string the table name
347 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200348 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200349 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300351 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // --------------------------------------------------------------------
355
356 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * Close DB Connection
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @return void
360 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300361 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300363 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366}
367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400369/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */