]>
src.bluestatic.org Git - bugdar.git/blob - framework/db_mysql_pdo.php
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 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
46 * Framework registry object
53 * Determines whether or not errors should be shown
57 var $showerrors = true;
60 * Current error number
67 * Description of current error
74 * Currend open MySQL connexion
88 * Current query string
95 * History of all executed queryies
99 var $history = array();
101 // ###################################################################
105 function __construct(&$registry)
107 $this->registry
=& $registry;
110 // ###################################################################
112 * Connect to a the specified database
116 * @param string Server name
117 * @param string User name
118 * @param string Password
119 * @param string Database name
120 * @param bool Use p-connect?
122 * @return bool Result of connect
124 function connect($server, $user, $password, $database, $pconnect)
126 define('ISSO_DB_LAYER', 'db_mysql_pdo');
127 if ($this->dblink
== false)
129 $this->dblink
= new PDO("mysql:dbname=$database;host=$server", $user, $password);
130 if ($this->dblink == false)
132 $this->error('DB-Link == false, cannot connect');
139 // ###################################################################
141 * Send a query to the open database link
145 * @param string Query string
147 * @return integer Result
149 function query($string)
153 $this->querystr = $string;
154 $this->result = $this->dblink->query($string);
158 $this->error('Invalid SQL query');
161 $this->history[] = $history = array('query' => $string, 'time' => ($this->registry->is_loaded('functions') ? $this->registry->modules['functions']->fetch_microtime_diff($time) : 0), 'trace' => $this->registry->format_debug_trace(debug_backtrace()));
163 if (defined('ISSO_SHOW_QUERIES_LIVE'))
165 if (constant('ISSO_SHOW_QUERIES_LIVE'))
167 print($this->construct_query_debug($history));
171 return $this->result;
174 // ###################################################################
176 * Escape a string (depending on character set, if supported)
180 * @param string String to be escaped
182 * @return string Escaped string
184 function escape_string($string)
186 $q = $this->dblink->quote($string, PDO::PARAM_STR);
187 // This will put quotes arround the string, which is incompatible
188 // with how queries are written in Bugdar. Remove them here.
189 return substr($q, 1, -1);
192 // ###################################################################
194 * Escapes a binary string for insertion into the database
198 * @param string Unescaped data
200 * @return string Escaped binary data
202 function escape_binary($binary)
204 return call_user_func($this->commands['escape_binary'], $binary);
207 // ###################################################################
209 * Fetch the query result as an array
213 * @param integer Result
214 * @param bool Return an associative array?
216 * @return array A row of the query result
218 function fetch_array($result, $assoc = true)
220 $style = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
221 return $result->fetch($style);
224 // ###################################################################
226 * Fetch the query result as an object
230 * @param integer Result
232 * @return object An object with the query result
234 function fetch_object($result)
236 return $result->fetch(PDO::FETCH_OBJ);
239 // ###################################################################
241 * Send a query and return the first row of the results
245 * @param string Query string
246 * @param string Result return function (in the database layer)
248 * @return mixed Results in variable formats
250 function query_first($string, $callback = 'fetch_array')
252 $resource = $this->query($string);
255 $return = $this->$callback($resource);
256 $this->free_result($resource);
265 // ###################################################################
267 * Free the current query result
271 * @param integer Result
273 function free_result($result)
275 $result->closeCursor();
276 $this->result = null;
277 $this->querystr = '';
280 // ###################################################################
282 * Fetch the unique ID of the record just inserted
286 * @return integer Insert-ID
290 return $this->dblink->lastInsertId();
293 // ###################################################################
295 * Fetch the number of rows in the result
299 * @param integer Result
301 * @return integer Number of rows
303 function num_rows($result)
305 return $result->rowCount();
308 // ###################################################################
310 * Fetch the number of rows affected by the query
314 * @param integer Result
316 * @return integer Number of affected rows
318 function affected_rows($result)
320 return $result->rowCount();
323 // ###################################################################
325 * Constructs a table of query information output that is used in some
326 * other modules to display a list of queries. This merely formats
327 * a DB->history array entry nicely
331 * @param array An entry from DB->history
333 * @return string A formatted table block
335 function construct_query_debug($query)
337 $block = "<strong
>Query
:</strong
>\n\n<div
>" . $this->registry->entity_encode($query['query']) . "</div
>\n";
338 $block .= "<tr style
=\"background
-color
: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
339 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
340 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
342 return $this->registry
->message('Query Debug', $block, 1, true, false, 0);
345 // ###################################################################
347 * Error wrapper for ISSO->message()
351 * @param string User defined error message
353 function error($message)
355 if ($this->showerrors
)
359 $err = $this->dblink
->errorInfo();
360 $this->errnum
= $err[0];
361 $this->errstr
= $err[1] . ': ' . $err[2];
364 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
366 $message_prepped = "<blockquote>\n<p>";
367 $message_prepped .= "\n\t» <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr
) ."</pre>\n<br />";
368 $message_prepped .= "\n\t» <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum
. "</span>\n<br />";
369 $message_prepped .= "\n\t» <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr
. "</span>\n<br />";
370 $message_prepped .= "\n\t» <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
371 $message_prepped .= "\n\t» <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
372 $message_prepped .= "\n</p>\n</blockquote>";
374 $this->registry
->message('Database Error in `<em>' . $this->registry
->application
. '</em>`', $message_prepped, 3);