blob: 992e2479e05120e45fbd0642a62015fd715174bd [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';
Rick Ellis59523592008-10-17 04:07:40 +000036
37 // The character used to escape with - not needed for SQLite
38 var $_escape_char = '';
Rick Ellis5aa8c602008-10-07 01:24:07 +000039
Derek Allardd2df9bc2007-04-15 17:41:17 +000040 /**
Derek Allard694b5b82007-12-18 15:58:03 +000041 * The syntax to count rows is slightly different across different
42 * database engines, so this string appears in each driver and is
43 * used for the count_all() and count_all_results() functions.
44 */
Derek Allard39b622d2008-01-16 21:10:09 +000045 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000046 var $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard694b5b82007-12-18 15:58:03 +000047
48 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000049 * Non-persistent database connection
50 *
51 * @access private called by the base class
52 * @return resource
53 */
54 function db_connect()
55 {
Derek Jones0b59f272008-05-13 04:22:33 +000056 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000057 {
58 log_message('error', $error);
59
60 if ($this->db_debug)
61 {
62 $this->display_error($error, '', TRUE);
63 }
64
65 return FALSE;
66 }
67
68 return $conn_id;
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Persistent database connection
75 *
76 * @access private called by the base class
77 * @return resource
78 */
79 function db_pconnect()
80 {
Derek Jones0b59f272008-05-13 04:22:33 +000081 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
Derek Allardd2df9bc2007-04-15 17:41:17 +000082 {
83 log_message('error', $error);
84
85 if ($this->db_debug)
86 {
87 $this->display_error($error, '', TRUE);
88 }
89
90 return FALSE;
91 }
92
93 return $conn_id;
94 }
95
96 // --------------------------------------------------------------------
97
98 /**
99 * Select the database
100 *
101 * @access private called by the base class
102 * @return resource
103 */
104 function db_select()
105 {
106 return TRUE;
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000112 * Set client character set
113 *
114 * @access public
115 * @param string
116 * @param string
117 * @return resource
118 */
119 function db_set_charset($charset, $collation)
120 {
Rick Ellisff734012008-09-30 20:38:12 +0000121 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +0000122 return TRUE;
123 }
124
125 // --------------------------------------------------------------------
126
127 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000128 * Version number query string
129 *
130 * @access public
131 * @return string
132 */
133 function _version()
134 {
135 return sqlite_libversion();
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Execute the query
142 *
143 * @access private called by the base class
144 * @param string an SQL query
145 * @return resource
146 */
147 function _execute($sql)
148 {
149 $sql = $this->_prep_query($sql);
150 return @sqlite_query($this->conn_id, $sql);
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Prep the query
157 *
158 * If needed, each database adapter can prep the query string
159 *
160 * @access private called by execute()
161 * @param string an SQL query
162 * @return string
163 */
164 function _prep_query($sql)
165 {
166 return $sql;
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Begin Transaction
173 *
174 * @access public
175 * @return bool
176 */
177 function trans_begin($test_mode = FALSE)
178 {
Derek Jones0b59f272008-05-13 04:22:33 +0000179 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000180 {
181 return TRUE;
182 }
183
184 // When transactions are nested we only begin/commit/rollback the outermost ones
185 if ($this->_trans_depth > 0)
186 {
187 return TRUE;
188 }
189
190 // Reset the transaction failure flag.
191 // If the $test_mode flag is set to TRUE transactions will be rolled back
192 // even if the queries produce a successful result.
193 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
194
195 $this->simple_query('BEGIN TRANSACTION');
196 return TRUE;
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
202 * Commit Transaction
203 *
204 * @access public
205 * @return bool
206 */
207 function trans_commit()
208 {
Derek Jones0b59f272008-05-13 04:22:33 +0000209 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000210 {
211 return TRUE;
212 }
213
214 // When transactions are nested we only begin/commit/rollback the outermost ones
215 if ($this->_trans_depth > 0)
216 {
217 return TRUE;
218 }
219
220 $this->simple_query('COMMIT');
221 return TRUE;
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Rollback Transaction
228 *
229 * @access public
230 * @return bool
231 */
232 function trans_rollback()
233 {
Derek Jones0b59f272008-05-13 04:22:33 +0000234 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000235 {
236 return TRUE;
237 }
238
239 // When transactions are nested we only begin/commit/rollback the outermost ones
240 if ($this->_trans_depth > 0)
241 {
242 return TRUE;
243 }
244
245 $this->simple_query('ROLLBACK');
246 return TRUE;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Escape String
253 *
254 * @access public
255 * @param string
256 * @return string
257 */
258 function escape_str($str)
259 {
260 return sqlite_escape_string($str);
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Affected Rows
267 *
268 * @access public
269 * @return integer
270 */
271 function affected_rows()
272 {
273 return sqlite_changes($this->conn_id);
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Insert ID
280 *
281 * @access public
282 * @return integer
283 */
284 function insert_id()
285 {
286 return @sqlite_last_insert_rowid($this->conn_id);
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * "Count All" query
293 *
294 * Generates a platform-specific query string that counts all records in
295 * the specified database
296 *
297 * @access public
298 * @param string
299 * @return string
300 */
301 function count_all($table = '')
302 {
303 if ($table == '')
304 return '0';
305
Rick Ellis59523592008-10-17 04:07:40 +0000306 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000307
308 if ($query->num_rows() == 0)
309 return '0';
310
311 $row = $query->row();
312 return $row->numrows;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * List table query
319 *
320 * Generates a platform-specific query string so that the table names can be fetched
321 *
322 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000323 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000324 * @return string
325 */
Derek Allard39b622d2008-01-16 21:10:09 +0000326 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000327 {
Derek Allard39b622d2008-01-16 21:10:09 +0000328 $sql = "SELECT name from sqlite_master WHERE type='table'";
329
330 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
331 {
332 $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
333 }
334 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Show column query
341 *
342 * Generates a platform-specific query string so that the column names can be fetched
343 *
344 * @access public
345 * @param string the table name
346 * @return string
347 */
348 function _list_columns($table = '')
349 {
350 // Not supported
351 return FALSE;
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Field data query
358 *
359 * Generates a platform-specific query so that the column data can be retrieved
360 *
361 * @access public
362 * @param string the table name
363 * @return object
364 */
365 function _field_data($table)
366 {
Rick Ellis59523592008-10-17 04:07:40 +0000367 return "SELECT * FROM ".$table." LIMIT 1";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * The error message string
374 *
375 * @access private
376 * @return string
377 */
378 function _error_message()
379 {
380 return sqlite_error_string(sqlite_last_error($this->conn_id));
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * The error message number
387 *
388 * @access private
389 * @return integer
390 */
391 function _error_number()
392 {
393 return sqlite_last_error($this->conn_id);
394 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000395
396 // --------------------------------------------------------------------
397
398 /**
Rick Ellis59523592008-10-17 04:07:40 +0000399 * Escape the SQL Identifiers
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000400 *
Rick Ellis59523592008-10-17 04:07:40 +0000401 * This function escapes column and table names
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000402 *
403 * @access private
Rick Ellis59523592008-10-17 04:07:40 +0000404 * @param string
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000405 * @return string
406 */
Rick Ellis59523592008-10-17 04:07:40 +0000407 function _escape_identifiers($item)
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000408 {
Rick Ellis59523592008-10-17 04:07:40 +0000409 if ($this->_escape_char == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000410 {
Rick Ellis59523592008-10-17 04:07:40 +0000411 return $item;
412 }
Rick Ellisa0e86292008-10-26 22:46:55 +0000413
414 foreach ($this->_reserved_identifiers as $id)
415 {
416 if (strpos($item, '.'.$id) !== FALSE)
417 {
418 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
419
420 // remove duplicates if the user already included the escape
421 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
422 }
423 }
Rick Ellis59523592008-10-17 04:07:40 +0000424
425 if (strpos($item, '.') !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000426 {
Rick Ellis59523592008-10-17 04:07:40 +0000427 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard61579382008-01-16 22:22:42 +0000428 }
429 else
430 {
Rick Ellis59523592008-10-17 04:07:40 +0000431 $str = $this->_escape_char.$item.$this->_escape_char;
Derek Allard39b622d2008-01-16 21:10:09 +0000432 }
Derek Allard39b622d2008-01-16 21:10:09 +0000433
Rick Ellis59523592008-10-17 04:07:40 +0000434 // remove duplicates if the user already included the escape
435 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Derek Allard39b622d2008-01-16 21:10:09 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000441 * From Tables
442 *
443 * This function implicitly groups FROM tables so there is no confusion
444 * about operator precedence in harmony with SQL standards
445 *
446 * @access public
447 * @param type
448 * @return type
449 */
450 function _from_tables($tables)
451 {
Derek Jones0b59f272008-05-13 04:22:33 +0000452 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000453 {
454 $tables = array($tables);
455 }
456
457 return '('.implode(', ', $tables).')';
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000463 * Insert statement
464 *
465 * Generates a platform-specific insert string from the supplied data
466 *
467 * @access public
468 * @param string the table name
469 * @param array the insert keys
470 * @param array the insert values
471 * @return string
472 */
473 function _insert($table, $keys, $values)
474 {
Rick Ellis59523592008-10-17 04:07:40 +0000475 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Update statement
482 *
483 * Generates a platform-specific update string from the supplied data
484 *
485 * @access public
486 * @param string the table name
487 * @param array the update data
488 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000489 * @param array the orderby clause
490 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000491 * @return string
492 */
Derek Allard39b622d2008-01-16 21:10:09 +0000493 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000494 {
495 foreach($values as $key => $val)
496 {
497 $valstr[] = $key." = ".$val;
498 }
Derek Allardda6d2402007-12-19 14:49:29 +0000499
Derek Jones0b59f272008-05-13 04:22:33 +0000500 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000501
502 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000503
Rick Ellis59523592008-10-17 04:07:40 +0000504 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
505
Derek Allard32cf7eb2008-02-05 16:03:50 +0000506 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Rick Ellis59523592008-10-17 04:07:40 +0000507
Derek Allard32cf7eb2008-02-05 16:03:50 +0000508 $sql .= $orderby.$limit;
509
510 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000511 }
512
513
514 // --------------------------------------------------------------------
515
516 /**
517 * Truncate statement
518 *
519 * Generates a platform-specific truncate string from the supplied data
520 * If the database does not support the truncate() command
521 * This function maps to "DELETE FROM table"
522 *
523 * @access public
524 * @param string the table name
525 * @return string
526 */
527 function _truncate($table)
528 {
529 return $this->_delete($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Delete statement
536 *
537 * Generates a platform-specific delete string from the supplied data
538 *
539 * @access public
540 * @param string the table name
541 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000542 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000543 * @return string
544 */
Derek Allard39b622d2008-01-16 21:10:09 +0000545 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000546 {
Derek Allard39b622d2008-01-16 21:10:09 +0000547 $conditions = '';
548
Derek Jones0b59f272008-05-13 04:22:33 +0000549 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000550 {
551 $conditions = "\nWHERE ";
552 $conditions .= implode("\n", $this->ar_where);
553
554 if (count($where) > 0 && count($like) > 0)
555 {
556 $conditions .= " AND ";
557 }
558 $conditions .= implode("\n", $like);
559 }
560
Derek Jones0b59f272008-05-13 04:22:33 +0000561 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000562
Derek Allard39b622d2008-01-16 21:10:09 +0000563 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000564 }
Derek Allard39b622d2008-01-16 21:10:09 +0000565
Derek Allardd2df9bc2007-04-15 17:41:17 +0000566 // --------------------------------------------------------------------
567
568 /**
569 * Limit string
570 *
571 * Generates a platform-specific LIMIT clause
572 *
573 * @access public
574 * @param string the sql query string
575 * @param integer the number of rows to limit the query to
576 * @param integer the offset value
577 * @return string
578 */
579 function _limit($sql, $limit, $offset)
580 {
581 if ($offset == 0)
582 {
583 $offset = '';
584 }
585 else
586 {
587 $offset .= ", ";
588 }
589
590 return $sql."LIMIT ".$offset.$limit;
591 }
592
593 // --------------------------------------------------------------------
594
595 /**
596 * Close DB Connection
597 *
598 * @access public
599 * @param resource
600 * @return void
601 */
602 function _close($conn_id)
603 {
604 @sqlite_close($conn_id);
605 }
606
607
608}
609
Derek Jones0b59f272008-05-13 04:22:33 +0000610
611/* End of file sqlite_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000612/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */