blob: ad2848ed8ca5a632b7d5a6242ce2d394e1f8d0fc [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 *
Andrey Andreeva6fe36e2012-04-05 16:00:32 +03005 * An open source application development framework for PHP 5.2.4 or newer
Andrey Andreev8ae24c52012-01-16 13:05:23 +02006 *
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 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +030019 * @package CodeIgniter
20 * @author EllisLab Dev Team
Andrey Andreev8ae24c52012-01-16 13:05:23 +020021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreev17ceeae2012-04-05 15:38:30 +030022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
24 * @since Version 1.0
Andrey Andreev8ae24c52012-01-16 13:05:23 +020025 * @filesource
26 */
27
28/**
29 * SQLite3 Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelowffe7a0a2012-04-26 13:48:18 +010032 * creates dynamically based on whether the query builder
Andrey Andreev8ae24c52012-01-16 13:05:23 +020033 * class is being used or not.
34 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +030035 * @package CodeIgniter
Andrey Andreev8ae24c52012-01-16 13:05:23 +020036 * @subpackage Drivers
37 * @category Database
Andrey Andreev17ceeae2012-04-05 15:38:30 +030038 * @author Andrey Andreev
39 * @link http://codeigniter.com/user_guide/database/
40 * @since Version 3.0
Andrey Andreev8ae24c52012-01-16 13:05:23 +020041 */
42class CI_DB_sqlite3_driver extends CI_DB {
43
44 public $dbdriver = 'sqlite3';
45
46 // The character used for escaping
Andrey Andreevcb9f3612012-01-26 02:06:48 +020047 protected $_escape_char = '"';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020048
49 // clause and character used for LIKE escape sequences
Andrey Andreevcb9f3612012-01-26 02:06:48 +020050 protected $_like_escape_str = ' ESCAPE \'%s\' ';
51 protected $_like_escape_chr = '!';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020052
53 /**
54 * The syntax to count rows is slightly different across different
55 * database engines, so this string appears in each driver and is
56 * used for the count_all() and count_all_results() functions.
57 */
Andrey Andreevcb9f3612012-01-26 02:06:48 +020058 protected $_count_string = 'SELECT COUNT(*) AS ';
59 protected $_random_keyword = ' RANDOM()';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020060
61 /**
62 * Non-persistent database connection
63 *
64 * @return object type SQLite3
65 */
66 public function db_connect()
67 {
68 try
69 {
70 return ( ! $this->password)
71 ? new SQLite3($this->database)
72 : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
73 }
74 catch (Exception $e)
75 {
76 return FALSE;
77 }
78 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Persistent database connection
84 *
85 * @return object type SQLite3
86 */
87 public function db_pconnect()
88 {
89 log_message('debug', 'SQLite3 doesn\'t support persistent connections');
90 return $this->db_pconnect();
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
Andrey Andreev80e34f92012-03-03 03:25:23 +020096 * Database version number
Andrey Andreev8ae24c52012-01-16 13:05:23 +020097 *
98 * @return string
99 */
Andrey Andreev80e34f92012-03-03 03:25:23 +0200100 public function version()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200101 {
Andrey Andreev80e34f92012-03-03 03:25:23 +0200102 if (isset($this->data_cache['version']))
103 {
104 return $this->data_cache['version'];
105 }
106
107 $version = $this->conn_id->version();
108 return $this->data_cache['version'] = $version['versionString'];
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Execute the query
115 *
116 * @param string an SQL query
117 * @return mixed SQLite3Result object or bool
118 */
119 protected function _execute($sql)
120 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200121 // TODO: Implement use of SQLite3::querySingle(), if needed
Andrey Andreeva92c7cd2012-03-02 13:51:22 +0200122
123 return $this->is_write_type($sql)
124 ? $this->conn_id->exec($sql)
125 : $this->conn_id->query($sql);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200126 }
127
128 // --------------------------------------------------------------------
129
130 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200131 * Begin Transaction
132 *
133 * @return bool
134 */
135 public function trans_begin($test_mode = FALSE)
136 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200137 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200138 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200139 {
140 return TRUE;
141 }
142
143 // Reset the transaction failure flag.
144 // If the $test_mode flag is set to TRUE transactions will be rolled back
145 // even if the queries produce a successful result.
146 $this->_trans_failure = ($test_mode === TRUE);
147
148 return $this->conn_id->exec('BEGIN TRANSACTION');
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Commit Transaction
155 *
156 * @return bool
157 */
158 public function trans_commit()
159 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200160 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200161 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200162 {
163 return TRUE;
164 }
165
166 return $this->conn_id->exec('END TRANSACTION');
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Rollback Transaction
173 *
174 * @return bool
175 */
176 public function trans_rollback()
177 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200178 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200179 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200180 {
181 return TRUE;
182 }
183
184 return $this->conn_id->exec('ROLLBACK');
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Escape String
191 *
192 * @param string
193 * @param bool whether or not the string will be used in a LIKE condition
194 * @return string
195 */
196 public function escape_str($str, $like = FALSE)
197 {
198 if (is_array($str))
199 {
200 foreach ($str as $key => $val)
201 {
202 $str[$key] = $this->escape_str($val, $like);
203 }
204
205 return $str;
206 }
207
208 $str = $this->conn_id->escapeString(remove_invisible_characters($str));
209
210 // escape LIKE condition wildcards
211 if ($like === TRUE)
212 {
Andrey Andreevdc3de152012-03-12 15:41:49 +0200213 return str_replace(array($this->_like_escape_chr, '%', '_'),
214 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200215 $str);
216 }
217
218 return $str;
219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Affected Rows
225 *
226 * @return int
227 */
228 public function affected_rows()
229 {
230 return $this->conn_id->changes();
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Insert ID
237 *
238 * @return int
239 */
240 public function insert_id()
241 {
242 return $this->conn_id->lastInsertRowID();
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * "Count All" query
249 *
250 * Generates a platform-specific query string that counts all records in
251 * the specified database
252 *
253 * @param string
254 * @return int
255 */
256 public function count_all($table = '')
257 {
258 if ($table == '')
259 {
260 return 0;
261 }
262
Andrey Andreev0aa9f2e2012-03-09 14:55:20 +0200263 $result = $this->conn_id->querySingle($this->_count_string.$this->protect_identifiers('numrows')
264 .' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200265
266 return empty($result) ? 0 : (int) $result;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Show table query
273 *
274 * Generates a platform-specific query string so that the table names can be fetched
275 *
276 * @param bool
277 * @return string
278 */
279 protected function _list_tables($prefix_limit = FALSE)
280 {
281 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
282 .(($prefix_limit !== FALSE && $this->dbprefix != '')
283 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
284 : '');
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Show column query
291 *
292 * Generates a platform-specific query string so that the column names can be fetched
293 *
294 * @param string the table name
295 * @return string
296 */
297 protected function _list_columns($table = '')
298 {
299 // Not supported
300 return FALSE;
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Field data query
307 *
308 * Generates a platform-specific query so that the column data can be retrieved
309 *
310 * @param string the table name
311 * @return string
312 */
313 protected function _field_data($table)
314 {
315 return 'SELECT * FROM '.$table.' LIMIT 0,1';
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * The error message string
322 *
323 * @return string
324 */
325 protected function _error_message()
326 {
327 return $this->conn_id->lastErrorMsg();
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * The error message number
334 *
335 * @return int
336 */
337 protected function _error_number()
338 {
339 return $this->conn_id->lastErrorCode();
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200345 * From Tables
346 *
347 * This function implicitly groups FROM tables so there is no confusion
348 * about operator precedence in harmony with SQL standards
349 *
350 * @param string
351 * @return string
352 */
353 protected function _from_tables($tables)
354 {
355 if ( ! is_array($tables))
356 {
357 $tables = array($tables);
358 }
359
360 return '('.implode(', ', $tables).')';
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300366 * Replace statement
367 *
368 * Generates a platform-specific replace string from the supplied data
369 *
370 * @param string the table name
371 * @param array the insert keys
372 * @param array the insert values
373 * @return string
374 */
375 protected function _replace($table, $keys, $values)
376 {
377 return 'INSERT OR '.parent::_replace($table, $keys, $values);
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200383 * Truncate statement
384 *
385 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300386 *
387 * If the database does not support the truncate() command, then,
388 * then this method maps to 'DELETE FROM table'
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200389 *
390 * @param string the table name
391 * @return string
392 */
393 protected function _truncate($table)
394 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300395 return 'DELETE FROM '.$table;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200396 }
397
398 // --------------------------------------------------------------------
399
400 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200401 * Limit string
402 *
403 * Generates a platform-specific LIMIT clause
404 *
405 * @param string the sql query string
406 * @param int the number of rows to limit the query to
407 * @param int the offset value
408 * @return string
409 */
410 protected function _limit($sql, $limit, $offset)
411 {
Andrey Andreev20bd7bc2012-03-12 09:26:40 +0200412 return $sql.' LIMIT '.($offset ? $offset.',' : '').$limit;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Close DB Connection
419 *
420 * @return void
421 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300422 protected function _close()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200423 {
424 $this->conn_id->close();
425 }
426
427}
428
429/* End of file sqlite3_driver.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200430/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */