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