blob: 6189b1ff0039688efcea2ecf880946d0b6efbc12 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
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, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @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 /**
Derek Allard694b5b82007-12-18 15:58:03 +000036 * The syntax to count rows is slightly different across different
37 * database engines, so this string appears in each driver and is
38 * used for the count_all() and count_all_results() functions.
39 */
Derek Allard6ddb5a12007-12-18 17:22:50 +000040 var $_count_string = "SELECT COUNT(*) AS numrows ";
41 var $_random_keyword = ' Random()'; // database specific random keyword
Derek Allard694b5b82007-12-18 15:58:03 +000042
43 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000044 * Non-persistent database connection
45 *
46 * @access private called by the base class
47 * @return resource
48 */
49 function db_connect()
50 {
51 if ( ! $conn_id = @sqlite_open($this->database, 0666, $error))
52 {
53 log_message('error', $error);
54
55 if ($this->db_debug)
56 {
57 $this->display_error($error, '', TRUE);
58 }
59
60 return FALSE;
61 }
62
63 return $conn_id;
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Persistent database connection
70 *
71 * @access private called by the base class
72 * @return resource
73 */
74 function db_pconnect()
75 {
76 if ( ! $conn_id = @sqlite_popen($this->database, 0666, $error))
77 {
78 log_message('error', $error);
79
80 if ($this->db_debug)
81 {
82 $this->display_error($error, '', TRUE);
83 }
84
85 return FALSE;
86 }
87
88 return $conn_id;
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Select the database
95 *
96 * @access private called by the base class
97 * @return resource
98 */
99 function db_select()
100 {
101 return TRUE;
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Version number query string
108 *
109 * @access public
110 * @return string
111 */
112 function _version()
113 {
114 return sqlite_libversion();
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Execute the query
121 *
122 * @access private called by the base class
123 * @param string an SQL query
124 * @return resource
125 */
126 function _execute($sql)
127 {
128 $sql = $this->_prep_query($sql);
129 return @sqlite_query($this->conn_id, $sql);
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Prep the query
136 *
137 * If needed, each database adapter can prep the query string
138 *
139 * @access private called by execute()
140 * @param string an SQL query
141 * @return string
142 */
143 function _prep_query($sql)
144 {
145 return $sql;
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Begin Transaction
152 *
153 * @access public
154 * @return bool
155 */
156 function trans_begin($test_mode = FALSE)
157 {
158 if ( ! $this->trans_enabled)
159 {
160 return TRUE;
161 }
162
163 // When transactions are nested we only begin/commit/rollback the outermost ones
164 if ($this->_trans_depth > 0)
165 {
166 return TRUE;
167 }
168
169 // Reset the transaction failure flag.
170 // If the $test_mode flag is set to TRUE transactions will be rolled back
171 // even if the queries produce a successful result.
172 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
173
174 $this->simple_query('BEGIN TRANSACTION');
175 return TRUE;
176 }
177
178 // --------------------------------------------------------------------
179
180 /**
181 * Commit Transaction
182 *
183 * @access public
184 * @return bool
185 */
186 function trans_commit()
187 {
188 if ( ! $this->trans_enabled)
189 {
190 return TRUE;
191 }
192
193 // When transactions are nested we only begin/commit/rollback the outermost ones
194 if ($this->_trans_depth > 0)
195 {
196 return TRUE;
197 }
198
199 $this->simple_query('COMMIT');
200 return TRUE;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Rollback Transaction
207 *
208 * @access public
209 * @return bool
210 */
211 function trans_rollback()
212 {
213 if ( ! $this->trans_enabled)
214 {
215 return TRUE;
216 }
217
218 // When transactions are nested we only begin/commit/rollback the outermost ones
219 if ($this->_trans_depth > 0)
220 {
221 return TRUE;
222 }
223
224 $this->simple_query('ROLLBACK');
225 return TRUE;
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Escape String
232 *
233 * @access public
234 * @param string
235 * @return string
236 */
237 function escape_str($str)
238 {
239 return sqlite_escape_string($str);
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Affected Rows
246 *
247 * @access public
248 * @return integer
249 */
250 function affected_rows()
251 {
252 return sqlite_changes($this->conn_id);
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Insert ID
259 *
260 * @access public
261 * @return integer
262 */
263 function insert_id()
264 {
265 return @sqlite_last_insert_rowid($this->conn_id);
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * "Count All" query
272 *
273 * Generates a platform-specific query string that counts all records in
274 * the specified database
275 *
276 * @access public
277 * @param string
278 * @return string
279 */
280 function count_all($table = '')
281 {
282 if ($table == '')
283 return '0';
284
Derek Allard6ddb5a12007-12-18 17:22:50 +0000285 $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
Derek Allardd2df9bc2007-04-15 17:41:17 +0000286
287 if ($query->num_rows() == 0)
288 return '0';
289
290 $row = $query->row();
291 return $row->numrows;
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * List table query
298 *
299 * Generates a platform-specific query string so that the table names can be fetched
300 *
301 * @access private
302 * @return string
303 */
304 function _list_tables()
305 {
306 return "SELECT name from sqlite_master WHERE type='table'";
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Show column query
313 *
314 * Generates a platform-specific query string so that the column names can be fetched
315 *
316 * @access public
317 * @param string the table name
318 * @return string
319 */
320 function _list_columns($table = '')
321 {
322 // Not supported
323 return FALSE;
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Field data query
330 *
331 * Generates a platform-specific query so that the column data can be retrieved
332 *
333 * @access public
334 * @param string the table name
335 * @return object
336 */
337 function _field_data($table)
338 {
339 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * The error message string
346 *
347 * @access private
348 * @return string
349 */
350 function _error_message()
351 {
352 return sqlite_error_string(sqlite_last_error($this->conn_id));
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * The error message number
359 *
360 * @access private
361 * @return integer
362 */
363 function _error_number()
364 {
365 return sqlite_last_error($this->conn_id);
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Escape Table Name
372 *
373 * This function adds backticks if the table name has a period
374 * in it. Some DBs will get cranky unless periods are escaped
375 *
376 * @access private
377 * @param string the table name
378 * @return string
379 */
380 function _escape_table($table)
381 {
382 if (stristr($table, '.'))
383 {
384 $table = preg_replace("/\./", "`.`", $table);
385 }
386
387 return $table;
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Insert statement
394 *
395 * Generates a platform-specific insert string from the supplied data
396 *
397 * @access public
398 * @param string the table name
399 * @param array the insert keys
400 * @param array the insert values
401 * @return string
402 */
403 function _insert($table, $keys, $values)
404 {
405 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Update statement
412 *
413 * Generates a platform-specific update string from the supplied data
414 *
415 * @access public
416 * @param string the table name
417 * @param array the update data
418 * @param array the where clause
419 * @return string
420 */
Derek Allardda6d2402007-12-19 14:49:29 +0000421 function _update($table, $values, $where, $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000422 {
423 foreach($values as $key => $val)
424 {
425 $valstr[] = $key." = ".$val;
426 }
Derek Allardda6d2402007-12-19 14:49:29 +0000427
428 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000429
Derek Allardda6d2402007-12-19 14:49:29 +0000430 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Delete statement
437 *
438 * Generates a platform-specific delete string from the supplied data
439 *
440 * @access public
441 * @param string the table name
442 * @param array the where clause
443 * @return string
444 */
Derek Allarde77d77c2007-12-19 15:01:55 +0000445 function _delete($table, $where, $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000446 {
Derek Allarde77d77c2007-12-19 15:01:55 +0000447 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
448
449 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where).$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Limit string
456 *
457 * Generates a platform-specific LIMIT clause
458 *
459 * @access public
460 * @param string the sql query string
461 * @param integer the number of rows to limit the query to
462 * @param integer the offset value
463 * @return string
464 */
465 function _limit($sql, $limit, $offset)
466 {
467 if ($offset == 0)
468 {
469 $offset = '';
470 }
471 else
472 {
473 $offset .= ", ";
474 }
475
476 return $sql."LIMIT ".$offset.$limit;
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
482 * Close DB Connection
483 *
484 * @access public
485 * @param resource
486 * @return void
487 */
488 function _close($conn_id)
489 {
490 @sqlite_close($conn_id);
491 }
492
493
494}
495
admin46537622006-09-24 18:13:49 +0000496?>