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