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