blob: 6535d27535a30224311b4df5f565bc653a88d7ff [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
Derek Allard2067d1a2008-11-13 22:59:24 +000030/**
31 * SQLite Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_sqlite_driver extends CI_DB {
44
Timothy Warren1ed59952012-03-19 18:25:43 -040045 public $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // The character used to escape with - not needed for SQLite
Timothy Warren1ed59952012-03-19 18:25:43 -040048 protected $_escape_char = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Derek Jonese4ed5832009-02-20 21:44:59 +000050 // clause and character used for LIKE escape sequences
Timothy Warren1ed59952012-03-19 18:25:43 -040051 protected $_like_escape_str = " ESCAPE '%s' ";
52 protected $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
Timothy Warren1ed59952012-03-19 18:25:43 -040057 * used for the count_all() and count_all_results() public functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000058 */
Timothy Warren1ed59952012-03-19 18:25:43 -040059 protected $_count_string = "SELECT COUNT(*) AS ";
60 protected $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
63 * Non-persistent database connection
64 *
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Timothy Warren1ed59952012-03-19 18:25:43 -040067 protected function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000068 {
69 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
70 {
71 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 if ($this->db_debug)
74 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000075 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000076 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 return FALSE;
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 return $conn_id;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 // --------------------------------------------------------------------
85
86 /**
87 * Persistent database connection
88 *
Derek Allard2067d1a2008-11-13 22:59:24 +000089 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020090 */
Timothy Warren1ed59952012-03-19 18:25:43 -040091 protected function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
94 {
95 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 if ($this->db_debug)
98 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000099 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 return FALSE;
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 return $conn_id;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // --------------------------------------------------------------------
109
110 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000111 * Reconnect
112 *
113 * Keep / reestablish the db connection if no queries have been
114 * sent for a length of time exceeding the server's idle timeout
115 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000116 * @return void
117 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400118 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 {
120 // not implemented in SQLite
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * Select the database
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400130 protected function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 return TRUE;
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200138 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * @return string
141 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200142 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200144 return isset($this->data_cache['version'])
145 ? $this->data_cache['version']
146 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // --------------------------------------------------------------------
150
151 /**
152 * Execute the query
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @param string an SQL query
155 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200156 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400157 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 return @sqlite_query($this->conn_id, $sql);
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
163
164 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * Begin Transaction
166 *
Barry Mienydd671972010-10-04 16:33:58 +0200167 * @return bool
168 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400169 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 if ( ! $this->trans_enabled)
172 {
173 return TRUE;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 // 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 // Reset the transaction failure flag.
183 // If the $test_mode flag is set to TRUE transactions will be rolled back
184 // even if the queries produce a successful result.
185 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
186
187 $this->simple_query('BEGIN TRANSACTION');
188 return TRUE;
189 }
190
191 // --------------------------------------------------------------------
192
193 /**
194 * Commit Transaction
195 *
Barry Mienydd671972010-10-04 16:33:58 +0200196 * @return bool
197 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400198 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 if ( ! $this->trans_enabled)
201 {
202 return TRUE;
203 }
204
205 // When transactions are nested we only begin/commit/rollback the outermost ones
206 if ($this->_trans_depth > 0)
207 {
208 return TRUE;
209 }
210
211 $this->simple_query('COMMIT');
212 return TRUE;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Rollback Transaction
219 *
Barry Mienydd671972010-10-04 16:33:58 +0200220 * @return bool
221 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400222 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 if ( ! $this->trans_enabled)
225 {
226 return TRUE;
227 }
228
229 // When transactions are nested we only begin/commit/rollback the outermost ones
230 if ($this->_trans_depth > 0)
231 {
232 return TRUE;
233 }
234
235 $this->simple_query('ROLLBACK');
236 return TRUE;
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
240
241 /**
242 * Escape String
243 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000245 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @return string
247 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400248 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000250 if (is_array($str))
251 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500252 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200253 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000254 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200255 }
256
257 return $str;
258 }
259
Derek Jonese4ed5832009-02-20 21:44:59 +0000260 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Jonese4ed5832009-02-20 21:44:59 +0000262 // escape LIKE condition wildcards
263 if ($like === TRUE)
264 {
265 $str = str_replace( array('%', '_', $this->_like_escape_chr),
266 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
267 $str);
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Jonese4ed5832009-02-20 21:44:59 +0000270 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // --------------------------------------------------------------------
274
275 /**
276 * Affected Rows
277 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @return integer
279 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400280 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
282 return sqlite_changes($this->conn_id);
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Insert ID
289 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @return integer
291 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400292 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 return @sqlite_last_insert_rowid($this->conn_id);
295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * "Count All" query
301 *
302 * Generates a platform-specific query string that counts all records in
303 * the specified database
304 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @param string
306 * @return string
307 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400308 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000311 {
312 return 0;
313 }
314
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200315 $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 +0000316 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000317 {
318 return 0;
319 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000320
321 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500322 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000323 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * List table query
330 *
331 * Generates a platform-specific query string so that the table names can be fetched
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param boolean
334 * @return string
335 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400336 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $sql = "SELECT name from sqlite_master WHERE type='table'";
339
340 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
341 {
Greg Aker0d424892010-01-26 02:14:44 +0000342 $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 +0000343 }
344 return $sql;
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * Show column query
351 *
352 * Generates a platform-specific query string so that the column names can be fetched
353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @param string the table name
355 * @return string
356 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400357 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 // Not supported
360 return FALSE;
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * Field data query
367 *
368 * Generates a platform-specific query so that the column data can be retrieved
369 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 * @param string the table name
371 * @return object
372 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400373 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 return "SELECT * FROM ".$table." LIMIT 1";
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200381 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200383 * Returns an array containing code and message of the last
384 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200386 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 $error = array('code' => sqlite_last_error($this->conn_id));
391 $error['message'] = sqlite_error_string($error['code']);
392 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394
395 // --------------------------------------------------------------------
396
397 /**
398 * Escape the SQL Identifiers
399 *
Timothy Warren1ed59952012-03-19 18:25:43 -0400400 * This public function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @param string
403 * @return string
404 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400405 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 if ($this->_escape_char == '')
408 {
409 return $item;
410 }
411
412 foreach ($this->_reserved_identifiers as $id)
413 {
414 if (strpos($item, '.'.$id) !== FALSE)
415 {
Barry Mienydd671972010-10-04 16:33:58 +0200416 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // remove duplicates if the user already included the escape
419 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200420 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if (strpos($item, '.') !== FALSE)
424 {
Barry Mienydd671972010-10-04 16:33:58 +0200425 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427 else
428 {
429 $str = $this->_escape_char.$item.$this->_escape_char;
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 // remove duplicates if the user already included the escape
433 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
437
438 /**
439 * From Tables
440 *
Timothy Warren1ed59952012-03-19 18:25:43 -0400441 * This public function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * about operator precedence in harmony with SQL standards
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param type
445 * @return type
446 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400447 public function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 if ( ! is_array($tables))
450 {
451 $tables = array($tables);
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 return '('.implode(', ', $tables).')';
455 }
456
457 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 /**
460 * Insert statement
461 *
462 * Generates a platform-specific insert string from the supplied data
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string the table name
465 * @param array the insert keys
466 * @param array the insert values
467 * @return string
468 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400469 public function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200470 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
475
476 /**
477 * Update statement
478 *
479 * Generates a platform-specific update string from the supplied data
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param string the table name
482 * @param array the update data
483 * @param array the where clause
484 * @param array the orderby clause
485 * @param array the limit clause
486 * @return string
487 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400488 public function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500490 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
492 $valstr[] = $key." = ".$val;
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
500
501 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
502
503 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 return $sql;
506 }
507
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
511 /**
512 * Truncate statement
513 *
514 * Generates a platform-specific truncate string from the supplied data
515 * If the database does not support the truncate() command
Timothy Warren1ed59952012-03-19 18:25:43 -0400516 * This public function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 * @param string the table name
519 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200520 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400521 public function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 return $this->_delete($table);
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
527
528 /**
529 * Delete statement
530 *
531 * Generates a platform-specific delete string from the supplied data
532 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @param string the table name
534 * @param array the where clause
535 * @param string the limit clause
536 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200537 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400538 public function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 $conditions = '';
541
542 if (count($where) > 0 OR count($like) > 0)
543 {
544 $conditions = "\nWHERE ";
545 $conditions .= implode("\n", $this->ar_where);
546
547 if (count($where) > 0 && count($like) > 0)
548 {
549 $conditions .= " AND ";
550 }
551 $conditions .= implode("\n", $like);
552 }
553
554 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 return "DELETE FROM ".$table.$conditions.$limit;
557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
562 * Limit string
563 *
564 * Generates a platform-specific LIMIT clause
565 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * @param string the sql query string
567 * @param integer the number of rows to limit the query to
568 * @param integer the offset value
569 * @return string
570 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400571 public function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200572 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 if ($offset == 0)
574 {
575 $offset = '';
576 }
577 else
578 {
579 $offset .= ", ";
580 }
Barry Mienydd671972010-10-04 16:33:58 +0200581
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 return $sql."LIMIT ".$offset.$limit;
583 }
584
585 // --------------------------------------------------------------------
586
587 /**
588 * Close DB Connection
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param resource
591 * @return void
592 */
Timothy Warren1ed59952012-03-19 18:25:43 -0400593 public function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 @sqlite_close($conn_id);
596 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000597}
598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599/* End of file sqlite_driver.php */
Timothy Warren1ed59952012-03-19 18:25:43 -0400600/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */