blob: 2744a63cf204ed8bee90e05b633a6dcddf9681bd [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 *
Barry Mienydd671972010-10-04 16:33:58 +0200130 * @return bool
131 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200132 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200135 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
137 return TRUE;
138 }
139
140 // Reset the transaction failure flag.
141 // If the $test_mode flag is set to TRUE transactions will be rolled back
142 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200143 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000144
145 $this->simple_query('BEGIN TRANSACTION');
146 return TRUE;
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
152 * Commit Transaction
153 *
Barry Mienydd671972010-10-04 16:33:58 +0200154 * @return bool
155 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200156 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200159 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
161 return TRUE;
162 }
163
164 $this->simple_query('COMMIT');
165 return TRUE;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Rollback Transaction
172 *
Barry Mienydd671972010-10-04 16:33:58 +0200173 * @return bool
174 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200175 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200178 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return TRUE;
181 }
182
183 $this->simple_query('ROLLBACK');
184 return TRUE;
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 // --------------------------------------------------------------------
188
189 /**
190 * Escape String
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000193 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * @return string
195 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200196 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000198 if (is_array($str))
199 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500200 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000202 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200203 }
204
205 return $str;
206 }
207
Derek Jonese4ed5832009-02-20 21:44:59 +0000208 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Jonese4ed5832009-02-20 21:44:59 +0000210 // escape LIKE condition wildcards
211 if ($like === TRUE)
212 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300213 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300214 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreevd9038782012-01-26 12:38:49 +0200215 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Jonese4ed5832009-02-20 21:44:59 +0000218 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // --------------------------------------------------------------------
222
223 /**
224 * Affected Rows
225 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200226 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200228 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return sqlite_changes($this->conn_id);
231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // --------------------------------------------------------------------
234
235 /**
236 * Insert ID
237 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200238 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200240 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 return @sqlite_last_insert_rowid($this->conn_id);
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * List table query
249 *
250 * Generates a platform-specific query string so that the table names can be fetched
251 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200252 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @return string
254 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200255 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200257 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
Andrey Andreevd9038782012-01-26 12:38:49 +0200259 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200261 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 +0000262 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 return $sql;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Show column query
271 *
272 * Generates a platform-specific query string so that the column names can be fetched
273 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200275 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200277 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 // Not supported
280 return FALSE;
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Field data query
287 *
288 * Generates a platform-specific query so that the column data can be retrieved
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200291 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200293 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Andrey Andreev94115572012-06-07 21:34:56 +0300295 return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200301 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200303 * Returns an array containing code and message of the last
304 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200306 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200308 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200310 $error = array('code' => sqlite_last_error($this->conn_id));
311 $error['message'] = sqlite_error_string($error['code']);
312 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300318 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300320 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @param string the table name
323 * @param array the insert keys
324 * @param array the insert values
325 * @return string
326 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300327 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200328 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300329 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // --------------------------------------------------------------------
333
334 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * Truncate statement
336 *
337 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300338 *
339 * If the database does not support the truncate() command,
340 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @param string the table name
343 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200345 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300347 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * Close DB Connection
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return void
356 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300357 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300359 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362}
363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400365/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */