blob: cc35d319f9c6a982d1d37961467575fc4631795e [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
Andrey Andreevcb9f3612012-01-26 02:06:48 +020053 protected $_random_keyword = ' RANDOM()';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020054
55 /**
56 * Non-persistent database connection
57 *
58 * @return object type SQLite3
59 */
60 public function db_connect()
61 {
62 try
63 {
64 return ( ! $this->password)
65 ? new SQLite3($this->database)
66 : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
67 }
68 catch (Exception $e)
69 {
70 return FALSE;
71 }
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Persistent database connection
78 *
79 * @return object type SQLite3
80 */
81 public function db_pconnect()
82 {
83 log_message('debug', 'SQLite3 doesn\'t support persistent connections');
Andrey Andreev9d533ae2012-06-04 15:56:56 +030084 return $this->db_connect();
Andrey Andreev8ae24c52012-01-16 13:05:23 +020085 }
86
87 // --------------------------------------------------------------------
88
89 /**
Andrey Andreev80e34f92012-03-03 03:25:23 +020090 * Database version number
Andrey Andreev8ae24c52012-01-16 13:05:23 +020091 *
92 * @return string
93 */
Andrey Andreev80e34f92012-03-03 03:25:23 +020094 public function version()
Andrey Andreev8ae24c52012-01-16 13:05:23 +020095 {
Andrey Andreev80e34f92012-03-03 03:25:23 +020096 if (isset($this->data_cache['version']))
97 {
98 return $this->data_cache['version'];
99 }
100
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300101 $version = SQLite3::version();
Andrey Andreev80e34f92012-03-03 03:25:23 +0200102 return $this->data_cache['version'] = $version['versionString'];
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Execute the query
109 *
110 * @param string an SQL query
111 * @return mixed SQLite3Result object or bool
112 */
113 protected function _execute($sql)
114 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200115 // TODO: Implement use of SQLite3::querySingle(), if needed
Andrey Andreeva92c7cd2012-03-02 13:51:22 +0200116
117 return $this->is_write_type($sql)
118 ? $this->conn_id->exec($sql)
119 : $this->conn_id->query($sql);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200125 * Begin Transaction
126 *
127 * @return bool
128 */
129 public function trans_begin($test_mode = FALSE)
130 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200131 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200132 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200133 {
134 return TRUE;
135 }
136
137 // Reset the transaction failure flag.
138 // If the $test_mode flag is set to TRUE transactions will be rolled back
139 // even if the queries produce a successful result.
140 $this->_trans_failure = ($test_mode === TRUE);
141
142 return $this->conn_id->exec('BEGIN TRANSACTION');
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Commit Transaction
149 *
150 * @return bool
151 */
152 public function trans_commit()
153 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200154 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200155 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200156 {
157 return TRUE;
158 }
159
160 return $this->conn_id->exec('END TRANSACTION');
161 }
162
163 // --------------------------------------------------------------------
164
165 /**
166 * Rollback Transaction
167 *
168 * @return bool
169 */
170 public function trans_rollback()
171 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200172 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200173 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200174 {
175 return TRUE;
176 }
177
178 return $this->conn_id->exec('ROLLBACK');
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Escape String
185 *
186 * @param string
187 * @param bool whether or not the string will be used in a LIKE condition
188 * @return string
189 */
190 public function escape_str($str, $like = FALSE)
191 {
192 if (is_array($str))
193 {
194 foreach ($str as $key => $val)
195 {
196 $str[$key] = $this->escape_str($val, $like);
197 }
198
199 return $str;
200 }
201
202 $str = $this->conn_id->escapeString(remove_invisible_characters($str));
203
204 // escape LIKE condition wildcards
205 if ($like === TRUE)
206 {
Andrey Andreevdc3de152012-03-12 15:41:49 +0200207 return str_replace(array($this->_like_escape_chr, '%', '_'),
208 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200209 $str);
210 }
211
212 return $str;
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Affected Rows
219 *
220 * @return int
221 */
222 public function affected_rows()
223 {
224 return $this->conn_id->changes();
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Insert ID
231 *
232 * @return int
233 */
234 public function insert_id()
235 {
236 return $this->conn_id->lastInsertRowID();
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200242 * Show table query
243 *
244 * Generates a platform-specific query string so that the table names can be fetched
245 *
246 * @param bool
247 * @return string
248 */
249 protected function _list_tables($prefix_limit = FALSE)
250 {
251 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
252 .(($prefix_limit !== FALSE && $this->dbprefix != '')
253 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
254 : '');
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Show column query
261 *
262 * Generates a platform-specific query string so that the column names can be fetched
263 *
264 * @param string the table name
265 * @return string
266 */
267 protected function _list_columns($table = '')
268 {
269 // Not supported
270 return FALSE;
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Field data query
277 *
278 * Generates a platform-specific query so that the column data can be retrieved
279 *
280 * @param string the table name
281 * @return string
282 */
283 protected function _field_data($table)
284 {
285 return 'SELECT * FROM '.$table.' LIMIT 0,1';
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * The error message string
292 *
293 * @return string
294 */
295 protected function _error_message()
296 {
297 return $this->conn_id->lastErrorMsg();
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * The error message number
304 *
305 * @return int
306 */
307 protected function _error_number()
308 {
309 return $this->conn_id->lastErrorCode();
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300315 * Replace statement
316 *
317 * Generates a platform-specific replace string from the supplied data
318 *
319 * @param string the table name
320 * @param array the insert keys
321 * @param array the insert values
322 * @return string
323 */
324 protected function _replace($table, $keys, $values)
325 {
326 return 'INSERT OR '.parent::_replace($table, $keys, $values);
327 }
328
329 // --------------------------------------------------------------------
330
331 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200332 * Truncate statement
333 *
334 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300335 *
336 * If the database does not support the truncate() command, then,
337 * then this method maps to 'DELETE FROM table'
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200338 *
339 * @param string the table name
340 * @return string
341 */
342 protected function _truncate($table)
343 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300344 return 'DELETE FROM '.$table;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200345 }
346
347 // --------------------------------------------------------------------
348
349 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200350 * Close DB Connection
351 *
352 * @return void
353 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300354 protected function _close()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200355 {
356 $this->conn_id->close();
357 }
358
359}
360
361/* End of file sqlite3_driver.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200362/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */