blob: 8116cfb18af0d259a081fe573fc1a605e7dae6ab [file] [log] [blame]
Andrey Andreevd9038782012-01-26 12:38:49 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevd9038782012-01-26 12:38:49 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevd9038782012-01-26 12:38:49 +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 Andreevd9038782012-01-26 12:38:49 +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 Andreevd33e24c2012-02-12 03:23:07 +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 Andreevd9038782012-01-26 12:38:49 +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 Andreevd9038782012-01-26 12:38:49 +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 Andreevd9038782012-01-26 12:38:49 +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 Andreevd9038782012-01-26 12:38:49 +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 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000109 * Reconnect
110 *
111 * Keep / reestablish the db connection if no queries have been
112 * sent for a length of time exceeding the server's idle timeout
113 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 * @return void
115 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200116 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200118 // Not supported in SQLite
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 }
120
121 // --------------------------------------------------------------------
122
123 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * Select the database
125 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200126 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200128 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200130 // Not needed, in SQLite every pseudo-connection is a database
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 return TRUE;
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * Version number query string
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @return string
140 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200141 public function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 return sqlite_libversion();
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
149 * Execute the query
150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @param string an SQL query
152 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200153 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200154 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
156 $sql = $this->_prep_query($sql);
Andrey Andreeva0c27852012-03-02 13:49:28 +0200157 return $this->is_write_type($sql)
158 ? @sqlite_exec($this->conn_id, $sql)
159 : @sqlite_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
163
164 /**
165 * Prep the query
166 *
167 * If needed, each database adapter can prep the query string
168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string an SQL query
170 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200171 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200172 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
174 return $sql;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Begin Transaction
181 *
Barry Mienydd671972010-10-04 16:33:58 +0200182 * @return bool
183 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200184 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200187 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 return TRUE;
190 }
191
192 // Reset the transaction failure flag.
193 // If the $test_mode flag is set to TRUE transactions will be rolled back
194 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200195 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
197 $this->simple_query('BEGIN TRANSACTION');
198 return TRUE;
199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Commit Transaction
205 *
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return bool
207 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200208 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200211 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 return TRUE;
214 }
215
216 $this->simple_query('COMMIT');
217 return TRUE;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Rollback Transaction
224 *
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return bool
226 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200227 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200230 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
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 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200248 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 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200265 return 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);
Derek Jonese4ed5832009-02-20 21:44:59 +0000268 }
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 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200278 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200280 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 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200290 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200292 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
Andrey Andreevd9038782012-01-26 12:38:49 +0200306 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200308 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 Andreevd9038782012-01-26 12:38:49 +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
Andrey Andreevd9038782012-01-26 12:38:49 +0200321 $query = $query->row();
Andrey Andreevd33e24c2012-02-12 03:23:07 +0200322 $this->_data_seek(0);
Andrey Andreevd9038782012-01-26 12:38:49 +0200323 return (int) $query->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 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200333 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * @return string
335 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200336 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200338 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000339
Andrey Andreevd9038782012-01-26 12:38:49 +0200340 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200342 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 +0000343 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 return $sql;
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Show column query
352 *
353 * Generates a platform-specific query string so that the column names can be fetched
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200356 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200358 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 // Not supported
361 return FALSE;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Field data query
368 *
369 * Generates a platform-specific query so that the column data can be retrieved
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200372 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200374 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200376 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * The error message string
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return string
385 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200386 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 return sqlite_error_string(sqlite_last_error($this->conn_id));
389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // --------------------------------------------------------------------
392
393 /**
394 * The error message number
395 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200396 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200398 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 return sqlite_last_error($this->conn_id);
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Escape the SQL Identifiers
407 *
408 * This function escapes column and table names
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @param string
411 * @return string
412 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200413 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 if ($this->_escape_char == '')
416 {
417 return $item;
418 }
419
420 foreach ($this->_reserved_identifiers as $id)
421 {
422 if (strpos($item, '.'.$id) !== FALSE)
423 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200424 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200427 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200428 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if (strpos($item, '.') !== FALSE)
432 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200433 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200437 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * From Tables
444 *
445 * This function implicitly groups FROM tables so there is no confusion
446 * about operator precedence in harmony with SQL standards
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param type
449 * @return type
450 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200451 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 if ( ! is_array($tables))
454 {
455 $tables = array($tables);
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 return '('.implode(', ', $tables).')';
459 }
460
461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
464 * Insert statement
465 *
466 * Generates a platform-specific insert string from the supplied data
467 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * @param string the table name
469 * @param array the insert keys
470 * @param array the insert values
471 * @return string
472 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200473 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200474 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200475 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * Update statement
482 *
483 * Generates a platform-specific update string from the supplied data
484 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @param string the table name
486 * @param array the update data
487 * @param array the where clause
488 * @param array the orderby clause
489 * @param array the limit clause
490 * @return string
491 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200492 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500494 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200496 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreevd9038782012-01-26 12:38:49 +0200499 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
500 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
501 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
502 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
509 * Truncate statement
510 *
511 * Generates a platform-specific truncate string from the supplied data
512 * If the database does not support the truncate() command
513 * This function maps to "DELETE FROM table"
514 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @param string the table name
516 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200517 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200518 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 return $this->_delete($table);
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 // --------------------------------------------------------------------
524
525 /**
526 * Delete statement
527 *
528 * Generates a platform-specific delete string from the supplied data
529 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @param string the table name
531 * @param array the where clause
532 * @param string the limit clause
533 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200534 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200535 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
537 $conditions = '';
538
539 if (count($where) > 0 OR count($like) > 0)
540 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200541 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000542
543 if (count($where) > 0 && count($like) > 0)
544 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200545 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
547 $conditions .= implode("\n", $like);
548 }
549
Andrey Andreevd9038782012-01-26 12:38:49 +0200550 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
555 /**
556 * Limit string
557 *
558 * Generates a platform-specific LIMIT clause
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string the sql query string
Andrey Andreevd9038782012-01-26 12:38:49 +0200561 * @param int the number of rows to limit the query to
562 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 * @return string
564 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200565 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200566 {
Andrey Andreevb537c4d2012-01-26 14:45:45 +0200567 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Close DB Connection
574 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 * @param resource
576 * @return void
577 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200578 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 {
580 @sqlite_close($conn_id);
581 }
582
583
584}
585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586/* End of file sqlite_driver.php */
Andrey Andreevd9038782012-01-26 12:38:49 +0200587/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */