3 /**********************************************************************
4 * Author: Justin Vincent (jv@jvmultimedia.com)
5 * Web...: http://twitter.com/justinvincent
7 * Desc..: SQLite component (part of ezSQL databse abstraction library)
11 /**********************************************************************
12 * ezSQL error strings - SQLite
15 $ezsql_pdo_str = array
17 1 => 'Require $dsn and $user and $password to create a connection'
20 /**********************************************************************
21 * ezSQL Database specific class - SQLite
24 if ( ! class_exists ('PDO') ) die('<b>Fatal Error:</b> ezSQL_pdo requires PDO Lib to be compiled and or linked in to the PHP engine');
25 if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_pdo requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
27 class ezSQL_pdo extends ezSQLcore
34 /**********************************************************************
35 * Constructor - allow the user to perform a qucik connect at the
36 * same time as initialising the ezSQL_sqlite class
39 function ezSQL_pdo($dsn='', $user='', $password='', $ssl=array())
41 // Turn on track errors
42 ini_set('track_errors',1);
44 if ( $dsn && $user && $password )
46 $this->connect($dsn, $user, $password);
50 /**********************************************************************
51 * Try to connect to SQLite database server
54 function connect($dsn='', $user='', $password='', $ssl=array())
57 global $ezsql_pdo_str; $return_val = false;
59 // Must have a user and a password
60 if ( ! $dsn || ! $user || ! $password )
62 $this->register_error($ezsql_pdo_str[1].' in '.__FILE__.' on line '.__LINE__);
63 $this->show_errors ? trigger_error($ezsql_pdo_str[1],E_USER_WARNING) : null;
66 // Establish PDO connection
71 $this->dbh = new PDO($dsn, $user, $password, $ssl);
75 $this->dbh = new PDO($dsn, $user, $password);
80 catch (PDOException $e)
82 $this->register_error($e->getMessage());
83 $this->show_errors ? trigger_error($e->getMessage(),E_USER_WARNING) : null;
89 /**********************************************************************
90 * In the case of SQLite quick_connect is not really needed
91 * because std. connect already does what quick connect does -
92 * but for the sake of consistency it has been included
95 function quick_connect($dsn='', $user='', $password='', $ssl=array())
97 return $this->connect($dsn, $user, $password);
100 /**********************************************************************
101 * No real equivalent of mySQL select in SQLite
102 * once again, function included for the sake of consistency
105 function select($dsn='', $user='', $password='', $ssl=array())
107 return $this->connect($dsn, $user, $password);
110 /**********************************************************************
111 * Format a SQLite string correctly for safe SQLite insert
112 * (no mater if magic quotes are on or not)
115 function escape($str)
117 switch (gettype($str))
119 case 'string' : $str = addslashes(stripslashes($str));
121 case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
123 default : $str = ($str === NULL) ? 'NULL' : $str;
130 /**********************************************************************
131 * Return SQLite specific system date syntax
132 * i.e. Oracle: SYSDATE Mysql: NOW()
137 return "datetime('now')";
140 /**********************************************************************
141 * Hooks into PDO error system and reports it to user
144 function catch_error()
146 $error_str = 'No error info';
148 $err_array = $this->dbh->errorInfo();
150 // Note: Ignoring error - bind or column index out of range
151 if ( isset($err_array[1]) && $err_array[1] != 25)
155 foreach ( $err_array as $entry )
157 $error_str .= $entry . ', ';
160 $error_str = substr($error_str,0,-2);
162 $this->register_error($error_str);
163 $this->show_errors ? trigger_error($error_str.' '.$this->last_query,E_USER_WARNING) : null;
170 // ==================================================================
171 // Basic Query - see docs for more detail
173 function query($query)
176 // For reg expressions
177 $query = str_replace("/[\n\r]/",'',trim($query));
182 // Flush cached values..
185 // Log how the function was called
186 $this->func_call = "\$db->query(\"$query\")";
188 // Keep track of the last query for debug..
189 $this->last_query = $query;
191 $this->num_queries++;
194 $this->timer_start($this->num_queries);
196 // Use core file cache function
197 if ( $cache = $this->get_cache($query) )
200 // Keep tack of how long all queries have taken
201 $this->timer_update_global($this->num_queries);
204 if ( $this->use_trace_log )
206 $this->trace_log[] = $this->debug(false);
212 // If there is no existing database connection then try to connect
213 if ( ! isset($this->dbh) || ! $this->dbh )
215 $this->connect($this->dsn, $this->user, $this->password);
218 // Query was an insert, delete, update, replace
219 if ( preg_match("/^(insert|delete|update|replace|drop|create)\s+/i",$query) )
222 // Perform the query and log number of affected rows
223 $this->rows_affected = $this->dbh->exec($query);
225 // If there is an error then take note of it..
226 if ( $this->catch_error() ) return false;
230 // Take note of the insert_id
231 if ( preg_match("/^(insert|replace)\s+/i",$query) )
233 $this->insert_id = @$this->dbh->lastInsertId();
236 // Return number fo rows affected
237 $return_val = $this->rows_affected;
240 // Query was an select
244 // Perform the query and log number of affected rows
245 $sth = $this->dbh->query($query);
247 // If there is an error then take note of it..
248 if ( $this->catch_error() ) return false;
252 $col_count = $sth->columnCount();
254 for ( $i=0 ; $i < $col_count ; $i++ )
256 $this->col_info[$i] = new stdClass();
258 if ( $meta = $sth->getColumnMeta($i) )
260 $this->col_info[$i]->name = $meta['name'];
261 $this->col_info[$i]->type = !empty($meta['native_type']) ? $meta['native_type'] : 'undefined';
262 $this->col_info[$i]->max_length = '';
266 $this->col_info[$i]->name = 'undefined';
267 $this->col_info[$i]->type = 'undefined';
268 $this->col_info[$i]->max_length = '';
272 // Store Query Results
274 while ( $row = @$sth->fetch(PDO::FETCH_ASSOC) )
276 // Store relults as an objects within main array
277 $this->last_result[$num_rows] = (object) $row;
281 // Log number of rows the query returned
282 $this->num_rows = $num_rows;
284 // Return number of rows selected
285 $return_val = $this->num_rows;
289 // disk caching of queries
290 $this->store_cache($query,$is_insert);
292 // If debug ALL queries
293 $this->trace || $this->debug_all ? $this->debug() : null ;
295 // Keep tack of how long all queries have taken
296 $this->timer_update_global($this->num_queries);
299 if ( $this->use_trace_log )
301 $this->trace_log[] = $this->debug(false);