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