]> src.bluestatic.org Git - isso.git/blob - db.php
Forgot to change this instance of a method name that was changed
[isso.git] / db.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Abstract Database Layer
24 * db.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Abstract Database Layer
31 *
32 * This class provides an abstract template for all RDBMS layers. All
33 * ISSO abstraction layers should inherit this class. It provides error
34 * reporting, SQL analysis, and general connection functionality.
35 *
36 * Constants:
37 * [required] ISSO_DB_LAYER - The name of the DB layer module used in the application
38 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
39 *
40 * @author Blue Static
41 * @copyright Copyright ©2002 - [#]year[#], Blue Static
42 * @version $Revision$
43 * @package ISSO
44 *
45 */
46 class DB_Abstract
47 {
48 /**
49 * Framework registry object
50 * @var object
51 */
52 protected $registry = null;
53
54 /**
55 * Determines whether or not errors should be shown
56 * @var bool
57 */
58 public $showerrors = true;
59
60 /**
61 * Current error number
62 * @var integer
63 */
64 protected $errnum = 0;
65
66 /**
67 * Description of current error
68 * @var string
69 */
70 protected $errstr = '';
71
72 /**
73 * Currend open MySQL connexion
74 * @var resource
75 */
76 protected $dblink = null;
77
78 /**
79 * Current query ID
80 * @var integer
81 */
82 protected $result = null;
83
84 /**
85 * Current query string
86 * @var string
87 */
88 protected $querystr = '';
89
90 /**
91 * History of all executed queryies
92 * @var array
93 */
94 protected $history = array();
95
96 /**
97 * Command mapping list
98 * @var array
99 */
100 protected $commands = array(
101 'pconnect' => '%server %user %password %database',
102 'connect' => '%server %user %password %database',
103 'query' => '%link %query',
104 'error_num' => '%link',
105 'error_str' => '%link',
106 'escape_string' => '%link %string',
107 'escape_binary' => '%string',
108 'unescape_binary' => '%string',
109 'fetch_assoc' => '%result',
110 'fetch_row' => '%result',
111 'fetch_object' => '%result',
112 'free_result' => '%result',
113 'insert_id' => '%link',
114 'num_rows' => '%result',
115 'affected_rows' => '%result'
116 );
117
118 // ###################################################################
119 /**
120 * Constructor
121 */
122 public function __construct(&$registry)
123 {
124 $this->registry =& $registry;
125
126 // because ivars and call_user_func() are conspiring against us...
127 foreach ($this->commands AS $key => $string)
128 {
129 if (strpos($string, '$this->') !== false)
130 {
131 $this->commands["$key"] = array($this, str_replace('$this->', '', $string));
132 }
133 }
134 }
135
136 // ###################################################################
137 /**
138 * Initializes the class and all subclasses under a common package name
139 *
140 * @return string The package name
141 */
142 protected function init_as_package()
143 {
144 if (!defined('ISSO_DB_LAYER'))
145 {
146 define('ISSO_DB_LAYER', get_class($this));
147 trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING);
148 }
149
150 return 'db';
151 }
152
153 // ###################################################################
154 /**
155 * Connect to a the specified database
156 *
157 * @param string Server name
158 * @param string User name
159 * @param string Password
160 * @param string Database name
161 * @param bool Use p-connect?
162 *
163 * @return bool Result of connect
164 */
165 public function connect($server, $user, $password, $database, $pconnect)
166 {
167 $this->registry->check_isso_fields(get_class($this));
168
169 if ($this->dblink == false)
170 {
171 $this->dblink = call_user_func(($pconnect ? $this->commands['pconnect'] : $this->commands['connect']), $server, $user, $password, $database);
172
173 if ($this->dblink == false)
174 {
175 $this->error('DB-Link == false, cannot connect');
176 return false;
177 }
178
179 return true;
180 }
181 }
182
183 // ###################################################################
184 /**
185 * Send a query to the open database link
186 *
187 * @param string Query string
188 *
189 * @return integer Result
190 */
191 public function query($string)
192 {
193 $time = microtime();
194
195 $this->querystr = $string;
196 $this->result = @call_user_func($this->commands['query'], $this->dblink, $string);
197
198 if (!$this->result)
199 {
200 $this->error('Invalid SQL query');
201 }
202
203 $this->history[] = $history = array('query' => $string, 'time' => ($this->registry->is_loaded('functions') ? $this->registry->modules['functions']->fetch_microtime_diff($time) : 0), 'trace' => $this->registry->format_debug_trace(debug_backtrace()));
204
205 if (defined('ISSO_SHOW_QUERIES_LIVE'))
206 {
207 if (constant('ISSO_SHOW_QUERIES_LIVE'))
208 {
209 print($this->construct_query_debug($history));
210 }
211 }
212
213 return $this->result;
214 }
215
216 // ###################################################################
217 /**
218 * Escape a string (depending on character set, if supported)
219 *
220 * @param string String to be escaped
221 *
222 * @return string Escaped string
223 */
224 public function escape_string($string)
225 {
226 return call_user_func($this->commands['escape_string'], $this->dblink, $string);
227 }
228
229 // ###################################################################
230 /**
231 * Escapes a binary string for insertion into the database
232 *
233 * @param string Unescaped data
234 *
235 * @return string Escaped binary data
236 */
237 public function escape_binary($binary)
238 {
239 return call_user_func($this->commands['escape_binary'], $binary);
240 }
241
242 // ###################################################################
243 /**
244 * Unescapes a binary string that was fetched from the database
245 *
246 * @param string Escaped data
247 *
248 * @return string Unescaped binary data
249 */
250 public function unescape_binary($binary)
251 {
252 return call_user_func($this->commands['unescape_binary'], $binary);
253 }
254
255 // ###################################################################
256 /**
257 * Fetch the query result as an array
258 *
259 * @param integer Result
260 * @param bool Return an associative array?
261 *
262 * @return array A row of the query result
263 */
264 public function fetch_array($result, $assoc = true)
265 {
266 return call_user_func($this->commands[ ($assoc ? 'fetch_assoc' : 'fetch_row') ], $result);
267 }
268
269 // ###################################################################
270 /**
271 * Fetch the query result as an object
272 *
273 * @param integer Result
274 *
275 * @return object An object with the query result
276 */
277 public function fetch_object($result)
278 {
279 return call_user_func($this->commands['fetch_object'], $result);
280 }
281
282 // ###################################################################
283 /**
284 * Send a query and return the first row of the results
285 *
286 * @param string Query string
287 * @param string Result return function (in the database layer)
288 *
289 * @return mixed Results in variable formats
290 */
291 public function query_first($string, $callback = 'fetch_array')
292 {
293 $resource = $this->query($string);
294 if ($resource)
295 {
296 $return = $this->$callback($resource);
297 $this->free_result($resource);
298 return $return;
299 }
300 else
301 {
302 return false;
303 }
304 }
305
306 // ###################################################################
307 /**
308 * Free the current query result
309 *
310 * @param integer Result
311 */
312 public function free_result($result)
313 {
314 call_user_func($this->commands['free_result'], $result);
315 $this->result = null;
316 $this->querystr = '';
317 }
318
319 // ###################################################################
320 /**
321 * Fetch the unique ID of the record just inserted
322 *
323 * @return integer Insert-ID
324 */
325 public function insert_id()
326 {
327 return call_user_func($this->commands['insert_id'], $this->dblink);
328 }
329
330 // ###################################################################
331 /**
332 * Fetch the number of rows in the result
333 *
334 * @param integer Result
335 *
336 * @return integer Number of rows
337 */
338 public function num_rows($result)
339 {
340 return call_user_func($this->commands['num_rows'], $result);
341 }
342
343 // ###################################################################
344 /**
345 * Fetch the number of rows affected by the query
346 *
347 * @param integer Result
348 *
349 * @return integer Number of affected rows
350 */
351 public function affected_rows($result)
352 {
353 return call_user_func($this->commands['affected_rows'], $result);
354 }
355
356 // ###################################################################
357 /**
358 * Sends the command to start a transaction. This command should never
359 * be reached as it's always overridden
360 */
361 public function transaction_start()
362 {
363 trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed', E_USER_ERROR);
364 }
365
366 // ###################################################################
367 /**
368 * Sends the command to set this as a savepoint. This command should never
369 * be reached as it's always overridden
370 *
371 * @param string Named savepoint
372 */
373 public function transaction_savepoint($name)
374 {
375 trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed', E_USER_ERROR);
376 }
377
378 // ###################################################################
379 /**
380 * Sends the command to rollback to a given savepoint. This command
381 * should never be reached as it's always overridden
382 *
383 * @param string Named savepoint
384 */
385 public function transaction_rollback($name)
386 {
387 trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed', E_USER_ERROR);
388 }
389
390 // ###################################################################
391 /**
392 * Sends the command to commit the entire transaction. This command
393 * should never be reached as it's always overridden
394 */
395 public function transaction_commit($name)
396 {
397 trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed', E_USER_ERROR);
398 }
399
400 // ###################################################################
401 /**
402 * Constructs a table of query information output that is used in some
403 * other modules to display a list of queries. This merely formats
404 * a DB->history array entry nicely
405 *
406 * @param array An entry from DB->history
407 *
408 * @return string A formatted table block
409 */
410 public function construct_query_debug($query)
411 {
412 $block = "<strong>Query:</strong>\n\n<div>" . $this->registry->entity_encode($query['query']) . "</div>\n";
413 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
414 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
415 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
416
417 return $this->registry->message('Query Debug', $block, 1, true, false, 0);
418 }
419
420 // ###################################################################
421 /**
422 * Error wrapper for ISSO->message()
423 *
424 * @param string User defined error message
425 */
426 protected function error($message)
427 {
428 if ($this->showerrors)
429 {
430 if ($this->dblink)
431 {
432 $this->errnum = call_user_func($this->commands['error_num'], $this->dblink);
433 $this->errstr = call_user_func($this->commands['error_str'], $this->dblink);
434 }
435
436 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
437
438 $message_prepped = "<blockquote>\n<p>";
439 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr) ."</pre>\n<br />";
440 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum . "</span>\n<br />";
441 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
442 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
443 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
444 $message_prepped .= "\n</p>\n</blockquote>";
445
446 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
447 exit;
448 }
449 }
450 }
451
452 /*=====================================================================*\
453 || ###################################################################
454 || # $HeadURL$
455 || # $Id$
456 || ###################################################################
457 \*=====================================================================*/
458 ?>