forked from ezSQL/ezsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathez_sql_sqlite.php
More file actions
executable file
·208 lines (162 loc) · 5.63 KB
/
ez_sql_sqlite.php
File metadata and controls
executable file
·208 lines (162 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**********************************************************************
* Author: Justin Vincent ([email protected])
* Web...: http://twitter.com/justinvincent
* Name..: ezSQL_sqlite
* Desc..: SQLite component (part of ezSQL databse abstraction library)
*
*/
/**********************************************************************
* ezSQL error strings - SQLite
*/
$ezsql_sqlite_str = array
(
1 => 'Require $dbpath and $dbname to open an SQLite database'
);
/**********************************************************************
* ezSQL Database specific class - SQLite
*/
if ( ! function_exists ('sqlite_open') ) die('<b>Fatal Error:</b> ezSQL_sqlite requires SQLite Lib to be compiled and or linked in to the PHP engine');
if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_sqlite requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
class ezSQL_sqlite extends ezSQLcore
{
var $rows_affected = false;
/**********************************************************************
* Constructor - allow the user to perform a qucik connect at the
* same time as initialising the ezSQL_sqlite class
*/
function ezSQL_sqlite($dbpath='', $dbname='')
{
// Turn on track errors
ini_set('track_errors',1);
if ( $dbpath && $dbname )
{
$this->connect($dbpath, $dbname);
}
}
/**********************************************************************
* Try to connect to SQLite database server
*/
function connect($dbpath='', $dbname='')
{
global $ezsql_sqlite_str; $return_val = false;
// Must have a user and a password
if ( ! $dbpath || ! $dbname )
{
$this->register_error($ezsql_sqlite_str[1].' in '.__FILE__.' on line '.__LINE__);
$this->show_errors ? trigger_error($ezsql_sqlite_str[1],E_USER_WARNING) : null;
}
// Try to establish the server database handle
else if ( ! $this->dbh = @sqlite_open($dbpath.$dbname) )
{
$this->register_error($php_errormsg);
$this->show_errors ? trigger_error($php_errormsg,E_USER_WARNING) : null;
}
else
$return_val = true;
return $return_val;
}
/**********************************************************************
* In the case of SQLite quick_connect is not really needed
* because std. connect already does what quick connect does -
* but for the sake of consistency it has been included
*/
function quick_connect($dbpath='', $dbname='')
{
return $this->connect($dbpath, $dbname);
}
/**********************************************************************
* No real equivalent of mySQL select in SQLite
* once again, function included for the sake of consistency
*/
function select($dbpath='', $dbname='')
{
return $this->connect($dbpath, $dbname);
}
/**********************************************************************
* Format a SQLite string correctly for safe SQLite insert
* (no mater if magic quotes are on or not)
*/
function escape($str)
{
return sqlite_escape_string(stripslashes(preg_replace("/[\r\n]/",'',$str)));
}
/**********************************************************************
* Return SQLite specific system date syntax
* i.e. Oracle: SYSDATE Mysql: NOW()
*/
function sysdate()
{
return 'now';
}
/**********************************************************************
* Perform SQLite query and try to detirmin result value
*/
// ==================================================================
// Basic Query - see docs for more detail
function query($query)
{
// For reg expressions
$query = str_replace("/[\n\r]/",'',trim($query));
// initialise return
$return_val = 0;
// Flush cached values..
$this->flush();
// Log how the function was called
$this->func_call = "\$db->query(\"$query\")";
// Keep track of the last query for debug..
$this->last_query = $query;
// Perform the query via std mysql_query function..
$this->result = @sqlite_query($this->dbh,$query);
$this->num_queries++;
// If there is an error then take note of it..
if (@sqlite_last_error($this->dbh))
{
$err_str = sqlite_error_string (sqlite_last_error($this->dbh));
$this->register_error($err_str);
$this->show_errors ? trigger_error($err_str,E_USER_WARNING) : null;
return false;
}
// Query was an insert, delete, update, replace
if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
{
$this->rows_affected = @sqlite_changes($this->dbh);
// Take note of the insert_id
if ( preg_match("/^(insert|replace)\s+/i",$query) )
{
$this->insert_id = @sqlite_last_insert_rowid($this->dbh);
}
// Return number fo rows affected
$return_val = $this->rows_affected;
}
// Query was an select
else
{
// Take note of column info
$i=0;
while ($i < @sqlite_num_fields($this->result))
{
$this->col_info[$i]->name = sqlite_field_name ( $this->result, $i);
$this->col_info[$i]->type = null;
$this->col_info[$i]->max_length = null;
$i++;
}
// Store Query Results
$num_rows=0;
while ($row = @sqlite_fetch_array($this->result,SQLITE_ASSOC) )
{
// Store relults as an objects within main array
$obj= (object) $row; //convert to object
$this->last_result[$num_rows] = $obj;
$num_rows++;
}
// Log number of rows the query returned
$this->num_rows = $num_rows;
// Return number of rows selected
$return_val = $this->num_rows;
}
// If debug ALL queries
$this->trace||$this->debug_all ? $this->debug() : null ;
return $return_val;
}
}