blob: 22c72b9b83a0d391fdc63c57bfec2c987b0cb05c [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
Andrey Andreevcb9f3612012-01-26 02:06:48 +020049 protected $_random_keyword = ' RANDOM()';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020050
51 /**
52 * Non-persistent database connection
53 *
54 * @return object type SQLite3
55 */
56 public function db_connect()
57 {
58 try
59 {
60 return ( ! $this->password)
61 ? new SQLite3($this->database)
62 : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
63 }
64 catch (Exception $e)
65 {
66 return FALSE;
67 }
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Persistent database connection
74 *
75 * @return object type SQLite3
76 */
77 public function db_pconnect()
78 {
79 log_message('debug', 'SQLite3 doesn\'t support persistent connections');
Andrey Andreev9d533ae2012-06-04 15:56:56 +030080 return $this->db_connect();
Andrey Andreev8ae24c52012-01-16 13:05:23 +020081 }
82
83 // --------------------------------------------------------------------
84
85 /**
Andrey Andreev80e34f92012-03-03 03:25:23 +020086 * Database version number
Andrey Andreev8ae24c52012-01-16 13:05:23 +020087 *
88 * @return string
89 */
Andrey Andreev80e34f92012-03-03 03:25:23 +020090 public function version()
Andrey Andreev8ae24c52012-01-16 13:05:23 +020091 {
Andrey Andreev80e34f92012-03-03 03:25:23 +020092 if (isset($this->data_cache['version']))
93 {
94 return $this->data_cache['version'];
95 }
96
Andrey Andreevfc11dcc2012-06-04 16:39:19 +030097 $version = SQLite3::version();
Andrey Andreev80e34f92012-03-03 03:25:23 +020098 return $this->data_cache['version'] = $version['versionString'];
Andrey Andreev8ae24c52012-01-16 13:05:23 +020099 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Execute the query
105 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300106 * @todo Implement use of SQLite3::querySingle(), if needed
107 * @param string $sql
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200108 * @return mixed SQLite3Result object or bool
109 */
110 protected function _execute($sql)
111 {
Andrey Andreeva92c7cd2012-03-02 13:51:22 +0200112 return $this->is_write_type($sql)
113 ? $this->conn_id->exec($sql)
114 : $this->conn_id->query($sql);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200115 }
116
117 // --------------------------------------------------------------------
118
119 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200120 * Begin Transaction
121 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300122 * @param bool $test_mode = FALSE
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200123 * @return bool
124 */
125 public function trans_begin($test_mode = FALSE)
126 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200127 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200128 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200129 {
130 return TRUE;
131 }
132
133 // Reset the transaction failure flag.
134 // If the $test_mode flag is set to TRUE transactions will be rolled back
135 // even if the queries produce a successful result.
136 $this->_trans_failure = ($test_mode === TRUE);
137
138 return $this->conn_id->exec('BEGIN TRANSACTION');
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Commit Transaction
145 *
146 * @return bool
147 */
148 public function trans_commit()
149 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200150 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200151 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200152 {
153 return TRUE;
154 }
155
156 return $this->conn_id->exec('END TRANSACTION');
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Rollback Transaction
163 *
164 * @return bool
165 */
166 public function trans_rollback()
167 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200168 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200169 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200170 {
171 return TRUE;
172 }
173
174 return $this->conn_id->exec('ROLLBACK');
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Escape String
181 *
182 * @param string
183 * @param bool whether or not the string will be used in a LIKE condition
184 * @return string
185 */
186 public function escape_str($str, $like = FALSE)
187 {
188 if (is_array($str))
189 {
190 foreach ($str as $key => $val)
191 {
192 $str[$key] = $this->escape_str($val, $like);
193 }
194
195 return $str;
196 }
197
198 $str = $this->conn_id->escapeString(remove_invisible_characters($str));
199
200 // escape LIKE condition wildcards
201 if ($like === TRUE)
202 {
Andrey Andreevdc3de152012-03-12 15:41:49 +0200203 return str_replace(array($this->_like_escape_chr, '%', '_'),
204 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200205 $str);
206 }
207
208 return $str;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Affected Rows
215 *
216 * @return int
217 */
218 public function affected_rows()
219 {
220 return $this->conn_id->changes();
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Insert ID
227 *
228 * @return int
229 */
230 public function insert_id()
231 {
232 return $this->conn_id->lastInsertRowID();
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200238 * Show table query
239 *
240 * Generates a platform-specific query string so that the table names can be fetched
241 *
242 * @param bool
243 * @return string
244 */
245 protected function _list_tables($prefix_limit = FALSE)
246 {
247 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
248 .(($prefix_limit !== FALSE && $this->dbprefix != '')
249 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
250 : '');
251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Show column query
257 *
258 * Generates a platform-specific query string so that the column names can be fetched
259 *
260 * @param string the table name
261 * @return string
262 */
263 protected function _list_columns($table = '')
264 {
265 // Not supported
266 return FALSE;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Field data query
273 *
274 * Generates a platform-specific query so that the column data can be retrieved
275 *
276 * @param string the table name
277 * @return string
278 */
279 protected function _field_data($table)
280 {
281 return 'SELECT * FROM '.$table.' LIMIT 0,1';
282 }
283
284 // --------------------------------------------------------------------
285
286 /**
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300287 * Error
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200288 *
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300289 * Returns an array containing code and message of the last
290 * database error that has occured.
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200291 *
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300292 * @return array
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200293 */
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300294 public function error()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200295 {
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300296 return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300302 * Replace statement
303 *
304 * Generates a platform-specific replace string from the supplied data
305 *
306 * @param string the table name
307 * @param array the insert keys
308 * @param array the insert values
309 * @return string
310 */
311 protected function _replace($table, $keys, $values)
312 {
313 return 'INSERT OR '.parent::_replace($table, $keys, $values);
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200319 * Truncate statement
320 *
321 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300322 *
323 * If the database does not support the truncate() command, then,
324 * then this method maps to 'DELETE FROM table'
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200325 *
326 * @param string the table name
327 * @return string
328 */
329 protected function _truncate($table)
330 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300331 return 'DELETE FROM '.$table;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200332 }
333
334 // --------------------------------------------------------------------
335
336 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200337 * Close DB Connection
338 *
339 * @return void
340 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300341 protected function _close()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200342 {
343 $this->conn_id->close();
344 }
345
346}
347
348/* End of file sqlite3_driver.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200349/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */