Pass the registry by reference
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]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 * MySQL Database Abstraction Layer
24 * db_mysql.php
25 *
26 * @package ISSO
27 */
28
29 $OBJECT = 'MySQL Database Abstraction Layer';
30 $CLASS = 'MySQL_Database_Driver';
31 $OBJ = 'db';
32
33 /**
34 * MySQL Database Abstraction Layer
35 *
36 * This framework is a function wrapper for MySQL functions so we can have
37 * better error reporting and query reporting.
38 *
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
41 * @version $Revision$
42 * @package ISSO
43 *
44 */
45 class MySQL_Database_Driver
46 {
47 /**
48 * Framework registry object
49 * @var object
50 */
51 var $registry = null;
52
53 /**
54 * Database that is used in the application
55 * @var string
56 */
57 var $database = '';
58
59 /**
60 * Determines whether or not errors should be shown
61 * @var bool
62 */
63 var $errshow = true;
64
65 /**
66 * Current error number
67 * @var integer
68 */
69 var $error_no = 0;
70
71 /**
72 * Description of current error
73 * @var string
74 */
75 var $error_str = '';
76
77 /**
78 * Currend open MySQL connexion
79 * @var resource
80 */
81 var $link_id = null;
82
83 /**
84 * Current query ID
85 * @var integer
86 */
87 var $query_id = null;
88
89 /**
90 * Current query string
91 * @var string
92 */
93 var $query_str = '';
94
95 /**
96 * History of all executed queryies
97 * @var array
98 */
99 var $history = array();
100
101 /**
102 * Constructor
103 */
104 function MySQL_Database_Driver(&$registry)
105 {
106 $this->registry =& $registry;
107 }
108
109 /**
110 * Connect to a the specified database
111 *
112 * @param string Server name
113 * @param string User name
114 * @param string Password
115 * @param bool Use p-connect?
116 *
117 * @return bool Result of connect
118 */
119 function connect($server, $user, $password, $pconnect)
120 {
121 if ($this->link_id == 0)
122 {
123 if ($pconnect)
124 {
125 $this->link_id = @mysql_pconnect($server, $user, $password);
126 }
127 else
128 {
129 $this->link_id = @mysql_connect($server, $user, $password);
130 }
131
132 if ($this->link_id == false)
133 {
134 $this->error('Link-Id == false, cannot connect');
135 return false;
136 }
137
138 if (@mysql_select_db($this->database, $this->link_id))
139 {
140 return true;
141 }
142 else
143 {
144 $this->error('Cannot use the database \'' . $this->database . '\'');
145 return false;
146 }
147 }
148 }
149
150 /**
151 * Send a query to MySQL
152 *
153 * @param string Query string
154 *
155 * @return integer Resource ID for query
156 */
157 function query($query_str)
158 {
159 $this->query_id = @mysql_query($query_str, $this->link_id);
160 $this->query_str = $query_str;
161 $this->history[] = $this->query_str;
162
163 if (defined('ISSO_SHOW_QUERIES_LIVE'))
164 {
165 if (constant('ISSO_SHOW_QUERIES_LIVE'))
166 {
167 print($this->query_str . '<hr />');
168 }
169 }
170
171 if (!$this->query_id)
172 {
173 $this->error('Invalid SQL query');
174 }
175
176 return $this->query_id;
177 }
178
179 /**
180 * Escape a string depending on character set
181 *
182 * @param string String to be escaped
183 *
184 * @return string Escaped string
185 */
186 function escape_string($string)
187 {
188 return @mysql_real_escape_string($string, $this->link_id);
189 }
190
191 /**
192 * Fetch the query result as an array
193 *
194 * @param integer Query ID
195 *
196 * @return array A row of the query result
197 */
198 function fetch_array($query_id)
199 {
200 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
201 }
202
203 /**
204 * Fetch the query result as an object
205 *
206 * @param integer Query ID
207 *
208 * @return object An object with the query result
209 */
210 function fetch_object($query_id)
211 {
212 return @mysql_fetch_object($query_id);
213 }
214
215 /**
216 * Free the current MySQL query result
217 *
218 * @param integer Query ID
219 */
220 function free_result($query_id)
221 {
222 @mysql_free_result($query_id);
223 $this->query_id = 0;
224 $this->query_str = '';
225 }
226
227 /**
228 * Send a MySQL query and return the first row of the results
229 *
230 * @param string Query string
231 * @param string Result return function (in the database layer)
232 *
233 * @return mixed Results in variable formats
234 */
235 function query_first($query_str, $callback = 'fetch_array')
236 {
237 $resource = $this->query($query_str);
238 $return = $this->$callback($resource);
239 $this->free_result($resource);
240 return $return;
241 }
242
243 /**
244 * Fetch the unique ID of the record just inserted
245 *
246 * @return integer MySQL insert ID
247 */
248 function insert_id()
249 {
250 return @mysql_insert_id($this->link_id);
251 }
252
253 /**
254 * Fetch the number of rows in the result
255 *
256 * @param integer Query ID
257 *
258 * @return integer Number of MySQL rows
259 */
260 function num_rows($query_id)
261 {
262 return @mysql_num_rows($query_id);
263 }
264
265 /**
266 * Fetch the number of rows affected by the query
267 *
268 * @param integer Query ID
269 *
270 * @return integer Number of affected rows
271 */
272 function affected_rows($query_id)
273 {
274 return @mysql_affected_rows($this->link_id);
275 }
276
277 /**
278 * MySQL error wrapper for ISSO::_message()
279 *
280 * @param string User defined error message
281 */
282 function error($message)
283 {
284 global $_isso;
285
286 if ($this->errshow)
287 {
288 if ($this->link_id)
289 {
290 $this->error_no = mysql_errno($this->link_id);
291 $this->error_str = mysql_error($this->link_id);
292 }
293
294 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
295
296 $message_prepped = "<blockquote>\n<p>";
297 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
298 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no . "</span>\n<br />";
299 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str . "</span>\n<br />";
300 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
301 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
302 $message_prepped .= "\n</p>\n</blockquote>";
303
304 $_isso->_message('Database Error in `<em>' . $_isso->application . '</em>`', $message_prepped, 3);
305 exit;
306 }
307 }
308 }
309
310 /*=====================================================================*\
311 || ###################################################################
312 || # $HeadURL$
313 || # $Id$
314 || ###################################################################
315 \*=====================================================================*/
316 ?>