blob: d8b869c2eb8d43c0d52695a52805a95b8c351037 [file] [log] [blame]
Andrey Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +020046 protected $_escape_char = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Derek Jonese4ed5832009-02-20 21:44:59 +000048 // clause and character used for LIKE escape sequences
Andrey Andreev979b5282012-03-20 16:45:39 +020049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev979b5282012-03-20 16:45:39 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000059
60 /**
61 * Non-persistent database connection
62 *
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev979b5282012-03-20 16:45:39 +020065 public function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
67 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
68 {
69 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if ($this->db_debug)
72 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000073 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000074 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 return FALSE;
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 return $conn_id;
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 // --------------------------------------------------------------------
83
84 /**
85 * Persistent database connection
86 *
Derek Allard2067d1a2008-11-13 22:59:24 +000087 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Andrey Andreev979b5282012-03-20 16:45:39 +020089 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
91 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
92 {
93 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 if ($this->db_debug)
96 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000097 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000098 }
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 return FALSE;
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 return $conn_id;
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200109 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @return string
112 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200113 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200115 return isset($this->data_cache['version'])
116 ? $this->data_cache['version']
117 : $this->data_cache['version'] = sqlite_libversion();
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 /**
123 * Execute the query
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @param string an SQL query
126 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200128 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return @sqlite_query($this->conn_id, $sql);
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * Begin Transaction
137 *
Barry Mienydd671972010-10-04 16:33:58 +0200138 * @return bool
139 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200140 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
142 if ( ! $this->trans_enabled)
143 {
144 return TRUE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // When transactions are nested we only begin/commit/rollback the outermost ones
148 if ($this->_trans_depth > 0)
149 {
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.
156 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
157
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 Andreev979b5282012-03-20 16:45:39 +0200169 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if ( ! $this->trans_enabled)
172 {
173 return TRUE;
174 }
175
176 // When transactions are nested we only begin/commit/rollback the outermost ones
177 if ($this->_trans_depth > 0)
178 {
179 return TRUE;
180 }
181
182 $this->simple_query('COMMIT');
183 return TRUE;
184 }
185
186 // --------------------------------------------------------------------
187
188 /**
189 * Rollback Transaction
190 *
Barry Mienydd671972010-10-04 16:33:58 +0200191 * @return bool
192 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200193 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 if ( ! $this->trans_enabled)
196 {
197 return TRUE;
198 }
199
200 // When transactions are nested we only begin/commit/rollback the outermost ones
201 if ($this->_trans_depth > 0)
202 {
203 return TRUE;
204 }
205
206 $this->simple_query('ROLLBACK');
207 return TRUE;
208 }
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // --------------------------------------------------------------------
211
212 /**
213 * Escape String
214 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000216 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @return string
218 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200219 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000221 if (is_array($str))
222 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500223 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200224 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000225 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
227
228 return $str;
229 }
230
Derek Jonese4ed5832009-02-20 21:44:59 +0000231 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Jonese4ed5832009-02-20 21:44:59 +0000233 // escape LIKE condition wildcards
234 if ($like === TRUE)
235 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300236 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300237 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev94708bd2012-03-26 12:09:28 +0300238 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Jonese4ed5832009-02-20 21:44:59 +0000241 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // --------------------------------------------------------------------
245
246 /**
247 * Affected Rows
248 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200249 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200251 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 return sqlite_changes($this->conn_id);
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
257
258 /**
259 * Insert ID
260 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200261 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200263 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 return @sqlite_last_insert_rowid($this->conn_id);
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 * List table query
272 *
273 * Generates a platform-specific query string so that the table names can be fetched
274 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200275 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @return string
277 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200278 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 $sql = "SELECT name from sqlite_master WHERE type='table'";
281
282 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
283 {
Greg Aker0d424892010-01-26 02:14:44 +0000284 $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 +0000285 }
286 return $sql;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Show column query
293 *
294 * Generates a platform-specific query string so that the column names can be fetched
295 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200297 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200299 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 // Not supported
302 return FALSE;
303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Field data query
309 *
310 * Generates a platform-specific query so that the column data can be retrieved
311 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200313 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200315 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 return "SELECT * FROM ".$table." LIMIT 1";
318 }
319
320 // --------------------------------------------------------------------
321
322 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200323 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200325 * Returns an array containing code and message of the last
326 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200328 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200330 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200332 $error = array('code' => sqlite_last_error($this->conn_id));
333 $error['message'] = sqlite_error_string($error['code']);
334 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336
337 // --------------------------------------------------------------------
338
339 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 * From Tables
341 *
342 * This function implicitly groups FROM tables so there is no confusion
343 * about operator precedence in harmony with SQL standards
344 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200345 * @param array
346 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200348 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 if ( ! is_array($tables))
351 {
352 $tables = array($tables);
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 return '('.implode(', ', $tables).')';
356 }
357
358 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300361 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300363 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @param string the table name
366 * @param array the insert keys
367 * @param array the insert values
368 * @return string
369 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300370 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200371 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300372 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 // --------------------------------------------------------------------
376
377 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 * Truncate statement
379 *
380 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 *
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300382 * If the database does not support the truncate() command,
383 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * @param string the table name
386 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200387 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200388 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300390 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // --------------------------------------------------------------------
394
395 /**
396 * Limit string
397 *
398 * Generates a platform-specific LIMIT clause
399 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @param string the sql query string
Andrey Andreev979b5282012-03-20 16:45:39 +0200401 * @param int the number of rows to limit the query to
402 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return string
404 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200405 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200406 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ($offset == 0)
408 {
409 $offset = '';
410 }
411 else
412 {
413 $offset .= ", ";
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 return $sql."LIMIT ".$offset.$limit;
417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Close DB Connection
423 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return void
425 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300426 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300428 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431}
432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433/* End of file sqlite_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300434/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */