Documenting another constant
[isso.git] / db.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # app [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Abstract Database Layer
24 * db.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Abstract Database Layer
31 *
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.
35 *
36 * Constants:
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
39 *
40 * @author Iris Studios, Inc.
41 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
42 * @version $Revision$
43 * @package ISSO
44 *
45 */
46 class DB_Abstract
47 {
48 /**
49 * Framework registry object
50 * @var object
51 * @access protected
52 */
53 var $registry = null;
54
55 /**
56 * Determines whether or not errors should be shown
57 * @var bool
58 * @access public
59 */
60 var $showerrors = true;
61
62 /**
63 * Current error number
64 * @var integer
65 * @access protected
66 */
67 var $errnum = 0;
68
69 /**
70 * Description of current error
71 * @var string
72 * @access protected
73 */
74 var $errstr = '';
75
76 /**
77 * Currend open MySQL connexion
78 * @var resource
79 * @access protected
80 */
81 var $dblink = null;
82
83 /**
84 * Current query ID
85 * @var integer
86 * @access protected
87 */
88 var $result = null;
89
90 /**
91 * Current query string
92 * @var string
93 * @access protected
94 */
95 var $querystr = '';
96
97 /**
98 * History of all executed queryies
99 * @var array
100 * @access protected
101 */
102 var $history = array();
103
104 /**
105 * Command mapping list
106 * @var array
107 * @access protected
108 */
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'
123 );
124
125 // ###################################################################
126 /**
127 * Constructor
128 */
129 function __construct(&$registry)
130 {
131 $this->registry =& $registry;
132
133 // because ivars and call_user_func() are conspiring against us...
134 foreach ($this->commands AS $key => $string)
135 {
136 if (strpos($string, '$this->') !== false)
137 {
138 $this->commands["$key"] = array($this, str_replace('$this->', '', $string));
139 }
140 }
141 }
142
143 // ###################################################################
144 /**
145 * (PHP 4) Constructor
146 */
147 function DB_Abstract(&$registry)
148 {
149 $this->__construct($registry);
150 }
151
152 // ###################################################################
153 /**
154 * Initializes the class and all subclasses under a common package name
155 *
156 * @access protected
157 *
158 * @return string The package name
159 */
160 function init_as_package()
161 {
162 if (!defined('ISSO_DB_LAYER'))
163 {
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);
166 }
167
168 return 'db';
169 }
170
171 // ###################################################################
172 /**
173 * Connect to a the specified database
174 *
175 * @access public
176 *
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?
182 *
183 * @return bool Result of connect
184 */
185 function connect($server, $user, $password, $database, $pconnect)
186 {
187 $this->registry->check_isso_fields(get_class($this));
188
189 if ($this->dblink == false)
190 {
191 $this->dblink = call_user_func(($pconnect ? $this->commands['pconnect'] : $this->commands['connect']), $server, $user, $password, $database);
192
193 if ($this->dblink == false)
194 {
195 $this->error('DB-Link == false, cannot connect');
196 return false;
197 }
198
199 return true;
200 }
201 }
202
203 // ###################################################################
204 /**
205 * Send a query to the open database link
206 *
207 * @access public
208 *
209 * @param string Query string
210 *
211 * @return integer Result
212 */
213 function query($string)
214 {
215 $this->querystr = $string;
216 $this->result = call_user_func($this->commands['query'], $this->dblink, $string);
217 $this->history[] = $string;
218
219 if (defined('ISSO_SHOW_QUERIES_LIVE'))
220 {
221 if (constant('ISSO_SHOW_QUERIES_LIVE'))
222 {
223 print($this->querystr . '<hr />');
224 }
225 }
226
227 if (!$this->result)
228 {
229 $this->error('Invalid SQL query');
230 }
231
232 return $this->result;
233 }
234
235 // ###################################################################
236 /**
237 * Escape a string (depending on character set, if supported)
238 *
239 * @access public
240 *
241 * @param string String to be escaped
242 *
243 * @return string Escaped string
244 */
245 function escape_string($string)
246 {
247 return call_user_func($this->commands['escape_string'], $this->dblink, $string);
248 }
249
250 // ###################################################################
251 /**
252 * Fetch the query result as an array
253 *
254 * @access public
255 *
256 * @param integer Result
257 * @param bool Return an associative array?
258 *
259 * @return array A row of the query result
260 */
261 function fetch_array($result, $assoc = true)
262 {
263 return call_user_func($this->commands[ ($assoc ? 'fetch_assoc' : 'fetch_row') ], $result);
264 }
265
266 // ###################################################################
267 /**
268 * Fetch the query result as an object
269 *
270 * @access public
271 *
272 * @param integer Result
273 *
274 * @return object An object with the query result
275 */
276 function fetch_object($result)
277 {
278 return call_user_func($this->commands['fetch_object'], $result);
279 }
280
281 // ###################################################################
282 /**
283 * Send a query and return the first row of the results
284 *
285 * @access public
286 *
287 * @param string Query string
288 * @param string Result return function (in the database layer)
289 *
290 * @return mixed Results in variable formats
291 */
292 function query_first($string, $callback = 'fetch_array')
293 {
294 $resource = $this->query($string);
295 $return = $this->$callback($resource);
296 $this->free_result($resource);
297 return $return;
298 }
299
300 // ###################################################################
301 /**
302 * Free the current query result
303 *
304 * @access public
305 *
306 * @param integer Result
307 */
308 function free_result($result)
309 {
310 call_user_func($this->commands['free_result'], $result);
311 $this->result = null;
312 $this->querystr = '';
313 }
314
315 // ###################################################################
316 /**
317 * Fetch the unique ID of the record just inserted
318 *
319 * @access public
320 *
321 * @return integer Insert-ID
322 */
323 function insert_id()
324 {
325 return call_user_func($this->commands['insert_id'], $this->dblink);
326 }
327
328 // ###################################################################
329 /**
330 * Fetch the number of rows in the result
331 *
332 * @access public
333 *
334 * @param integer Result
335 *
336 * @return integer Number of rows
337 */
338 function num_rows($result)
339 {
340 return call_user_func($this->commands['num_rows'], $result);
341 }
342
343 // ###################################################################
344 /**
345 * Fetch the number of rows affected by the query
346 *
347 * @access public
348 *
349 * @param integer Result
350 *
351 * @return integer Number of affected rows
352 */
353 function affected_rows($result)
354 {
355 return call_user_func($this->commands['affected_rows'], $result);
356 }
357
358 // ###################################################################
359 /**
360 * Error wrapper for ISSO->message()
361 *
362 * @access protected
363 *
364 * @param string User defined error message
365 */
366 function error($message)
367 {
368 if ($this->showerrors)
369 {
370 if ($this->dblink)
371 {
372 $this->errnum = call_user_func($this->commands['error_num'], $this->dblink);
373 $this->errstr = call_user_func($this->commands['error_str'], $this->dblink);
374 }
375
376 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
377
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>";
385
386 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
387 exit;
388 }
389 }
390 }
391
392 /*=====================================================================*\
393 || ###################################################################
394 || # $HeadURL$
395 || # $Id$
396 || ###################################################################
397 \*=====================================================================*/
398 ?>