blob: 96458e032ea1fe5a978e1946b6f2bbdcd31a2144 [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 Andreevd9038782012-01-26 12:38:49 +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 /**
137 * Set client character set
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string
140 * @param string
Andrey Andreevd9038782012-01-26 12:38:49 +0200141 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200143 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200145 // Not supported
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 return TRUE;
147 }
148
149 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 /**
152 * Version number query string
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @return string
155 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200156 public function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 return sqlite_libversion();
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
162
163 /**
164 * Execute the query
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string an SQL query
167 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200169 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 $sql = $this->_prep_query($sql);
Andrey Andreevd9038782012-01-26 12:38:49 +0200172
173 if ( ! preg_match('/^(SELECT|EXPLAIN).+$/i', ltrim($sql)))
174 {
175 return @sqlite_exec($this->conn_id, $sql);
176 }
177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 return @sqlite_query($this->conn_id, $sql);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Prep the query
185 *
186 * If needed, each database adapter can prep the query string
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @param string an SQL query
189 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200191 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 return $sql;
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Begin Transaction
200 *
Barry Mienydd671972010-10-04 16:33:58 +0200201 * @return bool
202 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200203 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200206 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return TRUE;
209 }
210
211 // Reset the transaction failure flag.
212 // If the $test_mode flag is set to TRUE transactions will be rolled back
213 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200214 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 $this->simple_query('BEGIN TRANSACTION');
217 return TRUE;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Commit Transaction
224 *
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return bool
226 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200227 public function trans_commit()
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('COMMIT');
236 return TRUE;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Rollback Transaction
243 *
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return bool
245 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200246 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 return TRUE;
252 }
253
254 $this->simple_query('ROLLBACK');
255 return TRUE;
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // --------------------------------------------------------------------
259
260 /**
261 * Escape String
262 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @return string
266 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200267 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 if (is_array($str))
270 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500271 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200272 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000273 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200274 }
275
276 return $str;
277 }
278
Derek Jonese4ed5832009-02-20 21:44:59 +0000279 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 // escape LIKE condition wildcards
282 if ($like === TRUE)
283 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200284 return str_replace(array('%', '_', $this->_like_escape_chr),
285 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
286 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // --------------------------------------------------------------------
293
294 /**
295 * Affected Rows
296 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200297 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200299 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return sqlite_changes($this->conn_id);
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
305
306 /**
307 * Insert ID
308 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200309 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200311 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 return @sqlite_last_insert_rowid($this->conn_id);
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * "Count All" query
320 *
321 * Generates a platform-specific query string that counts all records in
322 * the specified database
323 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 * @param string
Andrey Andreevd9038782012-01-26 12:38:49 +0200325 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200327 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000330 {
331 return 0;
332 }
333
Andrey Andreevd9038782012-01-26 12:38:49 +0200334 $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 +0000335 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000336 {
337 return 0;
338 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000339
Andrey Andreevd9038782012-01-26 12:38:49 +0200340 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500341 $this->_reset_select();
Andrey Andreevd9038782012-01-26 12:38:49 +0200342 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * List table query
349 *
350 * Generates a platform-specific query string so that the table names can be fetched
351 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200352 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @return string
354 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200355 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200357 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
Andrey Andreevd9038782012-01-26 12:38:49 +0200359 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200361 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 +0000362 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 return $sql;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Show column query
371 *
372 * Generates a platform-specific query string so that the column names can be fetched
373 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200375 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200377 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
379 // Not supported
380 return FALSE;
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Field data query
387 *
388 * Generates a platform-specific query so that the column data can be retrieved
389 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200391 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200393 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200395 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * The error message string
402 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return string
404 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200405 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
407 return sqlite_error_string(sqlite_last_error($this->conn_id));
408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 // --------------------------------------------------------------------
411
412 /**
413 * The error message number
414 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200415 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200417 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return sqlite_last_error($this->conn_id);
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Escape the SQL Identifiers
426 *
427 * This function escapes column and table names
428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @param string
430 * @return string
431 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200432 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 if ($this->_escape_char == '')
435 {
436 return $item;
437 }
438
439 foreach ($this->_reserved_identifiers as $id)
440 {
441 if (strpos($item, '.'.$id) !== FALSE)
442 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200443 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200446 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200447 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 if (strpos($item, '.') !== FALSE)
451 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200452 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200456 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 // --------------------------------------------------------------------
460
461 /**
462 * From Tables
463 *
464 * This function implicitly groups FROM tables so there is no confusion
465 * about operator precedence in harmony with SQL standards
466 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @param type
468 * @return type
469 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200470 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 if ( ! is_array($tables))
473 {
474 $tables = array($tables);
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 return '('.implode(', ', $tables).')';
478 }
479
480 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 /**
483 * Insert statement
484 *
485 * Generates a platform-specific insert string from the supplied data
486 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @param string the table name
488 * @param array the insert keys
489 * @param array the insert values
490 * @return string
491 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200492 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200493 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200494 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // --------------------------------------------------------------------
498
499 /**
500 * Update statement
501 *
502 * Generates a platform-specific update string from the supplied data
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string the table name
505 * @param array the update data
506 * @param array the where clause
507 * @param array the orderby clause
508 * @param array the limit clause
509 * @return string
510 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200511 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500513 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200515 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Andrey Andreevd9038782012-01-26 12:38:49 +0200518 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
519 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
520 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
521 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
523
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 // --------------------------------------------------------------------
526
527 /**
528 * Truncate statement
529 *
530 * Generates a platform-specific truncate string from the supplied data
531 * If the database does not support the truncate() command
532 * This function maps to "DELETE FROM table"
533 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @param string the table name
535 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200536 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200537 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 return $this->_delete($table);
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
543
544 /**
545 * Delete statement
546 *
547 * Generates a platform-specific delete string from the supplied data
548 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @param string the table name
550 * @param array the where clause
551 * @param string the limit clause
552 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200553 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200554 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 {
556 $conditions = '';
557
558 if (count($where) > 0 OR count($like) > 0)
559 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200560 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000561
562 if (count($where) > 0 && count($like) > 0)
563 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200564 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 }
566 $conditions .= implode("\n", $like);
567 }
568
Andrey Andreevd9038782012-01-26 12:38:49 +0200569 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 // --------------------------------------------------------------------
573
574 /**
575 * Limit string
576 *
577 * Generates a platform-specific LIMIT clause
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @param string the sql query string
Andrey Andreevd9038782012-01-26 12:38:49 +0200580 * @param int the number of rows to limit the query to
581 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 * @return string
583 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200584 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200585 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200586 return $sql.'LIMIT '.($offset == 0 ? '' : ', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Close DB Connection
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param resource
595 * @return void
596 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200597 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
599 @sqlite_close($conn_id);
600 }
601
602
603}
604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605/* End of file sqlite_driver.php */
Andrey Andreevd9038782012-01-26 12:38:49 +0200606/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */