]>
src.bluestatic.org Git - isso.git/blob - db.php
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
55 * Determines whether or not errors should be shown
59 var $showerrors = true;
62 * Current error number
69 * Description of current error
76 * Currend open MySQL connexion
90 * Current query string
97 * History of all executed queryies
101 var $history = array();
104 * Command mapping list
108 var $commands = array(
109 'pconnect' => '%server %user %password %database',
110 'connect' => '%server %user %password %database',
111 'query' => '%link %query',
112 'error_num' => '%link',
113 'error_str' => '%link',
114 'escape_string' => '%link %string',
115 'fetch_assoc' => '%result',
116 'fetch_object' => '%result',
117 'free_result' => '%result',
118 'insert_id' => '%link',
119 'num_rows' => '%result',
120 'affected_rows' => '%result'
123 // ###################################################################
127 function __construct(&$registry)
129 $this->registry
=& $registry;
131 // because ivars and call_user_func() are conspiring against us...
132 foreach ($this->commands
AS $key => $string)
134 if (strpos($string, '$this->') !== false)
136 $this->commands
["$key"] = array($this, str_replace('$this->', '', $string));
141 // ###################################################################
143 * (PHP 4) Constructor
145 function DB_Abstract(&$registry)
147 $this->__construct($registry);
150 // ###################################################################
152 * Initializes the class and all subclasses under a common package name
156 * @return string The package name
158 function init_as_package()
163 // ###################################################################
165 * Connect to a the specified database
169 * @param string Server name
170 * @param string User name
171 * @param string Password
172 * @param string Database name
173 * @param bool Use p-connect?
175 * @return bool Result of connect
177 function connect($server, $user, $password, $database, $pconnect)
179 $this->registry->check_isso_fields(get_class($this));
181 if ($this->dblink == false)
183 $this->dblink = call_user_func(($pconnect ? $this->commands['pconnect'] : $this->commands['connect']), $server, $user, $password, $database);
185 if ($this->dblink == false)
187 $this->error('DB-Link == false, cannot connect');
195 // ###################################################################
197 * Send a query to the open database link
201 * @param string Query string
203 * @return integer Result
205 function query($string)
207 $this->querystr = $string;
208 $this->result = call_user_func($this->commands['query'], $this->dblink, $string);
209 $this->history[] = $string;
211 if (defined('ISSO_SHOW_QUERIES_LIVE'))
213 if (constant('ISSO_SHOW_QUERIES_LIVE'))
215 print($this->querystr . '<hr />');
221 $this->error('Invalid SQL query');
224 return $this->result;
227 // ###################################################################
229 * Escape a string (depending on character set, if supported)
233 * @param string String to be escaped
235 * @return string Escaped string
237 function escape_string($string)
239 return call_user_func($this->commands['escape_string'], $this->dblink, $string);
242 // ###################################################################
244 * Fetch the query result as an array
248 * @param integer Result
250 * @return array A row of the query result
252 function fetch_array($result)
254 return call_user_func($this->commands['fetch_assoc'], $result);
257 // ###################################################################
259 * Fetch the query result as an object
263 * @param integer Result
265 * @return object An object with the query result
267 function fetch_object($result)
269 return call_user_func($this->commands['fetch_object'], $result);
272 // ###################################################################
274 * Send a query and return the first row of the results
278 * @param string Query string
279 * @param string Result return function (in the database layer)
281 * @return mixed Results in variable formats
283 function query_first($string, $callback = 'fetch_array')
285 $resource = $this->query($string);
286 $return = $this->$callback($resource);
287 $this->free_result($resource);
291 // ###################################################################
293 * Free the current query result
297 * @param integer Result
299 function free_result($result)
301 call_user_func($this->commands['free_result'], $result);
302 $this->result = null;
303 $this->querystr = '';
306 // ###################################################################
308 * Fetch the unique ID of the record just inserted
312 * @return integer Insert-ID
316 return call_user_func($this->commands['insert_id'], $this->dblink);
319 // ###################################################################
321 * Fetch the number of rows in the result
325 * @param integer Result
327 * @return integer Number of rows
329 function num_rows($result)
331 return call_user_func($this->commands['num_rows'], $result);
334 // ###################################################################
336 * Fetch the number of rows affected by the query
340 * @param integer Result
342 * @return integer Number of affected rows
344 function affected_rows($result)
346 return call_user_func($this->commands['affected_rows'], $result);
349 // ###################################################################
351 * Error wrapper for ISSO->message()
355 * @param string User defined error message
357 function error($message)
359 if ($this->showerrors)
363 $this->errnum = call_user_func($this->commands['error_num'], $this->dblink);
364 $this->errstr = call_user_func($this->commands['error_str'], $this->dblink);
367 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
369 $message_prepped = "<blockquote
>\n<p
>";
370 $message_prepped .= "\n\t
»
; <strong
>Query
:</strong
>\n<br
/> <pre style
=\"$style[code
]\">" . htmlspecialchars($this->querystr) ."</pre
>\n<br
/>";
371 $message_prepped .= "\n\t
»
; <strong
>Error Number
:</strong
> <span style
=\"$style[code
]\">" . $this->errnum . "</span
>\n<br
/>";
372 $message_prepped .= "\n\t
»
; <strong
>Error Message
:</strong
> <span style
=\"$style[code
]\">" . $this->errstr . "</span
>\n<br
/>";
373 $message_prepped .= "\n\t
»
; <strong
>Additional Notes
:</strong
> <span style
=\"$style[code
]\">" . $message . "</span
>\n<br
/>";
374 $message_prepped .= "\n\t
»
; <strong
>File
:</strong
> <span style
=\"$style[code
]\">" . $_SERVER['PHP_SELF'] . "</span
>\n";
375 $message_prepped .= "\n
</p
>\n</blockquote
>";
377 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
383 /*=====================================================================*\
384 || ###################################################################
387 || ###################################################################
388 \*=====================================================================*/