blob: 5e6746d4644c93858015a9570834d7d312d87245 [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 Andreevd9038782012-01-26 12:38:49 +020044 public $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 // The character used to escape with - not needed for SQLite
Andrey Andreevd33e24c2012-02-12 03:23:07 +020047 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Andrey Andreevd9038782012-01-26 12:38:49 +020049 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000050
51 /**
52 * Non-persistent database connection
53 *
Derek Allard2067d1a2008-11-13 22:59:24 +000054 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020055 */
Andrey Andreevd9038782012-01-26 12:38:49 +020056 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000057 {
58 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
59 {
60 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 if ($this->db_debug)
63 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000064 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000065 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 return FALSE;
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 return $conn_id;
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // --------------------------------------------------------------------
74
75 /**
76 * Persistent database connection
77 *
Derek Allard2067d1a2008-11-13 22:59:24 +000078 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020079 */
Andrey Andreevd9038782012-01-26 12:38:49 +020080 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
83 {
84 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 if ($this->db_debug)
87 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000088 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 return FALSE;
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 return $conn_id;
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 // --------------------------------------------------------------------
98
99 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200100 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @return string
103 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200104 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200106 return isset($this->data_cache['version'])
107 ? $this->data_cache['version']
108 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
112
113 /**
114 * Execute the query
115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @param string an SQL query
117 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200118 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200119 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200121 return $this->is_write_type($sql)
122 ? @sqlite_exec($this->conn_id, $sql)
123 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
127
128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Begin Transaction
130 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300131 * @param bool $test_mode = FALSE
Barry Mienydd671972010-10-04 16:33:58 +0200132 * @return bool
133 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200134 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200137 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 return TRUE;
140 }
141
142 // Reset the transaction failure flag.
143 // If the $test_mode flag is set to TRUE transactions will be rolled back
144 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200145 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000146
147 $this->simple_query('BEGIN TRANSACTION');
148 return TRUE;
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Commit Transaction
155 *
Barry Mienydd671972010-10-04 16:33:58 +0200156 * @return bool
157 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200158 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200161 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 return TRUE;
164 }
165
166 $this->simple_query('COMMIT');
167 return TRUE;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Rollback Transaction
174 *
Barry Mienydd671972010-10-04 16:33:58 +0200175 * @return bool
176 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200177 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200180 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 return TRUE;
183 }
184
185 $this->simple_query('ROLLBACK');
186 return TRUE;
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // --------------------------------------------------------------------
190
191 /**
192 * Escape String
193 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000195 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * @return string
197 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200198 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000200 if (is_array($str))
201 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500202 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000204 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200205 }
206
207 return $str;
208 }
209
Derek Jonese4ed5832009-02-20 21:44:59 +0000210 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Jonese4ed5832009-02-20 21:44:59 +0000212 // escape LIKE condition wildcards
213 if ($like === TRUE)
214 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300215 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300216 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200217 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Jonese4ed5832009-02-20 21:44:59 +0000220 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
224
225 /**
226 * Affected Rows
227 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200228 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200230 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 return sqlite_changes($this->conn_id);
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
236
237 /**
238 * Insert ID
239 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200240 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200242 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 return @sqlite_last_insert_rowid($this->conn_id);
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * List table query
251 *
252 * Generates a platform-specific query string so that the table names can be fetched
253 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200254 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @return string
256 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200257 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200259 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
Andrey Andreevd9038782012-01-26 12:38:49 +0200261 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200263 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 +0000264 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 return $sql;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Show column query
273 *
274 * Generates a platform-specific query string so that the column names can be fetched
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200277 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200279 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 // Not supported
282 return FALSE;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Field data query
289 *
290 * Generates a platform-specific query so that the column data can be retrieved
291 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200293 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200295 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Andrey Andreev94115572012-06-07 21:34:56 +0300297 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200303 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200305 * Returns an array containing code and message of the last
306 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200308 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200310 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200312 $error = array('code' => sqlite_last_error($this->conn_id));
313 $error['message'] = sqlite_error_string($error['code']);
314 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300320 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300322 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string the table name
325 * @param array the insert keys
326 * @param array the insert values
327 * @return string
328 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300329 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200330 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300331 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * Truncate statement
338 *
339 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300340 *
341 * If the database does not support the truncate() command,
342 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @param string the table name
345 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200346 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200347 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300349 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // --------------------------------------------------------------------
353
354 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * Close DB Connection
356 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * @return void
358 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300359 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300361 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364}
365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400367/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */