1 | <?php |
---|
2 | |
---|
3 | class DBConnection |
---|
4 | { |
---|
5 | function DBConnection() |
---|
6 | { |
---|
7 | if (!$this->id_ = mysql_connect(DB_HOST, DB_USER, DB_PASS)) |
---|
8 | die("Unable to connect to mysql database."); |
---|
9 | |
---|
10 | mysql_select_db(DB_NAME); |
---|
11 | } |
---|
12 | |
---|
13 | function id() |
---|
14 | { |
---|
15 | return $this->id_; |
---|
16 | } |
---|
17 | |
---|
18 | function affectedRows() |
---|
19 | { |
---|
20 | return mysql_affected_rows($this->id_); |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | class DBQuery |
---|
25 | { |
---|
26 | function DBQuery() |
---|
27 | { |
---|
28 | $this->executed_ = false; |
---|
29 | $this->_cache = array(); |
---|
30 | $this->_cached = false; |
---|
31 | |
---|
32 | // this is the minimum runtime a query has to run to be |
---|
33 | // eligible for caching in seconds |
---|
34 | $this->_minruntime = 0.1; |
---|
35 | |
---|
36 | // maximum size of a cached result set (512kB) |
---|
37 | $this->_maxcachesize = 524288; |
---|
38 | $this->d = true; |
---|
39 | } |
---|
40 | |
---|
41 | function checkCache() |
---|
42 | { |
---|
43 | // only cache selects |
---|
44 | // we don't use select ... into so there is no problem |
---|
45 | if (strtolower(substr($this->_sql, 0, 6)) != 'select' && strtolower(substr($this->_sql, 0, 4)) != 'show') |
---|
46 | { |
---|
47 | // this is no select, update the table |
---|
48 | $this->markAffectedTables(); |
---|
49 | return false; |
---|
50 | } |
---|
51 | |
---|
52 | if (file_exists(KB_CACHEDIR.'/qcache_qry_'.$this->_hash)) |
---|
53 | { |
---|
54 | $this->_mtime = filemtime(KB_CACHEDIR.'/qcache_qry_'.$this->_hash); |
---|
55 | if ($this->isCacheValid()) |
---|
56 | { |
---|
57 | return true; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | function parseSQL() |
---|
65 | { |
---|
66 | // gets all involved tables for a select statement |
---|
67 | $text = strtolower($this->_sql).' '; |
---|
68 | |
---|
69 | // we try to get the text from 'from' to 'where' because all involved |
---|
70 | // tables are declared in that part |
---|
71 | $from = strpos($text, 'from')+5; |
---|
72 | if (!$to = strpos($text, 'where')) |
---|
73 | { |
---|
74 | $to = strlen($text); |
---|
75 | } |
---|
76 | $parse = trim(substr($text, $from, $to-$from)); |
---|
77 | |
---|
78 | $tables = array(); |
---|
79 | if (strpos($parse, ',') !== false) |
---|
80 | { |
---|
81 | // , is a synonym for join so we'll replace them |
---|
82 | $parse = str_replace(',', ' join ', $parse); |
---|
83 | } |
---|
84 | if (strpos($parse, 'join')) |
---|
85 | { |
---|
86 | // if this query is a join we parse it with regexp to get all tables |
---|
87 | preg_match_all('/join (.*?) /', $parse, $match); |
---|
88 | $tables = $match[1]; |
---|
89 | } |
---|
90 | else |
---|
91 | { |
---|
92 | // no join so it is hopefully a simple table select |
---|
93 | $tables[] = $parse; |
---|
94 | } |
---|
95 | |
---|
96 | $this->_usedtables = $tables; |
---|
97 | } |
---|
98 | |
---|
99 | function isCacheValid() |
---|
100 | { |
---|
101 | // check if cachefiles are stil valid |
---|
102 | |
---|
103 | // first, we need to get all involved tables |
---|
104 | $this->parseSQL(); |
---|
105 | |
---|
106 | foreach ($this->_usedtables as $table) |
---|
107 | { |
---|
108 | $file = KB_CACHEDIR.'/qcache_tbl_'.trim($table); |
---|
109 | if (file_exists($file)) |
---|
110 | { |
---|
111 | // if one of the tables is outdated, the query is outdated |
---|
112 | if ($this->_mtime < filemtime($file)) |
---|
113 | { |
---|
114 | return false; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | function markAffectedTables() |
---|
122 | { |
---|
123 | // this function invalidates cache files for touched tables |
---|
124 | $text = trim(strtolower($this->_sql)); |
---|
125 | $text = str_replace(array('ignore','`', "\r\n", "\n"), '', $text); |
---|
126 | $ta = preg_split('/ /', $text, 0, PREG_SPLIT_NO_EMPTY); |
---|
127 | |
---|
128 | // check for sql keywords and get the table from the appropriate position |
---|
129 | $tables = array(); |
---|
130 | if ($ta[0] == 'update') |
---|
131 | { |
---|
132 | $tables[] = $ta[1]; |
---|
133 | } |
---|
134 | elseif ($ta[0] == 'insert') |
---|
135 | { |
---|
136 | $tables[] = $ta[2]; |
---|
137 | } |
---|
138 | elseif ($ta[0] == 'replace') |
---|
139 | { |
---|
140 | $tables[] = $ta[2]; |
---|
141 | } |
---|
142 | elseif ($ta[0] == 'delete') |
---|
143 | { |
---|
144 | $tables[] = $ta[2]; |
---|
145 | } |
---|
146 | elseif ($ta[0] == 'alter') |
---|
147 | { |
---|
148 | return false; |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | var_dump($ta); |
---|
153 | trigger_error('No suitable handler for query found.',E_USER_WARNING); |
---|
154 | return false; |
---|
155 | } |
---|
156 | |
---|
157 | foreach ($tables as $table) |
---|
158 | { |
---|
159 | $file = KB_CACHEDIR.'/qcache_tbl_'.$table; |
---|
160 | touch($file); |
---|
161 | } |
---|
162 | // refresh php's filestatcache so we dont get wrong timestamps on changed files |
---|
163 | clearstatcache(); |
---|
164 | } |
---|
165 | |
---|
166 | function genCache() |
---|
167 | { |
---|
168 | // this function fetches all rows and writes the data into a textfile |
---|
169 | |
---|
170 | // don't attemp to cache updates! |
---|
171 | if (strtolower(substr($this->_sql, 0, 6)) != 'select' && strtolower(substr($this->_sql, 0, 4)) != 'show') |
---|
172 | { |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | $bsize = 0; |
---|
177 | while ($row = $this->getRow()) |
---|
178 | { |
---|
179 | $this->_cache[] = $row; |
---|
180 | |
---|
181 | // if the bytesize of the table exceeds the limit we'll abort |
---|
182 | // the cache generation and leave this query unbuffered |
---|
183 | $bsize += join('', $row); |
---|
184 | if ($bsize > $this->_maxcachesize) |
---|
185 | { |
---|
186 | $this->_cache[] = array(); |
---|
187 | $this->_cached = false; |
---|
188 | $this->rewind(); |
---|
189 | return false; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | // write data into textfile |
---|
194 | file_put_contents(KB_CACHEDIR.'/qcache_qry_'.$this->_hash, serialize($this->_cache)); |
---|
195 | |
---|
196 | $this->_cached = true; |
---|
197 | $this->_currrow = 0; |
---|
198 | $this->executed_ = true; |
---|
199 | } |
---|
200 | |
---|
201 | function loadCache() |
---|
202 | { |
---|
203 | // loads the cachefile into the memory |
---|
204 | $this->_cache = unserialize(file_get_contents(KB_CACHEDIR.'/qcache_qry_'.$this->_hash)); |
---|
205 | |
---|
206 | $this->_cached = true; |
---|
207 | $this->_currrow = 0; |
---|
208 | $this->executed_ = true; |
---|
209 | } |
---|
210 | |
---|
211 | function execute($sql) |
---|
212 | { |
---|
213 | $this->_sql = trim($sql); |
---|
214 | $this->_hash = md5($this->_sql); |
---|
215 | $this->_cache = array(); |
---|
216 | $this->_cached = false; |
---|
217 | |
---|
218 | if ($this->checkCache()) |
---|
219 | { |
---|
220 | $this->loadCache(); |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | // we got no or no valid cache so open the connection and run the query |
---|
225 | $this->dbconn_ = new DBConnection; |
---|
226 | |
---|
227 | $t1 = strtok(microtime(), ' ') + strtok(''); |
---|
228 | |
---|
229 | $this->resid_ = mysql_query($sql, $this->dbconn_->id()); |
---|
230 | |
---|
231 | if ($this->resid_ == false) |
---|
232 | { |
---|
233 | if (DB_HALTONERROR === true) |
---|
234 | { |
---|
235 | echo "Database error: ".mysql_error($this->dbconn_->id())."<br/>"; |
---|
236 | echo "SQL: ".$this->_sql."<br/>"; |
---|
237 | exit; |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | return false; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | $this->exectime_ = strtok(microtime(), ' ') + strtok('') - $t1; |
---|
246 | $this->executed_ = true; |
---|
247 | |
---|
248 | if (KB_PROFILE == 2) |
---|
249 | { |
---|
250 | file_put_contents('/tmp/profile.lst', $sql."\nExecution time: ".$this->exectime_."\n", FILE_APPEND); |
---|
251 | } |
---|
252 | |
---|
253 | // if the query was too slow we'll fetch all rows and run it cached |
---|
254 | if ($this->exectime_ > $this->_minruntime) |
---|
255 | { |
---|
256 | $this->genCache(); |
---|
257 | } |
---|
258 | |
---|
259 | return true; |
---|
260 | } |
---|
261 | |
---|
262 | function recordCount() |
---|
263 | { |
---|
264 | if ($this->_cached) |
---|
265 | { |
---|
266 | return count($this->_cache); |
---|
267 | } |
---|
268 | return mysql_num_rows($this->resid_); |
---|
269 | } |
---|
270 | |
---|
271 | function getRow() |
---|
272 | { |
---|
273 | if ($this->_cached) |
---|
274 | { |
---|
275 | if (!isset($this->_cache[$this->_currrow])) |
---|
276 | { |
---|
277 | return false; |
---|
278 | } |
---|
279 | // return the current row and increase the pointer by one |
---|
280 | return $this->_cache[$this->_currrow++]; |
---|
281 | } |
---|
282 | if (is_resource($this->resid_)) |
---|
283 | { |
---|
284 | return mysql_fetch_assoc($this->resid_); |
---|
285 | } |
---|
286 | return false; |
---|
287 | } |
---|
288 | |
---|
289 | function rewind() |
---|
290 | { |
---|
291 | if ($this->_cached) |
---|
292 | { |
---|
293 | $this->_currrow = 0; |
---|
294 | } |
---|
295 | @mysql_data_seek($this->resid_, 0); |
---|
296 | } |
---|
297 | |
---|
298 | function getInsertID() |
---|
299 | { |
---|
300 | return mysql_insert_id(); |
---|
301 | } |
---|
302 | |
---|
303 | function execTime() |
---|
304 | { |
---|
305 | return $this->exectime_; |
---|
306 | } |
---|
307 | |
---|
308 | function executed() |
---|
309 | { |
---|
310 | return $this->executed_; |
---|
311 | } |
---|
312 | |
---|
313 | function getErrorMsg() |
---|
314 | { |
---|
315 | $msg = $this->sql_."<br>"; |
---|
316 | $msg .= "Query failed. ".mysql_error($this->dbconn_->id()); |
---|
317 | |
---|
318 | return $msg; |
---|
319 | } |
---|
320 | } |
---|
321 | ?> |
---|