]>
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 )
215 $this- >querystr = $string ;
216 $this- >result = call_user_func( $this- >commands['query'], $this- >dblink, $string );
217 $this- >history[] = $string ;
219 if (defined('ISSO_SHOW_QUERIES_LIVE'))
221 if (constant('ISSO_SHOW_QUERIES_LIVE'))
223 print( $this- >querystr . '<hr />');
229 $this- >error('Invalid SQL query');
232 return $this- >result;
235 // ###################################################################
237 * Escape a string (depending on character set, if supported)
241 * @param string String to be escaped
243 * @return string Escaped string
245 function escape_string( $string )
247 return call_user_func( $this- >commands['escape_string'], $this- >dblink, $string );
250 // ###################################################################
252 * Fetch the query result as an array
256 * @param integer Result
257 * @param bool Return an associative array?
259 * @return array A row of the query result
261 function fetch_array( $result , $assoc = true)
263 return call_user_func( $this- >commands[ ( $assoc ? 'fetch_assoc' : 'fetch_row') ], $result );
266 // ###################################################################
268 * Fetch the query result as an object
272 * @param integer Result
274 * @return object An object with the query result
276 function fetch_object( $result )
278 return call_user_func( $this- >commands['fetch_object'], $result );
281 // ###################################################################
283 * Send a query and return the first row of the results
287 * @param string Query string
288 * @param string Result return function (in the database layer)
290 * @return mixed Results in variable formats
292 function query_first( $string , $callback = 'fetch_array')
294 $resource = $this- >query( $string );
295 $return = $this- > $callback ( $resource );
296 $this- >free_result( $resource );
300 // ###################################################################
302 * Free the current query result
306 * @param integer Result
308 function free_result( $result )
310 call_user_func( $this- >commands['free_result'], $result );
311 $this- >result = null;
312 $this- >querystr = '';
315 // ###################################################################
317 * Fetch the unique ID of the record just inserted
321 * @return integer Insert-ID
325 return call_user_func( $this- >commands['insert_id'], $this- >dblink);
328 // ###################################################################
330 * Fetch the number of rows in the result
334 * @param integer Result
336 * @return integer Number of rows
338 function num_rows( $result )
340 return call_user_func( $this- >commands['num_rows'], $result );
343 // ###################################################################
345 * Fetch the number of rows affected by the query
349 * @param integer Result
351 * @return integer Number of affected rows
353 function affected_rows( $result )
355 return call_user_func( $this- >commands['affected_rows'], $result );
358 // ###################################################################
360 * Error wrapper for ISSO->message()
364 * @param string User defined error message
366 function error( $message )
368 if ( $this- >showerrors)
372 $this- >errnum = call_user_func( $this- >commands['error_num'], $this- >dblink);
373 $this- >errstr = call_user_func( $this- >commands['error_str'], $this- >dblink);
376 $style ['code'] = 'font-family: \' Courier New \' , Courier, mono; font-size: 11px;';
378 $message_prepped = " < blockquote
>\n< p
> ";
379 $message_prepped .= " \n\t
& raquo
; < strong
> Query
:</ strong
>\n< br
/> < pre style
=\" $style [ code
]\"> " . htmlspecialchars( $this- >querystr) ." </ pre
>\n< br
/> ";
380 $message_prepped .= " \n\t
& raquo
; < strong
> Error Number
:</ strong
> < span style
=\" $style [ code
]\"> " . $this- >errnum . " </ span
>\n< br
/> ";
381 $message_prepped .= " \n\t
& raquo
; < strong
> Error Message
:</ strong
> < span style
=\" $style [ code
]\"> " . $this- >errstr . " </ span
>\n< br
/> ";
382 $message_prepped .= " \n\t
& raquo
; < strong
> Additional Notes
:</ strong
> < span style
=\" $style [ code
]\"> " . $message . " </ span
>\n< br
/> ";
383 $message_prepped .= " \n\t
& raquo
; < strong
> File
:</ strong
> < span style
=\" $style [ code
]\"> " . $_SERVER ['PHP_SELF'] . " </ span
>\n ";
384 $message_prepped .= " \n
</ p
>\n</ blockquote
> ";
386 $this- >registry->message('Database Error in `<em>' . $this- >registry->application . '</em>`', $message_prepped , 3);
392 /*=====================================================================*\
393 || ###################################################################
396 || ###################################################################
397 \*=====================================================================*/