1 | <?php |
---|
2 | |
---|
3 | class DBConnection |
---|
4 | { |
---|
5 | function DBConnection() |
---|
6 | { |
---|
7 | static $conn_id; |
---|
8 | |
---|
9 | if (is_resource($conn_id)) |
---|
10 | { |
---|
11 | $this->id_ = $conn_id; |
---|
12 | return; |
---|
13 | } |
---|
14 | if(defined('DB_PORT')) |
---|
15 | { |
---|
16 | if (!$this->id_ = mysql_connect(DB_HOST.':'.DB_PORT, DB_USER, DB_PASS)) |
---|
17 | die("Unable to connect to mysql database."); |
---|
18 | } |
---|
19 | else |
---|
20 | { |
---|
21 | if (!$this->id_ = mysql_connect(DB_HOST, DB_USER, DB_PASS)) |
---|
22 | die("Unable to connect to mysql database."); |
---|
23 | } |
---|
24 | mysql_select_db(DB_NAME); |
---|
25 | $conn_id = $this->id_; |
---|
26 | } |
---|
27 | |
---|
28 | function id() |
---|
29 | { |
---|
30 | return $this->id_; |
---|
31 | } |
---|
32 | |
---|
33 | function affectedRows() |
---|
34 | { |
---|
35 | return mysql_affected_rows($this->id_); |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | class DBQuery |
---|
40 | { |
---|
41 | var $object; |
---|
42 | |
---|
43 | // php5 style object overloading |
---|
44 | // we internally load up the wanted object and reroute all |
---|
45 | // object actions to it |
---|
46 | function __construct($forceNormal = false) |
---|
47 | { |
---|
48 | if (DB_TYPE_USED === 'mysqli' ) |
---|
49 | { |
---|
50 | if (defined('DB_USE_MEMCACHE') && DB_USE_MEMCACHE === true && !$forceNormal) |
---|
51 | { |
---|
52 | $object = new DBMemCachedQuery_mysqli(); |
---|
53 | } |
---|
54 | elseif (defined('DB_USE_QCACHE') && DB_USE_QCACHE === true && !$forceNormal) |
---|
55 | { |
---|
56 | $object = new DBCachedQuery_mysqli(); |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | $object = new DBNormalQuery_mysqli(); |
---|
61 | } |
---|
62 | } |
---|
63 | elseif (defined('DB_USE_MEMCACHE') && DB_USE_MEMCACHE === true && !$forceNormal) |
---|
64 | { |
---|
65 | $object = new DBMemCachedQuery(); |
---|
66 | } |
---|
67 | elseif (defined('DB_USE_QCACHE') && DB_USE_QCACHE === true && !$forceNormal) |
---|
68 | { |
---|
69 | $object = new DBCachedQuery(); |
---|
70 | } |
---|
71 | else |
---|
72 | { |
---|
73 | $object = new DBNormalQuery(); |
---|
74 | } |
---|
75 | $this->object = $object; |
---|
76 | } |
---|
77 | |
---|
78 | function __call($name, $args) |
---|
79 | { |
---|
80 | return call_user_func_array(array($this->object, $name), $args); |
---|
81 | } |
---|
82 | |
---|
83 | function __set($name, $value) |
---|
84 | { |
---|
85 | $this->object->$name = $value; |
---|
86 | } |
---|
87 | |
---|
88 | function __unset($name) |
---|
89 | { |
---|
90 | unset($this->object->$name); |
---|
91 | } |
---|
92 | |
---|
93 | function __isset($name) |
---|
94 | { |
---|
95 | return isset($this->object->$name); |
---|
96 | } |
---|
97 | |
---|
98 | function __get($name) |
---|
99 | { |
---|
100 | return $this->object->$name; |
---|
101 | } |
---|
102 | |
---|
103 | // php4 style object overloading |
---|
104 | // we just hijack $this but we need to use a helper |
---|
105 | // function for this because php5 fatals if it sees |
---|
106 | // $this = ... in the src |
---|
107 | function DBQuery($forceNormal = false) |
---|
108 | { |
---|
109 | $object = &$this->getRef($this); |
---|
110 | if (DB_TYPE_USED === 'mysqli' ) |
---|
111 | { |
---|
112 | if (defined('DB_USE_MEMCACHE') && DB_USE_MEMCACHE === true && !$forceNormal) |
---|
113 | { |
---|
114 | $object = new DBMemCachedQuery_mysqli(); |
---|
115 | } |
---|
116 | elseif (defined('DB_USE_QCACHE') && DB_USE_QCACHE === true && !$forceNormal) |
---|
117 | { |
---|
118 | $object = new DBCachedQuery_mysqli(); |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | $object = new DBNormalQuery_mysqli(); |
---|
123 | } |
---|
124 | } |
---|
125 | elseif (defined('DB_USE_MEMCACHE') && DB_USE_MEMCACHE === true && !$forceNormal) |
---|
126 | { |
---|
127 | $object = new DBMemCachedQuery(); |
---|
128 | } |
---|
129 | elseif (defined('DB_USE_QCACHE') && DB_USE_QCACHE === true && !$forceNormal) |
---|
130 | { |
---|
131 | $object = new DBCachedQuery(); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | $object = new DBNormalQuery(); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | function &getRef(&$var) |
---|
140 | { |
---|
141 | return $var; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | //! mysql uncached query class. Manages SQL queries to a MySQL DB using mysql. |
---|
146 | class DBNormalQuery |
---|
147 | { |
---|
148 | //! Prepare a connection for a new mysql query. |
---|
149 | function DBNormalQuery() |
---|
150 | { |
---|
151 | static $totalexectime = 0; |
---|
152 | $this->totalexectime_ = &$totalexectime; |
---|
153 | $this->executed_ = false; |
---|
154 | $this->dbconn_ = new DBConnection; |
---|
155 | } |
---|
156 | |
---|
157 | //! Return the count of queries performed. |
---|
158 | |
---|
159 | /*! |
---|
160 | * \param $increase if true then increment the count. |
---|
161 | * \return the count of queries so far. |
---|
162 | */ |
---|
163 | function queryCount($increase = false) |
---|
164 | { |
---|
165 | static $count; |
---|
166 | |
---|
167 | if ($increase) |
---|
168 | { |
---|
169 | $count++; |
---|
170 | } |
---|
171 | |
---|
172 | return $count; |
---|
173 | } |
---|
174 | |
---|
175 | //! Return the count of cached queries performed - 0 for uncaches queries. |
---|
176 | function queryCachedCount($increase = false) |
---|
177 | { |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | //! Execute an SQL string. |
---|
182 | |
---|
183 | /* |
---|
184 | * If DB_HALTONERROR is set then this will exit on an error. |
---|
185 | * \return false on error or true if successful. |
---|
186 | */ |
---|
187 | function execute($sql) |
---|
188 | { |
---|
189 | $t1 = strtok(microtime(), ' ') + strtok(''); |
---|
190 | |
---|
191 | $this->resid_ = mysql_query($sql, $this->dbconn_->id()); |
---|
192 | |
---|
193 | if (!$this->resid_ || mysql_errno($this->dbconn_->id())) |
---|
194 | { |
---|
195 | if(defined('KB_PROFILE')) |
---|
196 | { |
---|
197 | DBDebug::recordError("Database error: ".mysql_error($this->dbconn_->id())); |
---|
198 | DBDebug::recordError("SQL: ".$sql); |
---|
199 | } |
---|
200 | if (defined('DB_HALTONERROR') && DB_HALTONERROR) |
---|
201 | { |
---|
202 | echo "Database error: " . mysql_error($this->dbconn_->id()) . "<br>"; |
---|
203 | echo "SQL: " . $sql . "<br>"; |
---|
204 | exit; |
---|
205 | } |
---|
206 | else |
---|
207 | { |
---|
208 | return false; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | $this->exectime_ = strtok(microtime(), ' ') + strtok('') - $t1; |
---|
213 | $this->totalexectime_ += $this->exectime_; |
---|
214 | $this->executed_ = true; |
---|
215 | |
---|
216 | if(defined('KB_PROFILE')) DBDebug::profile($sql); |
---|
217 | |
---|
218 | $this->queryCount(true); |
---|
219 | |
---|
220 | return true; |
---|
221 | } |
---|
222 | |
---|
223 | //! Return the number of rows returned by the last query. |
---|
224 | function recordCount() |
---|
225 | { |
---|
226 | return mysql_num_rows($this->resid_); |
---|
227 | } |
---|
228 | |
---|
229 | //! Return the next row of results from the last query. |
---|
230 | function getRow() |
---|
231 | { |
---|
232 | if ($this->resid_) |
---|
233 | { |
---|
234 | return mysql_fetch_assoc($this->resid_); |
---|
235 | } |
---|
236 | return false; |
---|
237 | } |
---|
238 | |
---|
239 | //! Reset list of results to return the first row from the last query. |
---|
240 | function rewind() |
---|
241 | { |
---|
242 | @mysql_data_seek($this->resid_, 0); |
---|
243 | } |
---|
244 | |
---|
245 | //! Return the auto-increment ID from the last insert operation. |
---|
246 | function getInsertID() |
---|
247 | { |
---|
248 | return mysql_insert_id(); |
---|
249 | } |
---|
250 | |
---|
251 | //! Return the execution time of the last query. |
---|
252 | function execTime() |
---|
253 | { |
---|
254 | return $this->exectime_; |
---|
255 | } |
---|
256 | |
---|
257 | //! Return true if a query has been executed or false if none has been. |
---|
258 | function executed() |
---|
259 | { |
---|
260 | return $this->executed_; |
---|
261 | } |
---|
262 | |
---|
263 | //! Return the most recent error message for the DB connection. |
---|
264 | function getErrorMsg() |
---|
265 | { |
---|
266 | $msg = $this->sql_ . "<br>"; |
---|
267 | $msg .= "Query failed. " . mysql_error($this->dbconn_->id()); |
---|
268 | |
---|
269 | return $msg; |
---|
270 | } |
---|
271 | //! Set the autocommit status. |
---|
272 | |
---|
273 | //! Not implemented with mysql library |
---|
274 | function autocommit($commit = true) |
---|
275 | { |
---|
276 | return false; |
---|
277 | } |
---|
278 | |
---|
279 | //! Rollback all queries in the current transaction. |
---|
280 | |
---|
281 | //! Not implemented with mysql library |
---|
282 | function rollback() |
---|
283 | { |
---|
284 | return false; |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | class DBDebug |
---|
289 | { |
---|
290 | function recordError($text) |
---|
291 | { |
---|
292 | $qerrfile = "/tmp/EDKprofile.lst"; |
---|
293 | if($text) file_put_contents($qerrfile, $text."\n", FILE_APPEND); |
---|
294 | } |
---|
295 | function profile($sql, $text='') |
---|
296 | { |
---|
297 | $qerrfile = "/tmp/EDKprofile.lst"; |
---|
298 | if($text) file_put_contents($qerrfile, $text."\n", FILE_APPEND); |
---|
299 | if (KB_PROFILE == 2) |
---|
300 | { |
---|
301 | file_put_contents($qerrfile, $sql . "\nExecution time: " . $this->exectime_ . "\n", FILE_APPEND); |
---|
302 | } |
---|
303 | if (KB_PROFILE == 3) |
---|
304 | { |
---|
305 | if(DB_TYPE == 'mysqli' && strtolower(substr($sql,0,6))=='select') |
---|
306 | { |
---|
307 | $prof_qry= mysqli_query($this->dbconn_->id(),'EXPLAIN extended '.$sql.";"); |
---|
308 | while($prof_row = mysqli_fetch_assoc($prof_qry)) |
---|
309 | $prof_out_exp .= implode(' | ', $prof_row)."\n"; |
---|
310 | $prof_qry= mysqli_query($this->dbconn_->id(),'show warnings'); |
---|
311 | |
---|
312 | while($prof_row = mysqli_fetch_assoc($prof_qry)) |
---|
313 | $prof_out_ext .= implode(' | ', $prof_row)."\n"; |
---|
314 | file_put_contents($qerrfile, $sql . "\n". |
---|
315 | $prof_out_ext. $prof_out_exp. |
---|
316 | "\n-- Execution time: " . $this->exectime_ . " --\n", FILE_APPEND); |
---|
317 | } |
---|
318 | else file_put_contents($qerrfile, $sql."\nExecution time: ".$this->exectime_."\n", FILE_APPEND); |
---|
319 | } |
---|
320 | |
---|
321 | if (KB_PROFILE == 4) |
---|
322 | { |
---|
323 | if($this->exectime_ > 0.1 && strtolower(substr($sql,0,6))=='select') |
---|
324 | { |
---|
325 | $prof_qry= mysqli_query($this->dbconn_->id(),'EXPLAIN extended '.$sql); |
---|
326 | while($prof_row = mysqli_fetch_assoc($prof_qry)) |
---|
327 | $prof_out_exp .= implode(' | ', $prof_row)."\n"; |
---|
328 | $prof_qry= mysqli_query($this->dbconn_->id(),'show warnings'); |
---|
329 | |
---|
330 | while($prof_row = mysqli_fetch_assoc($prof_qry)) |
---|
331 | $prof_out_ext .= implode(' | ', $prof_row)."\n"; |
---|
332 | file_put_contents($qerrfile, $sql . "\n". |
---|
333 | $prof_out_ext. $prof_out_exp. |
---|
334 | "\n-- Execution time: " . $this->exectime_ . " --\n", FILE_APPEND); |
---|
335 | } |
---|
336 | } |
---|
337 | |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | ?> |
---|