blob: f57c4b8c8fbc949dcf46e29a1fad262ad2ac6a3c [file] [log] [blame]
admin46537622006-09-24 18:13:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18
19
20/**
21 * SQLite Database Adapter Class
22 *
23 * Note: _DB is an extender class that the app controller
24 * creates dynamically based on whether the active record
25 * class is being used or not.
26 *
27 * @package CodeIgniter
28 * @subpackage Drivers
29 * @category Database
30 * @author Rick Ellis
31 * @link http://www.codeigniter.com/user_guide/database/
32 */
33class CI_DB_sqlite_driver extends CI_DB {
34
35 /**
36 * Non-persistent database connection
37 *
38 * @access private called by the base class
39 * @return resource
40 */
41 function db_connect()
42 {
43 if ( ! $conn_id = sqlite_open($this->database, 0666, $error))
44 {
45 log_message('error', $error);
46
47 if ($this->db_debug)
48 {
49 $this->display_error($error, '', TRUE);
50 }
51 }
52
53 return $conn_id;
54 }
55
56 // --------------------------------------------------------------------
57
58 /**
59 * Persistent database connection
60 *
61 * @access private called by the base class
62 * @return resource
63 */
64 function db_pconnect()
65 {
66 if ( ! $conn_id = sqlite_popen($this->database, 0666, $error))
67 {
68 log_message('error', $error);
69
70 if ($this->db_debug)
71 {
72 $this->display_error($error, '', TRUE);
73 }
74 }
75
76 return $conn_id;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Select the database
83 *
84 * @access private called by the base class
85 * @return resource
86 */
87 function db_select()
88 {
89 return TRUE;
90 }
admin9cd4e8e2006-09-25 23:26:25 +000091
92 // --------------------------------------------------------------------
93
94 /**
95 * Version number query string
96 *
97 * @access public
98 * @return string
99 */
100 function _version()
101 {
102 return sqlite_libversion();
103 }
admin46537622006-09-24 18:13:49 +0000104
105 // --------------------------------------------------------------------
106
107 /**
108 * Execute the query
109 *
110 * @access private called by the base class
111 * @param string an SQL query
112 * @return resource
113 */
114 function _execute($sql)
115 {
116 $sql = $this->_prep_query($sql);
117 return @sqlite_query($this->conn_id, $sql);
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Prep the query
124 *
125 * If needed, each database adapter can prep the query string
126 *
127 * @access private called by execute()
128 * @param string an SQL query
129 * @return string
130 */
131 function _prep_query($sql)
132 {
133 return $sql;
134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Begin Transaction
140 *
141 * @access public
142 * @return bool
143 */
144 function trans_begin($test_mode = FALSE)
145 {
146 if ( ! $this->trans_enabled)
147 {
148 return TRUE;
149 }
150
151 // When transactions are nested we only begin/commit/rollback the outermost ones
152 if ($this->_trans_depth > 0)
153 {
154 return TRUE;
155 }
156
157 // Reset the transaction failure flag.
158 // If the $test_mode flag is set to TRUE transactions will be rolled back
159 // even if the queries produce a successful result.
160 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
161
162 $this->simple_query('BEGIN TRANSACTION');
163 return TRUE;
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Commit Transaction
170 *
171 * @access public
172 * @return bool
173 */
174 function trans_commit()
175 {
176 if ( ! $this->trans_enabled)
177 {
178 return TRUE;
179 }
180
181 // When transactions are nested we only begin/commit/rollback the outermost ones
182 if ($this->_trans_depth > 0)
183 {
184 return TRUE;
185 }
186
187 $this->simple_query('COMMIT');
188 return TRUE;
189 }
190
191 // --------------------------------------------------------------------
192
193 /**
194 * Rollback Transaction
195 *
196 * @access public
197 * @return bool
198 */
199 function trans_rollback()
200 {
201 if ( ! $this->trans_enabled)
202 {
203 return TRUE;
204 }
205
206 // When transactions are nested we only begin/commit/rollback the outermost ones
207 if ($this->_trans_depth > 0)
208 {
209 return TRUE;
210 }
211
212 $this->simple_query('ROLLBACK');
213 return TRUE;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Escape String
220 *
221 * @access public
222 * @param string
223 * @return string
224 */
225 function escape_str($str)
226 {
227 return sqlite_escape_string($str);
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Affected Rows
234 *
235 * @access public
236 * @return integer
237 */
238 function affected_rows()
239 {
240 return sqlite_changes($this->conn_id);
241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Insert ID
247 *
248 * @access public
249 * @return integer
250 */
251 function insert_id()
252 {
253 return @sqlite_last_insert_rowid($this->conn_id);
254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * "Count All" query
260 *
261 * Generates a platform-specific query string that counts all records in
262 * the specified database
263 *
264 * @access public
265 * @param string
266 * @return string
267 */
268 function count_all($table = '')
269 {
270 if ($table == '')
271 return '0';
272
273 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
274
275 if ($query->num_rows() == 0)
276 return '0';
277
278 $row = $query->row();
279 return $row->numrows;
280 }
admin3dd978f2006-09-30 19:24:45 +0000281
282 // --------------------------------------------------------------------
283
284 /**
285 * List databases
286 *
287 * I don't believe you can do a database listing with SQLite
288 * since each database is its own file. I suppose we could
289 * try reading a directory looking for SQLite files, but
290 * that doesn't seem like a terribly good idea
291 *
292 * @access private
293 * @return bool
294 */
295 function _list_databases()
296 {
297 if ($this->db_debug)
298 {
299 return $this->display_error('db_unsuported_feature');
300 }
301 return array();
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * List table query
308 *
309 * Generates a platform-specific query string so that the table names can be fetched
310 *
311 * @access private
312 * @return string
313 */
314 function _list_tables()
315 {
316 return "SELECT name from sqlite_master WHERE type='table'";
317 }
318
admin46537622006-09-24 18:13:49 +0000319 // --------------------------------------------------------------------
320
321 /**
admin9cd4e8e2006-09-25 23:26:25 +0000322 * Show columnn query
323 *
324 * Generates a platform-specific query string so that the column names can be fetched
325 *
326 * @access public
327 * @param string the table name
328 * @return string
329 */
330 function _list_columns($table = '')
331 {
332 // Not supported
333 return FALSE;
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Field data query
340 *
341 * Generates a platform-specific query so that the column data can be retrieved
342 *
343 * @access public
344 * @param string the table name
345 * @return object
346 */
347 function _field_data($table)
348 {
349 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
admin46537622006-09-24 18:13:49 +0000355 * The error message string
356 *
adminbb1d4392006-09-24 20:14:38 +0000357 * @access private
admin46537622006-09-24 18:13:49 +0000358 * @return string
359 */
adminbb1d4392006-09-24 20:14:38 +0000360 function _error_message()
admin46537622006-09-24 18:13:49 +0000361 {
362 return sqlite_error_string(sqlite_last_error($this->conn_id));
363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * The error message number
369 *
adminbb1d4392006-09-24 20:14:38 +0000370 * @access private
admin46537622006-09-24 18:13:49 +0000371 * @return integer
372 */
adminbb1d4392006-09-24 20:14:38 +0000373 function _error_number()
admin46537622006-09-24 18:13:49 +0000374 {
375 return sqlite_last_error($this->conn_id);
376 }
adminbb1d4392006-09-24 20:14:38 +0000377
admin46537622006-09-24 18:13:49 +0000378 // --------------------------------------------------------------------
379
380 /**
381 * Escape Table Name
382 *
383 * This function adds backticks if the table name has a period
384 * in it. Some DBs will get cranky unless periods are escaped
385 *
adminbb1d4392006-09-24 20:14:38 +0000386 * @access private
admin46537622006-09-24 18:13:49 +0000387 * @param string the table name
388 * @return string
389 */
adminbb1d4392006-09-24 20:14:38 +0000390 function _escape_table($table)
admin46537622006-09-24 18:13:49 +0000391 {
392 if (stristr($table, '.'))
393 {
394 $table = preg_replace("/\./", "`.`", $table);
395 }
396
397 return $table;
398 }
adminbb1d4392006-09-24 20:14:38 +0000399
admin46537622006-09-24 18:13:49 +0000400 // --------------------------------------------------------------------
401
402 /**
403 * Insert statement
404 *
405 * Generates a platform-specific insert string from the supplied data
406 *
407 * @access public
408 * @param string the table name
409 * @param array the insert keys
410 * @param array the insert values
411 * @return string
412 */
413 function _insert($table, $keys, $values)
414 {
adminbb1d4392006-09-24 20:14:38 +0000415 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin46537622006-09-24 18:13:49 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Update statement
422 *
423 * Generates a platform-specific update string from the supplied data
424 *
425 * @access public
426 * @param string the table name
427 * @param array the update data
428 * @param array the where clause
429 * @return string
430 */
431 function _update($table, $values, $where)
432 {
433 foreach($values as $key => $val)
434 {
435 $valstr[] = $key." = ".$val;
436 }
437
adminbb1d4392006-09-24 20:14:38 +0000438 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000439 }
440
441 // --------------------------------------------------------------------
442
443 /**
444 * Delete statement
445 *
446 * Generates a platform-specific delete string from the supplied data
447 *
448 * @access public
449 * @param string the table name
450 * @param array the where clause
451 * @return string
452 */
453 function _delete($table, $where)
454 {
adminbb1d4392006-09-24 20:14:38 +0000455 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000456 }
admin46537622006-09-24 18:13:49 +0000457
admin46537622006-09-24 18:13:49 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * Limit string
462 *
463 * Generates a platform-specific LIMIT clause
464 *
465 * @access public
466 * @param string the sql query string
467 * @param integer the number of rows to limit the query to
468 * @param integer the offset value
469 * @return string
470 */
471 function _limit($sql, $limit, $offset)
472 {
473 if ($offset == 0)
474 {
475 $offset = '';
476 }
477 else
478 {
479 $offset .= ", ";
480 }
481
482 return $sql."LIMIT ".$offset.$limit;
483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * Close DB Connection
489 *
490 * @access public
491 * @param resource
492 * @return void
493 */
494 function _close($conn_id)
495 {
496 sqlite_close($conn_id);
497 }
498
adminbb1d4392006-09-24 20:14:38 +0000499
admin46537622006-09-24 18:13:49 +0000500}
501
502?>