blob: bb86c2296028abbd388db2cc2c8036488f5fe930 [file] [log] [blame]
Andrey Andreev979b5282012-03-20 16:45:39 +02001<?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
Andrey Andreev979b5282012-03-20 16:45:39 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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
Timothy Warren691a8e72012-03-19 19:06:00 -040055 * used for the count_all() and count_all_results() functions.
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Andrey Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +0200116 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 {
118 // not implemented in SQLite
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * Select the database
125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200127 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200128 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 return TRUE;
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200136 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @return string
139 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200140 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200142 return isset($this->data_cache['version'])
143 ? $this->data_cache['version']
144 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 // --------------------------------------------------------------------
148
149 /**
150 * Execute the query
151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @param string an SQL query
153 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200154 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200155 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 return @sqlite_query($this->conn_id, $sql);
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // --------------------------------------------------------------------
161
162 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * Begin Transaction
164 *
Barry Mienydd671972010-10-04 16:33:58 +0200165 * @return bool
166 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200167 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 if ( ! $this->trans_enabled)
170 {
171 return TRUE;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 // Reset the transaction failure flag.
181 // If the $test_mode flag is set to TRUE transactions will be rolled back
182 // even if the queries produce a successful result.
183 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
184
185 $this->simple_query('BEGIN TRANSACTION');
186 return TRUE;
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Commit Transaction
193 *
Barry Mienydd671972010-10-04 16:33:58 +0200194 * @return bool
195 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200196 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 if ( ! $this->trans_enabled)
199 {
200 return TRUE;
201 }
202
203 // When transactions are nested we only begin/commit/rollback the outermost ones
204 if ($this->_trans_depth > 0)
205 {
206 return TRUE;
207 }
208
209 $this->simple_query('COMMIT');
210 return TRUE;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Rollback Transaction
217 *
Barry Mienydd671972010-10-04 16:33:58 +0200218 * @return bool
219 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200220 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 if ( ! $this->trans_enabled)
223 {
224 return TRUE;
225 }
226
227 // When transactions are nested we only begin/commit/rollback the outermost ones
228 if ($this->_trans_depth > 0)
229 {
230 return TRUE;
231 }
232
233 $this->simple_query('ROLLBACK');
234 return TRUE;
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 // --------------------------------------------------------------------
238
239 /**
240 * Escape String
241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000243 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @return string
245 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200246 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000248 if (is_array($str))
249 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500250 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200251 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000252 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200253 }
254
255 return $str;
256 }
257
Derek Jonese4ed5832009-02-20 21:44:59 +0000258 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Jonese4ed5832009-02-20 21:44:59 +0000260 // escape LIKE condition wildcards
261 if ($like === TRUE)
262 {
263 $str = str_replace( array('%', '_', $this->_like_escape_chr),
264 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
265 $str);
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Jonese4ed5832009-02-20 21:44:59 +0000268 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
272
273 /**
274 * Affected Rows
275 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200276 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200278 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 return sqlite_changes($this->conn_id);
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // --------------------------------------------------------------------
284
285 /**
286 * Insert ID
287 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200288 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200290 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 return @sqlite_last_insert_rowid($this->conn_id);
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
298 * "Count All" query
299 *
300 * Generates a platform-specific query string that counts all records in
301 * the specified database
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @param string
304 * @return string
305 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200306 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000309 {
310 return 0;
311 }
312
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200313 $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 +0000314 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000315 {
316 return 0;
317 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000318
319 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500320 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000321 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * List table query
328 *
329 * Generates a platform-specific query string so that the table names can be fetched
330 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200331 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 * @return string
333 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200334 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $sql = "SELECT name from sqlite_master WHERE type='table'";
337
338 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
339 {
Greg Aker0d424892010-01-26 02:14:44 +0000340 $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 +0000341 }
342 return $sql;
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Show column query
349 *
350 * Generates a platform-specific query string so that the column names can be fetched
351 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200353 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200355 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
357 // Not supported
358 return FALSE;
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Field data query
365 *
366 * Generates a platform-specific query so that the column data can be retrieved
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200369 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200371 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 return "SELECT * FROM ".$table." LIMIT 1";
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200379 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200381 * Returns an array containing code and message of the last
382 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200384 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200386 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 $error = array('code' => sqlite_last_error($this->conn_id));
389 $error['message'] = sqlite_error_string($error['code']);
390 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * Escape the SQL Identifiers
397 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400398 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @param string
401 * @return string
402 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200403 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
405 if ($this->_escape_char == '')
406 {
407 return $item;
408 }
409
410 foreach ($this->_reserved_identifiers as $id)
411 {
412 if (strpos($item, '.'.$id) !== FALSE)
413 {
Barry Mienydd671972010-10-04 16:33:58 +0200414 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // remove duplicates if the user already included the escape
417 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200418 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 if (strpos($item, '.') !== FALSE)
422 {
Barry Mienydd671972010-10-04 16:33:58 +0200423 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425 else
426 {
427 $str = $this->_escape_char.$item.$this->_escape_char;
428 }
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
431 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
435
436 /**
437 * From Tables
438 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400439 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * about operator precedence in harmony with SQL standards
441 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200442 * @param array
443 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200445 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 if ( ! is_array($tables))
448 {
449 $tables = array($tables);
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 return '('.implode(', ', $tables).')';
453 }
454
455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 /**
458 * Insert statement
459 *
460 * Generates a platform-specific insert string from the supplied data
461 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @param string the table name
463 * @param array the insert keys
464 * @param array the insert values
465 * @return string
466 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200467 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200468 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // --------------------------------------------------------------------
473
474 /**
475 * Update statement
476 *
477 * Generates a platform-specific update string from the supplied data
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @param string the table name
480 * @param array the update data
481 * @param array the where clause
482 * @param array the orderby clause
483 * @param array the limit clause
484 * @return string
485 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200486 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500488 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
490 $valstr[] = $key." = ".$val;
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
498
499 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
500
501 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return $sql;
504 }
505
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 // --------------------------------------------------------------------
508
509 /**
510 * Truncate statement
511 *
512 * Generates a platform-specific truncate string from the supplied data
513 * If the database does not support the truncate() command
Timothy Warren691a8e72012-03-19 19:06:00 -0400514 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @param string the table name
517 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200518 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200519 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 return $this->_delete($table);
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 // --------------------------------------------------------------------
525
526 /**
527 * Delete statement
528 *
529 * Generates a platform-specific delete string from the supplied data
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string the table name
532 * @param array the where clause
533 * @param string the limit clause
534 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200535 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200536 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
538 $conditions = '';
539
540 if (count($where) > 0 OR count($like) > 0)
541 {
542 $conditions = "\nWHERE ";
543 $conditions .= implode("\n", $this->ar_where);
544
545 if (count($where) > 0 && count($like) > 0)
546 {
547 $conditions .= " AND ";
548 }
549 $conditions .= implode("\n", $like);
550 }
551
552 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 return "DELETE FROM ".$table.$conditions.$limit;
555 }
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 Andreev979b5282012-03-20 16:45:39 +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 Andreev979b5282012-03-20 16:45:39 +0200569 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200570 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 if ($offset == 0)
572 {
573 $offset = '';
574 }
575 else
576 {
577 $offset .= ", ";
578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return $sql."LIMIT ".$offset.$limit;
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Close DB Connection
587 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * @param resource
589 * @return void
590 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200591 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 @sqlite_close($conn_id);
594 }
Timothy Warren691a8e72012-03-19 19:06:00 -0400595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596}
597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400599/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */