]>
src.bluestatic.org Git - isso.git/blob - db.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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
41 * @copyright Copyright ©2002 - [#]year[#], Blue Static
49 * Framework registry object
52 protected $registry = null ;
55 * Determines whether or not errors should be shown
58 public $showerrors = true ;
61 * Current error number
64 protected $errnum = 0 ;
67 * Description of current error
70 protected $errstr = '' ;
73 * Currend open MySQL connexion
76 protected $dblink = null ;
82 protected $result = null ;
85 * Current query string
88 protected $querystr = '' ;
91 * History of all executed queryies
94 protected $history = array ();
97 * Command mapping list
100 protected $commands = array (
101 'pconnect' => '%server %user %password %database' ,
102 'connect' => '%server %user %password %database' ,
103 'query' => '%link %query' ,
104 'error_num' => '%link' ,
105 'error_str' => '%link' ,
106 'escape_string' => '%link %string' ,
107 'escape_binary' => '%string' ,
108 'unescape_binary' => '%string' ,
109 'fetch_assoc' => '%result' ,
110 'fetch_row' => '%result' ,
111 'fetch_object' => '%result' ,
112 'free_result' => '%result' ,
113 'insert_id' => '%link' ,
114 'num_rows' => '%result' ,
115 'affected_rows' => '%result'
118 // ###################################################################
122 function __construct (& $registry )
124 $this- > registry
=& $registry ;
126 // because ivars and call_user_func() are conspiring against us...
127 foreach ( $this- > commands
AS $key => $string )
129 if ( strpos ( $string , ' $this- >' ) !== false )
131 $this- > commands
[ " $key" ] = array( $this , str_replace(' $this- >', '', $string ));
136 // ###################################################################
138 * (PHP 4) Constructor
140 function DB_Abstract(& $registry )
142 $this- >__construct( $registry );
145 // ###################################################################
147 * Initializes the class and all subclasses under a common package name
151 * @return string The package name
153 function init_as_package()
155 if (!defined('ISSO_DB_LAYER'))
157 define('ISSO_DB_LAYER', get_class( $this ));
158 trigger_error('ISSO_DB_LAYER was defined automatically by DB::init_as_package(). Define the constant yourself to remove this warning', E_USER_WARNING);
164 // ###################################################################
166 * Connect to a the specified database
170 * @param string Server name
171 * @param string User name
172 * @param string Password
173 * @param string Database name
174 * @param bool Use p-connect?
176 * @return bool Result of connect
178 function connect( $server , $user , $password , $database , $pconnect )
180 $this- >registry->check_isso_fields(get_class( $this ));
182 if ( $this- >dblink == false)
184 $this- >dblink = call_user_func(( $pconnect ? $this- >commands['pconnect'] : $this- >commands['connect']), $server , $user , $password , $database );
186 if ( $this- >dblink == false)
188 $this- >error('DB-Link == false, cannot connect');
196 // ###################################################################
198 * Send a query to the open database link
202 * @param string Query string
204 * @return integer Result
206 function query( $string )
210 $this- >querystr = $string ;
211 $this- >result = @call_user_func( $this- >commands['query'], $this- >dblink, $string );
215 $this- >error('Invalid SQL query');
218 $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()));
220 if (defined('ISSO_SHOW_QUERIES_LIVE'))
222 if (constant('ISSO_SHOW_QUERIES_LIVE'))
224 print( $this- >construct_query_debug( $history ));
228 return $this- >result;
231 // ###################################################################
233 * Escape a string (depending on character set, if supported)
237 * @param string String to be escaped
239 * @return string Escaped string
241 function escape_string( $string )
243 return call_user_func( $this- >commands['escape_string'], $this- >dblink, $string );
246 // ###################################################################
248 * Escapes a binary string for insertion into the database
252 * @param string Unescaped data
254 * @return string Escaped binary data
256 function escape_binary( $binary )
258 return call_user_func( $this- >commands['escape_binary'], $binary );
261 // ###################################################################
263 * Unescapes a binary string that was fetched from the database
267 * @param string Escaped data
269 * @return string Unescaped binary data
271 function unescape_binary( $binary )
273 return call_user_func( $this- >commands['unescape_binary'], $binary );
276 // ###################################################################
278 * Fetch the query result as an array
282 * @param integer Result
283 * @param bool Return an associative array?
285 * @return array A row of the query result
287 function fetch_array( $result , $assoc = true)
289 return call_user_func( $this- >commands[ ( $assoc ? 'fetch_assoc' : 'fetch_row') ], $result );
292 // ###################################################################
294 * Fetch the query result as an object
298 * @param integer Result
300 * @return object An object with the query result
302 function fetch_object( $result )
304 return call_user_func( $this- >commands['fetch_object'], $result );
307 // ###################################################################
309 * Send a query and return the first row of the results
313 * @param string Query string
314 * @param string Result return function (in the database layer)
316 * @return mixed Results in variable formats
318 function query_first( $string , $callback = 'fetch_array')
320 $resource = $this- >query( $string );
323 $return = $this- > $callback ( $resource );
324 $this- >free_result( $resource );
333 // ###################################################################
335 * Free the current query result
339 * @param integer Result
341 function free_result( $result )
343 call_user_func( $this- >commands['free_result'], $result );
344 $this- >result = null;
345 $this- >querystr = '';
348 // ###################################################################
350 * Fetch the unique ID of the record just inserted
354 * @return integer Insert-ID
358 return call_user_func( $this- >commands['insert_id'], $this- >dblink);
361 // ###################################################################
363 * Fetch the number of rows in the result
367 * @param integer Result
369 * @return integer Number of rows
371 function num_rows( $result )
373 return call_user_func( $this- >commands['num_rows'], $result );
376 // ###################################################################
378 * Fetch the number of rows affected by the query
382 * @param integer Result
384 * @return integer Number of affected rows
386 function affected_rows( $result )
388 return call_user_func( $this- >commands['affected_rows'], $result );
391 // ###################################################################
393 * Sends the command to start a transaction. This command should never
394 * be reached as it's always overridden
398 function transaction_start()
400 trigger_error('DB_Abstract::transaction_start() needs to be overridden when subclassed', E_USER_ERROR);
403 // ###################################################################
405 * Sends the command to set this as a savepoint. This command should never
406 * be reached as it's always overridden
410 * @param string Named savepoint
412 function transaction_savepoint( $name )
414 trigger_error('DB_Abstract::transaction_savepoint() needs to be overridden when subclassed', E_USER_ERROR);
417 // ###################################################################
419 * Sends the command to rollback to a given savepoint. This command
420 * should never be reached as it's always overridden
424 * @param string Named savepoint
426 function transaction_rollback( $name )
428 trigger_error('DB_Abstract::transaction_rollback() needs to be overridden when subclassed', E_USER_ERROR);
431 // ###################################################################
433 * Sends the command to commit the entire transaction. This command
434 * should never be reached as it's always overridden
438 function transaction_commit( $name )
440 trigger_error('DB_Abstract::transaction_commit() needs to be overridden when subclassed', E_USER_ERROR);
443 // ###################################################################
445 * Constructs a table of query information output that is used in some
446 * other modules to display a list of queries. This merely formats
447 * a DB->history array entry nicely
451 * @param array An entry from DB->history
453 * @return string A formatted table block
455 function construct_query_debug( $query )
457 $block = " < strong
> Query
:</ strong
>\n\n< div
> " . $this- >registry->entity_encode( $query ['query']) . " </ div
>\n ";
458 $block .= " < tr style
=\" background
- color
: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
459 $block .= "<strong>Time:</strong> $query [time]<br /> \n\t\t <br /> \n\t\t " ;
460 $block .= "<strong>Backtrace:</strong> \n\t\t <div>" . implode ( "<br /> \n " , $query [ 'trace' ]) . "</div> \n\t </td> \n </tr>" ;
462 return $this- > registry
-> message ( 'Query Debug' , $block , 1 , true , false , 0 );
465 // ###################################################################
467 * Error wrapper for ISSO->message()
471 * @param string User defined error message
473 function error ( $message )
475 if ( $this- > showerrors
)
479 $this- > errnum
= call_user_func ( $this- > commands
[ 'error_num' ], $this- > dblink
);
480 $this- > errstr
= call_user_func ( $this- > commands
[ 'error_str' ], $this- > dblink
);
483 $style [ 'code' ] = 'font-family: \' Courier New \' , Courier, mono; font-size: 11px;' ;
485 $message_prepped = "<blockquote> \n <p>" ;
486 $message_prepped .= " \n\t » <strong>Query:</strong> \n <br /> <pre style= \" $style [code] \" >" . htmlspecialchars ( $this- > querystr
) . "</pre> \n <br />" ;
487 $message_prepped .= " \n\t » <strong>Error Number:</strong> <span style= \" $style [code] \" >" . $this- > errnum
. "</span> \n <br />" ;
488 $message_prepped .= " \n\t » <strong>Error Message:</strong> <span style= \" $style [code] \" >" . $this- > errstr
. "</span> \n <br />" ;
489 $message_prepped .= " \n\t » <strong>Additional Notes:</strong> <span style= \" $style [code] \" >" . $message . "</span> \n <br />" ;
490 $message_prepped .= " \n\t » <strong>File:</strong> <span style= \" $style [code] \" >" . $_SERVER [ 'PHP_SELF' ] . "</span> \n " ;
491 $message_prepped .= " \n </p> \n </blockquote>" ;
493 $this- > registry
-> message ( 'Database Error in `<em>' . $this- > registry
-> application
. '</em>`' , $message_prepped , 3 );
499 /*=====================================================================*\
500 || ###################################################################
503 || ###################################################################
504 \*=====================================================================*/