blob: 2acefbcf48e8ba1644f41627332827f037967216 [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');
Andrey Andreev9d533ae2012-06-04 15:56:56 +030090 return $this->db_connect();
Andrey Andreev8ae24c52012-01-16 13:05:23 +020091 }
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
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300107 $version = SQLite3::version();
Andrey Andreev80e34f92012-03-03 03:25:23 +0200108 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 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200248 * Show table query
249 *
250 * Generates a platform-specific query string so that the table names can be fetched
251 *
252 * @param bool
253 * @return string
254 */
255 protected function _list_tables($prefix_limit = FALSE)
256 {
257 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
258 .(($prefix_limit !== FALSE && $this->dbprefix != '')
259 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
260 : '');
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Show column query
267 *
268 * Generates a platform-specific query string so that the column names can be fetched
269 *
270 * @param string the table name
271 * @return string
272 */
273 protected function _list_columns($table = '')
274 {
275 // Not supported
276 return FALSE;
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Field data query
283 *
284 * Generates a platform-specific query so that the column data can be retrieved
285 *
286 * @param string the table name
287 * @return string
288 */
289 protected function _field_data($table)
290 {
291 return 'SELECT * FROM '.$table.' LIMIT 0,1';
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * The error message string
298 *
299 * @return string
300 */
301 protected function _error_message()
302 {
303 return $this->conn_id->lastErrorMsg();
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * The error message number
310 *
311 * @return int
312 */
313 protected function _error_number()
314 {
315 return $this->conn_id->lastErrorCode();
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200321 * From Tables
322 *
323 * This function implicitly groups FROM tables so there is no confusion
324 * about operator precedence in harmony with SQL standards
325 *
326 * @param string
327 * @return string
328 */
329 protected function _from_tables($tables)
330 {
331 if ( ! is_array($tables))
332 {
333 $tables = array($tables);
334 }
335
336 return '('.implode(', ', $tables).')';
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300342 * Replace statement
343 *
344 * Generates a platform-specific replace string from the supplied data
345 *
346 * @param string the table name
347 * @param array the insert keys
348 * @param array the insert values
349 * @return string
350 */
351 protected function _replace($table, $keys, $values)
352 {
353 return 'INSERT OR '.parent::_replace($table, $keys, $values);
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200359 * Truncate statement
360 *
361 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300362 *
363 * If the database does not support the truncate() command, then,
364 * then this method maps to 'DELETE FROM table'
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200365 *
366 * @param string the table name
367 * @return string
368 */
369 protected function _truncate($table)
370 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300371 return 'DELETE FROM '.$table;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200372 }
373
374 // --------------------------------------------------------------------
375
376 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200377 * Limit string
378 *
379 * Generates a platform-specific LIMIT clause
380 *
381 * @param string the sql query string
382 * @param int the number of rows to limit the query to
383 * @param int the offset value
384 * @return string
385 */
386 protected function _limit($sql, $limit, $offset)
387 {
Andrey Andreev20bd7bc2012-03-12 09:26:40 +0200388 return $sql.' LIMIT '.($offset ? $offset.',' : '').$limit;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Close DB Connection
395 *
396 * @return void
397 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300398 protected function _close()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200399 {
400 $this->conn_id->close();
401 }
402
403}
404
405/* End of file sqlite3_driver.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200406/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */