blob: 5cac04dfaefd9abd121f73d6fe83b3b86f8b9d75 [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
35 /**
Derek Allard694b5b82007-12-18 15:58:03 +000036 * The syntax to count rows is slightly different across different
37 * database engines, so this string appears in each driver and is
38 * used for the count_all() and count_all_results() functions.
39 */
Derek Allard39b622d2008-01-16 21:10:09 +000040 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000041 var $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard694b5b82007-12-18 15:58:03 +000042
43 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000044 * Non-persistent database connection
45 *
46 * @access private called by the base class
47 * @return resource
48 */
49 function db_connect()
50 {
Derek Jones0b59f272008-05-13 04:22:33 +000051 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000052 {
53 log_message('error', $error);
54
55 if ($this->db_debug)
56 {
57 $this->display_error($error, '', TRUE);
58 }
59
60 return FALSE;
61 }
62
63 return $conn_id;
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Persistent database connection
70 *
71 * @access private called by the base class
72 * @return resource
73 */
74 function db_pconnect()
75 {
Derek Jones0b59f272008-05-13 04:22:33 +000076 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000077 {
78 log_message('error', $error);
79
80 if ($this->db_debug)
81 {
82 $this->display_error($error, '', TRUE);
83 }
84
85 return FALSE;
86 }
87
88 return $conn_id;
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Select the database
95 *
96 * @access private called by the base class
97 * @return resource
98 */
99 function db_select()
100 {
101 return TRUE;
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000107 * Set client character set
108 *
109 * @access public
110 * @param string
111 * @param string
112 * @return resource
113 */
114 function db_set_charset($charset, $collation)
115 {
116 // TODO - add support if needed
117 return TRUE;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000123 * Version number query string
124 *
125 * @access public
126 * @return string
127 */
128 function _version()
129 {
130 return sqlite_libversion();
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Execute the query
137 *
138 * @access private called by the base class
139 * @param string an SQL query
140 * @return resource
141 */
142 function _execute($sql)
143 {
144 $sql = $this->_prep_query($sql);
145 return @sqlite_query($this->conn_id, $sql);
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Prep the query
152 *
153 * If needed, each database adapter can prep the query string
154 *
155 * @access private called by execute()
156 * @param string an SQL query
157 * @return string
158 */
159 function _prep_query($sql)
160 {
161 return $sql;
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Begin Transaction
168 *
169 * @access public
170 * @return bool
171 */
172 function trans_begin($test_mode = FALSE)
173 {
Derek Jones0b59f272008-05-13 04:22:33 +0000174 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000175 {
176 return TRUE;
177 }
178
179 // When transactions are nested we only begin/commit/rollback the outermost ones
180 if ($this->_trans_depth > 0)
181 {
182 return TRUE;
183 }
184
185 // Reset the transaction failure flag.
186 // If the $test_mode flag is set to TRUE transactions will be rolled back
187 // even if the queries produce a successful result.
188 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
189
190 $this->simple_query('BEGIN TRANSACTION');
191 return TRUE;
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Commit Transaction
198 *
199 * @access public
200 * @return bool
201 */
202 function trans_commit()
203 {
Derek Jones0b59f272008-05-13 04:22:33 +0000204 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000205 {
206 return TRUE;
207 }
208
209 // When transactions are nested we only begin/commit/rollback the outermost ones
210 if ($this->_trans_depth > 0)
211 {
212 return TRUE;
213 }
214
215 $this->simple_query('COMMIT');
216 return TRUE;
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Rollback Transaction
223 *
224 * @access public
225 * @return bool
226 */
227 function trans_rollback()
228 {
Derek Jones0b59f272008-05-13 04:22:33 +0000229 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000230 {
231 return TRUE;
232 }
233
234 // When transactions are nested we only begin/commit/rollback the outermost ones
235 if ($this->_trans_depth > 0)
236 {
237 return TRUE;
238 }
239
240 $this->simple_query('ROLLBACK');
241 return TRUE;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Escape String
248 *
249 * @access public
250 * @param string
251 * @return string
252 */
253 function escape_str($str)
254 {
255 return sqlite_escape_string($str);
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Affected Rows
262 *
263 * @access public
264 * @return integer
265 */
266 function affected_rows()
267 {
268 return sqlite_changes($this->conn_id);
269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Insert ID
275 *
276 * @access public
277 * @return integer
278 */
279 function insert_id()
280 {
281 return @sqlite_last_insert_rowid($this->conn_id);
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * "Count All" query
288 *
289 * Generates a platform-specific query string that counts all records in
290 * the specified database
291 *
292 * @access public
293 * @param string
294 * @return string
295 */
296 function count_all($table = '')
297 {
298 if ($table == '')
299 return '0';
300
Derek Allardf6cd45c2008-01-18 14:31:51 +0000301 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000302
303 if ($query->num_rows() == 0)
304 return '0';
305
306 $row = $query->row();
307 return $row->numrows;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * List table query
314 *
315 * Generates a platform-specific query string so that the table names can be fetched
316 *
317 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000318 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000319 * @return string
320 */
Derek Allard39b622d2008-01-16 21:10:09 +0000321 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000322 {
Derek Allard39b622d2008-01-16 21:10:09 +0000323 $sql = "SELECT name from sqlite_master WHERE type='table'";
324
325 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
326 {
327 $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
328 }
329 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Show column query
336 *
337 * Generates a platform-specific query string so that the column names can be fetched
338 *
339 * @access public
340 * @param string the table name
341 * @return string
342 */
343 function _list_columns($table = '')
344 {
345 // Not supported
346 return FALSE;
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Field data query
353 *
354 * Generates a platform-specific query so that the column data can be retrieved
355 *
356 * @access public
357 * @param string the table name
358 * @return object
359 */
360 function _field_data($table)
361 {
362 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * The error message string
369 *
370 * @access private
371 * @return string
372 */
373 function _error_message()
374 {
375 return sqlite_error_string(sqlite_last_error($this->conn_id));
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * The error message number
382 *
383 * @access private
384 * @return integer
385 */
386 function _error_number()
387 {
388 return sqlite_last_error($this->conn_id);
389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Escape Table Name
395 *
396 * This function adds backticks if the table name has a period
397 * in it. Some DBs will get cranky unless periods are escaped
398 *
399 * @access private
400 * @param string the table name
401 * @return string
402 */
403 function _escape_table($table)
404 {
Derek Allard7555ade2008-02-10 23:40:41 +0000405
406 // other database drivers use this to add backticks, hence this
407 // function is simply going to return the tablename for sqlite
Derek Allardd2df9bc2007-04-15 17:41:17 +0000408 return $table;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000414 * Protect Identifiers
415 *
416 * This function adds backticks if appropriate based on db type
417 *
418 * @access private
419 * @param mixed the item to escape
420 * @param boolean only affect the first word
421 * @return mixed the item with backticks
422 */
423 function _protect_identifiers($item, $first_word_only = FALSE)
424 {
425 if (is_array($item))
426 {
427 $escaped_array = array();
428
429 foreach($item as $k=>$v)
430 {
431 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
432 }
433
434 return $escaped_array;
435 }
436
437 // This function may get "item1 item2" as a string, and so
Derek Allard15648132008-02-10 21:46:18 +0000438 // we may need "item1 item2" and not "item1 item2"
Derek Allard61579382008-01-16 22:22:42 +0000439 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000440 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000441 if (strpos($item, '.') !== FALSE)
442 {
443 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
444 $table_name = substr($item, 0, strpos($item, '.')+1);
445 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
446 }
447
Derek Allard15648132008-02-10 21:46:18 +0000448 // This function may get "field >= 1", and need it to return "field >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000449 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000450
Derek Allard15648132008-02-10 21:46:18 +0000451 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1$2$3', $item);
Derek Allard61579382008-01-16 22:22:42 +0000452 }
453 else
454 {
Derek Allard15648132008-02-10 21:46:18 +0000455 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000456 }
457
Derek Allard9a4d1da2008-02-25 14:18:38 +0000458 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000459
460 foreach ($exceptions as $exception)
461 {
Derek Allard61579382008-01-16 22:22:42 +0000462
Derek Allard15648132008-02-10 21:46:18 +0000463 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000464 {
Derek Allard15648132008-02-10 21:46:18 +0000465 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000466 }
467 }
Derek Allard39b622d2008-01-16 21:10:09 +0000468 return $item;
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000474 * From Tables
475 *
476 * This function implicitly groups FROM tables so there is no confusion
477 * about operator precedence in harmony with SQL standards
478 *
479 * @access public
480 * @param type
481 * @return type
482 */
483 function _from_tables($tables)
484 {
Derek Jones0b59f272008-05-13 04:22:33 +0000485 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000486 {
487 $tables = array($tables);
488 }
489
490 return '('.implode(', ', $tables).')';
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000496 * Insert statement
497 *
498 * Generates a platform-specific insert string from the supplied data
499 *
500 * @access public
501 * @param string the table name
502 * @param array the insert keys
503 * @param array the insert values
504 * @return string
505 */
506 function _insert($table, $keys, $values)
507 {
508 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
509 }
510
511 // --------------------------------------------------------------------
512
513 /**
514 * Update statement
515 *
516 * Generates a platform-specific update string from the supplied data
517 *
518 * @access public
519 * @param string the table name
520 * @param array the update data
521 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000522 * @param array the orderby clause
523 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000524 * @return string
525 */
Derek Allard39b622d2008-01-16 21:10:09 +0000526 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000527 {
528 foreach($values as $key => $val)
529 {
530 $valstr[] = $key." = ".$val;
531 }
Derek Allardda6d2402007-12-19 14:49:29 +0000532
Derek Jones0b59f272008-05-13 04:22:33 +0000533 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000534
535 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000536
Derek Allard32cf7eb2008-02-05 16:03:50 +0000537 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
538 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
539 $sql .= $orderby.$limit;
540
541 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000542 }
543
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Truncate statement
549 *
550 * Generates a platform-specific truncate string from the supplied data
551 * If the database does not support the truncate() command
552 * This function maps to "DELETE FROM table"
553 *
554 * @access public
555 * @param string the table name
556 * @return string
557 */
558 function _truncate($table)
559 {
560 return $this->_delete($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000561 }
562
563 // --------------------------------------------------------------------
564
565 /**
566 * Delete statement
567 *
568 * Generates a platform-specific delete string from the supplied data
569 *
570 * @access public
571 * @param string the table name
572 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000573 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000574 * @return string
575 */
Derek Allard39b622d2008-01-16 21:10:09 +0000576 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000577 {
Derek Allard39b622d2008-01-16 21:10:09 +0000578 $conditions = '';
579
Derek Jones0b59f272008-05-13 04:22:33 +0000580 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000581 {
582 $conditions = "\nWHERE ";
583 $conditions .= implode("\n", $this->ar_where);
584
585 if (count($where) > 0 && count($like) > 0)
586 {
587 $conditions .= " AND ";
588 }
589 $conditions .= implode("\n", $like);
590 }
591
Derek Jones0b59f272008-05-13 04:22:33 +0000592 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000593
Derek Allard39b622d2008-01-16 21:10:09 +0000594 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000595 }
Derek Allard39b622d2008-01-16 21:10:09 +0000596
Derek Allardd2df9bc2007-04-15 17:41:17 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Limit string
601 *
602 * Generates a platform-specific LIMIT clause
603 *
604 * @access public
605 * @param string the sql query string
606 * @param integer the number of rows to limit the query to
607 * @param integer the offset value
608 * @return string
609 */
610 function _limit($sql, $limit, $offset)
611 {
612 if ($offset == 0)
613 {
614 $offset = '';
615 }
616 else
617 {
618 $offset .= ", ";
619 }
620
621 return $sql."LIMIT ".$offset.$limit;
622 }
623
624 // --------------------------------------------------------------------
625
626 /**
627 * Close DB Connection
628 *
629 * @access public
630 * @param resource
631 * @return void
632 */
633 function _close($conn_id)
634 {
635 @sqlite_close($conn_id);
636 }
637
Derek Allard2385d302008-04-07 14:01:01 +0000638 // --------------------------------------------------------------------
639
640 /**
641 * Rename a table
642 *
643 * Generates a platform-specific query so that a table can be renamed
644 *
645 * @access private
646 * @param string the old table name
647 * @param string the new table name
648 * @return string
649 */
650 function _rename_table($table_name, $new_table_name)
651 {
652 $sql = 'ALTER TABLE '.$this->db->_protect_identifiers($table_name)." RENAME TO ".$this->db->_protect_identifiers($new_table_name);
653 return $sql;
654 }
655
Derek Allardd2df9bc2007-04-15 17:41:17 +0000656
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 */