blob: fd9fd2c5067f6de38bda4b1298d53cee559aebde [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 /**
admin3dd978f2006-09-30 19:24:45 +0000285 * List table query
286 *
287 * Generates a platform-specific query string so that the table names can be fetched
288 *
289 * @access private
290 * @return string
291 */
292 function _list_tables()
293 {
294 return "SELECT name from sqlite_master WHERE type='table'";
295 }
296
admin46537622006-09-24 18:13:49 +0000297 // --------------------------------------------------------------------
298
299 /**
adminfafe28b2006-10-21 19:08:17 +0000300 * Show column query
admin9cd4e8e2006-09-25 23:26:25 +0000301 *
302 * Generates a platform-specific query string so that the column names can be fetched
303 *
304 * @access public
305 * @param string the table name
306 * @return string
307 */
308 function _list_columns($table = '')
309 {
310 // Not supported
311 return FALSE;
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Field data query
318 *
319 * Generates a platform-specific query so that the column data can be retrieved
320 *
321 * @access public
322 * @param string the table name
323 * @return object
324 */
325 function _field_data($table)
326 {
327 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
admin46537622006-09-24 18:13:49 +0000333 * The error message string
334 *
adminbb1d4392006-09-24 20:14:38 +0000335 * @access private
admin46537622006-09-24 18:13:49 +0000336 * @return string
337 */
adminbb1d4392006-09-24 20:14:38 +0000338 function _error_message()
admin46537622006-09-24 18:13:49 +0000339 {
340 return sqlite_error_string(sqlite_last_error($this->conn_id));
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * The error message number
347 *
adminbb1d4392006-09-24 20:14:38 +0000348 * @access private
admin46537622006-09-24 18:13:49 +0000349 * @return integer
350 */
adminbb1d4392006-09-24 20:14:38 +0000351 function _error_number()
admin46537622006-09-24 18:13:49 +0000352 {
353 return sqlite_last_error($this->conn_id);
354 }
adminbb1d4392006-09-24 20:14:38 +0000355
admin46537622006-09-24 18:13:49 +0000356 // --------------------------------------------------------------------
357
358 /**
359 * Escape Table Name
360 *
361 * This function adds backticks if the table name has a period
362 * in it. Some DBs will get cranky unless periods are escaped
363 *
adminbb1d4392006-09-24 20:14:38 +0000364 * @access private
admin46537622006-09-24 18:13:49 +0000365 * @param string the table name
366 * @return string
367 */
adminbb1d4392006-09-24 20:14:38 +0000368 function _escape_table($table)
admin46537622006-09-24 18:13:49 +0000369 {
370 if (stristr($table, '.'))
371 {
372 $table = preg_replace("/\./", "`.`", $table);
373 }
374
375 return $table;
376 }
adminbb1d4392006-09-24 20:14:38 +0000377
admin46537622006-09-24 18:13:49 +0000378 // --------------------------------------------------------------------
379
380 /**
381 * Insert statement
382 *
383 * Generates a platform-specific insert string from the supplied data
384 *
385 * @access public
386 * @param string the table name
387 * @param array the insert keys
388 * @param array the insert values
389 * @return string
390 */
391 function _insert($table, $keys, $values)
392 {
adminbb1d4392006-09-24 20:14:38 +0000393 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin46537622006-09-24 18:13:49 +0000394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Update statement
400 *
401 * Generates a platform-specific update string from the supplied data
402 *
403 * @access public
404 * @param string the table name
405 * @param array the update data
406 * @param array the where clause
407 * @return string
408 */
409 function _update($table, $values, $where)
410 {
411 foreach($values as $key => $val)
412 {
413 $valstr[] = $key." = ".$val;
414 }
415
adminbb1d4392006-09-24 20:14:38 +0000416 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Delete statement
423 *
424 * Generates a platform-specific delete string from the supplied data
425 *
426 * @access public
427 * @param string the table name
428 * @param array the where clause
429 * @return string
430 */
431 function _delete($table, $where)
432 {
adminbb1d4392006-09-24 20:14:38 +0000433 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin46537622006-09-24 18:13:49 +0000434 }
admin46537622006-09-24 18:13:49 +0000435
admin46537622006-09-24 18:13:49 +0000436 // --------------------------------------------------------------------
437
438 /**
439 * Limit string
440 *
441 * Generates a platform-specific LIMIT clause
442 *
443 * @access public
444 * @param string the sql query string
445 * @param integer the number of rows to limit the query to
446 * @param integer the offset value
447 * @return string
448 */
449 function _limit($sql, $limit, $offset)
450 {
451 if ($offset == 0)
452 {
453 $offset = '';
454 }
455 else
456 {
457 $offset .= ", ";
458 }
459
460 return $sql."LIMIT ".$offset.$limit;
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Close DB Connection
467 *
468 * @access public
469 * @param resource
470 * @return void
471 */
472 function _close($conn_id)
473 {
474 sqlite_close($conn_id);
475 }
476
adminbb1d4392006-09-24 20:14:38 +0000477
admin46537622006-09-24 18:13:49 +0000478}
479
480?>