blob: 718501b202aa66b1b9780e64df7b7fc4243101f1 [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 *
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
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
30
31
32/**
33 * SQLite Database Adapter Class
34 *
35 * Note: _DB is an extender class that the app controller
36 * creates dynamically based on whether the active record
37 * class is being used or not.
38 *
39 * @package CodeIgniter
40 * @subpackage Drivers
41 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050042 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @link http://codeigniter.com/user_guide/database/
44 */
45class CI_DB_sqlite_driver extends CI_DB {
46
47 var $dbdriver = 'sqlite';
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 // The character used to escape with - not needed for SQLite
50 var $_escape_char = '';
51
Derek Jonese4ed5832009-02-20 21:44:59 +000052 // clause and character used for LIKE escape sequences
Barry Mienydd671972010-10-04 16:33:58 +020053 var $_like_escape_str = " ESCAPE '%s' ";
Derek Jonese4ed5832009-02-20 21:44:59 +000054 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 /**
57 * The syntax to count rows is slightly different across different
58 * database engines, so this string appears in each driver and is
59 * used for the count_all() and count_all_results() functions.
60 */
61 var $_count_string = "SELECT COUNT(*) AS ";
62 var $_random_keyword = ' Random()'; // database specific random keyword
63
64 /**
65 * Non-persistent database connection
66 *
67 * @access private called by the base class
68 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020069 */
Derek Allard2067d1a2008-11-13 22:59:24 +000070 function db_connect()
71 {
72 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
73 {
74 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 if ($this->db_debug)
77 {
Derek Allardfac8fbc2010-02-05 16:14:49 +000078 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +000079 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 return FALSE;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 return $conn_id;
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
90 * Persistent database connection
91 *
92 * @access private called by the base class
93 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020094 */
Derek Allard2067d1a2008-11-13 22:59:24 +000095 function db_pconnect()
96 {
97 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
98 {
99 log_message('error', $error);
Barry Mienydd671972010-10-04 16:33:58 +0200100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 if ($this->db_debug)
102 {
Derek Allardfac8fbc2010-02-05 16:14:49 +0000103 $this->display_error($error, '', TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 return FALSE;
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 return $conn_id;
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // --------------------------------------------------------------------
113
114 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000115 * Reconnect
116 *
117 * Keep / reestablish the db connection if no queries have been
118 * sent for a length of time exceeding the server's idle timeout
119 *
120 * @access public
121 * @return void
122 */
123 function reconnect()
124 {
125 // not implemented in SQLite
126 }
127
128 // --------------------------------------------------------------------
129
130 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * Select the database
132 *
133 * @access private called by the base class
134 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 function db_select()
137 {
138 return TRUE;
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * Version number query string
145 *
146 * @access public
147 * @return string
148 */
149 function _version()
150 {
151 return sqlite_libversion();
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // --------------------------------------------------------------------
155
156 /**
157 * Execute the query
158 *
159 * @access private called by the base class
160 * @param string an SQL query
161 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200162 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 function _execute($sql)
164 {
165 $sql = $this->_prep_query($sql);
166 return @sqlite_query($this->conn_id, $sql);
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
170
171 /**
172 * Prep the query
173 *
174 * If needed, each database adapter can prep the query string
175 *
176 * @access private called by execute()
177 * @param string an SQL query
178 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 function _prep_query($sql)
181 {
182 return $sql;
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Begin Transaction
189 *
190 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200191 * @return bool
192 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 function trans_begin($test_mode = FALSE)
194 {
195 if ( ! $this->trans_enabled)
196 {
197 return TRUE;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // When transactions are nested we only begin/commit/rollback the outermost ones
201 if ($this->_trans_depth > 0)
202 {
203 return TRUE;
204 }
205
206 // Reset the transaction failure flag.
207 // If the $test_mode flag is set to TRUE transactions will be rolled back
208 // even if the queries produce a successful result.
209 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
210
211 $this->simple_query('BEGIN TRANSACTION');
212 return TRUE;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Commit Transaction
219 *
220 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200221 * @return bool
222 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 function trans_commit()
224 {
225 if ( ! $this->trans_enabled)
226 {
227 return TRUE;
228 }
229
230 // When transactions are nested we only begin/commit/rollback the outermost ones
231 if ($this->_trans_depth > 0)
232 {
233 return TRUE;
234 }
235
236 $this->simple_query('COMMIT');
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Rollback Transaction
244 *
245 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200246 * @return bool
247 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 function trans_rollback()
249 {
250 if ( ! $this->trans_enabled)
251 {
252 return TRUE;
253 }
254
255 // When transactions are nested we only begin/commit/rollback the outermost ones
256 if ($this->_trans_depth > 0)
257 {
258 return TRUE;
259 }
260
261 $this->simple_query('ROLLBACK');
262 return TRUE;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // --------------------------------------------------------------------
266
267 /**
268 * Escape String
269 *
270 * @access public
271 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000272 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
274 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000275 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 if (is_array($str))
278 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500279 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200280 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200282 }
283
284 return $str;
285 }
286
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 $str = sqlite_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 // escape LIKE condition wildcards
290 if ($like === TRUE)
291 {
292 $str = str_replace( array('%', '_', $this->_like_escape_chr),
293 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
294 $str);
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Affected Rows
304 *
305 * @access public
306 * @return integer
307 */
308 function affected_rows()
309 {
310 return sqlite_changes($this->conn_id);
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
316 * Insert ID
317 *
318 * @access public
319 * @return integer
320 */
321 function insert_id()
322 {
323 return @sqlite_last_insert_rowid($this->conn_id);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * "Count All" query
330 *
331 * Generates a platform-specific query string that counts all records in
332 * the specified database
333 *
334 * @access public
335 * @param string
336 * @return string
337 */
338 function count_all($table = '')
339 {
340 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000341 {
342 return 0;
343 }
344
345 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000348 {
349 return 0;
350 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000351
352 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500353 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000354 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * List table query
361 *
362 * Generates a platform-specific query string so that the table names can be fetched
363 *
364 * @access private
365 * @param boolean
366 * @return string
367 */
368 function _list_tables($prefix_limit = FALSE)
369 {
370 $sql = "SELECT name from sqlite_master WHERE type='table'";
371
372 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
373 {
Greg Aker0d424892010-01-26 02:14:44 +0000374 $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 +0000375 }
376 return $sql;
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Show column query
383 *
384 * Generates a platform-specific query string so that the column names can be fetched
385 *
386 * @access public
387 * @param string the table name
388 * @return string
389 */
390 function _list_columns($table = '')
391 {
392 // Not supported
393 return FALSE;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Field data query
400 *
401 * Generates a platform-specific query so that the column data can be retrieved
402 *
403 * @access public
404 * @param string the table name
405 * @return object
406 */
407 function _field_data($table)
408 {
409 return "SELECT * FROM ".$table." LIMIT 1";
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * The error message string
416 *
417 * @access private
418 * @return string
419 */
420 function _error_message()
421 {
422 return sqlite_error_string(sqlite_last_error($this->conn_id));
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // --------------------------------------------------------------------
426
427 /**
428 * The error message number
429 *
430 * @access private
431 * @return integer
432 */
433 function _error_number()
434 {
435 return sqlite_last_error($this->conn_id);
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Escape the SQL Identifiers
442 *
443 * This function escapes column and table names
444 *
445 * @access private
446 * @param string
447 * @return string
448 */
449 function _escape_identifiers($item)
450 {
451 if ($this->_escape_char == '')
452 {
453 return $item;
454 }
455
456 foreach ($this->_reserved_identifiers as $id)
457 {
458 if (strpos($item, '.'.$id) !== FALSE)
459 {
Barry Mienydd671972010-10-04 16:33:58 +0200460 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // remove duplicates if the user already included the escape
463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200464 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 if (strpos($item, '.') !== FALSE)
468 {
Barry Mienydd671972010-10-04 16:33:58 +0200469 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 }
471 else
472 {
473 $str = $this->_escape_char.$item.$this->_escape_char;
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // remove duplicates if the user already included the escape
477 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
478 }
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // --------------------------------------------------------------------
481
482 /**
483 * From Tables
484 *
485 * This function implicitly groups FROM tables so there is no confusion
486 * about operator precedence in harmony with SQL standards
487 *
488 * @access public
489 * @param type
490 * @return type
491 */
492 function _from_tables($tables)
493 {
494 if ( ! is_array($tables))
495 {
496 $tables = array($tables);
497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return '('.implode(', ', $tables).')';
500 }
501
502 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 /**
505 * Insert statement
506 *
507 * Generates a platform-specific insert string from the supplied data
508 *
509 * @access public
510 * @param string the table name
511 * @param array the insert keys
512 * @param array the insert values
513 * @return string
514 */
515 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200516 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
521
522 /**
523 * Update statement
524 *
525 * Generates a platform-specific update string from the supplied data
526 *
527 * @access public
528 * @param string the table name
529 * @param array the update data
530 * @param array the where clause
531 * @param array the orderby clause
532 * @param array the limit clause
533 * @return string
534 */
535 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
536 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500537 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
539 $valstr[] = $key." = ".$val;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
547
548 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
549
550 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 return $sql;
553 }
554
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
559 * Truncate statement
560 *
561 * Generates a platform-specific truncate string from the supplied data
562 * If the database does not support the truncate() command
563 * This function maps to "DELETE FROM table"
564 *
565 * @access public
566 * @param string the table name
567 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200568 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 function _truncate($table)
570 {
571 return $this->_delete($table);
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 // --------------------------------------------------------------------
575
576 /**
577 * Delete statement
578 *
579 * Generates a platform-specific delete string from the supplied data
580 *
581 * @access public
582 * @param string the table name
583 * @param array the where clause
584 * @param string the limit clause
585 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200586 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
588 {
589 $conditions = '';
590
591 if (count($where) > 0 OR count($like) > 0)
592 {
593 $conditions = "\nWHERE ";
594 $conditions .= implode("\n", $this->ar_where);
595
596 if (count($where) > 0 && count($like) > 0)
597 {
598 $conditions .= " AND ";
599 }
600 $conditions .= implode("\n", $like);
601 }
602
603 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 return "DELETE FROM ".$table.$conditions.$limit;
606 }
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 // --------------------------------------------------------------------
609
610 /**
611 * Limit string
612 *
613 * Generates a platform-specific LIMIT clause
614 *
615 * @access public
616 * @param string the sql query string
617 * @param integer the number of rows to limit the query to
618 * @param integer the offset value
619 * @return string
620 */
621 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200622 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 if ($offset == 0)
624 {
625 $offset = '';
626 }
627 else
628 {
629 $offset .= ", ";
630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 return $sql."LIMIT ".$offset.$limit;
633 }
634
635 // --------------------------------------------------------------------
636
637 /**
638 * Close DB Connection
639 *
640 * @access public
641 * @param resource
642 * @return void
643 */
644 function _close($conn_id)
645 {
646 @sqlite_close($conn_id);
647 }
648
649
650}
651
652
653/* End of file sqlite_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200654/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */