blob: de72b545450c063bd52001eea5788c3954a59e17 [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 Warren691a8e72012-03-19 19:06:00 -040045 var $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 Warren691a8e72012-03-19 19:06:00 -040048 var $_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 Warren691a8e72012-03-19 19:06:00 -040051 var $_like_escape_str = " ESCAPE '%s' ";
52 var $_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 Warren691a8e72012-03-19 19:06:00 -040057 * used for the count_all() and count_all_results() functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000058 */
Timothy Warren691a8e72012-03-19 19:06:00 -040059 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
63 * Non-persistent database connection
64 *
Timothy Warren691a8e72012-03-19 19:06:00 -040065 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020067 */
Timothy Warren691a8e72012-03-19 19:06:00 -040068 function db_connect()
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
71 {
72 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 if ($this->db_debug)
75 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000076 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000077 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Derek Allard2067d1a2008-11-13 22:59:24 +000079 return FALSE;
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 return $conn_id;
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
86
87 /**
88 * Persistent database connection
89 *
Timothy Warren691a8e72012-03-19 19:06:00 -040090 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020092 */
Timothy Warren691a8e72012-03-19 19:06:00 -040093 function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
95 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
96 {
97 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 if ($this->db_debug)
100 {
Derek Allardfac8fbc2010-02-05 16:14:49 +0000101 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 return FALSE;
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 return $conn_id;
108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // --------------------------------------------------------------------
111
112 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000113 * Reconnect
114 *
115 * Keep / reestablish the db connection if no queries have been
116 * sent for a length of time exceeding the server's idle timeout
117 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400118 * @access public
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 * @return void
120 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400121 function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000122 {
123 // not implemented in SQLite
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * Select the database
130 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400131 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400134 function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 return TRUE;
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200142 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * @return string
145 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200146 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200148 return isset($this->data_cache['version'])
149 ? $this->data_cache['version']
150 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
154
155 /**
156 * Execute the query
157 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400158 * @access private called by the base class
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * @param string an SQL query
160 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200161 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400162 function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 return @sqlite_query($this->conn_id, $sql);
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // --------------------------------------------------------------------
168
169 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * Begin Transaction
171 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400172 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200173 * @return bool
174 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400175 function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
177 if ( ! $this->trans_enabled)
178 {
179 return TRUE;
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 // When transactions are nested we only begin/commit/rollback the outermost ones
183 if ($this->_trans_depth > 0)
184 {
185 return TRUE;
186 }
187
188 // Reset the transaction failure flag.
189 // If the $test_mode flag is set to TRUE transactions will be rolled back
190 // even if the queries produce a successful result.
191 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
192
193 $this->simple_query('BEGIN TRANSACTION');
194 return TRUE;
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Commit Transaction
201 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400202 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200203 * @return bool
204 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400205 function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 if ( ! $this->trans_enabled)
208 {
209 return TRUE;
210 }
211
212 // When transactions are nested we only begin/commit/rollback the outermost ones
213 if ($this->_trans_depth > 0)
214 {
215 return TRUE;
216 }
217
218 $this->simple_query('COMMIT');
219 return TRUE;
220 }
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Rollback Transaction
226 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400227 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200228 * @return bool
229 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400230 function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 if ( ! $this->trans_enabled)
233 {
234 return TRUE;
235 }
236
237 // When transactions are nested we only begin/commit/rollback the outermost ones
238 if ($this->_trans_depth > 0)
239 {
240 return TRUE;
241 }
242
243 $this->simple_query('ROLLBACK');
244 return TRUE;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // --------------------------------------------------------------------
248
249 /**
250 * Escape String
251 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400252 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000254 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @return string
256 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400257 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000259 if (is_array($str))
260 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500261 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200262 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000263 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200264 }
265
266 return $str;
267 }
268
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Jonese4ed5832009-02-20 21:44:59 +0000271 // escape LIKE condition wildcards
272 if ($like === TRUE)
273 {
274 $str = str_replace( array('%', '_', $this->_like_escape_chr),
275 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
276 $str);
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Jonese4ed5832009-02-20 21:44:59 +0000279 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
283
284 /**
285 * Affected Rows
286 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400287 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return integer
289 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400290 function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 return sqlite_changes($this->conn_id);
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // --------------------------------------------------------------------
296
297 /**
298 * Insert ID
299 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400300 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @return integer
302 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400303 function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 return @sqlite_last_insert_rowid($this->conn_id);
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * "Count All" query
312 *
313 * Generates a platform-specific query string that counts all records in
314 * the specified database
315 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400316 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string
318 * @return string
319 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400320 function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000323 {
324 return 0;
325 }
326
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200327 $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 +0000328 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000329 {
330 return 0;
331 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000332
333 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500334 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000335 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * List table query
342 *
343 * Generates a platform-specific query string so that the table names can be fetched
344 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400345 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @param boolean
347 * @return string
348 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400349 function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $sql = "SELECT name from sqlite_master WHERE type='table'";
352
353 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
354 {
Greg Aker0d424892010-01-26 02:14:44 +0000355 $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 +0000356 }
357 return $sql;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Show column query
364 *
365 * Generates a platform-specific query string so that the column names can be fetched
366 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400367 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string the table name
369 * @return string
370 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400371 function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 // Not supported
374 return FALSE;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Field data query
381 *
382 * Generates a platform-specific query so that the column data can be retrieved
383 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400384 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * @param string the table name
386 * @return object
387 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400388 function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 return "SELECT * FROM ".$table." LIMIT 1";
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200396 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200398 * Returns an array containing code and message of the last
399 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200401 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200403 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200405 $error = array('code' => sqlite_last_error($this->conn_id));
406 $error['message'] = sqlite_error_string($error['code']);
407 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Escape the SQL Identifiers
414 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400415 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400417 * @access private
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * @param string
419 * @return string
420 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400421 function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
423 if ($this->_escape_char == '')
424 {
425 return $item;
426 }
427
428 foreach ($this->_reserved_identifiers as $id)
429 {
430 if (strpos($item, '.'.$id) !== FALSE)
431 {
Barry Mienydd671972010-10-04 16:33:58 +0200432 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // remove duplicates if the user already included the escape
435 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200436 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 if (strpos($item, '.') !== FALSE)
440 {
Barry Mienydd671972010-10-04 16:33:58 +0200441 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443 else
444 {
445 $str = $this->_escape_char.$item.$this->_escape_char;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // remove duplicates if the user already included the escape
449 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
453
454 /**
455 * From Tables
456 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400457 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * about operator precedence in harmony with SQL standards
459 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400460 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @param type
462 * @return type
463 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400464 function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 if ( ! is_array($tables))
467 {
468 $tables = array($tables);
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 return '('.implode(', ', $tables).')';
472 }
473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
477 * Insert statement
478 *
479 * Generates a platform-specific insert string from the supplied data
480 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400481 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string the table name
483 * @param array the insert keys
484 * @param array the insert values
485 * @return string
486 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400487 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200488 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // --------------------------------------------------------------------
493
494 /**
495 * Update statement
496 *
497 * Generates a platform-specific update string from the supplied data
498 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400499 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string the table name
501 * @param array the update data
502 * @param array the where clause
503 * @param array the orderby clause
504 * @param array the limit clause
505 * @return string
506 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400507 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500509 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 $valstr[] = $key." = ".$val;
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
519
520 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
521
522 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 return $sql;
525 }
526
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
529
530 /**
531 * Truncate statement
532 *
533 * Generates a platform-specific truncate string from the supplied data
534 * If the database does not support the truncate() command
Timothy Warren691a8e72012-03-19 19:06:00 -0400535 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400537 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string the table name
539 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200540 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400541 function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 return $this->_delete($table);
544 }
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 // --------------------------------------------------------------------
547
548 /**
549 * Delete statement
550 *
551 * Generates a platform-specific delete string from the supplied data
552 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400553 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param string the table name
555 * @param array the where clause
556 * @param string the limit clause
557 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200558 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400559 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
561 $conditions = '';
562
563 if (count($where) > 0 OR count($like) > 0)
564 {
565 $conditions = "\nWHERE ";
566 $conditions .= implode("\n", $this->ar_where);
567
568 if (count($where) > 0 && count($like) > 0)
569 {
570 $conditions .= " AND ";
571 }
572 $conditions .= implode("\n", $like);
573 }
574
575 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200576
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 return "DELETE FROM ".$table.$conditions.$limit;
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 // --------------------------------------------------------------------
581
582 /**
583 * Limit string
584 *
585 * Generates a platform-specific LIMIT clause
586 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400587 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param string the sql query string
589 * @param integer the number of rows to limit the query to
590 * @param integer the offset value
591 * @return string
592 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400593 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200594 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 if ($offset == 0)
596 {
597 $offset = '';
598 }
599 else
600 {
601 $offset .= ", ";
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 return $sql."LIMIT ".$offset.$limit;
605 }
606
607 // --------------------------------------------------------------------
608
609 /**
610 * Close DB Connection
611 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400612 * @access public
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @param resource
614 * @return void
615 */
Timothy Warren691a8e72012-03-19 19:06:00 -0400616 function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
618 @sqlite_close($conn_id);
619 }
Timothy Warren691a8e72012-03-19 19:06:00 -0400620
621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622}
623
Timothy Warren691a8e72012-03-19 19:06:00 -0400624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400626/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */