blob: a06122984a0b45e94873a0d390c6dec694471f8b [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevd9038782012-01-26 12:38:49 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevd9038782012-01-26 12:38:49 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * SQLite Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
53class CI_DB_sqlite_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreevd9038782012-01-26 12:38:49 +020060 public $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020061
Andrey Andreeva24e52e2012-11-02 03:54:12 +020062 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000063
Andrey Andreeva24e52e2012-11-02 03:54:12 +020064 /**
65 * ORDER BY random keyword
66 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020067 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020068 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020069 protected $_random_keyword = array('RANDOM()', 'RANDOM()');
Derek Allard2067d1a2008-11-13 22:59:24 +000070
Andrey Andreeva24e52e2012-11-02 03:54:12 +020071 // --------------------------------------------------------------------
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073 /**
74 * Non-persistent database connection
75 *
Andrey Andreev2e171022014-02-25 15:21:41 +020076 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020078 */
Andrey Andreev2e171022014-02-25 15:21:41 +020079 public function db_connect($persistent = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Andrey Andreev2e171022014-02-25 15:21:41 +020081 $error = NULL;
82 $conn_id = ($persistent === TRUE)
83 ? sqlite_popen($this->database, 0666, $error)
84 : sqlite_open($this->database, 0666, $error);
Barry Mienydd671972010-10-04 16:33:58 +020085
Andrey Andreev2e171022014-02-25 15:21:41 +020086 isset($error) && log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 return $conn_id;
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020094 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +000095 *
Derek Allard2067d1a2008-11-13 22:59:24 +000096 * @return string
97 */
Andrey Andreev08856b82012-03-03 03:19:28 +020098 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200100 return isset($this->data_cache['version'])
101 ? $this->data_cache['version']
102 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Execute the query
109 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200110 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200112 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200113 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreeva0c27852012-03-02 13:49:28 +0200115 return $this->is_write_type($sql)
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300116 ? sqlite_exec($this->conn_id, $sql)
117 : sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // --------------------------------------------------------------------
121
122 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * Begin Transaction
124 *
Barry Mienydd671972010-10-04 16:33:58 +0200125 * @return bool
126 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300127 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300129 return $this->simple_query('BEGIN TRANSACTION');
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Commit Transaction
136 *
Barry Mienydd671972010-10-04 16:33:58 +0200137 * @return bool
138 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300139 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300141 return $this->simple_query('COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Rollback Transaction
148 *
Barry Mienydd671972010-10-04 16:33:58 +0200149 * @return bool
150 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300151 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300153 return $this->simple_query('ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // --------------------------------------------------------------------
157
158 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200159 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200161 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @return string
163 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200164 protected function _escape_str($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200166 return sqlite_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
170
171 /**
172 * Affected Rows
173 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200174 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200176 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return sqlite_changes($this->conn_id);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Insert ID
185 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200186 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200188 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300190 return sqlite_last_insert_rowid($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193 // --------------------------------------------------------------------
194
195 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 * List table query
197 *
198 * Generates a platform-specific query string so that the table names can be fetched
199 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200200 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @return string
202 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200203 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200205 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
Andrey Andreevd9038782012-01-26 12:38:49 +0200207 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200209 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 +0000210 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 return $sql;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Show column query
219 *
220 * Generates a platform-specific query string so that the column names can be fetched
221 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200222 * @param string $table
Andrey Andreevd9038782012-01-26 12:38:49 +0200223 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200225 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 // Not supported
228 return FALSE;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
Andrey Andreev822e74e2012-11-16 02:33:30 +0200234 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200236 * @param string $table
Andrey Andreev822e74e2012-11-16 02:33:30 +0200237 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200239 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Andrey Andreev822e74e2012-11-16 02:33:30 +0200241 if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
242 {
243 return FALSE;
244 }
245
246 $query = $query->result_array();
247 if (empty($query))
248 {
249 return FALSE;
250 }
251
252 $retval = array();
253 for ($i = 0, $c = count($query); $i < $c; $i++)
254 {
255 $retval[$i] = new stdClass();
256 $retval[$i]->name = $query[$i]['name'];
257 $retval[$i]->type = $query[$i]['type'];
258 $retval[$i]->max_length = NULL;
259 $retval[$i]->default = $query[$i]['dflt_value'];
260 $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
261 }
262
263 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200269 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200271 * Returns an array containing code and message of the last
272 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200274 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200276 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200278 $error = array('code' => sqlite_last_error($this->conn_id));
279 $error['message'] = sqlite_error_string($error['code']);
280 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282
283 // --------------------------------------------------------------------
284
285 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300286 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300288 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200290 * @param string $table Table name
291 * @param array $keys INSERT keys
292 * @param array $values INSERT values
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 * @return string
294 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300295 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300297 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * Truncate statement
304 *
305 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300306 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200307 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300308 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200310 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200312 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200313 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300315 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * Close DB Connection
322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return void
324 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300325 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300327 sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330}