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
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
48 * Framework registry object
54 * Determines whether or not errors should be shown
57 var $showerrors = true
;
60 * Current error number
66 * Description of current error
72 * Currend open MySQL connexion
84 * Current query string
90 * History of all executed queryies
93 var $history = array();
96 * Command mapping list
99 var $commands = array(
100 'pconnect' => '%server %user %password %database',
101 'connect' => '%server %user %password %database',
102 'query' => '%link %query',
103 'error_num' => '%link',
104 'error_str' => '%link',
105 'escape_string' => '%link %string',
106 'fetch_assoc' => '%result',
107 'fetch_object' => '%result',
108 'free_result' => '%result',
109 'insert_id' => '%link',
110 'num_rows' => '%result',
111 'affected_rows' => '%result'
114 // ###################################################################
118 function __construct(&$registry)
120 $this->registry
=& $registry;
122 // because ivars and call_user_func() are conspiring against us...
123 foreach ($this->commands
AS $key => $string)
125 if (strpos($string, '$this->') !== false
)
127 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
132 // ###################################################################
134 * (PHP 4) Constructor
136 function DB_Abstract(&$registry)
138 $this->__construct($registry);
141 // ###################################################################
143 * Connect to a the specified database
147 * @param string Server name
148 * @param string User name
149 * @param string Password
150 * @param string Database name
151 * @param bool Use p-connect?
153 * @return bool Result of connect
155 function connect($server, $user, $password, $database, $pconnect)
157 if ($this->dblink
== false
)
159 $this->dblink
= call_user_func(($pconnect ?
$this->commands
['pconnect'] : $this->commands
['connect']), $server, $user, $password, $database);
161 if ($this->dblink
== false
)
163 $this->error('DB-Link == false, cannot connect');
171 // ###################################################################
173 * Send a query to the open database link
177 * @param string Query string
179 * @return integer Result
181 function query($string)
183 $this->querystr
= $string;
184 $this->result
= call_user_func($this->commands
['query'], $this->dblink
, $string);
185 $this->history
[] = $string;
187 if (defined('ISSO_SHOW_QUERIES_LIVE'))
189 if (constant('ISSO_SHOW_QUERIES_LIVE'))
191 print($this->querystr
. '<hr />');
197 $this->error('Invalid SQL query');
200 return $this->result
;
203 // ###################################################################
205 * Escape a string (depending on character set, if supported)
209 * @param string String to be escaped
211 * @return string Escaped string
213 function escape_string($string)
215 return call_user_func($this->commands
['escape_string'], $this->dblink
, $string);
218 // ###################################################################
220 * Fetch the query result as an array
224 * @param integer Result
226 * @return array A row of the query result
228 function fetch_array($result)
230 return call_user_func($this->commands
['fetch_assoc'], $result);
233 // ###################################################################
235 * Fetch the query result as an object
239 * @param integer Result
241 * @return object An object with the query result
243 function fetch_object($result)
245 return call_user_func($this->commands
['fetch_object'], $result);
248 // ###################################################################
250 * Send a query and return the first row of the results
254 * @param string Query string
255 * @param string Result return function (in the database layer)
257 * @return mixed Results in variable formats
259 function query_first($string, $callback = 'fetch_array')
261 $resource = $this->query($string);
262 $return = $this->$callback($resource);
263 $this->free_result($resource);
267 // ###################################################################
269 * Free the current query result
273 * @param integer Result
275 function free_result($result)
277 call_user_func($this->commands
['free_result'], $result);
278 $this->result
= null
;
279 $this->querystr
= '';
282 // ###################################################################
284 * Fetch the unique ID of the record just inserted
288 * @return integer Insert-ID
292 return call_user_func($this->commands
['insert_id'], $this->dblink
);
295 // ###################################################################
297 * Fetch the number of rows in the result
301 * @param integer Result
303 * @return integer Number of rows
305 function num_rows($result)
307 return call_user_func($this->commands
['num_rows'], $result);
310 // ###################################################################
312 * Fetch the number of rows affected by the query
316 * @param integer Result
318 * @return integer Number of affected rows
320 function affected_rows($result)
322 return call_user_func($this->commands
['affected_rows'], $result);
325 // ###################################################################
327 * Error wrapper for ISSO->message()
331 * @param string User defined error message
333 function error($message)
335 if ($this->showerrors
)
339 $this->errnum
= call_user_func($this->commands
['error_num'], $this->dblink
);
340 $this->errstr
= call_user_func($this->commands
['error_str'], $this->dblink
);
343 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
345 $message_prepped = "<blockquote>\n<p>";
346 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
347 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
348 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
349 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
350 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
351 $message_prepped .= "\n</p>\n</blockquote>";
353 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
359 /*=====================================================================*\
360 || ###################################################################
363 || ###################################################################
364 \*=====================================================================*/