blob: df19dba7885fbc547d58985bd420f80c522c9949 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis37b3ecf2008-09-12 23:34:18 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18
19
20/**
21 * SQLite Database Adapter Class
22 *
23 * Note: _DB is an extender class that the app controller
24 * creates dynamically based on whether the active record
25 * class is being used or not.
26 *
27 * @package CodeIgniter
28 * @subpackage Drivers
29 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000030 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000031 * @link http://codeigniter.com/user_guide/database/
Derek Allardd2df9bc2007-04-15 17:41:17 +000032 */
33class CI_DB_sqlite_driver extends CI_DB {
34
Rick Ellis5aa8c602008-10-07 01:24:07 +000035 var $dbdriver = 'sqlite';
36
Derek Allardd2df9bc2007-04-15 17:41:17 +000037 /**
Derek Allard694b5b82007-12-18 15:58:03 +000038 * The syntax to count rows is slightly different across different
39 * database engines, so this string appears in each driver and is
40 * used for the count_all() and count_all_results() functions.
41 */
Derek Allard39b622d2008-01-16 21:10:09 +000042 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000043 var $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard694b5b82007-12-18 15:58:03 +000044
45 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000046 * Non-persistent database connection
47 *
48 * @access private called by the base class
49 * @return resource
50 */
51 function db_connect()
52 {
Derek Jones0b59f272008-05-13 04:22:33 +000053 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000054 {
55 log_message('error', $error);
56
57 if ($this->db_debug)
58 {
59 $this->display_error($error, '', TRUE);
60 }
61
62 return FALSE;
63 }
64
65 return $conn_id;
66 }
67
68 // --------------------------------------------------------------------
69
70 /**
71 * Persistent database connection
72 *
73 * @access private called by the base class
74 * @return resource
75 */
76 function db_pconnect()
77 {
Derek Jones0b59f272008-05-13 04:22:33 +000078 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000079 {
80 log_message('error', $error);
81
82 if ($this->db_debug)
83 {
84 $this->display_error($error, '', TRUE);
85 }
86
87 return FALSE;
88 }
89
90 return $conn_id;
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
96 * Select the database
97 *
98 * @access private called by the base class
99 * @return resource
100 */
101 function db_select()
102 {
103 return TRUE;
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000109 * Set client character set
110 *
111 * @access public
112 * @param string
113 * @param string
114 * @return resource
115 */
116 function db_set_charset($charset, $collation)
117 {
Rick Ellisff734012008-09-30 20:38:12 +0000118 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +0000119 return TRUE;
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000125 * Version number query string
126 *
127 * @access public
128 * @return string
129 */
130 function _version()
131 {
132 return sqlite_libversion();
133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Execute the query
139 *
140 * @access private called by the base class
141 * @param string an SQL query
142 * @return resource
143 */
144 function _execute($sql)
145 {
146 $sql = $this->_prep_query($sql);
147 return @sqlite_query($this->conn_id, $sql);
148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Prep the query
154 *
155 * If needed, each database adapter can prep the query string
156 *
157 * @access private called by execute()
158 * @param string an SQL query
159 * @return string
160 */
161 function _prep_query($sql)
162 {
163 return $sql;
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Begin Transaction
170 *
171 * @access public
172 * @return bool
173 */
174 function trans_begin($test_mode = FALSE)
175 {
Derek Jones0b59f272008-05-13 04:22:33 +0000176 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000177 {
178 return TRUE;
179 }
180
181 // When transactions are nested we only begin/commit/rollback the outermost ones
182 if ($this->_trans_depth > 0)
183 {
184 return TRUE;
185 }
186
187 // Reset the transaction failure flag.
188 // If the $test_mode flag is set to TRUE transactions will be rolled back
189 // even if the queries produce a successful result.
190 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
191
192 $this->simple_query('BEGIN TRANSACTION');
193 return TRUE;
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Commit Transaction
200 *
201 * @access public
202 * @return bool
203 */
204 function trans_commit()
205 {
Derek Jones0b59f272008-05-13 04:22:33 +0000206 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000207 {
208 return TRUE;
209 }
210
211 // When transactions are nested we only begin/commit/rollback the outermost ones
212 if ($this->_trans_depth > 0)
213 {
214 return TRUE;
215 }
216
217 $this->simple_query('COMMIT');
218 return TRUE;
219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Rollback Transaction
225 *
226 * @access public
227 * @return bool
228 */
229 function trans_rollback()
230 {
Derek Jones0b59f272008-05-13 04:22:33 +0000231 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000232 {
233 return TRUE;
234 }
235
236 // When transactions are nested we only begin/commit/rollback the outermost ones
237 if ($this->_trans_depth > 0)
238 {
239 return TRUE;
240 }
241
242 $this->simple_query('ROLLBACK');
243 return TRUE;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Escape String
250 *
251 * @access public
252 * @param string
253 * @return string
254 */
255 function escape_str($str)
256 {
257 return sqlite_escape_string($str);
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Affected Rows
264 *
265 * @access public
266 * @return integer
267 */
268 function affected_rows()
269 {
270 return sqlite_changes($this->conn_id);
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Insert ID
277 *
278 * @access public
279 * @return integer
280 */
281 function insert_id()
282 {
283 return @sqlite_last_insert_rowid($this->conn_id);
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * "Count All" query
290 *
291 * Generates a platform-specific query string that counts all records in
292 * the specified database
293 *
294 * @access public
295 * @param string
296 * @return string
297 */
298 function count_all($table = '')
299 {
300 if ($table == '')
301 return '0';
302
Derek Allardf6cd45c2008-01-18 14:31:51 +0000303 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000304
305 if ($query->num_rows() == 0)
306 return '0';
307
308 $row = $query->row();
309 return $row->numrows;
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * List table query
316 *
317 * Generates a platform-specific query string so that the table names can be fetched
318 *
319 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000320 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000321 * @return string
322 */
Derek Allard39b622d2008-01-16 21:10:09 +0000323 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000324 {
Derek Allard39b622d2008-01-16 21:10:09 +0000325 $sql = "SELECT name from sqlite_master WHERE type='table'";
326
327 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
328 {
329 $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
330 }
331 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Show column query
338 *
339 * Generates a platform-specific query string so that the column names can be fetched
340 *
341 * @access public
342 * @param string the table name
343 * @return string
344 */
345 function _list_columns($table = '')
346 {
347 // Not supported
348 return FALSE;
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Field data query
355 *
356 * Generates a platform-specific query so that the column data can be retrieved
357 *
358 * @access public
359 * @param string the table name
360 * @return object
361 */
362 function _field_data($table)
363 {
364 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * The error message string
371 *
372 * @access private
373 * @return string
374 */
375 function _error_message()
376 {
377 return sqlite_error_string(sqlite_last_error($this->conn_id));
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * The error message number
384 *
385 * @access private
386 * @return integer
387 */
388 function _error_number()
389 {
390 return sqlite_last_error($this->conn_id);
391 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000392
393 // --------------------------------------------------------------------
394
395 /**
396 * Escape Column Name
397 *
398 * This function adds backticks around supplied column name
399 *
400 * @access private
401 * @param string the column name
402 * @return string
403 */
404 function _escape_column($column)
405 {
406 // Not necessary with SQLite so we simply return the value
407 return $column;
408 }
409
Derek Allardd2df9bc2007-04-15 17:41:17 +0000410 // --------------------------------------------------------------------
411
412 /**
413 * Escape Table Name
414 *
415 * This function adds backticks if the table name has a period
416 * in it. Some DBs will get cranky unless periods are escaped
417 *
418 * @access private
419 * @param string the table name
420 * @return string
421 */
422 function _escape_table($table)
423 {
Derek Allard7555ade2008-02-10 23:40:41 +0000424 // other database drivers use this to add backticks, hence this
425 // function is simply going to return the tablename for sqlite
Derek Allardd2df9bc2007-04-15 17:41:17 +0000426 return $table;
427 }
428
429 // --------------------------------------------------------------------
430
431 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000432 * Protect Identifiers
433 *
434 * This function adds backticks if appropriate based on db type
435 *
436 * @access private
437 * @param mixed the item to escape
438 * @param boolean only affect the first word
439 * @return mixed the item with backticks
440 */
441 function _protect_identifiers($item, $first_word_only = FALSE)
442 {
443 if (is_array($item))
444 {
445 $escaped_array = array();
446
447 foreach($item as $k=>$v)
448 {
449 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
450 }
451
452 return $escaped_array;
453 }
454
455 // This function may get "item1 item2" as a string, and so
Derek Allard15648132008-02-10 21:46:18 +0000456 // we may need "item1 item2" and not "item1 item2"
Derek Allard61579382008-01-16 22:22:42 +0000457 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000458 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000459 if (strpos($item, '.') !== FALSE)
460 {
461 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
462 $table_name = substr($item, 0, strpos($item, '.')+1);
463 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
464 }
465
Derek Allard15648132008-02-10 21:46:18 +0000466 // This function may get "field >= 1", and need it to return "field >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000467 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000468
Derek Allard15648132008-02-10 21:46:18 +0000469 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1$2$3', $item);
Derek Allard61579382008-01-16 22:22:42 +0000470 }
471 else
472 {
Derek Allard15648132008-02-10 21:46:18 +0000473 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000474 }
475
Derek Allard9a4d1da2008-02-25 14:18:38 +0000476 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000477
478 foreach ($exceptions as $exception)
479 {
Derek Allard61579382008-01-16 22:22:42 +0000480
Derek Allard15648132008-02-10 21:46:18 +0000481 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000482 {
Derek Allard15648132008-02-10 21:46:18 +0000483 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000484 }
485 }
Derek Allard39b622d2008-01-16 21:10:09 +0000486 return $item;
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000492 * From Tables
493 *
494 * This function implicitly groups FROM tables so there is no confusion
495 * about operator precedence in harmony with SQL standards
496 *
497 * @access public
498 * @param type
499 * @return type
500 */
501 function _from_tables($tables)
502 {
Derek Jones0b59f272008-05-13 04:22:33 +0000503 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000504 {
505 $tables = array($tables);
506 }
507
508 return '('.implode(', ', $tables).')';
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000514 * Insert statement
515 *
516 * Generates a platform-specific insert string from the supplied data
517 *
518 * @access public
519 * @param string the table name
520 * @param array the insert keys
521 * @param array the insert values
522 * @return string
523 */
524 function _insert($table, $keys, $values)
525 {
526 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * Update statement
533 *
534 * Generates a platform-specific update string from the supplied data
535 *
536 * @access public
537 * @param string the table name
538 * @param array the update data
539 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000540 * @param array the orderby clause
541 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000542 * @return string
543 */
Derek Allard39b622d2008-01-16 21:10:09 +0000544 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000545 {
546 foreach($values as $key => $val)
547 {
548 $valstr[] = $key." = ".$val;
549 }
Derek Allardda6d2402007-12-19 14:49:29 +0000550
Derek Jones0b59f272008-05-13 04:22:33 +0000551 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000552
553 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000554
Derek Allard32cf7eb2008-02-05 16:03:50 +0000555 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
556 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
557 $sql .= $orderby.$limit;
558
559 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000560 }
561
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Truncate statement
567 *
568 * Generates a platform-specific truncate string from the supplied data
569 * If the database does not support the truncate() command
570 * This function maps to "DELETE FROM table"
571 *
572 * @access public
573 * @param string the table name
574 * @return string
575 */
576 function _truncate($table)
577 {
578 return $this->_delete($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000579 }
580
581 // --------------------------------------------------------------------
582
583 /**
584 * Delete statement
585 *
586 * Generates a platform-specific delete string from the supplied data
587 *
588 * @access public
589 * @param string the table name
590 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000591 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000592 * @return string
593 */
Derek Allard39b622d2008-01-16 21:10:09 +0000594 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000595 {
Derek Allard39b622d2008-01-16 21:10:09 +0000596 $conditions = '';
597
Derek Jones0b59f272008-05-13 04:22:33 +0000598 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000599 {
600 $conditions = "\nWHERE ";
601 $conditions .= implode("\n", $this->ar_where);
602
603 if (count($where) > 0 && count($like) > 0)
604 {
605 $conditions .= " AND ";
606 }
607 $conditions .= implode("\n", $like);
608 }
609
Derek Jones0b59f272008-05-13 04:22:33 +0000610 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000611
Derek Allard39b622d2008-01-16 21:10:09 +0000612 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000613 }
Derek Allard39b622d2008-01-16 21:10:09 +0000614
Derek Allardd2df9bc2007-04-15 17:41:17 +0000615 // --------------------------------------------------------------------
616
617 /**
618 * Limit string
619 *
620 * Generates a platform-specific LIMIT clause
621 *
622 * @access public
623 * @param string the sql query string
624 * @param integer the number of rows to limit the query to
625 * @param integer the offset value
626 * @return string
627 */
628 function _limit($sql, $limit, $offset)
629 {
630 if ($offset == 0)
631 {
632 $offset = '';
633 }
634 else
635 {
636 $offset .= ", ";
637 }
638
639 return $sql."LIMIT ".$offset.$limit;
640 }
641
642 // --------------------------------------------------------------------
643
644 /**
645 * Close DB Connection
646 *
647 * @access public
648 * @param resource
649 * @return void
650 */
651 function _close($conn_id)
652 {
653 @sqlite_close($conn_id);
654 }
655
656
657}
658
Derek Jones0b59f272008-05-13 04:22:33 +0000659
660/* End of file sqlite_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000661/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */