]>
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
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->is_loaded('functions') ? $this- >registry->modules['functions']->fetch_microtime_diff( $time ) : 0), '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 * Sends the command to start a transaction. This command should never
364 * be reached as it's always overridden
368 function transaction_start()
370 trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed', E_USER_ERROR);
373 // ###################################################################
375 * Sends the command to set this as a savepoint. This command should never
376 * be reached as it's always overridden
380 * @param string Named savepoint
382 function transaction_savepoint( $name )
384 trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed', E_USER_ERROR);
387 // ###################################################################
389 * Sends the command to rollback to a given savepoint. This command
390 * should never be reached as it's always overridden
394 * @param string Named savepoint
396 function transaction_rollback( $name )
398 trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed', E_USER_ERROR);
401 // ###################################################################
403 * Sends the command to commit the entire transaction. This command
404 * should never be reached as it's always overridden
408 function transaction_commit( $name )
410 trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed', E_USER_ERROR);
413 // ###################################################################
415 * Constructs a table of query information output that is used in some
416 * other modules to display a list of queries. This merely formats
417 * a DB->history array entry nicely
421 * @param array An entry from DB->history
423 * @return string A formatted table block
425 function construct_query_debug( $query )
427 $block = " < strong
> Query
:</ strong
>\n\n< div
> " . $this- >registry->entity_encode( $query ['query']) . " </ div
>\n ";
428 $block .= " < tr style
=\" background
- color
: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
429 $block .= "<strong>Time:</strong> $query [time]<br /> \n\t\t <br /> \n\t\t " ;
430 $block .= "<strong>Backtrace:</strong> \n\t\t <div>" . implode ( "<br /> \n " , $query [ 'trace' ]) . "</div> \n\t </td> \n </tr>" ;
432 return $this- > registry
-> message ( 'Query Debug' , $block , 1 , true , false , 0 );
435 // ###################################################################
437 * Error wrapper for ISSO->message()
441 * @param string User defined error message
443 function error ( $message )
445 if ( $this- > showerrors
)
449 $this- > errnum
= call_user_func ( $this- > commands
[ 'error_num' ], $this- > dblink
);
450 $this- > errstr
= call_user_func ( $this- > commands
[ 'error_str' ], $this- > dblink
);
453 $style [ 'code' ] = 'font-family: \' Courier New \' , Courier, mono; font-size: 11px;' ;
455 $message_prepped = "<blockquote> \n <p>" ;
456 $message_prepped .= " \n\t » <strong>Query:</strong> \n <br /> <pre style= \" $style [code] \" >" . htmlspecialchars ( $this- > querystr
) . "</pre> \n <br />" ;
457 $message_prepped .= " \n\t » <strong>Error Number:</strong> <span style= \" $style [code] \" >" . $this- > errnum
. "</span> \n <br />" ;
458 $message_prepped .= " \n\t » <strong>Error Message:</strong> <span style= \" $style [code] \" >" . $this- > errstr
. "</span> \n <br />" ;
459 $message_prepped .= " \n\t » <strong>Additional Notes:</strong> <span style= \" $style [code] \" >" . $message . "</span> \n <br />" ;
460 $message_prepped .= " \n\t » <strong>File:</strong> <span style= \" $style [code] \" >" . $_SERVER [ 'PHP_SELF' ] . "</span> \n " ;
461 $message_prepped .= " \n </p> \n </blockquote>" ;
463 $this- > registry
-> message ( 'Database Error in `<em>' . $this- > registry
-> application
. '</em>`' , $message_prepped , 3 );
469 /*=====================================================================*\
470 || ###################################################################
473 || ###################################################################
474 \*=====================================================================*/