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