Finished database abstraction layer.
[isso.git] / db_mysql.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 $OBJECT = 'MySQL Database Abstraction Layer';
14 $CLASS = 'MySQL_Database_Driver';
15 $OBJ = 'db';
16
17 /**
18 * MySQL Database Abstraction Layer
19 *
20 * This framework is a function wrapper for MySQL functions so we can have
21 * better error reporting and query reporting.
22 *
23 * @author Iris Studios, Inc.
24 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
25 * @version $Revision$
26 *
27 */
28 class MySQL_Database_Driver
29 {
30 /**
31 * Global environment variables
32 *
33 * @var database The name of the MySQL database used
34 * @var errshow Report errors durring execution?
35 * @var errno Current error number
36 * @var errstr Description of the current error
37 * @var link_id Link-ID for current connexion
38 * @var query_id Current query ID
39 * @var query_str Current query string
40 * @var history A list of all query reporting information
41 */
42 var $database = '';
43 var $errshow = true;
44 var $errno = 0;
45 var $errstr = '';
46 var $link_id = 0;
47 var $query_id = 0;
48 var $query_str = '';
49 var $history = array();
50
51 /**
52 * Connect to a the specified database
53 *
54 * @param str Server name
55 * @param str User name
56 * @param str Password
57 * @param bool Use p-connect?
58 *
59 * @return bool Result of connect
60 */
61 function connect($server, $user, $password, $pconnect)
62 {
63 if ($this->link_id == 0)
64 {
65 if ($pconnect)
66 {
67 $this->link_id = @mysql_pconnect($server, $user, $password);
68 }
69 else
70 {
71 $this->link_id = @mysql_connect($server, $user, $password);
72 }
73
74 if ($this->link_id == false)
75 {
76 $this->error('Link-Id == false, cannot connect');
77 return false;
78 }
79
80 if (@mysql_select_db($this->database, $this->link_id))
81 {
82 return true;
83 }
84 else
85 {
86 $this->error('Cannot use the database \'' . $this->database . '\'');
87 return false;
88 }
89 }
90 }
91
92 /**
93 * Send a query to MySQL
94 *
95 * @param str Query string
96 *
97 * @return res Resource ID for query
98 */
99 function query($query_str)
100 {
101 $this->query_id = @mysql_query($query_str, $this->link_id);
102 $this->query_str = $query_str;
103 $this->history[] = $this->query_str;
104
105 if (!$this->query_id)
106 {
107 $this->error('Invalid SQL query');
108 }
109
110 return $this->query_id;
111 }
112
113 /**
114 * Escape a string depending on character set
115 *
116 * @param str String to be escaped
117 *
118 * @return str Escaped string
119 */
120 function escape_string($string)
121 {
122 return @mysql_real_escape_string($string, $this->link_id);
123 }
124
125 /**
126 * Fetch the query result as an array
127 *
128 * @param res Query-ID
129 *
130 * @return array A row of the query result
131 */
132 function fetch_array($query_id)
133 {
134 return @mysql_fetch_array($query_id, MYSQL_ASSOC);
135 }
136
137 /**
138 * Fetch the query result as an object
139 *
140 * @param res Query-ID
141 *
142 * @return obj An object with the query result
143 */
144 function fetch_object($query_id)
145 {
146 return @mysql_fetch_object($query_id);
147 }
148
149 /**
150 * Free the current MySQL query result
151 *
152 * @param res Query-ID
153 */
154 function free_result($query_id)
155 {
156 @mysql_free_result($query_id);
157 $this->query_id = 0;
158 $this->query_str = '';
159 }
160
161 /**
162 * Send a MySQL query and return the first row of the results
163 *
164 * @param str Query string
165 * @param str Result return function (in the database layer)
166 *
167 * @return mixed Results in variable formats
168 */
169 function query_first($query_str, $callback = 'fetch_array')
170 {
171 $resource = $this->query($query_str);
172 $return = $this->$callback($resource);
173 $this->free_result($resource);
174 return $return;
175 }
176
177 /**
178 * Fetch the unique ID of the record just inserted
179 *
180 * @return int MySQL insert ID
181 */
182 function insert_id()
183 {
184 return @mysql_insert_id($this->link_id);
185 }
186
187 /**
188 * Fetch the number of rows in the result
189 *
190 * @param res Query-ID
191 *
192 * @return int Number of MySQL rows
193 */
194 function num_rows($query_id)
195 {
196 return @mysql_num_rows($query_id);
197 }
198
199 /**
200 * Fetch the number of rows affected by the query
201 *
202 * @return int Number of affected rows
203 */
204 function affected_rows($query_id)
205 {
206 return @mysql_affected_rows($this->link_id);
207 }
208
209 /**
210 * MySQL error wrapper for ISSO::_message()
211 *
212 * @param str User-defined error message
213 */
214 function error($message)
215 {
216 global $_isso;
217
218 if ($this->errshow)
219 {
220 if ($this->link_id)
221 {
222 $this->errno = mysql_errno($this->link_id);
223 $this->errstr = mysql_error($this->link_id);
224 }
225
226 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
227
228 $message_prepped = "<blockquote>\n<p>";
229 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str ."</pre>\n<br />";
230 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errno . "</span>\n<br />";
231 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
232 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
233 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
234 $message_prepped .= "\n</p>\n</blockquote>";
235
236 $_isso->_message('Database Error in `<em>' . $_isso->application . '</em>`', $message_prepped, 3);
237 exit;
238 }
239 }
240 }
241
242 /*=====================================================================*\
243 || ###################################################################
244 || # $HeadURL$
245 || # $Id$
246 || ###################################################################
247 \*=====================================================================*/
248 ?>