]>
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()
160 if (!defined('ISSO_DB_LAYER'))
162 define('ISSO_DB_LAYER', get_class( $this ));
163 trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING);
169 // ###################################################################
171 * Connect to a the specified database
175 * @param string Server name
176 * @param string User name
177 * @param string Password
178 * @param string Database name
179 * @param bool Use p-connect?
181 * @return bool Result of connect
183 function connect( $server , $user , $password , $database , $pconnect )
185 $this- >registry->check_isso_fields(get_class( $this ));
187 if ( $this- >dblink == false)
189 $this- >dblink = call_user_func(( $pconnect ? $this- >commands['pconnect'] : $this- >commands['connect']), $server , $user , $password , $database );
191 if ( $this- >dblink == false)
193 $this- >error('DB-Link == false, cannot connect');
201 // ###################################################################
203 * Send a query to the open database link
207 * @param string Query string
209 * @return integer Result
211 function query( $string )
213 $this- >querystr = $string ;
214 $this- >result = call_user_func( $this- >commands['query'], $this- >dblink, $string );
215 $this- >history[] = $string ;
217 if (defined('ISSO_SHOW_QUERIES_LIVE'))
219 if (constant('ISSO_SHOW_QUERIES_LIVE'))
221 print( $this- >querystr . '<hr />');
227 $this- >error('Invalid SQL query');
230 return $this- >result;
233 // ###################################################################
235 * Escape a string (depending on character set, if supported)
239 * @param string String to be escaped
241 * @return string Escaped string
243 function escape_string( $string )
245 return call_user_func( $this- >commands['escape_string'], $this- >dblink, $string );
248 // ###################################################################
250 * Fetch the query result as an array
254 * @param integer Result
256 * @return array A row of the query result
258 function fetch_array( $result )
260 return call_user_func( $this- >commands['fetch_assoc'], $result );
263 // ###################################################################
265 * Fetch the query result as an object
269 * @param integer Result
271 * @return object An object with the query result
273 function fetch_object( $result )
275 return call_user_func( $this- >commands['fetch_object'], $result );
278 // ###################################################################
280 * Send a query and return the first row of the results
284 * @param string Query string
285 * @param string Result return function (in the database layer)
287 * @return mixed Results in variable formats
289 function query_first( $string , $callback = 'fetch_array')
291 $resource = $this- >query( $string );
292 $return = $this- > $callback ( $resource );
293 $this- >free_result( $resource );
297 // ###################################################################
299 * Free the current query result
303 * @param integer Result
305 function free_result( $result )
307 call_user_func( $this- >commands['free_result'], $result );
308 $this- >result = null;
309 $this- >querystr = '';
312 // ###################################################################
314 * Fetch the unique ID of the record just inserted
318 * @return integer Insert-ID
322 return call_user_func( $this- >commands['insert_id'], $this- >dblink);
325 // ###################################################################
327 * Fetch the number of rows in the result
331 * @param integer Result
333 * @return integer Number of rows
335 function num_rows( $result )
337 return call_user_func( $this- >commands['num_rows'], $result );
340 // ###################################################################
342 * Fetch the number of rows affected by the query
346 * @param integer Result
348 * @return integer Number of affected rows
350 function affected_rows( $result )
352 return call_user_func( $this- >commands['affected_rows'], $result );
355 // ###################################################################
357 * Error wrapper for ISSO->message()
361 * @param string User defined error message
363 function error( $message )
365 if ( $this- >showerrors)
369 $this- >errnum = call_user_func( $this- >commands['error_num'], $this- >dblink);
370 $this- >errstr = call_user_func( $this- >commands['error_str'], $this- >dblink);
373 $style ['code'] = 'font-family: \' Courier New \' , Courier, mono; font-size: 11px;';
375 $message_prepped = " < blockquote
>\n< p
> ";
376 $message_prepped .= " \n\t
& raquo
; < strong
> Query
:</ strong
>\n< br
/> < pre style
=\" $style [ code
]\"> " . htmlspecialchars( $this- >querystr) ." </ pre
>\n< br
/> ";
377 $message_prepped .= " \n\t
& raquo
; < strong
> Error Number
:</ strong
> < span style
=\" $style [ code
]\"> " . $this- >errnum . " </ span
>\n< br
/> ";
378 $message_prepped .= " \n\t
& raquo
; < strong
> Error Message
:</ strong
> < span style
=\" $style [ code
]\"> " . $this- >errstr . " </ span
>\n< br
/> ";
379 $message_prepped .= " \n\t
& raquo
; < strong
> Additional Notes
:</ strong
> < span style
=\" $style [ code
]\"> " . $message . " </ span
>\n< br
/> ";
380 $message_prepped .= " \n\t
& raquo
; < strong
> File
:</ strong
> < span style
=\" $style [ code
]\"> " . $_SERVER ['PHP_SELF'] . " </ span
>\n ";
381 $message_prepped .= " \n
</ p
>\n</ blockquote
> ";
383 $this- >registry->message('Database Error in `<em>' . $this- >registry->application . '</em>`', $message_prepped , 3);
389 /*=====================================================================*\
390 || ###################################################################
393 || ###################################################################
394 \*=====================================================================*/