blob: 6a3397f6f005a4a03c0b15d5ee2725357e5b4ba6 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200203 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200205 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @return string
207 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200208 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200210 return sqlite_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // --------------------------------------------------------------------
214
215 /**
216 * Affected Rows
217 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200218 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200220 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 return sqlite_changes($this->conn_id);
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // --------------------------------------------------------------------
226
227 /**
228 * Insert ID
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 insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return @sqlite_last_insert_rowid($this->conn_id);
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 * List table query
241 *
242 * Generates a platform-specific query string so that the table names can be fetched
243 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200244 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 * @return string
246 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200247 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200249 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
Andrey Andreevd9038782012-01-26 12:38:49 +0200251 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200253 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 +0000254 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 return $sql;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Show column query
263 *
264 * Generates a platform-specific query string so that the column names can be fetched
265 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200266 * @param string $table
Andrey Andreevd9038782012-01-26 12:38:49 +0200267 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200269 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 // Not supported
272 return FALSE;
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
Andrey Andreev822e74e2012-11-16 02:33:30 +0200278 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200280 * @param string $table
Andrey Andreev822e74e2012-11-16 02:33:30 +0200281 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 */
Andrey Andreev822e74e2012-11-16 02:33:30 +0200283 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
Andrey Andreev822e74e2012-11-16 02:33:30 +0200285 if ($table === '')
286 {
287 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
288 }
289
290 if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
291 {
292 return FALSE;
293 }
294
295 $query = $query->result_array();
296 if (empty($query))
297 {
298 return FALSE;
299 }
300
301 $retval = array();
302 for ($i = 0, $c = count($query); $i < $c; $i++)
303 {
304 $retval[$i] = new stdClass();
305 $retval[$i]->name = $query[$i]['name'];
306 $retval[$i]->type = $query[$i]['type'];
307 $retval[$i]->max_length = NULL;
308 $retval[$i]->default = $query[$i]['dflt_value'];
309 $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
310 }
311
312 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314
315 // --------------------------------------------------------------------
316
317 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200318 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200320 * Returns an array containing code and message of the last
321 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200323 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200325 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200327 $error = array('code' => sqlite_last_error($this->conn_id));
328 $error['message'] = sqlite_error_string($error['code']);
329 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300335 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300337 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200339 * @param string $table Table name
340 * @param array $keys INSERT keys
341 * @param array $values INSERT values
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * @return string
343 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300344 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200345 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300346 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
350
351 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * Truncate statement
353 *
354 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300355 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200356 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300357 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200359 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200361 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200362 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300364 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
368
369 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * Close DB Connection
371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @return void
373 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300374 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300376 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379}
380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400382/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */