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