blob: 0ba483f8bc2d995dfbfd2602af785b89c1b856bc [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 }
413
414 if (strpos($item, '.') !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000415 {
Rick Ellis59523592008-10-17 04:07:40 +0000416 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard61579382008-01-16 22:22:42 +0000417 }
418 else
419 {
Rick Ellis59523592008-10-17 04:07:40 +0000420 $str = $this->_escape_char.$item.$this->_escape_char;
Derek Allard39b622d2008-01-16 21:10:09 +0000421 }
Derek Allard39b622d2008-01-16 21:10:09 +0000422
Rick Ellis59523592008-10-17 04:07:40 +0000423 // remove duplicates if the user already included the escape
424 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Derek Allard39b622d2008-01-16 21:10:09 +0000425 }
426
427 // --------------------------------------------------------------------
428
429 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000430 * From Tables
431 *
432 * This function implicitly groups FROM tables so there is no confusion
433 * about operator precedence in harmony with SQL standards
434 *
435 * @access public
436 * @param type
437 * @return type
438 */
439 function _from_tables($tables)
440 {
Derek Jones0b59f272008-05-13 04:22:33 +0000441 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000442 {
443 $tables = array($tables);
444 }
445
446 return '('.implode(', ', $tables).')';
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000452 * Insert statement
453 *
454 * Generates a platform-specific insert string from the supplied data
455 *
456 * @access public
457 * @param string the table name
458 * @param array the insert keys
459 * @param array the insert values
460 * @return string
461 */
462 function _insert($table, $keys, $values)
463 {
Rick Ellis59523592008-10-17 04:07:40 +0000464 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allardd2df9bc2007-04-15 17:41:17 +0000465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Update statement
471 *
472 * Generates a platform-specific update string from the supplied data
473 *
474 * @access public
475 * @param string the table name
476 * @param array the update data
477 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000478 * @param array the orderby clause
479 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000480 * @return string
481 */
Derek Allard39b622d2008-01-16 21:10:09 +0000482 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000483 {
484 foreach($values as $key => $val)
485 {
486 $valstr[] = $key." = ".$val;
487 }
Derek Allardda6d2402007-12-19 14:49:29 +0000488
Derek Jones0b59f272008-05-13 04:22:33 +0000489 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000490
491 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000492
Rick Ellis59523592008-10-17 04:07:40 +0000493 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
494
Derek Allard32cf7eb2008-02-05 16:03:50 +0000495 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Rick Ellis59523592008-10-17 04:07:40 +0000496
Derek Allard32cf7eb2008-02-05 16:03:50 +0000497 $sql .= $orderby.$limit;
498
499 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000500 }
501
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Truncate statement
507 *
508 * Generates a platform-specific truncate string from the supplied data
509 * If the database does not support the truncate() command
510 * This function maps to "DELETE FROM table"
511 *
512 * @access public
513 * @param string the table name
514 * @return string
515 */
516 function _truncate($table)
517 {
518 return $this->_delete($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Delete statement
525 *
526 * Generates a platform-specific delete string from the supplied data
527 *
528 * @access public
529 * @param string the table name
530 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000531 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000532 * @return string
533 */
Derek Allard39b622d2008-01-16 21:10:09 +0000534 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000535 {
Derek Allard39b622d2008-01-16 21:10:09 +0000536 $conditions = '';
537
Derek Jones0b59f272008-05-13 04:22:33 +0000538 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000539 {
540 $conditions = "\nWHERE ";
541 $conditions .= implode("\n", $this->ar_where);
542
543 if (count($where) > 0 && count($like) > 0)
544 {
545 $conditions .= " AND ";
546 }
547 $conditions .= implode("\n", $like);
548 }
549
Derek Jones0b59f272008-05-13 04:22:33 +0000550 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000551
Derek Allard39b622d2008-01-16 21:10:09 +0000552 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000553 }
Derek Allard39b622d2008-01-16 21:10:09 +0000554
Derek Allardd2df9bc2007-04-15 17:41:17 +0000555 // --------------------------------------------------------------------
556
557 /**
558 * Limit string
559 *
560 * Generates a platform-specific LIMIT clause
561 *
562 * @access public
563 * @param string the sql query string
564 * @param integer the number of rows to limit the query to
565 * @param integer the offset value
566 * @return string
567 */
568 function _limit($sql, $limit, $offset)
569 {
570 if ($offset == 0)
571 {
572 $offset = '';
573 }
574 else
575 {
576 $offset .= ", ";
577 }
578
579 return $sql."LIMIT ".$offset.$limit;
580 }
581
582 // --------------------------------------------------------------------
583
584 /**
585 * Close DB Connection
586 *
587 * @access public
588 * @param resource
589 * @return void
590 */
591 function _close($conn_id)
592 {
593 @sqlite_close($conn_id);
594 }
595
596
597}
598
Derek Jones0b59f272008-05-13 04:22:33 +0000599
600/* End of file sqlite_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000601/* Location: ./system/database/drivers/sqlite/sqlite_driver.php */