blob: dd9e1e8499c58d2c12a2e154314a41b0a5622b17 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 *
Andrey Andreev2e171022014-02-25 15:21:41 +020065 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020067 */
Andrey Andreev2e171022014-02-25 15:21:41 +020068 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Andrey Andreev2e171022014-02-25 15:21:41 +020070 $error = NULL;
71 $conn_id = ($persistent === TRUE)
72 ? sqlite_popen($this->database, 0666, $error)
73 : sqlite_open($this->database, 0666, $error);
Barry Mienydd671972010-10-04 16:33:58 +020074
Andrey Andreev2e171022014-02-25 15:21:41 +020075 isset($error) && log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 return $conn_id;
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 // --------------------------------------------------------------------
81
82 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020083 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +000084 *
Derek Allard2067d1a2008-11-13 22:59:24 +000085 * @return string
86 */
Andrey Andreev08856b82012-03-03 03:19:28 +020087 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreev08856b82012-03-03 03:19:28 +020089 return isset($this->data_cache['version'])
90 ? $this->data_cache['version']
91 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +000092 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
97 * Execute the query
98 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +020099 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200101 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200102 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200104 return $this->is_write_type($sql)
105 ? @sqlite_exec($this->conn_id, $sql)
106 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
110
111 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * Begin Transaction
113 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200114 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200115 * @return bool
116 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200117 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200120 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
122 return TRUE;
123 }
124
125 // Reset the transaction failure flag.
126 // If the $test_mode flag is set to TRUE transactions will be rolled back
127 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200128 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129
130 $this->simple_query('BEGIN TRANSACTION');
131 return TRUE;
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Commit Transaction
138 *
Barry Mienydd671972010-10-04 16:33:58 +0200139 * @return bool
140 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200141 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200144 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return TRUE;
147 }
148
149 $this->simple_query('COMMIT');
150 return TRUE;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Rollback Transaction
157 *
Barry Mienydd671972010-10-04 16:33:58 +0200158 * @return bool
159 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200160 public function trans_rollback()
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('ROLLBACK');
169 return TRUE;
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // --------------------------------------------------------------------
173
174 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200175 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200177 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return string
179 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200180 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200182 return sqlite_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // --------------------------------------------------------------------
186
187 /**
188 * Affected Rows
189 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200190 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200192 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
194 return sqlite_changes($this->conn_id);
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
200 * Insert ID
201 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200202 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200204 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return @sqlite_last_insert_rowid($this->conn_id);
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * List table query
213 *
214 * Generates a platform-specific query string so that the table names can be fetched
215 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200216 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @return string
218 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200219 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200221 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
Andrey Andreevd9038782012-01-26 12:38:49 +0200223 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200225 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 +0000226 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 return $sql;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Show column query
235 *
236 * Generates a platform-specific query string so that the column names can be fetched
237 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200238 * @param string $table
Andrey Andreevd9038782012-01-26 12:38:49 +0200239 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200241 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 // Not supported
244 return FALSE;
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
Andrey Andreev822e74e2012-11-16 02:33:30 +0200250 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200252 * @param string $table
Andrey Andreev822e74e2012-11-16 02:33:30 +0200253 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 */
Andrey Andreev822e74e2012-11-16 02:33:30 +0200255 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreev822e74e2012-11-16 02:33:30 +0200257 if ($table === '')
258 {
259 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
260 }
261
262 if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
263 {
264 return FALSE;
265 }
266
267 $query = $query->result_array();
268 if (empty($query))
269 {
270 return FALSE;
271 }
272
273 $retval = array();
274 for ($i = 0, $c = count($query); $i < $c; $i++)
275 {
276 $retval[$i] = new stdClass();
277 $retval[$i]->name = $query[$i]['name'];
278 $retval[$i]->type = $query[$i]['type'];
279 $retval[$i]->max_length = NULL;
280 $retval[$i]->default = $query[$i]['dflt_value'];
281 $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
282 }
283
284 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200290 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200292 * Returns an array containing code and message of the last
293 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200295 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200297 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200299 $error = array('code' => sqlite_last_error($this->conn_id));
300 $error['message'] = sqlite_error_string($error['code']);
301 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303
304 // --------------------------------------------------------------------
305
306 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300307 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300309 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200311 * @param string $table Table name
312 * @param array $keys INSERT keys
313 * @param array $values INSERT values
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 * @return string
315 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300316 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200317 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300318 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // --------------------------------------------------------------------
322
323 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * Truncate statement
325 *
326 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300327 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200328 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300329 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200331 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200334 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300336 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * Close DB Connection
343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @return void
345 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300346 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300348 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351}
352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400354/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */