sqlite_driver.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 
00019 
00020 /**
00021  * SQLite Database Adapter Class
00022  *
00023  * Note: _DB is an extender class that the app controller
00024  * creates dynamically based on whether the active record
00025  * class is being used or not.
00026  *
00027  * @package             CodeIgniter
00028  * @subpackage  Drivers
00029  * @category    Database
00030  * @author              ExpressionEngine Dev Team
00031  * @link                http://codeigniter.com/user_guide/database/
00032  */
00033 class CI_DB_sqlite_driver extends CI_DB {
00034 
00035         var $dbdriver = 'sqlite';
00036         
00037         // The character used to escape with - not needed for SQLite
00038         var $_escape_char = '';
00039 
00040         /**
00041          * The syntax to count rows is slightly different across different
00042          * database engines, so this string appears in each driver and is
00043          * used for the count_all() and count_all_results() functions.
00044          */
00045         var $_count_string = "SELECT COUNT(*) AS ";
00046         var $_random_keyword = ' Random()'; // database specific random keyword
00047 
00048         /**
00049          * Non-persistent database connection
00050          *
00051          * @access      private called by the base class
00052          * @return      resource
00053          */     
00054         function db_connect()
00055         {
00056                 if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error))
00057                 {
00058                         log_message('error', $error);
00059                         
00060                         if ($this->db_debug)
00061                         {
00062                                 $this->display_error($error, '', TRUE);
00063                         }
00064                         
00065                         return FALSE;
00066                 }
00067                 
00068                 return $conn_id;
00069         }
00070         
00071         // --------------------------------------------------------------------
00072 
00073         /**
00074          * Persistent database connection
00075          *
00076          * @access      private called by the base class
00077          * @return      resource
00078          */     
00079         function db_pconnect()
00080         {
00081                 if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error))
00082                 {
00083                         log_message('error', $error);
00084                         
00085                         if ($this->db_debug)
00086                         {
00087                                 $this->display_error($error, '', TRUE);
00088                         }
00089                         
00090                         return FALSE;
00091                 }
00092                 
00093                 return $conn_id;
00094         }
00095         
00096         // --------------------------------------------------------------------
00097 
00098         /**
00099          * Select the database
00100          *
00101          * @access      private called by the base class
00102          * @return      resource
00103          */     
00104         function db_select()
00105         {
00106                 return TRUE;
00107         }
00108 
00109         // --------------------------------------------------------------------
00110 
00111         /**
00112          * Set client character set
00113          *
00114          * @access      public
00115          * @param       string
00116          * @param       string
00117          * @return      resource
00118          */
00119         function db_set_charset($charset, $collation)
00120         {
00121                 // @todo - add support if needed
00122                 return TRUE;
00123         }
00124 
00125         // --------------------------------------------------------------------
00126         
00127         /**
00128          * Version number query string
00129          *
00130          * @access      public
00131          * @return      string
00132          */
00133         function _version()
00134         {
00135                 return sqlite_libversion();
00136         }
00137         
00138         // --------------------------------------------------------------------
00139 
00140         /**
00141          * Execute the query
00142          *
00143          * @access      private called by the base class
00144          * @param       string  an SQL query
00145          * @return      resource
00146          */     
00147         function _execute($sql)
00148         {
00149                 $sql = $this->_prep_query($sql);
00150                 return @sqlite_query($this->conn_id, $sql);
00151         }
00152         
00153         // --------------------------------------------------------------------
00154 
00155         /**
00156          * Prep the query
00157          *
00158          * If needed, each database adapter can prep the query string
00159          *
00160          * @access      private called by execute()
00161          * @param       string  an SQL query
00162          * @return      string
00163          */     
00164         function _prep_query($sql)
00165         {
00166                 return $sql;
00167         }
00168 
00169         // --------------------------------------------------------------------
00170 
00171         /**
00172          * Begin Transaction
00173          *
00174          * @access      public
00175          * @return      bool            
00176          */     
00177         function trans_begin($test_mode = FALSE)
00178         {
00179                 if ( ! $this->trans_enabled)
00180                 {
00181                         return TRUE;
00182                 }
00183                 
00184                 // When transactions are nested we only begin/commit/rollback the outermost ones
00185                 if ($this->_trans_depth > 0)
00186                 {
00187                         return TRUE;
00188                 }
00189 
00190                 // Reset the transaction failure flag.
00191                 // If the $test_mode flag is set to TRUE transactions will be rolled back
00192                 // even if the queries produce a successful result.
00193                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
00194 
00195                 $this->simple_query('BEGIN TRANSACTION');
00196                 return TRUE;
00197         }
00198 
00199         // --------------------------------------------------------------------
00200 
00201         /**
00202          * Commit Transaction
00203          *
00204          * @access      public
00205          * @return      bool            
00206          */     
00207         function trans_commit()
00208         {
00209                 if ( ! $this->trans_enabled)
00210                 {
00211                         return TRUE;
00212                 }
00213 
00214                 // When transactions are nested we only begin/commit/rollback the outermost ones
00215                 if ($this->_trans_depth > 0)
00216                 {
00217                         return TRUE;
00218                 }
00219 
00220                 $this->simple_query('COMMIT');
00221                 return TRUE;
00222         }
00223 
00224         // --------------------------------------------------------------------
00225 
00226         /**
00227          * Rollback Transaction
00228          *
00229          * @access      public
00230          * @return      bool            
00231          */     
00232         function trans_rollback()
00233         {
00234                 if ( ! $this->trans_enabled)
00235                 {
00236                         return TRUE;
00237                 }
00238 
00239                 // When transactions are nested we only begin/commit/rollback the outermost ones
00240                 if ($this->_trans_depth > 0)
00241                 {
00242                         return TRUE;
00243                 }
00244 
00245                 $this->simple_query('ROLLBACK');
00246                 return TRUE;
00247         }
00248         
00249         // --------------------------------------------------------------------
00250 
00251         /**
00252          * Escape String
00253          *
00254          * @access      public
00255          * @param       string
00256          * @return      string
00257          */
00258         function escape_str($str)       
00259         {
00260                 return sqlite_escape_string($str);
00261         }
00262                 
00263         // --------------------------------------------------------------------
00264 
00265         /**
00266          * Affected Rows
00267          *
00268          * @access      public
00269          * @return      integer
00270          */
00271         function affected_rows()
00272         {
00273                 return sqlite_changes($this->conn_id);
00274         }
00275         
00276         // --------------------------------------------------------------------
00277 
00278         /**
00279          * Insert ID
00280          *
00281          * @access      public
00282          * @return      integer
00283          */
00284         function insert_id()
00285         {
00286                 return @sqlite_last_insert_rowid($this->conn_id);
00287         }
00288 
00289         // --------------------------------------------------------------------
00290 
00291         /**
00292          * "Count All" query
00293          *
00294          * Generates a platform-specific query string that counts all records in
00295          * the specified database
00296          *
00297          * @access      public
00298          * @param       string
00299          * @return      string
00300          */
00301         function count_all($table = '')
00302         {
00303                 if ($table == '')
00304                         return '0';
00305         
00306                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
00307                 
00308                 if ($query->num_rows() == 0)
00309                         return '0';
00310 
00311                 $row = $query->row();
00312                 return $row->numrows;
00313         }
00314 
00315         // --------------------------------------------------------------------
00316 
00317         /**
00318          * List table query
00319          *
00320          * Generates a platform-specific query string so that the table names can be fetched
00321          *
00322          * @access      private
00323          * @param       boolean
00324          * @return      string
00325          */
00326         function _list_tables($prefix_limit = FALSE)
00327         {
00328                 $sql = "SELECT name from sqlite_master WHERE type='table'";
00329 
00330                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
00331                 {
00332                         $sql .= " AND 'name' LIKE '".$this->dbprefix."%'";
00333                 }
00334                 return $sql;
00335         }
00336 
00337         // --------------------------------------------------------------------
00338 
00339         /**
00340          * Show column query
00341          *
00342          * Generates a platform-specific query string so that the column names can be fetched
00343          *
00344          * @access      public
00345          * @param       string  the table name
00346          * @return      string
00347          */
00348         function _list_columns($table = '')
00349         {
00350                 // Not supported
00351                 return FALSE;
00352         }
00353 
00354         // --------------------------------------------------------------------
00355 
00356         /**
00357          * Field data query
00358          *
00359          * Generates a platform-specific query so that the column data can be retrieved
00360          *
00361          * @access      public
00362          * @param       string  the table name
00363          * @return      object
00364          */
00365         function _field_data($table)
00366         {
00367                 return "SELECT * FROM ".$table." LIMIT 1";
00368         }
00369 
00370         // --------------------------------------------------------------------
00371 
00372         /**
00373          * The error message string
00374          *
00375          * @access      private
00376          * @return      string
00377          */
00378         function _error_message()
00379         {
00380                 return sqlite_error_string(sqlite_last_error($this->conn_id));
00381         }
00382         
00383         // --------------------------------------------------------------------
00384 
00385         /**
00386          * The error message number
00387          *
00388          * @access      private
00389          * @return      integer
00390          */
00391         function _error_number()
00392         {
00393                 return sqlite_last_error($this->conn_id);
00394         }
00395 
00396         // --------------------------------------------------------------------
00397 
00398         /**
00399          * Escape the SQL Identifiers
00400          *
00401          * This function escapes column and table names
00402          *
00403          * @access      private
00404          * @param       string
00405          * @return      string
00406          */
00407         function _escape_identifiers($item)
00408         {
00409                 if ($this->_escape_char == '')
00410                 {
00411                         return $item;
00412                 }
00413         
00414                 if (strpos($item, '.') !== FALSE)
00415                 {
00416                         $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;                    
00417                 }
00418                 else
00419                 {
00420                         $str = $this->_escape_char.$item.$this->_escape_char;
00421                 }
00422                 
00423                 // remove duplicates if the user already included the escape
00424                 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
00425         }
00426                         
00427         // --------------------------------------------------------------------
00428 
00429         /**
00430          * From Tables
00431          *
00432          * This function implicitly groups FROM tables so there is no confusion
00433          * about operator precedence in harmony with SQL standards
00434          *
00435          * @access      public
00436          * @param       type
00437          * @return      type
00438          */
00439         function _from_tables($tables)
00440         {
00441                 if ( ! is_array($tables))
00442                 {
00443                         $tables = array($tables);
00444                 }
00445                 
00446                 return '('.implode(', ', $tables).')';
00447         }
00448 
00449         // --------------------------------------------------------------------
00450         
00451         /**
00452          * Insert statement
00453          *
00454          * Generates a platform-specific insert string from the supplied data
00455          *
00456          * @access      public
00457          * @param       string  the table name
00458          * @param       array   the insert keys
00459          * @param       array   the insert values
00460          * @return      string
00461          */
00462         function _insert($table, $keys, $values)
00463         {       
00464                 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
00465         }
00466         
00467         // --------------------------------------------------------------------
00468 
00469         /**
00470          * Update statement
00471          *
00472          * Generates a platform-specific update string from the supplied data
00473          *
00474          * @access      public
00475          * @param       string  the table name
00476          * @param       array   the update data
00477          * @param       array   the where clause
00478          * @param       array   the orderby clause
00479          * @param       array   the limit clause
00480          * @return      string
00481          */
00482         function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
00483         {
00484                 foreach($values as $key => $val)
00485                 {
00486                         $valstr[] = $key." = ".$val;
00487                 }
00488                 
00489                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00490                 
00491                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
00492         
00493                 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
00494 
00495                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
00496 
00497                 $sql .= $orderby.$limit;
00498                 
00499                 return $sql;
00500         }
00501 
00502         
00503         // --------------------------------------------------------------------
00504 
00505         /**
00506          * Truncate statement
00507          *
00508          * Generates a platform-specific truncate string from the supplied data
00509          * If the database does not support the truncate() command
00510          * This function maps to "DELETE FROM table"
00511          *
00512          * @access      public
00513          * @param       string  the table name
00514          * @return      string
00515          */     
00516         function _truncate($table)
00517         {
00518                 return $this->_delete($table);
00519         }
00520         
00521         // --------------------------------------------------------------------
00522 
00523         /**
00524          * Delete statement
00525          *
00526          * Generates a platform-specific delete string from the supplied data
00527          *
00528          * @access      public
00529          * @param       string  the table name
00530          * @param       array   the where clause
00531          * @param       string  the limit clause
00532          * @return      string
00533          */     
00534         function _delete($table, $where = array(), $like = array(), $limit = FALSE)
00535         {
00536                 $conditions = '';
00537 
00538                 if (count($where) > 0 OR count($like) > 0)
00539                 {
00540                         $conditions = "\nWHERE ";
00541                         $conditions .= implode("\n", $this->ar_where);
00542 
00543                         if (count($where) > 0 && count($like) > 0)
00544                         {
00545                                 $conditions .= " AND ";
00546                         }
00547                         $conditions .= implode("\n", $like);
00548                 }
00549 
00550                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00551         
00552                 return "DELETE FROM ".$table.$conditions.$limit;
00553         }
00554         
00555         // --------------------------------------------------------------------
00556 
00557         /**
00558          * Limit string
00559          *
00560          * Generates a platform-specific LIMIT clause
00561          *
00562          * @access      public
00563          * @param       string  the sql query string
00564          * @param       integer the number of rows to limit the query to
00565          * @param       integer the offset value
00566          * @return      string
00567          */
00568         function _limit($sql, $limit, $offset)
00569         {       
00570                 if ($offset == 0)
00571                 {
00572                         $offset = '';
00573                 }
00574                 else
00575                 {
00576                         $offset .= ", ";
00577                 }
00578                 
00579                 return $sql."LIMIT ".$offset.$limit;
00580         }
00581 
00582         // --------------------------------------------------------------------
00583 
00584         /**
00585          * Close DB Connection
00586          *
00587          * @access      public
00588          * @param       resource
00589          * @return      void
00590          */
00591         function _close($conn_id)
00592         {
00593                 @sqlite_close($conn_id);
00594         }
00595 
00596 
00597 }
00598 
00599 
00600 /* End of file sqlite_driver.php */
00601 /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */