2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-[#]year[#] Blue Static
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 * Abstract Database Layer
32 * This class wraps PDO-MySQL in the old DB_Abstract ISSO API.
35 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
43 * Framework registry object
50 * Determines whether or not errors should be shown
54 var $showerrors = true
;
57 * Current error number
64 * Description of current error
71 * Currend open MySQL connexion
85 * Current query string
91 // ###################################################################
95 function __construct(&$registry)
97 $this->registry
=& $registry;
100 // ###################################################################
102 * Connect to a the specified database
106 * @param PDO The database link.
108 * @return bool Result of connect
110 function connect($dblink)
112 define('ISSO_DB_LAYER', 'db_mysql_pdo');
113 if ($this->dblink
== false
)
115 $this->dblink
= $dblink;
116 if ($this->dblink
== false
)
118 $this->error('DB-Link == false, cannot connect');
125 // ###################################################################
127 * Send a query to the open database link
131 * @param string Query string
133 * @return integer Result
135 function query($string)
139 $this->querystr
= $string;
140 $this->result
= $this->dblink
->query($string);
144 $this->error('Invalid SQL query');
147 return $this->result
;
150 // ###################################################################
152 * Escape a string (depending on character set, if supported)
156 * @param string String to be escaped
158 * @return string Escaped string
160 function escape_string($string)
162 $q = $this->dblink
->quote($string, PDO
::PARAM_STR
);
163 // This will put quotes arround the string, which is incompatible
164 // with how queries are written in Bugdar. Remove them here.
165 return substr($q, 1, -1);
168 // ###################################################################
170 * Escapes a binary string for insertion into the database
174 * @param string Unescaped data
176 * @return string Escaped binary data
178 function escape_binary($binary)
180 return call_user_func($this->commands
['escape_binary'], $binary);
183 // ###################################################################
185 * Fetch the query result as an array
189 * @param integer Result
190 * @param bool Return an associative array?
192 * @return array A row of the query result
194 function fetch_array($result, $assoc = true
)
196 $style = $assoc ? PDO
::FETCH_ASSOC
: PDO
::FETCH_NUM
;
197 return $result->fetch($style);
200 // ###################################################################
202 * Fetch the query result as an object
206 * @param integer Result
208 * @return object An object with the query result
210 function fetch_object($result)
212 return $result->fetch(PDO
::FETCH_OBJ
);
215 // ###################################################################
217 * Send a query and return the first row of the results
221 * @param string Query string
222 * @param string Result return function (in the database layer)
224 * @return mixed Results in variable formats
226 function query_first($string, $callback = 'fetch_array')
228 $resource = $this->query($string);
231 $return = $this->$callback($resource);
232 $this->free_result($resource);
241 // ###################################################################
243 * Free the current query result
247 * @param integer Result
249 function free_result($result)
251 $result->closeCursor();
252 $this->result
= null
;
253 $this->querystr
= '';
256 // ###################################################################
258 * Fetch the unique ID of the record just inserted
262 * @return integer Insert-ID
266 return $this->dblink
->lastInsertId();
269 // ###################################################################
271 * Fetch the number of rows in the result
275 * @param integer Result
277 * @return integer Number of rows
279 function num_rows($result)
281 return $result->rowCount();
284 // ###################################################################
286 * Fetch the number of rows affected by the query
290 * @param integer Result
292 * @return integer Number of affected rows
294 function affected_rows($result)
296 return $result->rowCount();
299 // ###################################################################
301 * Error wrapper for ISSO->message()
305 * @param string User defined error message
307 function error($message)
309 if ($this->showerrors
)
313 $err = $this->dblink
->errorInfo();
314 $this->errnum
= $err[0];
315 $this->errstr
= $err[1] . ': ' . $err[2];
318 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
320 $message_prepped = "<blockquote>\n<p>";
321 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
322 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
323 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
324 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
325 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
326 $message_prepped .= "\n</p>\n</blockquote>";
328 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);