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