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