| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2242 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2242 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.3.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* SQLite Database Adapter Class
|
|
|
42 |
*
|
|
|
43 |
* Note: _DB is an extender class that the app controller
|
|
|
44 |
* creates dynamically based on whether the query builder
|
|
|
45 |
* class is being used or not.
|
|
|
46 |
*
|
|
|
47 |
* @package CodeIgniter
|
|
|
48 |
* @subpackage Drivers
|
|
|
49 |
* @category Database
|
|
|
50 |
* @author EllisLab Dev Team
|
|
|
51 |
* @link https://codeigniter.com/user_guide/database/
|
|
|
52 |
*/
|
|
|
53 |
class CI_DB_sqlite_driver extends CI_DB {
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Database driver
|
|
|
57 |
*
|
|
|
58 |
* @var string
|
|
|
59 |
*/
|
|
|
60 |
public $dbdriver = 'sqlite';
|
|
|
61 |
|
|
|
62 |
// --------------------------------------------------------------------
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* ORDER BY random keyword
|
|
|
66 |
*
|
|
|
67 |
* @var array
|
|
|
68 |
*/
|
|
|
69 |
protected $_random_keyword = array('RANDOM()', 'RANDOM()');
|
|
|
70 |
|
|
|
71 |
// --------------------------------------------------------------------
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Non-persistent database connection
|
|
|
75 |
*
|
|
|
76 |
* @param bool $persistent
|
|
|
77 |
* @return resource
|
|
|
78 |
*/
|
|
|
79 |
public function db_connect($persistent = FALSE)
|
|
|
80 |
{
|
|
|
81 |
$error = NULL;
|
|
|
82 |
$conn_id = ($persistent === TRUE)
|
|
|
83 |
? sqlite_popen($this->database, 0666, $error)
|
|
|
84 |
: sqlite_open($this->database, 0666, $error);
|
|
|
85 |
|
|
|
86 |
isset($error) && log_message('error', $error);
|
|
|
87 |
|
|
|
88 |
return $conn_id;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// --------------------------------------------------------------------
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Database version number
|
|
|
95 |
*
|
|
|
96 |
* @return string
|
|
|
97 |
*/
|
|
|
98 |
public function version()
|
|
|
99 |
{
|
|
|
100 |
return isset($this->data_cache['version'])
|
|
|
101 |
? $this->data_cache['version']
|
|
|
102 |
: $this->data_cache['version'] = sqlite_libversion();
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
// --------------------------------------------------------------------
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* Execute the query
|
|
|
109 |
*
|
|
|
110 |
* @param string $sql an SQL query
|
|
|
111 |
* @return resource
|
|
|
112 |
*/
|
|
|
113 |
protected function _execute($sql)
|
|
|
114 |
{
|
|
|
115 |
return $this->is_write_type($sql)
|
|
|
116 |
? sqlite_exec($this->conn_id, $sql)
|
|
|
117 |
: sqlite_query($this->conn_id, $sql);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
// --------------------------------------------------------------------
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Begin Transaction
|
|
|
124 |
*
|
|
|
125 |
* @return bool
|
|
|
126 |
*/
|
|
|
127 |
protected function _trans_begin()
|
|
|
128 |
{
|
|
|
129 |
return $this->simple_query('BEGIN TRANSACTION');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// --------------------------------------------------------------------
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* Commit Transaction
|
|
|
136 |
*
|
|
|
137 |
* @return bool
|
|
|
138 |
*/
|
|
|
139 |
protected function _trans_commit()
|
|
|
140 |
{
|
|
|
141 |
return $this->simple_query('COMMIT');
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
// --------------------------------------------------------------------
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Rollback Transaction
|
|
|
148 |
*
|
|
|
149 |
* @return bool
|
|
|
150 |
*/
|
|
|
151 |
protected function _trans_rollback()
|
|
|
152 |
{
|
|
|
153 |
return $this->simple_query('ROLLBACK');
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// --------------------------------------------------------------------
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Platform-dependant string escape
|
|
|
160 |
*
|
|
|
161 |
* @param string
|
|
|
162 |
* @return string
|
|
|
163 |
*/
|
|
|
164 |
protected function _escape_str($str)
|
|
|
165 |
{
|
|
|
166 |
return sqlite_escape_string($str);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// --------------------------------------------------------------------
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Affected Rows
|
|
|
173 |
*
|
|
|
174 |
* @return int
|
|
|
175 |
*/
|
|
|
176 |
public function affected_rows()
|
|
|
177 |
{
|
|
|
178 |
return sqlite_changes($this->conn_id);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
// --------------------------------------------------------------------
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Insert ID
|
|
|
185 |
*
|
|
|
186 |
* @return int
|
|
|
187 |
*/
|
|
|
188 |
public function insert_id()
|
|
|
189 |
{
|
|
|
190 |
return sqlite_last_insert_rowid($this->conn_id);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
// --------------------------------------------------------------------
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* List table query
|
|
|
197 |
*
|
|
|
198 |
* Generates a platform-specific query string so that the table names can be fetched
|
|
|
199 |
*
|
|
|
200 |
* @param bool $prefix_limit
|
|
|
201 |
* @return string
|
|
|
202 |
*/
|
|
|
203 |
protected function _list_tables($prefix_limit = FALSE)
|
|
|
204 |
{
|
|
|
205 |
$sql = "SELECT name FROM sqlite_master WHERE type='table'";
|
|
|
206 |
|
|
|
207 |
if ($prefix_limit !== FALSE && $this->dbprefix != '')
|
|
|
208 |
{
|
|
|
209 |
return $sql." AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
return $sql;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
// --------------------------------------------------------------------
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Show column query
|
|
|
219 |
*
|
|
|
220 |
* Generates a platform-specific query string so that the column names can be fetched
|
|
|
221 |
*
|
|
|
222 |
* @param string $table
|
|
|
223 |
* @return bool
|
|
|
224 |
*/
|
|
|
225 |
protected function _list_columns($table = '')
|
|
|
226 |
{
|
|
|
227 |
// Not supported
|
|
|
228 |
return FALSE;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
// --------------------------------------------------------------------
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Returns an object with field data
|
|
|
235 |
*
|
|
|
236 |
* @param string $table
|
|
|
237 |
* @return array
|
|
|
238 |
*/
|
|
|
239 |
public function field_data($table)
|
|
|
240 |
{
|
|
|
241 |
if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
|
|
|
242 |
{
|
|
|
243 |
return FALSE;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
$query = $query->result_array();
|
|
|
247 |
if (empty($query))
|
|
|
248 |
{
|
|
|
249 |
return FALSE;
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
$retval = array();
|
|
|
253 |
for ($i = 0, $c = count($query); $i < $c; $i++)
|
|
|
254 |
{
|
|
|
255 |
$retval[$i] = new stdClass();
|
|
|
256 |
$retval[$i]->name = $query[$i]['name'];
|
|
|
257 |
$retval[$i]->type = $query[$i]['type'];
|
|
|
258 |
$retval[$i]->max_length = NULL;
|
|
|
259 |
$retval[$i]->default = $query[$i]['dflt_value'];
|
|
|
260 |
$retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
return $retval;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
// --------------------------------------------------------------------
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* Error
|
|
|
270 |
*
|
|
|
271 |
* Returns an array containing code and message of the last
|
|
|
272 |
* database error that has occured.
|
|
|
273 |
*
|
|
|
274 |
* @return array
|
|
|
275 |
*/
|
|
|
276 |
public function error()
|
|
|
277 |
{
|
|
|
278 |
$error = array('code' => sqlite_last_error($this->conn_id));
|
|
|
279 |
$error['message'] = sqlite_error_string($error['code']);
|
|
|
280 |
return $error;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
// --------------------------------------------------------------------
|
|
|
284 |
|
|
|
285 |
/**
|
|
|
286 |
* Replace statement
|
|
|
287 |
*
|
|
|
288 |
* Generates a platform-specific replace string from the supplied data
|
|
|
289 |
*
|
|
|
290 |
* @param string $table Table name
|
|
|
291 |
* @param array $keys INSERT keys
|
|
|
292 |
* @param array $values INSERT values
|
|
|
293 |
* @return string
|
|
|
294 |
*/
|
|
|
295 |
protected function _replace($table, $keys, $values)
|
|
|
296 |
{
|
|
|
297 |
return 'INSERT OR '.parent::_replace($table, $keys, $values);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
// --------------------------------------------------------------------
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Truncate statement
|
|
|
304 |
*
|
|
|
305 |
* Generates a platform-specific truncate string from the supplied data
|
|
|
306 |
*
|
|
|
307 |
* If the database does not support the TRUNCATE statement,
|
|
|
308 |
* then this function maps to 'DELETE FROM table'
|
|
|
309 |
*
|
|
|
310 |
* @param string $table
|
|
|
311 |
* @return string
|
|
|
312 |
*/
|
|
|
313 |
protected function _truncate($table)
|
|
|
314 |
{
|
|
|
315 |
return 'DELETE FROM '.$table;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
// --------------------------------------------------------------------
|
|
|
319 |
|
|
|
320 |
/**
|
|
|
321 |
* Close DB Connection
|
|
|
322 |
*
|
|
|
323 |
* @return void
|
|
|
324 |
*/
|
|
|
325 |
protected function _close()
|
|
|
326 |
{
|
|
|
327 |
sqlite_close($this->conn_id);
|
|
|
328 |
}
|
|
|
329 |
|
|
|
330 |
}
|