blob: b21241a1f0bde252e92677b7a0e00a827671fcc0 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25: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/libraries/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 */
admine885d782006-09-23 20:25:05 +0000101 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +0000102 {
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 */
adminb071bb52006-08-26 19:28:37 +0000118 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000119 {
120 return $sql;
121 }
admine885d782006-09-23 20:25:05 +0000122
123 // --------------------------------------------------------------------
124
125 /**
126 * Begin Transaction
127 *
128 * @access public
129 * @return bool
130 */
admin8b180be2006-09-24 01:12:22 +0000131 function trans_begin($test_mode = FALSE)
admine885d782006-09-23 20:25:05 +0000132 {
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
admin8b180be2006-09-24 01:12:22 +0000144 // 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
admine885d782006-09-23 20:25:05 +0000149 $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 }
adminb0dd10f2006-08-25 17:25:49 +0000202
203 // --------------------------------------------------------------------
204
205 /**
206 * Escape String
207 *
208 * @access public
209 * @param string
210 * @return string
211 */
212 function escape_str($str)
213 {
adminb0dd10f2006-08-25 17:25:49 +0000214 return sqlite_escape_string($str);
215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Close DB Connection
221 *
222 * @access public
223 * @param resource
224 * @return void
225 */
226 function destroy($conn_id)
227 {
228 sqlite_close($conn_id);
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Affected Rows
235 *
236 * @access public
237 * @return integer
238 */
239 function affected_rows()
240 {
241 return sqlite_changes($this->conn_id);
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Insert ID
248 *
249 * @access public
250 * @return integer
251 */
252 function insert_id()
253 {
254 return @sqlite_last_insert_rowid($this->conn_id);
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * "Count All" query
261 *
262 * Generates a platform-specific query string that counts all records in
263 * the specified database
264 *
265 * @access public
266 * @param string
267 * @return string
268 */
269 function count_all($table = '')
270 {
271 if ($table == '')
272 return '0';
273
274 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
275
276 if ($query->num_rows() == 0)
277 return '0';
278
279 $row = $query->row();
280 return $row->numrows;
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * The error message string
287 *
288 * @access public
289 * @return string
290 */
291 function error_message()
292 {
293 return sqlite_error_string(sqlite_last_error($this->conn_id));
294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * The error message number
300 *
301 * @access public
302 * @return integer
303 */
304 function error_number()
305 {
306 return sqlite_last_error($this->conn_id);
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Version number query string
313 *
314 * @access public
315 * @return string
316 */
317 function version()
318 {
319 return sqlite_libversion();
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Escape Table Name
326 *
327 * This function adds backticks if the table name has a period
328 * in it. Some DBs will get cranky unless periods are escaped
329 *
330 * @access public
331 * @param string the table name
332 * @return string
333 */
334 function escape_table($table)
335 {
336 if (stristr($table, '.'))
337 {
338 $table = preg_replace("/\./", "`.`", $table);
339 }
340
341 return $table;
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Field data query
348 *
349 * Generates a platform-specific query so that the column data can be retrieved
350 *
351 * @access public
352 * @param string the table name
353 * @return object
354 */
355 function _field_data($table)
356 {
357 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
358 $query = $this->query($sql);
359 return $query->field_data();
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Insert statement
366 *
367 * Generates a platform-specific insert string from the supplied data
368 *
369 * @access public
370 * @param string the table name
371 * @param array the insert keys
372 * @param array the insert values
373 * @return string
374 */
375 function _insert($table, $keys, $values)
376 {
377 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Update statement
384 *
385 * Generates a platform-specific update string from the supplied data
386 *
387 * @access public
388 * @param string the table name
389 * @param array the update data
390 * @param array the where clause
391 * @return string
392 */
393 function _update($table, $values, $where)
394 {
395 foreach($values as $key => $val)
396 {
397 $valstr[] = $key." = ".$val;
398 }
399
400 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Delete statement
407 *
408 * Generates a platform-specific delete string from the supplied data
409 *
410 * @access public
411 * @param string the table name
412 * @param array the where clause
413 * @return string
414 */
415 function _delete($table, $where)
416 {
417 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Show table query
424 *
425 * Generates a platform-specific query string so that the table names can be fetched
426 *
427 * @access public
428 * @return string
429 */
430 function _show_tables()
431 {
432 return "SELECT name from sqlite_master WHERE type='table'";
433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Show columnn query
439 *
440 * Generates a platform-specific query string so that the column names can be fetched
441 *
442 * @access public
443 * @param string the table name
444 * @return string
445 */
446 function _show_columns($table = '')
447 {
448 // Not supported
449 return FALSE;
450 }
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/**
483 * SQLite Result Class
484 *
485 * This class extends the parent result class: CI_DB_result
486 *
487 * @category Database
488 * @author Rick Ellis
489 * @link http://www.codeigniter.com/user_guide/libraries/database/
490 */
491class CI_DB_sqlite_result extends CI_DB_result {
492
493 /**
494 * Number of rows in the result set
495 *
496 * @access public
497 * @return integer
498 */
499 function num_rows()
500 {
501 return @sqlite_num_rows($this->result_id);
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Number of fields in the result set
508 *
509 * @access public
510 * @return integer
511 */
512 function num_fields()
513 {
514 return @sqlite_num_fields($this->result_id);
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Field data
521 *
522 * Generates an array of objects containing field meta-data
523 *
524 * @access public
525 * @return array
526 */
527 function field_data()
528 {
529 $retval = array();
530 for ($i = 0; $i < $this->num_fields(); $i++)
531 {
admine348efb2006-09-20 21:13:26 +0000532 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000533 $F->name = sqlite_field_name($this->result_id, $i);
534 $F->type = 'varchar';
535 $F->max_length = 0;
536 $F->primary_key = 0;
537 $F->default = '';
538
539 $retval[] = $F;
540 }
541
542 return $retval;
543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Result - associative array
549 *
550 * Returns the result set as an array
551 *
552 * @access private
553 * @return array
554 */
555 function _fetch_assoc()
556 {
557 return sqlite_fetch_array($this->result_id);
558 }
559
560 // --------------------------------------------------------------------
561
562 /**
563 * Result - object
564 *
565 * Returns the result set as an object
566 *
567 * @access private
568 * @return object
569 */
570 function _fetch_object()
571 {
admin6a285fb2006-08-27 19:54:10 +0000572 if (function_exists('sqlite_fetch_object'))
573 {
574 return sqlite_fetch_object($this->result_id);
575 }
576 else
577 {
578 return $this->_fetch_assoc();
579 }
adminb0dd10f2006-08-25 17:25:49 +0000580 }
581
582}
583
584?>