Take two:
[www-register-wizard.git] / database / drivers / sqlite / sqlite_driver.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 \r
19 \r
20 /**\r
21  * SQLite Database Adapter Class\r
22  *\r
23  * Note: _DB is an extender class that the app controller\r
24  * creates dynamically based on whether the active record\r
25  * class is being used or not.\r
26  *\r
27  * @package             CodeIgniter\r
28  * @subpackage  Drivers\r
29  * @category    Database\r
30  * @author              ExpressionEngine Dev Team\r
31  * @link                http://codeigniter.com/user_guide/database/\r
32  */\r
33 class CI_DB_sqlite_driver extends CI_DB {\r
34 \r
35         var $dbdriver = 'sqlite';\r
36         \r
37         // The character used to escape with - not needed for SQLite\r
38         var $_escape_char = '';\r
39 \r
40         /**\r
41          * The syntax to count rows is slightly different across different\r
42          * database engines, so this string appears in each driver and is\r
43          * used for the count_all() and count_all_results() functions.\r
44          */\r
45         var $_count_string = "SELECT COUNT(*) AS ";\r
46         var $_random_keyword = ' Random()'; // database specific random keyword\r
47 \r
48         /**\r
49          * Non-persistent database connection\r
50          *\r
51          * @access      private called by the base class\r
52          * @return      resource\r
53          */     \r
54         function db_connect()\r
55         {\r
56                 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))\r
57                 {\r
58                         log_message('error', $error);\r
59                         \r
60                         if ($this->db_debug)\r
61                         {\r
62                                 $this->display_error($error, '', TRUE);\r
63                         }\r
64                         \r
65                         return FALSE;\r
66                 }\r
67                 \r
68                 return $conn_id;\r
69         }\r
70         \r
71         // --------------------------------------------------------------------\r
72 \r
73         /**\r
74          * Persistent database connection\r
75          *\r
76          * @access      private called by the base class\r
77          * @return      resource\r
78          */     \r
79         function db_pconnect()\r
80         {\r
81                 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))\r
82                 {\r
83                         log_message('error', $error);\r
84                         \r
85                         if ($this->db_debug)\r
86                         {\r
87                                 $this->display_error($error, '', TRUE);\r
88                         }\r
89                         \r
90                         return FALSE;\r
91                 }\r
92                 \r
93                 return $conn_id;\r
94         }\r
95         \r
96         // --------------------------------------------------------------------\r
97 \r
98         /**\r
99          * Select the database\r
100          *\r
101          * @access      private called by the base class\r
102          * @return      resource\r
103          */     \r
104         function db_select()\r
105         {\r
106                 return TRUE;\r
107         }\r
108 \r
109         // --------------------------------------------------------------------\r
110 \r
111         /**\r
112          * Set client character set\r
113          *\r
114          * @access      public\r
115          * @param       string\r
116          * @param       string\r
117          * @return      resource\r
118          */\r
119         function db_set_charset($charset, $collation)\r
120         {\r
121                 // @todo - add support if needed\r
122                 return TRUE;\r
123         }\r
124 \r
125         // --------------------------------------------------------------------\r
126         \r
127         /**\r
128          * Version number query string\r
129          *\r
130          * @access      public\r
131          * @return      string\r
132          */\r
133         function _version()\r
134         {\r
135                 return sqlite_libversion();\r
136         }\r
137         \r
138         // --------------------------------------------------------------------\r
139 \r
140         /**\r
141          * Execute the query\r
142          *\r
143          * @access      private called by the base class\r
144          * @param       string  an SQL query\r
145          * @return      resource\r
146          */     \r
147         function _execute($sql)\r
148         {\r
149                 $sql = $this->_prep_query($sql);\r
150                 return @sqlite_query($this->conn_id, $sql);\r
151         }\r
152         \r
153         // --------------------------------------------------------------------\r
154 \r
155         /**\r
156          * Prep the query\r
157          *\r
158          * If needed, each database adapter can prep the query string\r
159          *\r
160          * @access      private called by execute()\r
161          * @param       string  an SQL query\r
162          * @return      string\r
163          */     \r
164         function _prep_query($sql)\r
165         {\r
166                 return $sql;\r
167         }\r
168 \r
169         // --------------------------------------------------------------------\r
170 \r
171         /**\r
172          * Begin Transaction\r
173          *\r
174          * @access      public\r
175          * @return      bool            \r
176          */     \r
177         function trans_begin($test_mode = FALSE)\r
178         {\r
179                 if ( ! $this->trans_enabled)\r
180                 {\r
181                         return TRUE;\r
182                 }\r
183                 \r
184                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
185                 if ($this->_trans_depth > 0)\r
186                 {\r
187                         return TRUE;\r
188                 }\r
189 \r
190                 // Reset the transaction failure flag.\r
191                 // If the $test_mode flag is set to TRUE transactions will be rolled back\r
192                 // even if the queries produce a successful result.\r
193                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;\r
194 \r
195                 $this->simple_query('BEGIN TRANSACTION');\r
196                 return TRUE;\r
197         }\r
198 \r
199         // --------------------------------------------------------------------\r
200 \r
201         /**\r
202          * Commit Transaction\r
203          *\r
204          * @access      public\r
205          * @return      bool            \r
206          */     \r
207         function trans_commit()\r
208         {\r
209                 if ( ! $this->trans_enabled)\r
210                 {\r
211                         return TRUE;\r
212                 }\r
213 \r
214                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
215                 if ($this->_trans_depth > 0)\r
216                 {\r
217                         return TRUE;\r
218                 }\r
219 \r
220                 $this->simple_query('COMMIT');\r
221                 return TRUE;\r
222         }\r
223 \r
224         // --------------------------------------------------------------------\r
225 \r
226         /**\r
227          * Rollback Transaction\r
228          *\r
229          * @access      public\r
230          * @return      bool            \r
231          */     \r
232         function trans_rollback()\r
233         {\r
234                 if ( ! $this->trans_enabled)\r
235                 {\r
236                         return TRUE;\r
237                 }\r
238 \r
239                 // When transactions are nested we only begin/commit/rollback the outermost ones\r
240                 if ($this->_trans_depth > 0)\r
241                 {\r
242                         return TRUE;\r
243                 }\r
244 \r
245                 $this->simple_query('ROLLBACK');\r
246                 return TRUE;\r
247         }\r
248         \r
249         // --------------------------------------------------------------------\r
250 \r
251         /**\r
252          * Escape String\r
253          *\r
254          * @access      public\r
255          * @param       string\r
256          * @return      string\r
257          */\r
258         function escape_str($str)       \r
259         {\r
260                 return sqlite_escape_string($str);\r
261         }\r
262                 \r
263         // --------------------------------------------------------------------\r
264 \r
265         /**\r
266          * Affected Rows\r
267          *\r
268          * @access      public\r
269          * @return      integer\r
270          */\r
271         function affected_rows()\r
272         {\r
273                 return sqlite_changes($this->conn_id);\r
274         }\r
275         \r
276         // --------------------------------------------------------------------\r
277 \r
278         /**\r
279          * Insert ID\r
280          *\r
281          * @access      public\r
282          * @return      integer\r
283          */\r
284         function insert_id()\r
285         {\r
286                 return @sqlite_last_insert_rowid($this->conn_id);\r
287         }\r
288 \r
289         // --------------------------------------------------------------------\r
290 \r
291         /**\r
292          * "Count All" query\r
293          *\r
294          * Generates a platform-specific query string that counts all records in\r
295          * the specified database\r
296          *\r
297          * @access      public\r
298          * @param       string\r
299          * @return      string\r
300          */\r
301         function count_all($table = '')\r
302         {\r
303                 if ($table == '')\r
304                         return '0';\r
305         \r
306                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));\r
307                 \r
308                 if ($query->num_rows() == 0)\r
309                         return '0';\r
310 \r
311                 $row = $query->row();\r
312                 return $row->numrows;\r
313         }\r
314 \r
315         // --------------------------------------------------------------------\r
316 \r
317         /**\r
318          * List table query\r
319          *\r
320          * Generates a platform-specific query string so that the table names can be fetched\r
321          *\r
322          * @access      private\r
323          * @param       boolean\r
324          * @return      string\r
325          */\r
326         function _list_tables($prefix_limit = FALSE)\r
327         {\r
328                 $sql = "SELECT name from sqlite_master WHERE type='table'";\r
329 \r
330                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')\r
331                 {\r
332                         $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";\r
333                 }\r
334                 return $sql;\r
335         }\r
336 \r
337         // --------------------------------------------------------------------\r
338 \r
339         /**\r
340          * Show column query\r
341          *\r
342          * Generates a platform-specific query string so that the column names can be fetched\r
343          *\r
344          * @access      public\r
345          * @param       string  the table name\r
346          * @return      string\r
347          */\r
348         function _list_columns($table = '')\r
349         {\r
350                 // Not supported\r
351                 return FALSE;\r
352         }\r
353 \r
354         // --------------------------------------------------------------------\r
355 \r
356         /**\r
357          * Field data query\r
358          *\r
359          * Generates a platform-specific query so that the column data can be retrieved\r
360          *\r
361          * @access      public\r
362          * @param       string  the table name\r
363          * @return      object\r
364          */\r
365         function _field_data($table)\r
366         {\r
367                 return "SELECT * FROM ".$table." LIMIT 1";\r
368         }\r
369 \r
370         // --------------------------------------------------------------------\r
371 \r
372         /**\r
373          * The error message string\r
374          *\r
375          * @access      private\r
376          * @return      string\r
377          */\r
378         function _error_message()\r
379         {\r
380                 return sqlite_error_string(sqlite_last_error($this->conn_id));\r
381         }\r
382         \r
383         // --------------------------------------------------------------------\r
384 \r
385         /**\r
386          * The error message number\r
387          *\r
388          * @access      private\r
389          * @return      integer\r
390          */\r
391         function _error_number()\r
392         {\r
393                 return sqlite_last_error($this->conn_id);\r
394         }\r
395 \r
396         // --------------------------------------------------------------------\r
397 \r
398         /**\r
399          * Escape the SQL Identifiers\r
400          *\r
401          * This function escapes column and table names\r
402          *\r
403          * @access      private\r
404          * @param       string\r
405          * @return      string\r
406          */\r
407         function _escape_identifiers($item)\r
408         {\r
409                 if ($this->_escape_char == '')\r
410                 {\r
411                         return $item;\r
412                 }\r
413         \r
414                 if (strpos($item, '.') !== FALSE)\r
415                 {\r
416                         $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;                    \r
417                 }\r
418                 else\r
419                 {\r
420                         $str = $this->_escape_char.$item.$this->_escape_char;\r
421                 }\r
422                 \r
423                 // remove duplicates if the user already included the escape\r
424                 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);\r
425         }\r
426                         \r
427         // --------------------------------------------------------------------\r
428 \r
429         /**\r
430          * From Tables\r
431          *\r
432          * This function implicitly groups FROM tables so there is no confusion\r
433          * about operator precedence in harmony with SQL standards\r
434          *\r
435          * @access      public\r
436          * @param       type\r
437          * @return      type\r
438          */\r
439         function _from_tables($tables)\r
440         {\r
441                 if ( ! is_array($tables))\r
442                 {\r
443                         $tables = array($tables);\r
444                 }\r
445                 \r
446                 return '('.implode(', ', $tables).')';\r
447         }\r
448 \r
449         // --------------------------------------------------------------------\r
450         \r
451         /**\r
452          * Insert statement\r
453          *\r
454          * Generates a platform-specific insert string from the supplied data\r
455          *\r
456          * @access      public\r
457          * @param       string  the table name\r
458          * @param       array   the insert keys\r
459          * @param       array   the insert values\r
460          * @return      string\r
461          */\r
462         function _insert($table, $keys, $values)\r
463         {       \r
464                 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";\r
465         }\r
466         \r
467         // --------------------------------------------------------------------\r
468 \r
469         /**\r
470          * Update statement\r
471          *\r
472          * Generates a platform-specific update string from the supplied data\r
473          *\r
474          * @access      public\r
475          * @param       string  the table name\r
476          * @param       array   the update data\r
477          * @param       array   the where clause\r
478          * @param       array   the orderby clause\r
479          * @param       array   the limit clause\r
480          * @return      string\r
481          */\r
482         function _update($table, $values, $where, $orderby = array(), $limit = FALSE)\r
483         {\r
484                 foreach($values as $key => $val)\r
485                 {\r
486                         $valstr[] = $key." = ".$val;\r
487                 }\r
488                 \r
489                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;\r
490                 \r
491                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';\r
492         \r
493                 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);\r
494 \r
495                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';\r
496 \r
497                 $sql .= $orderby.$limit;\r
498                 \r
499                 return $sql;\r
500         }\r
501 \r
502         \r
503         // --------------------------------------------------------------------\r
504 \r
505         /**\r
506          * Truncate statement\r
507          *\r
508          * Generates a platform-specific truncate string from the supplied data\r
509          * If the database does not support the truncate() command\r
510          * This function maps to "DELETE FROM table"\r
511          *\r
512          * @access      public\r
513          * @param       string  the table name\r
514          * @return      string\r
515          */     \r
516         function _truncate($table)\r
517         {\r
518                 return $this->_delete($table);\r
519         }\r
520         \r
521         // --------------------------------------------------------------------\r
522 \r
523         /**\r
524          * Delete statement\r
525          *\r
526          * Generates a platform-specific delete string from the supplied data\r
527          *\r
528          * @access      public\r
529          * @param       string  the table name\r
530          * @param       array   the where clause\r
531          * @param       string  the limit clause\r
532          * @return      string\r
533          */     \r
534         function _delete($table, $where = array(), $like = array(), $limit = FALSE)\r
535         {\r
536                 $conditions = '';\r
537 \r
538                 if (count($where) > 0 OR count($like) > 0)\r
539                 {\r
540                         $conditions = "\nWHERE ";\r
541                         $conditions .= implode("\n", $this->ar_where);\r
542 \r
543                         if (count($where) > 0 && count($like) > 0)\r
544                         {\r
545                                 $conditions .= " AND ";\r
546                         }\r
547                         $conditions .= implode("\n", $like);\r
548                 }\r
549 \r
550                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;\r
551         \r
552                 return "DELETE FROM ".$table.$conditions.$limit;\r
553         }\r
554         \r
555         // --------------------------------------------------------------------\r
556 \r
557         /**\r
558          * Limit string\r
559          *\r
560          * Generates a platform-specific LIMIT clause\r
561          *\r
562          * @access      public\r
563          * @param       string  the sql query string\r
564          * @param       integer the number of rows to limit the query to\r
565          * @param       integer the offset value\r
566          * @return      string\r
567          */\r
568         function _limit($sql, $limit, $offset)\r
569         {       \r
570                 if ($offset == 0)\r
571                 {\r
572                         $offset = '';\r
573                 }\r
574                 else\r
575                 {\r
576                         $offset .= ", ";\r
577                 }\r
578                 \r
579                 return $sql."LIMIT ".$offset.$limit;\r
580         }\r
581 \r
582         // --------------------------------------------------------------------\r
583 \r
584         /**\r
585          * Close DB Connection\r
586          *\r
587          * @access      public\r
588          * @param       resource\r
589          * @return      void\r
590          */\r
591         function _close($conn_id)\r
592         {\r
593                 @sqlite_close($conn_id);\r
594         }\r
595 \r
596 \r
597 }\r
598 \r
599 \r
600 /* End of file sqlite_driver.php */\r
601 /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */