blob: 877f445bb83201a9bd3a50e3a99290854e277c6d [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin46537622006-09-24 18:13:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin46537622006-09-24 18:13:49 +000016// ------------------------------------------------------------------------
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 {
admin7acd5812006-10-23 21:37:22 +000043 if ( ! $conn_id = @sqlite_open($this->database, 0666, $error))
admin46537622006-09-24 18:13:49 +000044 {
45 log_message('error', $error);
46
admine334c472006-10-21 19:44:22 +000047 if ($this->db_debug)
48 {
admin46537622006-09-24 18:13:49 +000049 $this->display_error($error, '', TRUE);
admin7acd5812006-10-23 21:37:22 +000050 }
51
52 return FALSE;
admin46537622006-09-24 18:13:49 +000053 }
54
55 return $conn_id;
56 }
57
58 // --------------------------------------------------------------------
59
60 /**
61 * Persistent database connection
62 *
63 * @access private called by the base class
64 * @return resource
65 */
66 function db_pconnect()
67 {
admin7acd5812006-10-23 21:37:22 +000068 if ( ! $conn_id = @sqlite_popen($this->database, 0666, $error))
admin46537622006-09-24 18:13:49 +000069 {
70 log_message('error', $error);
71
admine334c472006-10-21 19:44:22 +000072 if ($this->db_debug)
73 {
admin46537622006-09-24 18:13:49 +000074 $this->display_error($error, '', TRUE);
admin7acd5812006-10-23 21:37:22 +000075 }
76
77 return FALSE;
admin46537622006-09-24 18:13:49 +000078 }
79
80 return $conn_id;
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Select the database
87 *
88 * @access private called by the base class
89 * @return resource
90 */
91 function db_select()
92 {
93 return TRUE;
94 }
admin9cd4e8e2006-09-25 23:26:25 +000095
96 // --------------------------------------------------------------------
97
98 /**
99 * Version number query string
100 *
101 * @access public
102 * @return string
103 */
104 function _version()
105 {
106 return sqlite_libversion();
107 }
admin46537622006-09-24 18:13:49 +0000108
109 // --------------------------------------------------------------------
110
111 /**
112 * Execute the query
113 *
114 * @access private called by the base class
115 * @param string an SQL query
116 * @return resource
117 */
118 function _execute($sql)
119 {
120 $sql = $this->_prep_query($sql);
121 return @sqlite_query($this->conn_id, $sql);
122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Prep the query
128 *
129 * If needed, each database adapter can prep the query string
130 *
131 * @access private called by execute()
132 * @param string an SQL query
133 * @return string
134 */
admine334c472006-10-21 19:44:22 +0000135 function _prep_query($sql)
136 {
admin46537622006-09-24 18:13:49 +0000137 return $sql;
admine334c472006-10-21 19:44:22 +0000138 }
admin46537622006-09-24 18:13:49 +0000139
140 // --------------------------------------------------------------------
141
142 /**
143 * Begin Transaction
admine334c472006-10-21 19:44:22 +0000144 *
admin46537622006-09-24 18:13:49 +0000145 * @access public
admine334c472006-10-21 19:44:22 +0000146 * @return bool
admin46537622006-09-24 18:13:49 +0000147 */
148 function trans_begin($test_mode = FALSE)
149 {
150 if ( ! $this->trans_enabled)
151 {
152 return TRUE;
153 }
154
155 // When transactions are nested we only begin/commit/rollback the outermost ones
156 if ($this->_trans_depth > 0)
157 {
158 return TRUE;
159 }
160
161 // Reset the transaction failure flag.
admine334c472006-10-21 19:44:22 +0000162 // If the $test_mode flag is set to TRUE transactions will be rolled back
163 // even if the queries produce a successful result.
admin46537622006-09-24 18:13:49 +0000164 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
165
166 $this->simple_query('BEGIN TRANSACTION');
167 return TRUE;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Commit Transaction
admine334c472006-10-21 19:44:22 +0000174 *
admin46537622006-09-24 18:13:49 +0000175 * @access public
admine334c472006-10-21 19:44:22 +0000176 * @return bool
admin46537622006-09-24 18:13:49 +0000177 */
178 function trans_commit()
179 {
180 if ( ! $this->trans_enabled)
181 {
182 return TRUE;
183 }
184
185 // When transactions are nested we only begin/commit/rollback the outermost ones
186 if ($this->_trans_depth > 0)
187 {
188 return TRUE;
189 }
190
191 $this->simple_query('COMMIT');
192 return TRUE;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Rollback Transaction
admine334c472006-10-21 19:44:22 +0000199 *
admin46537622006-09-24 18:13:49 +0000200 * @access public
admine334c472006-10-21 19:44:22 +0000201 * @return bool
admin46537622006-09-24 18:13:49 +0000202 */
203 function trans_rollback()
204 {
205 if ( ! $this->trans_enabled)
206 {
207 return TRUE;
208 }
209
210 // When transactions are nested we only begin/commit/rollback the outermost ones
211 if ($this->_trans_depth > 0)
212 {
213 return TRUE;
214 }
215
216 $this->simple_query('ROLLBACK');
217 return TRUE;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Escape String
224 *
225 * @access public
226 * @param string
227 * @return string
228 */
229 function escape_str($str)
230 {
231 return sqlite_escape_string($str);
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Affected Rows
238 *
239 * @access public
240 * @return integer
241 */
242 function affected_rows()
243 {
244 return sqlite_changes($this->conn_id);
245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Insert ID
251 *
252 * @access public
253 * @return integer
254 */
255 function insert_id()
256 {
257 return @sqlite_last_insert_rowid($this->conn_id);
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * "Count All" query
264 *
265 * Generates a platform-specific query string that counts all records in
266 * the specified database
267 *
268 * @access public
269 * @param string
270 * @return string
271 */
272 function count_all($table = '')
273 {
274 if ($table == '')
275 return '0';
276
277 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
278
279 if ($query->num_rows() == 0)
280 return '0';
281
282 $row = $query->row();
283 return $row->numrows;
284 }
admin3dd978f2006-09-30 19:24:45 +0000285
286 // --------------------------------------------------------------------
287
288 /**
admin3dd978f2006-09-30 19:24:45 +0000289 * List table query
290 *
291 * Generates a platform-specific query string so that the table names can be fetched
292 *
293 * @access private
294 * @return string
295 */
296 function _list_tables()
297 {
298 return "SELECT name from sqlite_master WHERE type='table'";
299 }
300
admin46537622006-09-24 18:13:49 +0000301 // --------------------------------------------------------------------
302
303 /**
adminfafe28b2006-10-21 19:08:17 +0000304 * Show column query
admin9cd4e8e2006-09-25 23:26:25 +0000305 *
306 * Generates a platform-specific query string so that the column names can be fetched
307 *
308 * @access public
309 * @param string the table name
310 * @return string
311 */
312 function _list_columns($table = '')
313 {
314 // Not supported
315 return FALSE;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * Field data query
322 *
323 * Generates a platform-specific query so that the column data can be retrieved
324 *
325 * @access public
326 * @param string the table name
327 * @return object
328 */
329 function _field_data($table)
330 {
331 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
admin46537622006-09-24 18:13:49 +0000337 * The error message string
338 *
adminbb1d4392006-09-24 20:14:38 +0000339 * @access private
admin46537622006-09-24 18:13:49 +0000340 * @return string
341 */
adminbb1d4392006-09-24 20:14:38 +0000342 function _error_message()
admin46537622006-09-24 18:13:49 +0000343 {
344 return sqlite_error_string(sqlite_last_error($this->conn_id));
345 }
346
347 // --------------------------------------------------------------------
348
349 /**
350 * The error message number
351 *
adminbb1d4392006-09-24 20:14:38 +0000352 * @access private
admin46537622006-09-24 18:13:49 +0000353 * @return integer
354 */
adminbb1d4392006-09-24 20:14:38 +0000355 function _error_number()
admin46537622006-09-24 18:13:49 +0000356 {
357 return sqlite_last_error($this->conn_id);
358 }
adminbb1d4392006-09-24 20:14:38 +0000359
admin46537622006-09-24 18:13:49 +0000360 // --------------------------------------------------------------------
361
362 /**
363 * Escape Table Name
364 *
365 * This function adds backticks if the table name has a period
366 * in it. Some DBs will get cranky unless periods are escaped
367 *
adminbb1d4392006-09-24 20:14:38 +0000368 * @access private
admin46537622006-09-24 18:13:49 +0000369 * @param string the table name
370 * @return string
371 */
adminbb1d4392006-09-24 20:14:38 +0000372 function _escape_table($table)
admin46537622006-09-24 18:13:49 +0000373 {
374 if (stristr($table, '.'))
375 {
376 $table = preg_replace("/\./", "`.`", $table);
377 }
378
379 return $table;
380 }
adminbb1d4392006-09-24 20:14:38 +0000381
admin46537622006-09-24 18:13:49 +0000382 // --------------------------------------------------------------------
383
384 /**
385 * Insert statement
386 *
387 * Generates a platform-specific insert string from the supplied data
388 *
389 * @access public
390 * @param string the table name
391 * @param array the insert keys
392 * @param array the insert values
393 * @return string
394 */
395 function _insert($table, $keys, $values)
396 {
adminbb1d4392006-09-24 20:14:38 +0000397 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin46537622006-09-24 18:13:49 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Update statement
404 *
405 * Generates a platform-specific update string from the supplied data
406 *
407 * @access public
408 * @param string the table name
409 * @param array the update data
410 * @param array the where clause
411 * @return string
412 */
413 function _update($table, $values, $where)
414 {
415 foreach($values as $key => $val)
416 {
417 $valstr[] = $key." = ".$val;
418 }
419
adminbb1d4392006-09-24 20:14:38 +0000420 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Delete statement
427 *
428 * Generates a platform-specific delete string from the supplied data
429 *
430 * @access public
431 * @param string the table name
432 * @param array the where clause
433 * @return string
434 */
435 function _delete($table, $where)
436 {
adminbb1d4392006-09-24 20:14:38 +0000437 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000438 }
admin46537622006-09-24 18:13:49 +0000439
admin46537622006-09-24 18:13:49 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * Limit string
444 *
445 * Generates a platform-specific LIMIT clause
446 *
447 * @access public
448 * @param string the sql query string
449 * @param integer the number of rows to limit the query to
450 * @param integer the offset value
451 * @return string
452 */
453 function _limit($sql, $limit, $offset)
454 {
455 if ($offset == 0)
456 {
457 $offset = '';
458 }
459 else
460 {
461 $offset .= ", ";
462 }
463
464 return $sql."LIMIT ".$offset.$limit;
465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Close DB Connection
471 *
472 * @access public
473 * @param resource
474 * @return void
475 */
476 function _close($conn_id)
477 {
adminbefd4c22006-10-26 01:49:26 +0000478 @sqlite_close($conn_id);
admin46537622006-09-24 18:13:49 +0000479 }
480
adminbb1d4392006-09-24 20:14:38 +0000481
admin46537622006-09-24 18:13:49 +0000482}
483
484?>