Profile the database using hoplite\data\ProfilingPDO instead of DB_MySQL_PDO.
[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 * @author Blue Static
35 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
36 * @version $Revision$
37 * @package ISSO
38 *
39 */
40 class DB_MySQL_PDO
41 {
42 /**
43 * Framework registry object
44 * @var object
45 * @access protected
46 */
47 var $registry = null;
48
49 /**
50 * Determines whether or not errors should be shown
51 * @var bool
52 * @access public
53 */
54 var $showerrors = true;
55
56 /**
57 * Current error number
58 * @var integer
59 * @access protected
60 */
61 var $errnum = 0;
62
63 /**
64 * Description of current error
65 * @var string
66 * @access protected
67 */
68 var $errstr = '';
69
70 /**
71 * Currend open MySQL connexion
72 * @var resource
73 * @access protected
74 */
75 var $dblink = null;
76
77 /**
78 * Current query ID
79 * @var integer
80 * @access protected
81 */
82 var $result = null;
83
84 /**
85 * Current query string
86 * @var string
87 * @access protected
88 */
89 var $querystr = '';
90
91 // ###################################################################
92 /**
93 * Constructor
94 */
95 function __construct(&$registry)
96 {
97 $this->registry =& $registry;
98 }
99
100 // ###################################################################
101 /**
102 * Connect to a the specified database
103 *
104 * @access public
105 *
106 * @param PDO The database link.
107 *
108 * @return bool Result of connect
109 */
110 function connect($dblink)
111 {
112 define('ISSO_DB_LAYER', 'db_mysql_pdo');
113 if ($this->dblink == false)
114 {
115 $this->dblink = $dblink;
116 if ($this->dblink == false)
117 {
118 $this->error('DB-Link == false, cannot connect');
119 return false;
120 }
121 return true;
122 }
123 }
124
125 // ###################################################################
126 /**
127 * Send a query to the open database link
128 *
129 * @access public
130 *
131 * @param string Query string
132 *
133 * @return integer Result
134 */
135 function query($string)
136 {
137 $time = microtime();
138
139 $this->querystr = $string;
140 $this->result = $this->dblink->query($string);
141
142 if (!$this->result)
143 {
144 $this->error('Invalid SQL query');
145 }
146
147 return $this->result;
148 }
149
150 // ###################################################################
151 /**
152 * Escape a string (depending on character set, if supported)
153 *
154 * @access public
155 *
156 * @param string String to be escaped
157 *
158 * @return string Escaped string
159 */
160 function escape_string($string)
161 {
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);
166 }
167
168 // ###################################################################
169 /**
170 * Escapes a binary string for insertion into the database
171 *
172 * @access public
173 *
174 * @param string Unescaped data
175 *
176 * @return string Escaped binary data
177 */
178 function escape_binary($binary)
179 {
180 return call_user_func($this->commands['escape_binary'], $binary);
181 }
182
183 // ###################################################################
184 /**
185 * Fetch the query result as an array
186 *
187 * @access public
188 *
189 * @param integer Result
190 * @param bool Return an associative array?
191 *
192 * @return array A row of the query result
193 */
194 function fetch_array($result, $assoc = true)
195 {
196 $style = $assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM;
197 return $result->fetch($style);
198 }
199
200 // ###################################################################
201 /**
202 * Fetch the query result as an object
203 *
204 * @access public
205 *
206 * @param integer Result
207 *
208 * @return object An object with the query result
209 */
210 function fetch_object($result)
211 {
212 return $result->fetch(PDO::FETCH_OBJ);
213 }
214
215 // ###################################################################
216 /**
217 * Send a query and return the first row of the results
218 *
219 * @access public
220 *
221 * @param string Query string
222 * @param string Result return function (in the database layer)
223 *
224 * @return mixed Results in variable formats
225 */
226 function query_first($string, $callback = 'fetch_array')
227 {
228 $resource = $this->query($string);
229 if ($resource)
230 {
231 $return = $this->$callback($resource);
232 $this->free_result($resource);
233 return $return;
234 }
235 else
236 {
237 return false;
238 }
239 }
240
241 // ###################################################################
242 /**
243 * Free the current query result
244 *
245 * @access public
246 *
247 * @param integer Result
248 */
249 function free_result($result)
250 {
251 $result->closeCursor();
252 $this->result = null;
253 $this->querystr = '';
254 }
255
256 // ###################################################################
257 /**
258 * Fetch the unique ID of the record just inserted
259 *
260 * @access public
261 *
262 * @return integer Insert-ID
263 */
264 function insert_id()
265 {
266 return $this->dblink->lastInsertId();
267 }
268
269 // ###################################################################
270 /**
271 * Fetch the number of rows in the result
272 *
273 * @access public
274 *
275 * @param integer Result
276 *
277 * @return integer Number of rows
278 */
279 function num_rows($result)
280 {
281 return $result->rowCount();
282 }
283
284 // ###################################################################
285 /**
286 * Fetch the number of rows affected by the query
287 *
288 * @access public
289 *
290 * @param integer Result
291 *
292 * @return integer Number of affected rows
293 */
294 function affected_rows($result)
295 {
296 return $result->rowCount();
297 }
298
299 // ###################################################################
300 /**
301 * Error wrapper for ISSO->message()
302 *
303 * @access protected
304 *
305 * @param string User defined error message
306 */
307 function error($message)
308 {
309 if ($this->showerrors)
310 {
311 if ($this->dblink)
312 {
313 $err = $this->dblink->errorInfo();
314 $this->errnum = $err[0];
315 $this->errstr = $err[1] . ': ' . $err[2];
316 }
317
318 $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;';
319
320 $message_prepped = "<blockquote>\n<p>";
321 $message_prepped .= "\n\t&raquo; <strong>Query:</strong>\n<br /> <pre style=\"$style[code]\">" . htmlspecialchars($this->querystr) ."</pre>\n<br />";
322 $message_prepped .= "\n\t&raquo; <strong>Error Number:</strong> <span style=\"$style[code]\">" . $this->errnum . "</span>\n<br />";
323 $message_prepped .= "\n\t&raquo; <strong>Error Message:</strong> <span style=\"$style[code]\">" . $this->errstr . "</span>\n<br />";
324 $message_prepped .= "\n\t&raquo; <strong>Additional Notes:</strong> <span style=\"$style[code]\">" . $message . "</span>\n<br />";
325 $message_prepped .= "\n\t&raquo; <strong>File:</strong> <span style=\"$style[code]\">" . $_SERVER['PHP_SELF'] . "</span>\n";
326 $message_prepped .= "\n</p>\n</blockquote>";
327
328 $this->registry->message('Database Error in `<em>' . $this->registry->application . '</em>`', $message_prepped, 3);
329 exit;
330 }
331 }
332 }
333