2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * MySQL Database Abstraction Layer
29 $OBJECT = 'MySQL Database Abstraction Layer';
30 $CLASS = 'MySQL_Database_Driver';
34 * MySQL Database Abstraction Layer
36 * This framework is a function wrapper for MySQL functions so we can have
37 * better error reporting and query reporting.
39 * @author Iris Studios, Inc.
40 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
45 class MySQL_Database_Driver
48 * Database that is used in the application
55 * Determines whether or not errors should be shown
62 * Current error number
69 * Description of current error
76 * Currend open MySQL connexion
90 * Current query string
97 * History of all executed queryies
100 var $history = array();
103 * Connect to a the specified database
105 * @param str Server name
106 * @param str User name
107 * @param str Password
108 * @param bool Use p-connect?
110 * @return bool Result of connect
112 function connect($server, $user, $password, $pconnect)
114 if ($this->link_id
== 0)
118 $this->link_id
= @mysql_pconnect($server, $user, $password);
122 $this->link_id
= @mysql_connect($server, $user, $password);
125 if ($this->link_id
== false
)
127 $this->error('Link-Id == false, cannot connect');
131 if (@mysql_select_db($this->database
, $this->link_id
))
137 $this->error('Cannot use the database \'' . $this->database
. '\'');
144 * Send a query to MySQL
146 * @param str Query string
148 * @return res Resource ID for query
150 function query($query_str)
152 $this->query_id
= @mysql_query($query_str, $this->link_id
);
153 $this->query_str
= $query_str;
154 $this->history
[] = $this->query_str
;
156 if (defined('ISSO_SHOW_QUERIES_LIVE'))
158 if (constant('ISSO_SHOW_QUERIES_LIVE'))
160 print($this->query_str
. '<hr />');
164 if (!$this->query_id
)
166 $this->error('Invalid SQL query');
169 return $this->query_id
;
173 * Escape a string depending on character set
175 * @param str String to be escaped
177 * @return str Escaped string
179 function escape_string($string)
181 return @mysql_real_escape_string($string, $this->link_id
);
185 * Fetch the query result as an array
187 * @param res Query ID
189 * @return array A row of the query result
191 function fetch_array($query_id)
193 return @mysql_fetch_array($query_id, MYSQL_ASSOC
);
197 * Fetch the query result as an object
199 * @param res Query ID
201 * @return obj An object with the query result
203 function fetch_object($query_id)
205 return @mysql_fetch_object($query_id);
209 * Free the current MySQL query result
211 * @param res Query ID
213 function free_result($query_id)
215 @mysql_free_result($query_id);
217 $this->query_str
= '';
221 * Send a MySQL query and return the first row of the results
223 * @param str Query string
224 * @param str Result return function (in the database layer)
226 * @return mixed Results in variable formats
228 function query_first($query_str, $callback = 'fetch_array')
230 $resource = $this->query($query_str);
231 $return = $this->$callback($resource);
232 $this->free_result($resource);
237 * Fetch the unique ID of the record just inserted
239 * @return int MySQL insert ID
243 return @mysql_insert_id($this->link_id
);
247 * Fetch the number of rows in the result
249 * @param res Query ID
251 * @return int Number of MySQL rows
253 function num_rows($query_id)
255 return @mysql_num_rows($query_id);
259 * Fetch the number of rows affected by the query
261 * @param res Query ID
263 * @return int Number of affected rows
265 function affected_rows($query_id)
267 return @mysql_affected_rows($this->link_id
);
271 * MySQL error wrapper for ISSO::_message()
273 * @param str User defined error message
275 function error($message)
283 $this->error_no
= mysql_errno($this->link_id
);
284 $this->error_str
= mysql_error($this->link_id
);
287 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
289 $message_prepped = "<blockquote>\n<p>";
290 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . $this->query_str
."</pre>\n<br />";
291 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->error_no
. "</span>\n<br />";
292 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->error_str
. "</span>\n<br />";
293 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
294 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
295 $message_prepped .= "\n</p>\n</blockquote>";
297 $_isso->_message('Database Error in `<em>' . $_isso->application
. '</em>`', $message_prepped, 3);
303 /*=====================================================================*\
304 || ###################################################################
307 || ###################################################################
308 \*=====================================================================*/