blob: 2fd39346f78ef7265716f14f673ee14fa84eb7af [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
Andrey Andreevd9038782012-01-26 12:38:49 +020048 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
51 * Non-persistent database connection
52 *
Derek Allard2067d1a2008-11-13 22:59:24 +000053 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Andrey Andreevd9038782012-01-26 12:38:49 +020055 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
57 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
58 {
59 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if ($this->db_debug)
62 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000063 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000064 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 return FALSE;
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 return $conn_id;
70 }
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 // --------------------------------------------------------------------
73
74 /**
75 * Persistent database connection
76 *
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020078 */
Andrey Andreevd9038782012-01-26 12:38:49 +020079 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
81 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
82 {
83 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 if ($this->db_debug)
86 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000087 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 return FALSE;
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Derek Allard2067d1a2008-11-13 22:59:24 +000093 return $conn_id;
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
97
98 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020099 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 * @return string
102 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200103 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200105 return isset($this->data_cache['version'])
106 ? $this->data_cache['version']
107 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // --------------------------------------------------------------------
111
112 /**
113 * Execute the query
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @param string an SQL query
116 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200118 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200120 return $this->is_write_type($sql)
121 ? @sqlite_exec($this->conn_id, $sql)
122 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
126
127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Begin Transaction
129 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300130 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200131 * @return bool
132 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200133 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200136 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
138 return TRUE;
139 }
140
141 // Reset the transaction failure flag.
142 // If the $test_mode flag is set to TRUE transactions will be rolled back
143 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200144 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000145
146 $this->simple_query('BEGIN TRANSACTION');
147 return TRUE;
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Commit Transaction
154 *
Barry Mienydd671972010-10-04 16:33:58 +0200155 * @return bool
156 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200157 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200160 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return TRUE;
163 }
164
165 $this->simple_query('COMMIT');
166 return TRUE;
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Rollback Transaction
173 *
Barry Mienydd671972010-10-04 16:33:58 +0200174 * @return bool
175 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200176 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200179 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
181 return TRUE;
182 }
183
184 $this->simple_query('ROLLBACK');
185 return TRUE;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // --------------------------------------------------------------------
189
190 /**
191 * Escape String
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000194 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * @return string
196 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200197 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000199 if (is_array($str))
200 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500201 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000203 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200204 }
205
206 return $str;
207 }
208
Derek Jonese4ed5832009-02-20 21:44:59 +0000209 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Jonese4ed5832009-02-20 21:44:59 +0000211 // escape LIKE condition wildcards
212 if ($like === TRUE)
213 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300214 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300215 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200216 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Jonese4ed5832009-02-20 21:44:59 +0000219 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Affected Rows
226 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200227 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200229 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
231 return sqlite_changes($this->conn_id);
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
235
236 /**
237 * Insert ID
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 insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 return @sqlite_last_insert_rowid($this->conn_id);
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * List table query
250 *
251 * Generates a platform-specific query string so that the table names can be fetched
252 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200253 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @return string
255 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200256 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200258 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
Andrey Andreevd9038782012-01-26 12:38:49 +0200260 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200262 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 +0000263 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 return $sql;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Show column query
272 *
273 * Generates a platform-specific query string so that the column names can be fetched
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200276 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200278 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 // Not supported
281 return FALSE;
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Field data query
288 *
289 * Generates a platform-specific query so that the column data can be retrieved
290 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200292 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200294 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Andrey Andreev94115572012-06-07 21:34:56 +0300296 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200302 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200304 * Returns an array containing code and message of the last
305 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200307 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200309 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200311 $error = array('code' => sqlite_last_error($this->conn_id));
312 $error['message'] = sqlite_error_string($error['code']);
313 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300319 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300321 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @param string the table name
324 * @param array the insert keys
325 * @param array the insert values
326 * @return string
327 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300328 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300330 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // --------------------------------------------------------------------
334
335 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 * Truncate statement
337 *
338 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300339 *
340 * If the database does not support the truncate() command,
341 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @param string the table name
344 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200346 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300348 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // --------------------------------------------------------------------
352
353 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * Close DB Connection
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return void
357 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300358 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300360 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363}
364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400366/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */