blob: d710b945df94c595939a61cc3622be59f2b2d856 [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 /**
271 * "Count All" query
272 *
273 * Generates a platform-specific query string that counts all records in
274 * the specified database
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
277 * @return string
278 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200279 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000282 {
283 return 0;
284 }
285
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200286 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000288 {
289 return 0;
290 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
292 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500293 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000294 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * List table query
301 *
302 * Generates a platform-specific query string so that the table names can be fetched
303 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200304 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return string
306 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200307 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
309 $sql = "SELECT name from sqlite_master WHERE type='table'";
310
311 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
312 {
Greg Aker0d424892010-01-26 02:14:44 +0000313 $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 +0000314 }
315 return $sql;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Show column query
322 *
323 * Generates a platform-specific query string so that the column names can be fetched
324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200326 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200328 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 // Not supported
331 return FALSE;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Field data query
338 *
339 * Generates a platform-specific query so that the column data can be retrieved
340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200342 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200344 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
346 return "SELECT * FROM ".$table." LIMIT 1";
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200352 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200354 * Returns an array containing code and message of the last
355 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200357 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200359 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200361 $error = array('code' => sqlite_last_error($this->conn_id));
362 $error['message'] = sqlite_error_string($error['code']);
363 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // --------------------------------------------------------------------
367
368 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * From Tables
370 *
371 * This function implicitly groups FROM tables so there is no confusion
372 * about operator precedence in harmony with SQL standards
373 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200374 * @param array
375 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200377 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 if ( ! is_array($tables))
380 {
381 $tables = array($tables);
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 return '('.implode(', ', $tables).')';
385 }
386
387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300390 * Replace statement
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300392 * Generates a platform-specific replace string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param string the table name
395 * @param array the insert keys
396 * @param array the insert values
397 * @return string
398 */
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300399 protected function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200400 {
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300401 return 'INSERT OR '.parent::_replace($table, $keys, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // --------------------------------------------------------------------
405
406 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 * Truncate statement
408 *
409 * Generates a platform-specific truncate string from the supplied data
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 *
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300411 * If the database does not support the truncate() command,
412 * then this function maps to 'DELETE FROM table'
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param string the table name
415 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200417 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300419 return 'DELETE FROM '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
423
424 /**
425 * Limit string
426 *
427 * Generates a platform-specific LIMIT clause
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param string the sql query string
Andrey Andreev979b5282012-03-20 16:45:39 +0200430 * @param int the number of rows to limit the query to
431 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return string
433 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200434 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200435 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if ($offset == 0)
437 {
438 $offset = '';
439 }
440 else
441 {
442 $offset .= ", ";
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $sql."LIMIT ".$offset.$limit;
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Close DB Connection
452 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return void
454 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300455 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300457 @sqlite_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460}
461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462/* End of file sqlite_driver.php */
Andrey Andreev79922c02012-05-23 12:27:17 +0300463/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */