blob: 20b05af0d1845ba9d464c5311f8c04e5a518d256 [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 Andreevd9038782012-01-26 12:38:49 +0200157
158 if ( ! preg_match('/^(SELECT|EXPLAIN).+$/i', ltrim($sql)))
159 {
160 return @sqlite_exec($this->conn_id, $sql);
161 }
162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 return @sqlite_query($this->conn_id, $sql);
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // --------------------------------------------------------------------
167
168 /**
169 * Prep the query
170 *
171 * If needed, each database adapter can prep the query string
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string an SQL query
174 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200176 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
178 return $sql;
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Begin Transaction
185 *
Barry Mienydd671972010-10-04 16:33:58 +0200186 * @return bool
187 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200188 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200191 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
193 return TRUE;
194 }
195
196 // Reset the transaction failure flag.
197 // If the $test_mode flag is set to TRUE transactions will be rolled back
198 // even if the queries produce a successful result.
Andrey Andreevd9038782012-01-26 12:38:49 +0200199 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000200
201 $this->simple_query('BEGIN TRANSACTION');
202 return TRUE;
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Commit Transaction
209 *
Barry Mienydd671972010-10-04 16:33:58 +0200210 * @return bool
211 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200212 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200215 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
217 return TRUE;
218 }
219
220 $this->simple_query('COMMIT');
221 return TRUE;
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Rollback Transaction
228 *
Barry Mienydd671972010-10-04 16:33:58 +0200229 * @return bool
230 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200231 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreevd9038782012-01-26 12:38:49 +0200234 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return TRUE;
237 }
238
239 $this->simple_query('ROLLBACK');
240 return TRUE;
241 }
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // --------------------------------------------------------------------
244
245 /**
246 * Escape String
247 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000249 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 * @return string
251 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200252 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000254 if (is_array($str))
255 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500256 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200257 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000258 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200259 }
260
261 return $str;
262 }
263
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Jonese4ed5832009-02-20 21:44:59 +0000266 // escape LIKE condition wildcards
267 if ($like === TRUE)
268 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200269 return str_replace(array('%', '_', $this->_like_escape_chr),
270 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
271 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // --------------------------------------------------------------------
278
279 /**
280 * Affected Rows
281 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200282 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200284 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 return sqlite_changes($this->conn_id);
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // --------------------------------------------------------------------
290
291 /**
292 * Insert ID
293 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200294 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200296 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 return @sqlite_last_insert_rowid($this->conn_id);
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * "Count All" query
305 *
306 * Generates a platform-specific query string that counts all records in
307 * the specified database
308 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 * @param string
Andrey Andreevd9038782012-01-26 12:38:49 +0200310 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200312 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000315 {
316 return 0;
317 }
318
Andrey Andreevd9038782012-01-26 12:38:49 +0200319 $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 +0000320 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000321 {
322 return 0;
323 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000324
Andrey Andreevd9038782012-01-26 12:38:49 +0200325 $query = $query->row();
Andrey Andreevd33e24c2012-02-12 03:23:07 +0200326 $this->_data_seek(0);
Andrey Andreevd9038782012-01-26 12:38:49 +0200327 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * List table query
334 *
335 * Generates a platform-specific query string so that the table names can be fetched
336 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200337 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 * @return string
339 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200340 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200342 $sql = "SELECT name FROM sqlite_master WHERE type='table'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000343
Andrey Andreevd9038782012-01-26 12:38:49 +0200344 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200346 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 +0000347 }
Andrey Andreevd9038782012-01-26 12:38:49 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 return $sql;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Show column query
356 *
357 * Generates a platform-specific query string so that the column names can be fetched
358 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200360 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200362 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 // Not supported
365 return FALSE;
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Field data query
372 *
373 * Generates a platform-specific query so that the column data can be retrieved
374 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * @param string the table name
Andrey Andreevd9038782012-01-26 12:38:49 +0200376 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200378 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200380 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * The error message string
387 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 * @return string
389 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200390 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
392 return sqlite_error_string(sqlite_last_error($this->conn_id));
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // --------------------------------------------------------------------
396
397 /**
398 * The error message number
399 *
Andrey Andreevd9038782012-01-26 12:38:49 +0200400 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200402 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return sqlite_last_error($this->conn_id);
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Escape the SQL Identifiers
411 *
412 * This function escapes column and table names
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param string
415 * @return string
416 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200417 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 if ($this->_escape_char == '')
420 {
421 return $item;
422 }
423
424 foreach ($this->_reserved_identifiers as $id)
425 {
426 if (strpos($item, '.'.$id) !== FALSE)
427 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200428 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200431 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200432 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 if (strpos($item, '.') !== FALSE)
436 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200437 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // remove duplicates if the user already included the escape
Andrey Andreevd9038782012-01-26 12:38:49 +0200441 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
445
446 /**
447 * From Tables
448 *
449 * This function implicitly groups FROM tables so there is no confusion
450 * about operator precedence in harmony with SQL standards
451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param type
453 * @return type
454 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200455 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 if ( ! is_array($tables))
458 {
459 $tables = array($tables);
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 return '('.implode(', ', $tables).')';
463 }
464
465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
468 * Insert statement
469 *
470 * Generates a platform-specific insert string from the supplied data
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string the table name
473 * @param array the insert keys
474 * @param array the insert values
475 * @return string
476 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200477 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200478 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200479 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Update statement
486 *
487 * Generates a platform-specific update string from the supplied data
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string the table name
490 * @param array the update data
491 * @param array the where clause
492 * @param array the orderby clause
493 * @param array the limit clause
494 * @return string
495 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200496 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500498 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200500 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Andrey Andreevd9038782012-01-26 12:38:49 +0200503 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
504 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
505 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
506 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 // --------------------------------------------------------------------
511
512 /**
513 * Truncate statement
514 *
515 * Generates a platform-specific truncate string from the supplied data
516 * If the database does not support the truncate() command
517 * This function maps to "DELETE FROM table"
518 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 * @param string the table name
520 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200521 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200522 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return $this->_delete($table);
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // --------------------------------------------------------------------
528
529 /**
530 * Delete statement
531 *
532 * Generates a platform-specific delete string from the supplied data
533 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @param string the table name
535 * @param array the where clause
536 * @param string the limit clause
537 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200538 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200539 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 $conditions = '';
542
543 if (count($where) > 0 OR count($like) > 0)
544 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200545 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000546
547 if (count($where) > 0 && count($like) > 0)
548 {
Andrey Andreevd9038782012-01-26 12:38:49 +0200549 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
551 $conditions .= implode("\n", $like);
552 }
553
Andrey Andreevd9038782012-01-26 12:38:49 +0200554 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 // --------------------------------------------------------------------
558
559 /**
560 * Limit string
561 *
562 * Generates a platform-specific LIMIT clause
563 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 * @param string the sql query string
Andrey Andreevd9038782012-01-26 12:38:49 +0200565 * @param int the number of rows to limit the query to
566 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 * @return string
568 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200569 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200570 {
Andrey Andreevb537c4d2012-01-26 14:45:45 +0200571 return $sql.'LIMIT '.($offset == 0 ? '' : $offset.', ').$limit;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Close DB Connection
578 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000579 * @param resource
580 * @return void
581 */
Andrey Andreevd9038782012-01-26 12:38:49 +0200582 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
584 @sqlite_close($conn_id);
585 }
586
587
588}
589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590/* End of file sqlite_driver.php */
Andrey Andreevd9038782012-01-26 12:38:49 +0200591/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */