blob: 6d7b4294a5901fce636a766792d29e52b7820937 [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 */
131 function trans_begin()
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 $this->simple_query('BEGIN TRANSACTION');
145 return TRUE;
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Commit Transaction
152 *
153 * @access public
154 * @return bool
155 */
156 function trans_commit()
157 {
158 if ( ! $this->trans_enabled)
159 {
160 return TRUE;
161 }
162
163 // When transactions are nested we only begin/commit/rollback the outermost ones
164 if ($this->_trans_depth > 0)
165 {
166 return TRUE;
167 }
168
169 $this->simple_query('COMMIT');
170 return TRUE;
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Rollback Transaction
177 *
178 * @access public
179 * @return bool
180 */
181 function trans_rollback()
182 {
183 if ( ! $this->trans_enabled)
184 {
185 return TRUE;
186 }
187
188 // When transactions are nested we only begin/commit/rollback the outermost ones
189 if ($this->_trans_depth > 0)
190 {
191 return TRUE;
192 }
193
194 $this->simple_query('ROLLBACK');
195 return TRUE;
196 }
adminb0dd10f2006-08-25 17:25:49 +0000197
198 // --------------------------------------------------------------------
199
200 /**
201 * Escape String
202 *
203 * @access public
204 * @param string
205 * @return string
206 */
207 function escape_str($str)
208 {
adminb0dd10f2006-08-25 17:25:49 +0000209 return sqlite_escape_string($str);
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Close DB Connection
216 *
217 * @access public
218 * @param resource
219 * @return void
220 */
221 function destroy($conn_id)
222 {
223 sqlite_close($conn_id);
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Affected Rows
230 *
231 * @access public
232 * @return integer
233 */
234 function affected_rows()
235 {
236 return sqlite_changes($this->conn_id);
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Insert ID
243 *
244 * @access public
245 * @return integer
246 */
247 function insert_id()
248 {
249 return @sqlite_last_insert_rowid($this->conn_id);
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * "Count All" query
256 *
257 * Generates a platform-specific query string that counts all records in
258 * the specified database
259 *
260 * @access public
261 * @param string
262 * @return string
263 */
264 function count_all($table = '')
265 {
266 if ($table == '')
267 return '0';
268
269 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
270
271 if ($query->num_rows() == 0)
272 return '0';
273
274 $row = $query->row();
275 return $row->numrows;
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * The error message string
282 *
283 * @access public
284 * @return string
285 */
286 function error_message()
287 {
288 return sqlite_error_string(sqlite_last_error($this->conn_id));
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * The error message number
295 *
296 * @access public
297 * @return integer
298 */
299 function error_number()
300 {
301 return sqlite_last_error($this->conn_id);
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Version number query string
308 *
309 * @access public
310 * @return string
311 */
312 function version()
313 {
314 return sqlite_libversion();
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Escape Table Name
321 *
322 * This function adds backticks if the table name has a period
323 * in it. Some DBs will get cranky unless periods are escaped
324 *
325 * @access public
326 * @param string the table name
327 * @return string
328 */
329 function escape_table($table)
330 {
331 if (stristr($table, '.'))
332 {
333 $table = preg_replace("/\./", "`.`", $table);
334 }
335
336 return $table;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Field data query
343 *
344 * Generates a platform-specific query so that the column data can be retrieved
345 *
346 * @access public
347 * @param string the table name
348 * @return object
349 */
350 function _field_data($table)
351 {
352 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
353 $query = $this->query($sql);
354 return $query->field_data();
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Insert statement
361 *
362 * Generates a platform-specific insert string from the supplied data
363 *
364 * @access public
365 * @param string the table name
366 * @param array the insert keys
367 * @param array the insert values
368 * @return string
369 */
370 function _insert($table, $keys, $values)
371 {
372 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * Update statement
379 *
380 * Generates a platform-specific update string from the supplied data
381 *
382 * @access public
383 * @param string the table name
384 * @param array the update data
385 * @param array the where clause
386 * @return string
387 */
388 function _update($table, $values, $where)
389 {
390 foreach($values as $key => $val)
391 {
392 $valstr[] = $key." = ".$val;
393 }
394
395 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Delete statement
402 *
403 * Generates a platform-specific delete string from the supplied data
404 *
405 * @access public
406 * @param string the table name
407 * @param array the where clause
408 * @return string
409 */
410 function _delete($table, $where)
411 {
412 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Show table query
419 *
420 * Generates a platform-specific query string so that the table names can be fetched
421 *
422 * @access public
423 * @return string
424 */
425 function _show_tables()
426 {
427 return "SELECT name from sqlite_master WHERE type='table'";
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Show columnn query
434 *
435 * Generates a platform-specific query string so that the column names can be fetched
436 *
437 * @access public
438 * @param string the table name
439 * @return string
440 */
441 function _show_columns($table = '')
442 {
443 // Not supported
444 return FALSE;
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/**
478 * SQLite Result Class
479 *
480 * This class extends the parent result class: CI_DB_result
481 *
482 * @category Database
483 * @author Rick Ellis
484 * @link http://www.codeigniter.com/user_guide/libraries/database/
485 */
486class CI_DB_sqlite_result extends CI_DB_result {
487
488 /**
489 * Number of rows in the result set
490 *
491 * @access public
492 * @return integer
493 */
494 function num_rows()
495 {
496 return @sqlite_num_rows($this->result_id);
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Number of fields in the result set
503 *
504 * @access public
505 * @return integer
506 */
507 function num_fields()
508 {
509 return @sqlite_num_fields($this->result_id);
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Field data
516 *
517 * Generates an array of objects containing field meta-data
518 *
519 * @access public
520 * @return array
521 */
522 function field_data()
523 {
524 $retval = array();
525 for ($i = 0; $i < $this->num_fields(); $i++)
526 {
admine348efb2006-09-20 21:13:26 +0000527 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000528 $F->name = sqlite_field_name($this->result_id, $i);
529 $F->type = 'varchar';
530 $F->max_length = 0;
531 $F->primary_key = 0;
532 $F->default = '';
533
534 $retval[] = $F;
535 }
536
537 return $retval;
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Result - associative array
544 *
545 * Returns the result set as an array
546 *
547 * @access private
548 * @return array
549 */
550 function _fetch_assoc()
551 {
552 return sqlite_fetch_array($this->result_id);
553 }
554
555 // --------------------------------------------------------------------
556
557 /**
558 * Result - object
559 *
560 * Returns the result set as an object
561 *
562 * @access private
563 * @return object
564 */
565 function _fetch_object()
566 {
admin6a285fb2006-08-27 19:54:10 +0000567 if (function_exists('sqlite_fetch_object'))
568 {
569 return sqlite_fetch_object($this->result_id);
570 }
571 else
572 {
573 return $this->_fetch_assoc();
574 }
adminb0dd10f2006-08-25 17:25:49 +0000575 }
576
577}
578
579?>