blob: 9a2c1eefcc78a0c09fbe20cc696ecf309bc17397 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Andrey Andreev8ae24c52012-01-16 13:05:23 +02002/**
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Andrey Andreev8ae24c52012-01-16 13:05:23 +020028
29/**
30 * SQLite3 Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelowffe7a0a2012-04-26 13:48:18 +010033 * creates dynamically based on whether the query builder
Andrey Andreev8ae24c52012-01-16 13:05:23 +020034 * class is being used or not.
35 *
Andrey Andreev17ceeae2012-04-05 15:38:30 +030036 * @package CodeIgniter
Andrey Andreev8ae24c52012-01-16 13:05:23 +020037 * @subpackage Drivers
38 * @category Database
Andrey Andreev17ceeae2012-04-05 15:38:30 +030039 * @author Andrey Andreev
40 * @link http://codeigniter.com/user_guide/database/
41 * @since Version 3.0
Andrey Andreev8ae24c52012-01-16 13:05:23 +020042 */
43class CI_DB_sqlite3_driver extends CI_DB {
44
Andrey Andreeva24e52e2012-11-02 03:54:12 +020045 /**
46 * Database driver
47 *
48 * @var string
49 */
Andrey Andreev8ae24c52012-01-16 13:05:23 +020050 public $dbdriver = 'sqlite3';
51
Andrey Andreeva24e52e2012-11-02 03:54:12 +020052 // --------------------------------------------------------------------
Andrey Andreev8ae24c52012-01-16 13:05:23 +020053
Andrey Andreeva24e52e2012-11-02 03:54:12 +020054 /**
55 * ORDER BY random keyword
56 *
57 * @var string
58 */
Andrey Andreevcb9f3612012-01-26 02:06:48 +020059 protected $_random_keyword = ' RANDOM()';
Andrey Andreev8ae24c52012-01-16 13:05:23 +020060
Andrey Andreeva24e52e2012-11-02 03:54:12 +020061 // --------------------------------------------------------------------
62
Andrey Andreev8ae24c52012-01-16 13:05:23 +020063 /**
64 * Non-persistent database connection
65 *
66 * @return object type SQLite3
67 */
68 public function db_connect()
69 {
70 try
71 {
72 return ( ! $this->password)
73 ? new SQLite3($this->database)
74 : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
75 }
76 catch (Exception $e)
77 {
78 return FALSE;
79 }
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * Persistent database connection
86 *
87 * @return object type SQLite3
88 */
89 public function db_pconnect()
90 {
91 log_message('debug', 'SQLite3 doesn\'t support persistent connections');
Andrey Andreev9d533ae2012-06-04 15:56:56 +030092 return $this->db_connect();
Andrey Andreev8ae24c52012-01-16 13:05:23 +020093 }
94
95 // --------------------------------------------------------------------
96
97 /**
Andrey Andreev80e34f92012-03-03 03:25:23 +020098 * Database version number
Andrey Andreev8ae24c52012-01-16 13:05:23 +020099 *
100 * @return string
101 */
Andrey Andreev80e34f92012-03-03 03:25:23 +0200102 public function version()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200103 {
Andrey Andreev80e34f92012-03-03 03:25:23 +0200104 if (isset($this->data_cache['version']))
105 {
106 return $this->data_cache['version'];
107 }
108
Andrey Andreevfc11dcc2012-06-04 16:39:19 +0300109 $version = SQLite3::version();
Andrey Andreev80e34f92012-03-03 03:25:23 +0200110 return $this->data_cache['version'] = $version['versionString'];
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Execute the query
117 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300118 * @todo Implement use of SQLite3::querySingle(), if needed
119 * @param string $sql
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200120 * @return mixed SQLite3Result object or bool
121 */
122 protected function _execute($sql)
123 {
Andrey Andreeva92c7cd2012-03-02 13:51:22 +0200124 return $this->is_write_type($sql)
125 ? $this->conn_id->exec($sql)
126 : $this->conn_id->query($sql);
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200127 }
128
129 // --------------------------------------------------------------------
130
131 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200132 * Begin Transaction
133 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200134 * @param bool $test_mode
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200135 * @return bool
136 */
137 public function trans_begin($test_mode = FALSE)
138 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200139 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200140 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200141 {
142 return TRUE;
143 }
144
145 // Reset the transaction failure flag.
146 // If the $test_mode flag is set to TRUE transactions will be rolled back
147 // even if the queries produce a successful result.
148 $this->_trans_failure = ($test_mode === TRUE);
149
150 return $this->conn_id->exec('BEGIN TRANSACTION');
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Commit Transaction
157 *
158 * @return bool
159 */
160 public function trans_commit()
161 {
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200162 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev72d7a6e2012-01-19 16:02:32 +0200163 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200164 {
165 return TRUE;
166 }
167
168 return $this->conn_id->exec('END TRANSACTION');
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Rollback Transaction
175 *
176 * @return bool
177 */
178 public function trans_rollback()
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 return $this->conn_id->exec('ROLLBACK');
187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Escape String
193 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200194 * @param string $str
195 * @param bool $like Whether or not the string will be used in a LIKE condition
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200196 * @return string
197 */
198 public function escape_str($str, $like = FALSE)
199 {
200 if (is_array($str))
201 {
202 foreach ($str as $key => $val)
203 {
204 $str[$key] = $this->escape_str($val, $like);
205 }
206
207 return $str;
208 }
209
210 $str = $this->conn_id->escapeString(remove_invisible_characters($str));
211
212 // escape LIKE condition wildcards
213 if ($like === TRUE)
214 {
Andrey Andreevdc3de152012-03-12 15:41:49 +0200215 return str_replace(array($this->_like_escape_chr, '%', '_'),
216 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200217 $str);
218 }
219
220 return $str;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Affected Rows
227 *
228 * @return int
229 */
230 public function affected_rows()
231 {
232 return $this->conn_id->changes();
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Insert ID
239 *
240 * @return int
241 */
242 public function insert_id()
243 {
244 return $this->conn_id->lastInsertRowID();
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200250 * Show table query
251 *
252 * Generates a platform-specific query string so that the table names can be fetched
253 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200254 * @param bool $prefix_limit
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200255 * @return string
256 */
257 protected function _list_tables($prefix_limit = FALSE)
258 {
259 return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
260 .(($prefix_limit !== FALSE && $this->dbprefix != '')
261 ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
262 : '');
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Show column query
269 *
270 * Generates a platform-specific query string so that the column names can be fetched
271 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200272 * @param string $table
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200273 * @return string
274 */
275 protected function _list_columns($table = '')
276 {
277 // Not supported
278 return FALSE;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Field data query
285 *
286 * Generates a platform-specific query so that the column data can be retrieved
287 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200288 * @param string $table
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200289 * @return string
290 */
291 protected function _field_data($table)
292 {
293 return 'SELECT * FROM '.$table.' LIMIT 0,1';
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300299 * Error
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200300 *
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300301 * Returns an array containing code and message of the last
302 * database error that has occured.
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200303 *
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300304 * @return array
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200305 */
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300306 public function error()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200307 {
Andrey Andreevebbfefa2012-10-05 17:46:47 +0300308 return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300314 * Replace statement
315 *
316 * Generates a platform-specific replace string from the supplied data
317 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200318 * @param string $table Table name
319 * @param array $keys INSERT keys
320 * @param array $values INSERT values
Andrey Andreev17ceeae2012-04-05 15:38:30 +0300321 * @return string
322 */
323 protected function _replace($table, $keys, $values)
324 {
325 return 'INSERT OR '.parent::_replace($table, $keys, $values);
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200331 * Truncate statement
332 *
333 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300334 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300336 * then this method maps to 'DELETE FROM table'
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200337 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200338 * @param string $table
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200339 * @return string
340 */
341 protected function _truncate($table)
342 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300343 return 'DELETE FROM '.$table;
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200344 }
345
346 // --------------------------------------------------------------------
347
348 /**
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200349 * Close DB Connection
350 *
351 * @return void
352 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300353 protected function _close()
Andrey Andreev8ae24c52012-01-16 13:05:23 +0200354 {
355 $this->conn_id->close();
356 }
357
358}
359
360/* End of file sqlite3_driver.php */
Andrey Andreevf944d3b2012-03-20 22:12:55 +0200361/* Location: ./system/database/drivers/sqlite3/sqlite3_driver.php */