blob: 48e2c87147fa20b9cd79050d7ce7e66c22f9a671 [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 */
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 */
adminb071bb52006-08-26 19:28:37 +0000118 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +0000119 {
120 return $sql;
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Escape String
127 *
128 * @access public
129 * @param string
130 * @return string
131 */
132 function escape_str($str)
133 {
adminb0dd10f2006-08-25 17:25:49 +0000134 return sqlite_escape_string($str);
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Close DB Connection
141 *
142 * @access public
143 * @param resource
144 * @return void
145 */
146 function destroy($conn_id)
147 {
148 sqlite_close($conn_id);
149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Affected Rows
155 *
156 * @access public
157 * @return integer
158 */
159 function affected_rows()
160 {
161 return sqlite_changes($this->conn_id);
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
167 * Insert ID
168 *
169 * @access public
170 * @return integer
171 */
172 function insert_id()
173 {
174 return @sqlite_last_insert_rowid($this->conn_id);
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * "Count All" query
181 *
182 * Generates a platform-specific query string that counts all records in
183 * the specified database
184 *
185 * @access public
186 * @param string
187 * @return string
188 */
189 function count_all($table = '')
190 {
191 if ($table == '')
192 return '0';
193
194 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
195
196 if ($query->num_rows() == 0)
197 return '0';
198
199 $row = $query->row();
200 return $row->numrows;
201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * The error message string
207 *
208 * @access public
209 * @return string
210 */
211 function error_message()
212 {
213 return sqlite_error_string(sqlite_last_error($this->conn_id));
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * The error message number
220 *
221 * @access public
222 * @return integer
223 */
224 function error_number()
225 {
226 return sqlite_last_error($this->conn_id);
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Version number query string
233 *
234 * @access public
235 * @return string
236 */
237 function version()
238 {
239 return sqlite_libversion();
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Escape Table Name
246 *
247 * This function adds backticks if the table name has a period
248 * in it. Some DBs will get cranky unless periods are escaped
249 *
250 * @access public
251 * @param string the table name
252 * @return string
253 */
254 function escape_table($table)
255 {
256 if (stristr($table, '.'))
257 {
258 $table = preg_replace("/\./", "`.`", $table);
259 }
260
261 return $table;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Field data query
268 *
269 * Generates a platform-specific query so that the column data can be retrieved
270 *
271 * @access public
272 * @param string the table name
273 * @return object
274 */
275 function _field_data($table)
276 {
277 $sql = "SELECT * FROM ".$this->escape_table($table)." LIMIT 1";
278 $query = $this->query($sql);
279 return $query->field_data();
280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Insert statement
286 *
287 * Generates a platform-specific insert string from the supplied data
288 *
289 * @access public
290 * @param string the table name
291 * @param array the insert keys
292 * @param array the insert values
293 * @return string
294 */
295 function _insert($table, $keys, $values)
296 {
297 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Update statement
304 *
305 * Generates a platform-specific update string from the supplied data
306 *
307 * @access public
308 * @param string the table name
309 * @param array the update data
310 * @param array the where clause
311 * @return string
312 */
313 function _update($table, $values, $where)
314 {
315 foreach($values as $key => $val)
316 {
317 $valstr[] = $key." = ".$val;
318 }
319
320 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Delete statement
327 *
328 * Generates a platform-specific delete string from the supplied data
329 *
330 * @access public
331 * @param string the table name
332 * @param array the where clause
333 * @return string
334 */
335 function _delete($table, $where)
336 {
337 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Show table query
344 *
345 * Generates a platform-specific query string so that the table names can be fetched
346 *
347 * @access public
348 * @return string
349 */
350 function _show_tables()
351 {
352 return "SELECT name from sqlite_master WHERE type='table'";
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Show columnn query
359 *
360 * Generates a platform-specific query string so that the column names can be fetched
361 *
362 * @access public
363 * @param string the table name
364 * @return string
365 */
366 function _show_columns($table = '')
367 {
368 // Not supported
369 return FALSE;
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * Limit string
376 *
377 * Generates a platform-specific LIMIT clause
378 *
379 * @access public
380 * @param string the sql query string
381 * @param integer the number of rows to limit the query to
382 * @param integer the offset value
383 * @return string
384 */
385 function _limit($sql, $limit, $offset)
386 {
387 if ($offset == 0)
388 {
389 $offset = '';
390 }
391 else
392 {
393 $offset .= ", ";
394 }
395
396 return $sql."LIMIT ".$offset.$limit;
397 }
398
399}
400
401
402/**
403 * SQLite Result Class
404 *
405 * This class extends the parent result class: CI_DB_result
406 *
407 * @category Database
408 * @author Rick Ellis
409 * @link http://www.codeigniter.com/user_guide/libraries/database/
410 */
411class CI_DB_sqlite_result extends CI_DB_result {
412
413 /**
414 * Number of rows in the result set
415 *
416 * @access public
417 * @return integer
418 */
419 function num_rows()
420 {
421 return @sqlite_num_rows($this->result_id);
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Number of fields in the result set
428 *
429 * @access public
430 * @return integer
431 */
432 function num_fields()
433 {
434 return @sqlite_num_fields($this->result_id);
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Field data
441 *
442 * Generates an array of objects containing field meta-data
443 *
444 * @access public
445 * @return array
446 */
447 function field_data()
448 {
449 $retval = array();
450 for ($i = 0; $i < $this->num_fields(); $i++)
451 {
admine348efb2006-09-20 21:13:26 +0000452 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000453 $F->name = sqlite_field_name($this->result_id, $i);
454 $F->type = 'varchar';
455 $F->max_length = 0;
456 $F->primary_key = 0;
457 $F->default = '';
458
459 $retval[] = $F;
460 }
461
462 return $retval;
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Result - associative array
469 *
470 * Returns the result set as an array
471 *
472 * @access private
473 * @return array
474 */
475 function _fetch_assoc()
476 {
477 return sqlite_fetch_array($this->result_id);
478 }
479
480 // --------------------------------------------------------------------
481
482 /**
483 * Result - object
484 *
485 * Returns the result set as an object
486 *
487 * @access private
488 * @return object
489 */
490 function _fetch_object()
491 {
admin6a285fb2006-08-27 19:54:10 +0000492 if (function_exists('sqlite_fetch_object'))
493 {
494 return sqlite_fetch_object($this->result_id);
495 }
496 else
497 {
498 return $this->_fetch_assoc();
499 }
adminb0dd10f2006-08-25 17:25:49 +0000500 }
501
502}
503
504?>