2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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
40 * @author Iris Studios, Inc.
41 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
49 * Framework registry object
56 * Determines whether or not errors should be shown
60 var $showerrors = true
;
63 * Current error number
70 * Description of current error
77 * Currend open MySQL connexion
91 * Current query string
98 * History of all executed queryies
102 var $history = array();
105 * Command mapping list
109 var $commands = array(
110 'pconnect' => '%server %user %password %database',
111 'connect' => '%server %user %password %database',
112 'query' => '%link %query',
113 'error_num' => '%link',
114 'error_str' => '%link',
115 'escape_string' => '%link %string',
116 'fetch_assoc' => '%result',
117 'fetch_row' => '%result',
118 'fetch_object' => '%result',
119 'free_result' => '%result',
120 'insert_id' => '%link',
121 'num_rows' => '%result',
122 'affected_rows' => '%result'
125 // ###################################################################
129 function __construct(&$registry)
131 $this->registry
=& $registry;
133 // because ivars and call_user_func() are conspiring against us...
134 foreach ($this->commands
AS $key => $string)
136 if (strpos($string, '$this->') !== false
)
138 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
143 // ###################################################################
145 * (PHP 4) Constructor
147 function DB_Abstract(&$registry)
149 $this->__construct($registry);
152 // ###################################################################
154 * Initializes the class and all subclasses under a common package name
158 * @return string The package name
160 function init_as_package()
162 if (!defined('ISSO_DB_LAYER'))
164 define('ISSO_DB_LAYER', get_class($this));
165 trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING
);
171 // ###################################################################
173 * Connect to a the specified database
177 * @param string Server name
178 * @param string User name
179 * @param string Password
180 * @param string Database name
181 * @param bool Use p-connect?
183 * @return bool Result of connect
185 function connect($server, $user, $password, $database, $pconnect)
187 $this->registry
->check_isso_fields(get_class($this));
189 if ($this->dblink
== false
)
191 $this->dblink
= call_user_func(($pconnect ?
$this->commands
['pconnect'] : $this->commands
['connect']), $server, $user, $password, $database);
193 if ($this->dblink
== false
)
195 $this->error('DB-Link == false, cannot connect');
203 // ###################################################################
205 * Send a query to the open database link
209 * @param string Query string
211 * @return integer Result
213 function query($string)
217 $this->querystr
= $string;
218 $this->result
= call_user_func($this->commands
['query'], $this->dblink
, $string);
222 $this->error('Invalid SQL query');
225 $this->history
[] = $history = array('query' => $string, 'time' => $this->registry
->modules
['functions']->fetch_microtime_diff($time), 'trace' => $this->registry
->format_debug_trace(debug_backtrace()));
227 if (defined('ISSO_SHOW_QUERIES_LIVE'))
229 if (constant('ISSO_SHOW_QUERIES_LIVE'))
231 print($this->construct_query_debug($history));
235 return $this->result
;
238 // ###################################################################
240 * Escape a string (depending on character set, if supported)
244 * @param string String to be escaped
246 * @return string Escaped string
248 function escape_string($string)
250 return call_user_func($this->commands
['escape_string'], $this->dblink
, $string);
253 // ###################################################################
255 * 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 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
275 * @param integer Result
277 * @return object An object with the query result
279 function fetch_object($result)
281 return call_user_func($this->commands
['fetch_object'], $result);
284 // ###################################################################
286 * Send a query and return the first row of the results
290 * @param string Query string
291 * @param string Result return function (in the database layer)
293 * @return mixed Results in variable formats
295 function query_first($string, $callback = 'fetch_array')
297 $resource = $this->query($string);
298 $return = $this->$callback($resource);
299 $this->free_result($resource);
303 // ###################################################################
305 * Free the current query result
309 * @param integer Result
311 function free_result($result)
313 call_user_func($this->commands
['free_result'], $result);
314 $this->result
= null
;
315 $this->querystr
= '';
318 // ###################################################################
320 * Fetch the unique ID of the record just inserted
324 * @return integer Insert-ID
328 return call_user_func($this->commands
['insert_id'], $this->dblink
);
331 // ###################################################################
333 * Fetch the number of rows in the result
337 * @param integer Result
339 * @return integer Number of rows
341 function num_rows($result)
343 return call_user_func($this->commands
['num_rows'], $result);
346 // ###################################################################
348 * Fetch the number of rows affected by the query
352 * @param integer Result
354 * @return integer Number of affected rows
356 function affected_rows($result)
358 return call_user_func($this->commands
['affected_rows'], $result);
361 // ###################################################################
363 * Constructs a table of query information output that is used in some
364 * other modules to display a list of queries. This merely formats
365 * a DB->history array entry nicely
369 * @param array An entry from DB->history
371 * @return string A formatted table block
373 function construct_query_debug($query)
375 $block = "<strong>Query:</strong>\n\n<div>" . $this->registry
->entity_encode($query['query']) . "</div>\n";
376 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
377 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
378 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
380 return $this->registry
->message('Query Debug', $block, 1, true
, false
, 0);
383 // ###################################################################
385 * Error wrapper for ISSO->message()
389 * @param string User defined error message
391 function error($message)
393 if ($this->showerrors
)
397 $this->errnum
= call_user_func($this->commands
['error_num'], $this->dblink
);
398 $this->errstr
= call_user_func($this->commands
['error_str'], $this->dblink
);
401 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
403 $message_prepped = "<blockquote>\n<p>";
404 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
405 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
406 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
407 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
408 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
409 $message_prepped .= "\n</p>\n</blockquote>";
411 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
417 /*=====================================================================*\
418 || ###################################################################
421 || ###################################################################
422 \*=====================================================================*/