blob: 6affb8745b508c72252ade13ef6c375fb31f7ed2 [file] [log] [blame]
Andrey Andreev8ae24c52012-01-16 13:05:23 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * 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
Andrey Andreevf20fb982012-01-24 15:26:01 +020012 * bundled with this package in the files license.txt / license.rst. It is
Andrey Andreev8ae24c52012-01-16 13:05:23 +020013 * 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 *
19 * @package CodeIgniter
20 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28/**
29 * SQLite3 Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
38 * @author Andrey Andreev
39 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_sqlite3_driver extends CI_DB {
42
43 public $dbdriver = 'sqlite3';
44
45 // The character used for escaping
Andrey Andreevcb9f3612012-01-26 02:06:48 +020046 protected $_escape_char = '"';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020047
48 // clause and character used for LIKE escape sequences
Andrey Andreevcb9f3612012-01-26 02:06:48 +020049 protected $_like_escape_str = ' ESCAPE \'%s\' ';
50 protected $_like_escape_chr = '!';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreevcb9f3612012-01-26 02:06:48 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' RANDOM()';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020059
60 /**
61 * Non-persistent database connection
62 *
63 * @return object type SQLite3
64 */
65 public function db_connect()
66 {
67 try
68 {
69 return ( ! $this->password)
70 ? new SQLite3($this->database)
71 : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
72 }
73 catch (Exception $e)
74 {
75 return FALSE;
76 }
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Persistent database connection
83 *
84 * @return object type SQLite3
85 */
86 public function db_pconnect()
87 {
88 log_message('debug', 'SQLite3 doesn\'t support persistent connections');
89 return $this->db_pconnect();
90 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Reconnect
96 *
97 * Keep / reestablish the db connection if no queries have been
98 * sent for a length of time exceeding the server's idle timeout
99 *
100 * @return void
101 */
102 public function reconnect()
103 {
104 // Not supported
105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Select the database
111 *
112 * @return bool
113 */
114 public function db_select()
115 {
116 // Not needed, in SQLite every pseudo-connection is a database
117 return TRUE;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
Andrey Andreev80e34f92012-03-03 03:25:23 +0200123 * Database version number
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200124 *
125 * @return string
126 */
Andrey Andreev80e34f92012-03-03 03:25:23 +0200127 public function version()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200128 {
Andrey Andreev80e34f92012-03-03 03:25:23 +0200129 if (isset($this->data_cache['version']))
130 {
131 return $this->data_cache['version'];
132 }
133
134 $version = $this->conn_id->version();
135 return $this->data_cache['version'] = $version['versionString'];
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Execute the query
142 *
143 * @param string an SQL query
144 * @return mixed SQLite3Result object or bool
145 */
146 protected function _execute($sql)
147 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200148 // TODO: Implement use of SQLite3::querySingle(), if needed
149 // TODO: Use $this->_prep_query(), if needed
Andrey Andreeva92c7cd2012-03-02 13:51:22 +0200150
151 return $this->is_write_type($sql)
152 ? $this->conn_id->exec($sql)
153 : $this->conn_id->query($sql);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Prep the query
160 *
161 * If needed, each database adapter can prep the query string
162 *
163 * @param string an SQL query
164 * @return string
165 */
Andrey Andreeva3ed0862012-01-25 14:46:52 +0200166 protected function _prep_query($sql)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200167 {
168 return $this->conn_id->prepare($sql);
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Begin Transaction
175 *
176 * @return bool
177 */
178 public function trans_begin($test_mode = FALSE)
179 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200180 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200181 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200182 {
183 return TRUE;
184 }
185
186 // Reset the transaction failure flag.
187 // If the $test_mode flag is set to TRUE transactions will be rolled back
188 // even if the queries produce a successful result.
189 $this->_trans_failure = ($test_mode === TRUE);
190
191 return $this->conn_id->exec('BEGIN TRANSACTION');
192 }
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Commit Transaction
198 *
199 * @return bool
200 */
201 public function trans_commit()
202 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200203 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200204 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200205 {
206 return TRUE;
207 }
208
209 return $this->conn_id->exec('END TRANSACTION');
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Rollback Transaction
216 *
217 * @return bool
218 */
219 public function trans_rollback()
220 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200221 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200222 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200223 {
224 return TRUE;
225 }
226
227 return $this->conn_id->exec('ROLLBACK');
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Escape String
234 *
235 * @param string
236 * @param bool whether or not the string will be used in a LIKE condition
237 * @return string
238 */
239 public function escape_str($str, $like = FALSE)
240 {
241 if (is_array($str))
242 {
243 foreach ($str as $key => $val)
244 {
245 $str[$key] = $this->escape_str($val, $like);
246 }
247
248 return $str;
249 }
250
251 $str = $this->conn_id->escapeString(remove_invisible_characters($str));
252
253 // escape LIKE condition wildcards
254 if ($like === TRUE)
255 {
256 return str_replace(array('%', '_', $this->_like_escape_chr),
257 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
258 $str);
259 }
260
261 return $str;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Affected Rows
268 *
269 * @return int
270 */
271 public function affected_rows()
272 {
273 return $this->conn_id->changes();
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Insert ID
280 *
281 * @return int
282 */
283 public function insert_id()
284 {
285 return $this->conn_id->lastInsertRowID();
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * "Count All" query
292 *
293 * Generates a platform-specific query string that counts all records in
294 * the specified database
295 *
296 * @param string
297 * @return int
298 */
299 public function count_all($table = '')
300 {
301 if ($table == '')
302 {
303 return 0;
304 }
305
Andrey Andreev0aa9f2e2012-03-09 14:55:20 +0200306 $result = $this->conn_id->querySingle($this->_count_string.$this->protect_identifiers('numrows')
307 .' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200308
309 return empty($result) ? 0 : (int) $result;
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Show table query
316 *
317 * Generates a platform-specific query string so that the table names can be fetched
318 *
319 * @param bool
320 * @return string
321 */
322 protected function _list_tables($prefix_limit = FALSE)
323 {
324 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
325 .(($prefix_limit !== FALSE && $this->dbprefix != '')
326 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
327 : '');
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Show column query
334 *
335 * Generates a platform-specific query string so that the column names can be fetched
336 *
337 * @param string the table name
338 * @return string
339 */
340 protected function _list_columns($table = '')
341 {
342 // Not supported
343 return FALSE;
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Field data query
350 *
351 * Generates a platform-specific query so that the column data can be retrieved
352 *
353 * @param string the table name
354 * @return string
355 */
356 protected function _field_data($table)
357 {
358 return 'SELECT * FROM '.$table.' LIMIT 0,1';
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * The error message string
365 *
366 * @return string
367 */
368 protected function _error_message()
369 {
370 return $this->conn_id->lastErrorMsg();
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * The error message number
377 *
378 * @return int
379 */
380 protected function _error_number()
381 {
382 return $this->conn_id->lastErrorCode();
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Escape the SQL Identifiers
389 *
390 * This function escapes column and table names
391 *
392 * @param string
393 * @return string
394 */
Andrey Andreevcb9f3612012-01-26 02:06:48 +0200395 public function _escape_identifiers($item)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200396 {
397 if ($this->_escape_char == '')
398 {
399 return $item;
400 }
401
402 foreach ($this->_reserved_identifiers as $id)
403 {
404 if (strpos($item, '.'.$id) !== FALSE)
405 {
406 $item = str_replace('.', $this->_escape_char.'.', $item);
407
408 // remove duplicates if the user already included the escape
409 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
410 }
411 }
412
413 if (strpos($item, '.') !== FALSE)
414 {
415 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
416 }
417
418 // remove duplicates if the user already included the escape
419 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * From Tables
426 *
427 * This function implicitly groups FROM tables so there is no confusion
428 * about operator precedence in harmony with SQL standards
429 *
430 * @param string
431 * @return string
432 */
433 protected function _from_tables($tables)
434 {
435 if ( ! is_array($tables))
436 {
437 $tables = array($tables);
438 }
439
440 return '('.implode(', ', $tables).')';
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Insert statement
447 *
448 * Generates a platform-specific insert string from the supplied data
449 *
450 * @param string the table name
451 * @param array the insert keys
452 * @param array the insert values
453 * @return string
454 */
455 protected function _insert($table, $keys, $values)
456 {
457 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Update statement
464 *
465 * Generates a platform-specific update string from the supplied data
466 *
467 * @param string the table name
468 * @param array the update data
469 * @param array the where clause
470 * @param array the orderby clause
471 * @param array the limit clause
472 * @return string
473 */
474 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
475 {
476 foreach ($values as $key => $val)
477 {
478 $valstr[] = $key.' = '.$val;
479 }
480
481 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
482 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
483 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
484 .( ! $limit ? '' : ' LIMIT '.$limit);
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Truncate statement
491 *
492 * Generates a platform-specific truncate string from the supplied data
493 * If the database does not support the truncate() command, then
494 * this method maps to "DELETE FROM table"
495 *
496 * @param string the table name
497 * @return string
498 */
499 protected function _truncate($table)
500 {
501 return $this->_delete($table);
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Delete statement
508 *
509 * Generates a platform-specific delete string from the supplied data
510 *
511 * @param string the table name
512 * @param array the where clause
513 * @param string the limit clause
514 * @return string
515 */
516 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
517 {
518 $conditions = '';
519 if (count($where) > 0 OR count($like) > 0)
520 {
521 $conditions .= "\nWHERE ".implode("\n", $this->ar_where);
522
523 if (count($where) > 0 && count($like) > 0)
524 {
525 $conditions .= ' AND ';
526 }
527 $conditions .= implode("\n", $like);
528 }
529
530 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Limit string
537 *
538 * Generates a platform-specific LIMIT clause
539 *
540 * @param string the sql query string
541 * @param int the number of rows to limit the query to
542 * @param int the offset value
543 * @return string
544 */
545 protected function _limit($sql, $limit, $offset)
546 {
547 return $sql.($offset ? $offset.',' : '').$limit;
548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Close DB Connection
554 *
555 * @return void
556 */
557 protected function _close()
558 {
559 $this->conn_id->close();
560 }
561
562}
563
564/* End of file sqlite3_driver.php */
565/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */