blob: 102c79bb3a791a40b3e092f3cf2bef44c815377e [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 /**
109 * Select the database
110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200112 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200113 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
115 return TRUE;
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200121 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return string
124 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200125 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200127 return isset($this->data_cache['version'])
128 ? $this->data_cache['version']
129 : $this->data_cache['version'] = sqlite_libversion();
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 // --------------------------------------------------------------------
133
134 /**
135 * Execute the query
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @param string an SQL query
138 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200139 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200140 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 return @sqlite_query($this->conn_id, $sql);
143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
146
147 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * Begin Transaction
149 *
Barry Mienydd671972010-10-04 16:33:58 +0200150 * @return bool
151 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200152 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
154 if ( ! $this->trans_enabled)
155 {
156 return TRUE;
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // When transactions are nested we only begin/commit/rollback the outermost ones
160 if ($this->_trans_depth > 0)
161 {
162 return TRUE;
163 }
164
165 // Reset the transaction failure flag.
166 // If the $test_mode flag is set to TRUE transactions will be rolled back
167 // even if the queries produce a successful result.
168 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
169
170 $this->simple_query('BEGIN TRANSACTION');
171 return TRUE;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Commit Transaction
178 *
Barry Mienydd671972010-10-04 16:33:58 +0200179 * @return bool
180 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200181 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
183 if ( ! $this->trans_enabled)
184 {
185 return TRUE;
186 }
187
188 // When transactions are nested we only begin/commit/rollback the outermost ones
189 if ($this->_trans_depth > 0)
190 {
191 return TRUE;
192 }
193
194 $this->simple_query('COMMIT');
195 return TRUE;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Rollback Transaction
202 *
Barry Mienydd671972010-10-04 16:33:58 +0200203 * @return bool
204 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200205 public function trans_rollback()
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('ROLLBACK');
219 return TRUE;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Escape String
226 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000228 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return string
230 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200231 public function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000233 if (is_array($str))
234 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500235 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200236 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000237 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200238 }
239
240 return $str;
241 }
242
Derek Jonese4ed5832009-02-20 21:44:59 +0000243 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Jonese4ed5832009-02-20 21:44:59 +0000245 // escape LIKE condition wildcards
246 if ($like === TRUE)
247 {
Andrey Andreev94708bd2012-03-26 12:09:28 +0300248 return str_replace(array($this->_like_escape_chr, '%', '_'),
Andrey Andreev830f5af2012-03-26 12:11:38 +0300249 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev94708bd2012-03-26 12:09:28 +0300250 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Jonese4ed5832009-02-20 21:44:59 +0000253 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
257
258 /**
259 * Affected Rows
260 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200261 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200263 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 return sqlite_changes($this->conn_id);
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
271 * Insert ID
272 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200273 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200275 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 return @sqlite_last_insert_rowid($this->conn_id);
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * "Count All" query
284 *
285 * Generates a platform-specific query string that counts all records in
286 * the specified database
287 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @param string
289 * @return string
290 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200291 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000294 {
295 return 0;
296 }
297
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200298 $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 +0000299 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000300 {
301 return 0;
302 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000303
304 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500305 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000306 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * List table query
313 *
314 * Generates a platform-specific query string so that the table names can be fetched
315 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200316 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @return string
318 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200319 protected function _list_tables($prefix_limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $sql = "SELECT name from sqlite_master WHERE type='table'";
322
323 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
324 {
Greg Aker0d424892010-01-26 02:14:44 +0000325 $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 +0000326 }
327 return $sql;
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Show column query
334 *
335 * Generates a platform-specific query string so that the column names can be fetched
336 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200338 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200340 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 // Not supported
343 return FALSE;
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Field data query
350 *
351 * Generates a platform-specific query so that the column data can be retrieved
352 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 * @param string the table name
Andrey Andreev979b5282012-03-20 16:45:39 +0200354 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200356 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
358 return "SELECT * FROM ".$table." LIMIT 1";
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200364 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200366 * Returns an array containing code and message of the last
367 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200369 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200371 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200373 $error = array('code' => sqlite_last_error($this->conn_id));
374 $error['message'] = sqlite_error_string($error['code']);
375 return $error;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Escape the SQL Identifiers
382 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400383 * This function escapes column and table names
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 * @param string
386 * @return string
387 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200388 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 if ($this->_escape_char == '')
391 {
392 return $item;
393 }
394
395 foreach ($this->_reserved_identifiers as $id)
396 {
397 if (strpos($item, '.'.$id) !== FALSE)
398 {
Barry Mienydd671972010-10-04 16:33:58 +0200399 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 // remove duplicates if the user already included the escape
402 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 if (strpos($item, '.') !== FALSE)
407 {
Barry Mienydd671972010-10-04 16:33:58 +0200408 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410 else
411 {
412 $str = $this->_escape_char.$item.$this->_escape_char;
413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 // remove duplicates if the user already included the escape
416 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
420
421 /**
422 * From Tables
423 *
Timothy Warren691a8e72012-03-19 19:06:00 -0400424 * This function implicitly groups FROM tables so there is no confusion
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * about operator precedence in harmony with SQL standards
426 *
Andrey Andreev979b5282012-03-20 16:45:39 +0200427 * @param array
428 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200430 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 if ( ! is_array($tables))
433 {
434 $tables = array($tables);
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 return '('.implode(', ', $tables).')';
438 }
439
440 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 /**
443 * Insert statement
444 *
445 * Generates a platform-specific insert string from the supplied data
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param string the table name
448 * @param array the insert keys
449 * @param array the insert values
450 * @return string
451 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200452 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200453 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
455 }
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 // --------------------------------------------------------------------
458
459 /**
460 * Update statement
461 *
462 * Generates a platform-specific update string from the supplied data
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string the table name
465 * @param array the update data
466 * @param array the where clause
467 * @param array the orderby clause
468 * @param array the limit clause
469 * @return string
470 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200471 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500473 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $valstr[] = $key." = ".$val;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
483
484 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
485
486 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 return $sql;
489 }
490
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // --------------------------------------------------------------------
493
494 /**
495 * Truncate statement
496 *
497 * Generates a platform-specific truncate string from the supplied data
498 * If the database does not support the truncate() command
Timothy Warren691a8e72012-03-19 19:06:00 -0400499 * This function maps to "DELETE FROM table"
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @param string the table name
502 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200504 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 return $this->_delete($table);
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
511 /**
512 * Delete statement
513 *
514 * Generates a platform-specific delete string from the supplied data
515 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 * @param string the table name
517 * @param array the where clause
518 * @param string the limit clause
519 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200520 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200521 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $conditions = '';
524
525 if (count($where) > 0 OR count($like) > 0)
526 {
527 $conditions = "\nWHERE ";
528 $conditions .= implode("\n", $this->ar_where);
529
530 if (count($where) > 0 && count($like) > 0)
531 {
532 $conditions .= " AND ";
533 }
534 $conditions .= implode("\n", $like);
535 }
536
537 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 return "DELETE FROM ".$table.$conditions.$limit;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
543
544 /**
545 * Limit string
546 *
547 * Generates a platform-specific LIMIT clause
548 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @param string the sql query string
Andrey Andreev979b5282012-03-20 16:45:39 +0200550 * @param int the number of rows to limit the query to
551 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 * @return string
553 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200554 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200555 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 if ($offset == 0)
557 {
558 $offset = '';
559 }
560 else
561 {
562 $offset .= ", ";
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 return $sql."LIMIT ".$offset.$limit;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Close DB Connection
572 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * @param resource
574 * @return void
575 */
Andrey Andreev979b5282012-03-20 16:45:39 +0200576 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
578 @sqlite_close($conn_id);
579 }
Timothy Warren691a8e72012-03-19 19:06:00 -0400580
Derek Allard2067d1a2008-11-13 22:59:24 +0000581}
582
Derek Allard2067d1a2008-11-13 22:59:24 +0000583/* End of file sqlite_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400584/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */