]>
src.bluestatic.org Git - isso.git/blob - db.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
23 * Abstract Database Layer
30 * Abstract Database Layer
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.
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
41 * @copyright Copyright ©2002 - [#]year[#], Blue Static
49 * Framework registry object
52 protected $registry = null;
55 * Determines whether or not errors should be shown
58 public $showerrors = true;
61 * Current error number
64 protected $errnum = 0;
67 * Description of current error
70 protected $errstr = '';
73 * Currend open MySQL connexion
76 protected $dblink = null;
82 protected $result = null;
85 * Current query string
88 protected $querystr = '';
91 * History of all executed queryies
94 protected $history = array();
97 * Command mapping list
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'
118 // ###################################################################
122 public function __construct(&$registry)
124 $this->registry
=& $registry;
126 // because ivars and call_user_func() are conspiring against us...
127 foreach ($this->commands
AS $key => $string)
129 if (strpos($string, '$this->') !== false)
131 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
136 // ###################################################################
138 * Initializes the class and all subclasses under a common package name
140 * @return string The package name
142 protected function init_as_package()
144 if (!defined('ISSO_DB_LAYER'))
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');
153 // ###################################################################
155 * Connect to a the specified database
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?
163 * @return bool Result of connect
165 public function connect($server, $user, $password, $database, $pconnect)
167 $this->registry->check_isso_fields(get_class($this));
169 if ($this->dblink == false)
171 $this->dblink = call_user_func(($pconnect ? $this->commands['pconnect'] : $this->commands['connect']), $server, $user, $password, $database);
173 if ($this->dblink == false)
175 $this->error('DB-Link == false, cannot connect');
183 // ###################################################################
185 * Send a query to the open database link
187 * @param string Query string
189 * @return integer Result
191 public function query($string)
195 $this->querystr = $string;
196 $this->result = @call_user_func($this->commands['query'], $this->dblink, $string);
200 $this->error('Invalid SQL query');
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()));
205 if (defined('ISSO_SHOW_QUERIES_LIVE'))
207 if (constant('ISSO_SHOW_QUERIES_LIVE'))
209 print($this->construct_query_debug($history));
213 return $this->result;
216 // ###################################################################
218 * Escape a string (depending on character set, if supported)
220 * @param string String to be escaped
222 * @return string Escaped string
224 public function escape_string($string)
226 return call_user_func($this->commands['escape_string'], $this->dblink, $string);
229 // ###################################################################
231 * Escapes a binary string for insertion into the database
233 * @param string Unescaped data
235 * @return string Escaped binary data
237 public function escape_binary($binary)
239 return call_user_func($this->commands['escape_binary'], $binary);
242 // ###################################################################
244 * Unescapes a binary string that was fetched from the database
246 * @param string Escaped data
248 * @return string Unescaped binary data
250 public function unescape_binary($binary)
252 return call_user_func($this->commands['unescape_binary'], $binary);
255 // ###################################################################
257 * Fetch the query result as an array
259 * @param integer Result
260 * @param bool Return an associative array?
262 * @return array A row of the query result
264 public function fetch_array($result, $assoc = true)
266 return call_user_func($this->commands[ ($assoc ? 'fetch_assoc' : 'fetch_row') ], $result);
269 // ###################################################################
271 * Fetch the query result as an object
273 * @param integer Result
275 * @return object An object with the query result
277 public function fetch_object($result)
279 return call_user_func($this->commands['fetch_object'], $result);
282 // ###################################################################
284 * Send a query and return the first row of the results
286 * @param string Query string
287 * @param string Result return function (in the database layer)
289 * @return mixed Results in variable formats
291 public function query_first($string, $callback = 'fetch_array')
293 $resource = $this->query($string);
296 $return = $this->$callback($resource);
297 $this->free_result($resource);
306 // ###################################################################
308 * Free the current query result
310 * @param integer Result
312 public function free_result($result)
314 call_user_func($this->commands['free_result'], $result);
315 $this->result = null;
316 $this->querystr = '';
319 // ###################################################################
321 * Fetch the unique ID of the record just inserted
323 * @return integer Insert-ID
325 public function insert_id()
327 return call_user_func($this->commands['insert_id'], $this->dblink);
330 // ###################################################################
332 * Fetch the number of rows in the result
334 * @param integer Result
336 * @return integer Number of rows
338 public function num_rows($result)
340 return call_user_func($this->commands['num_rows'], $result);
343 // ###################################################################
345 * Fetch the number of rows affected by the query
347 * @param integer Result
349 * @return integer Number of affected rows
351 public function affected_rows($result)
353 return call_user_func($this->commands['affected_rows'], $result);
356 // ###################################################################
358 * Sends the command to start a transaction. This command should never
359 * be reached as it's always overridden
361 public function transaction_start()
363 trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed');
366 // ###################################################################
368 * Sends the command to set this as a savepoint. This command should never
369 * be reached as it's always overridden
371 * @param string Named savepoint
373 public function transaction_savepoint($name)
375 trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed');
378 // ###################################################################
380 * Sends the command to rollback to a given savepoint. This command
381 * should never be reached as it's always overridden
383 * @param string Named savepoint
385 public function transaction_rollback($name)
387 trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed');
390 // ###################################################################
392 * Sends the command to commit the entire transaction. This command
393 * should never be reached as it's always overridden
395 public function transaction_commit($name)
397 trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed');
400 // ###################################################################
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
406 * @param array An entry from DB->history
408 * @return string A formatted table block
410 public function construct_query_debug($query)
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>";
417 return $this->registry
->message('Query Debug', $block, 1, true, false, 0);
420 // ###################################################################
422 * Error wrapper for ISSO->message()
424 * @param string User defined error message
426 protected function error($message)
428 if ($this->showerrors
)
432 $this->errnum
= call_user_func($this->commands
['error_num'], $this->dblink
);
433 $this->errstr
= call_user_func($this->commands
['error_str'], $this->dblink
);
436 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
438 $message_prepped = "<blockquote>\n<p>";
439 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
440 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
441 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
442 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
443 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
444 $message_prepped .= "\n</p>\n</blockquote>";
446 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
452 /*=====================================================================*\
453 || ###################################################################
456 || ###################################################################
457 \*=====================================================================*/