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