Remove the database abstraction layer and write a new PDO backend.
[bugdar.git] / framework / db_mysql_pdo.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-[#]year[#] Blue Static
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 * Abstract Database Layer
24 * db.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * PDO MySQL Driver
31 *
32 * This class wraps PDO-MySQL in the old DB_Abstract ISSO API.
33 *
34 * Constants:
35 * ISSO_SHOW_QUERIES_LIVE - Show queries in page output as they are sent
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class DB_MySQL_PDO
44 {
45 /**
46 * Framework registry object
47 * @var object
48 * @access protected
49 */
50 var $registry = null;
51
52 /**
53 * Determines whether or not errors should be shown
54 * @var bool
55 * @access public
56 */
57 var $showerrors = true;
58
59 /**
60 * Current error number
61 * @var integer
62 * @access protected
63 */
64 var $errnum = 0;
65
66 /**
67 * Description of current error
68 * @var string
69 * @access protected
70 */
71 var $errstr = '';
72
73 /**
74 * Currend open MySQL connexion
75 * @var resource
76 * @access protected
77 */
78 var $dblink = null;
79
80 /**
81 * Current query ID
82 * @var integer
83 * @access protected
84 */
85 var $result = null;
86
87 /**
88 * Current query string
89 * @var string
90 * @access protected
91 */
92 var $querystr = '';
93
94 /**
95 * History of all executed queryies
96 * @var array
97 * @access protected
98 */
99 var $history = array();
100
101 // ###################################################################
102 /**
103 * Constructor
104 */
105 function __construct(&$registry)
106 {
107 $this->registry =& $registry;
108 }
109
110 // ###################################################################
111 /**
112 * Connect to a the specified database
113 *
114 * @access public
115 *
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?
121 *
122 * @return bool Result of connect
123 */
124 function connect($server, $user, $password, $database, $pconnect)
125 {
126 define('ISSO_DB_LAYER', 'db_mysql_pdo');
127 if ($this->dblink == false)
128 {
129 $this->dblink = new PDO("mysql:dbname=$database;host=$server", $user, $password);
130 if ($this->dblink == false)
131 {
132 $this->error('DB-Link == false, cannot connect');
133 return false;
134 }
135 return true;
136 }
137 }
138
139 // ###################################################################
140 /**
141 * Send a query to the open database link
142 *
143 * @access public
144 *
145 * @param string Query string
146 *
147 * @return integer Result
148 */
149 function query($string)
150 {
151 $time = microtime();
152
153 $this->querystr = $string;
154 $this->result = $this->dblink->query($string);
155
156 if (!$this->result)
157 {
158 $this->error('Invalid SQL query');
159 }
160
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()));
162
163 if (defined('ISSO_SHOW_QUERIES_LIVE'))
164 {
165 if (constant('ISSO_SHOW_QUERIES_LIVE'))
166 {
167 print($this->construct_query_debug($history));
168 }
169 }
170
171 return $this->result;
172 }
173
174 // ###################################################################
175 /**
176 * Escape a string (depending on character set, if supported)
177 *
178 * @access public
179 *
180 * @param string String to be escaped
181 *
182 * @return string Escaped string
183 */
184 function escape_string($string)
185 {
186 return $this->dblink->quote($string);
187 }
188
189 // ###################################################################
190 /**
191 * Escapes a binary string for insertion into the database
192 *
193 * @access public
194 *
195 * @param string Unescaped data
196 *
197 * @return string Escaped binary data
198 */
199 function escape_binary($binary)
200 {
201 return call_user_func($this->commands['escape_binary'], $binary);
202 }
203
204 // ###################################################################
205 /**
206 * Fetch the query result as an array
207 *
208 * @access public
209 *
210 * @param integer Result
211 * @param bool Return an associative array?
212 *
213 * @return array A row of the query result
214 */
215 function fetch_array($result, $assoc = true)
216 {
217 $style = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
218 return $result->fetch($style);
219 }
220
221 // ###################################################################
222 /**
223 * Fetch the query result as an object
224 *
225 * @access public
226 *
227 * @param integer Result
228 *
229 * @return object An object with the query result
230 */
231 function fetch_object($result)
232 {
233 return $result->fetch(PDO::FETCH_OBJ);
234 }
235
236 // ###################################################################
237 /**
238 * Send a query and return the first row of the results
239 *
240 * @access public
241 *
242 * @param string Query string
243 * @param string Result return function (in the database layer)
244 *
245 * @return mixed Results in variable formats
246 */
247 function query_first($string, $callback = 'fetch_array')
248 {
249 $resource = $this->query($string);
250 if ($resource)
251 {
252 $return = $this->$callback($resource);
253 $this->free_result($resource);
254 return $return;
255 }
256 else
257 {
258 return false;
259 }
260 }
261
262 // ###################################################################
263 /**
264 * Free the current query result
265 *
266 * @access public
267 *
268 * @param integer Result
269 */
270 function free_result($result)
271 {
272 $result->closeCursor();
273 $this->result = null;
274 $this->querystr = '';
275 }
276
277 // ###################################################################
278 /**
279 * Fetch the unique ID of the record just inserted
280 *
281 * @access public
282 *
283 * @return integer Insert-ID
284 */
285 function insert_id()
286 {
287 return $this->dblink->lastInsertId();
288 }
289
290 // ###################################################################
291 /**
292 * Fetch the number of rows in the result
293 *
294 * @access public
295 *
296 * @param integer Result
297 *
298 * @return integer Number of rows
299 */
300 function num_rows($result)
301 {
302 return $result->rowCount();
303 }
304
305 // ###################################################################
306 /**
307 * Fetch the number of rows affected by the query
308 *
309 * @access public
310 *
311 * @param integer Result
312 *
313 * @return integer Number of affected rows
314 */
315 function affected_rows($result)
316 {
317 return $result->rowCount();
318 }
319
320 // ###################################################################
321 /**
322 * Constructs a table of query information output that is used in some
323 * other modules to display a list of queries. This merely formats
324 * a DB->history array entry nicely
325 *
326 * @access public
327 *
328 * @param array An entry from DB->history
329 *
330 * @return string A formatted table block
331 */
332 function construct_query_debug($query)
333 {
334 $block = "<strong>Query:</strong>\n\n<div>" . $this->registry->entity_encode($query['query']) . "</div>\n";
335 $block .= "<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>\n\t\t";
336 $block .= "<strong>Time:</strong> $query[time]<br />\n\t\t<br />\n\t\t";
337 $block .= "<strong>Backtrace:</strong>\n\t\t<div>" . implode("<br />\n", $query['trace']) . "</div>\n\t</td>\n</tr>";
338
339 return $this->registry->message('Query Debug', $block, 1, true, false, 0);
340 }
341
342 // ###################################################################
343 /**
344 * Error wrapper for ISSO->message()
345 *
346 * @access protected
347 *
348 * @param string User defined error message
349 */
350 function error($message)
351 {
352 if ($this->showerrors)
353 {
354 if ($this->dblink)
355 {
356 $err = $this->dblink->errorInfo();
357 $this->errnum = $err[0];
358 $this->errstr = $err[1] . ': ' . $err[2];
359 }
360
361 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
362
363 $message_prepped = "<blockquote>\n<p>";
364 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr) ."</pre>\n<br />";
365 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum . "</span>\n<br />";
366 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
367 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
368 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
369 $message_prepped .= "\n</p>\n</blockquote>";
370
371 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
372 exit;
373 }
374 }
375 }
376