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.
36 * @author Iris Studios, Inc.
37 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
45 * Framework registry object
51 * Determines whether or not errors should be shown
54 var $showerrors = true
;
57 * Current error number
63 * Description of current error
69 * Currend open MySQL connexion
81 * Current query string
87 * History of all executed queryies
90 var $history = array();
93 * Command mapping list
96 var $commands = array(
97 'pconnect' => '%server %user %password %database',
98 'connect' => '%server %user %password %database',
99 'query' => '%link %query',
100 'error_num' => '%link',
101 'error_str' => '%link',
102 'escape_string' => '%link %string',
103 'fetch_assoc' => '%result',
104 'fetch_object' => '%result',
105 'free_result' => '%result',
106 'insert_id' => '%link',
107 'num_rows' => '%result',
108 'affected_rows' => '%result'
111 // ###################################################################
115 function __construct(&$registry)
117 $this->registry
=& $registry;
119 // because ivars and call_user_func() are conspiring against us...
120 foreach ($this->commands
AS $key => $string)
122 if (strpos($string, '$this->') !== false
)
124 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
129 // ###################################################################
131 * (PHP 4) Constructor
133 function DB_Abstract(&$registry)
135 $this->__construct($registry);
138 // ###################################################################
140 * Connect to a the specified database
144 * @param string Server name
145 * @param string User name
146 * @param string Password
147 * @param string Database name
148 * @param bool Use p-connect?
150 * @return bool Result of connect
152 function connect($server, $user, $password, $database, $pconnect)
154 if ($this->dblink
== false
)
156 $this->dblink
= call_user_func(($pconnect ?
$this->commands
['pconnect'] : $this->commands
['connect']), $server, $user, $password, $database);
158 if ($this->dblink
== false
)
160 $this->error('DB-Link == false, cannot connect');
168 // ###################################################################
170 * Send a query to the open database link
174 * @param string Query string
176 * @return integer Query-ID
178 function query($string)
180 $this->querystr
= $string;
181 $this->result
= call_user_func($this->commands
['query'], $this->dblink
, $string);
182 $this->history
[] = $string;
184 if (defined('ISSO_SHOW_QUERIES_LIVE'))
186 if (constant('ISSO_SHOW_QUERIES_LIVE'))
188 print($this->querystr
. '<hr />');
194 $this->error('Invalid SQL query');
197 return $this->result
;
200 // ###################################################################
202 * Escape a string (depending on character set, if supported)
206 * @param string String to be escaped
208 * @return string Escaped string
210 function escape_string($string)
212 return call_user_func($this->commands
['escape_string'], $this->dblink
, $string);
215 // ###################################################################
217 * Fetch the query result as an array
221 * @param integer Query-ID
223 * @return array A row of the query result
225 function fetch_array($result)
227 return call_user_func($this->commands
['fetch_assoc'], $result);
230 // ###################################################################
232 * Fetch the query result as an object
236 * @param integer Query-ID
238 * @return object An object with the query result
240 function fetch_object($result)
242 return call_user_func($this->commands
['fetch_object'], $result);
245 // ###################################################################
247 * Send a query and return the first row of the results
251 * @param string Query string
252 * @param string Result return function (in the database layer)
254 * @return mixed Results in variable formats
256 function query_first($string, $callback = 'fetch_array')
258 $resource = $this->query($string);
259 $return = $this->$callback($resource);
260 $this->free_result($resource);
264 // ###################################################################
266 * Free the current query result
270 * @param integer Query-ID
272 function free_result($result)
274 call_user_func($this->commands
['free_result'], $result);
275 $this->result
= null
;
276 $this->querystr
= '';
279 // ###################################################################
281 * Fetch the unique ID of the record just inserted
285 * @return integer Insert ID
289 return call_user_func($this->commands
['insert_id'], $this->dblink
);
292 // ###################################################################
294 * Fetch the number of rows in the result
298 * @param integer Query-ID
300 * @return integer Number of rows
302 function num_rows($result)
304 return call_user_func($this->commands
['num_rows'], $result);
307 // ###################################################################
309 * Fetch the number of rows affected by the query
313 * @param integer Query-ID
315 * @return integer Number of affected rows
317 function affected_rows($result)
319 return call_uesr_func($this->commands
['affected_rows'], $result);
322 // ###################################################################
324 * Error wrapper for ISSO->message()
328 * @param string User defined error message
330 function error($message)
332 if ($this->showerrors
)
336 $this->errnum
= $this->commands
['error_num']($this->dblink
);
337 $this->errstr
= $this->commands
['error_str']($this->dblink
);
340 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
342 $message_prepped = "<blockquote>\n<p>";
343 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
344 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
345 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
346 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
347 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
348 $message_prepped .= "\n</p>\n</blockquote>";
350 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);
356 /*=====================================================================*\
357 || ###################################################################
360 || ###################################################################
361 \*=====================================================================*/