blob: ef543c9b54b544f8a68e439506a7c4e8d2335026 [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
32 * creates dynamically based on whether the active record
33 * 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
Timothy Warren691a8e72012-03-19 19:06:00 -040055 * used for the count_all() and count_all_results() functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
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 /**
369 * Escape the SQL Identifiers
370 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400371 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @param string
374 * @return string
375 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200376 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 if ($this->_escape_char == '')
379 {
380 return $item;
381 }
382
383 foreach ($this->_reserved_identifiers as $id)
384 {
385 if (strpos($item, '.'.$id) !== FALSE)
386 {
Barry Mienydd671972010-10-04 16:33:58 +0200387 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // remove duplicates if the user already included the escape
390 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200391 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 if (strpos($item, '.') !== FALSE)
395 {
Barry Mienydd671972010-10-04 16:33:58 +0200396 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398 else
399 {
400 $str = $this->_escape_char.$item.$this->_escape_char;
401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // remove duplicates if the user already included the escape
404 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // --------------------------------------------------------------------
408
409 /**
410 * From Tables
411 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400412 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * about operator precedence in harmony with SQL standards
414 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200415 * @param array
416 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200418 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
420 if ( ! is_array($tables))
421 {
422 $tables = array($tables);
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 return '('.implode(', ', $tables).')';
426 }
427
428 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 /**
431 * Insert statement
432 *
433 * Generates a platform-specific insert string from the supplied data
434 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @param string the table name
436 * @param array the insert keys
437 * @param array the insert values
438 * @return string
439 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200440 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200441 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // --------------------------------------------------------------------
446
447 /**
448 * Update statement
449 *
450 * Generates a platform-specific update string from the supplied data
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param string the table name
453 * @param array the update data
454 * @param array the where clause
455 * @param array the orderby clause
456 * @param array the limit clause
457 * @return string
458 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200459 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500461 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 $valstr[] = $key." = ".$val;
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
471
472 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
473
474 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 return $sql;
477 }
478
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * Truncate statement
484 *
485 * Generates a platform-specific truncate string from the supplied data
486 * If the database does not support the truncate() command
Timothy Warren691a8e72012-03-19 19:06:00 -0400487 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string the table name
490 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200492 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 return $this->_delete($table);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Delete statement
501 *
502 * Generates a platform-specific delete string from the supplied data
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string the table name
505 * @param array the where clause
506 * @param string the limit clause
507 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200508 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200509 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 $conditions = '';
512
513 if (count($where) > 0 OR count($like) > 0)
514 {
515 $conditions = "\nWHERE ";
516 $conditions .= implode("\n", $this->ar_where);
517
518 if (count($where) > 0 && count($like) > 0)
519 {
520 $conditions .= " AND ";
521 }
522 $conditions .= implode("\n", $like);
523 }
524
525 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 return "DELETE FROM ".$table.$conditions.$limit;
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 // --------------------------------------------------------------------
531
532 /**
533 * Limit string
534 *
535 * Generates a platform-specific LIMIT clause
536 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 * @param string the sql query string
Andrey Andreev979b5282012-03-20 16:45:39 +0200538 * @param int the number of rows to limit the query to
539 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @return string
541 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200542 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200543 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 if ($offset == 0)
545 {
546 $offset = '';
547 }
548 else
549 {
550 $offset .= ", ";
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 return $sql."LIMIT ".$offset.$limit;
554 }
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Close DB Connection
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param resource
562 * @return void
563 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200564 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
566 @sqlite_close($conn_id);
567 }
Timothy Warren691a8e72012-03-19 19:06:00 -0400568
Derek Allard2067d1a2008-11-13 22:59:24 +0000569}
570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400572/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */