commit | author | age
|
d1403f
|
1 |
<?php |
A |
2 |
// vim: set et ts=4 sw=4 fdm=marker: |
|
3 |
// +----------------------------------------------------------------------+ |
|
4 |
// | PHP versions 4 and 5 | |
|
5 |
// +----------------------------------------------------------------------+ |
|
6 |
// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, | |
|
7 |
// | Stig. S. Bakken, Lukas Smith | |
|
8 |
// | All rights reserved. | |
|
9 |
// +----------------------------------------------------------------------+ |
|
10 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
11 |
// | API as well as database abstraction for PHP applications. | |
|
12 |
// | This LICENSE is in the BSD license style. | |
|
13 |
// | | |
|
14 |
// | Redistribution and use in source and binary forms, with or without | |
|
15 |
// | modification, are permitted provided that the following conditions | |
|
16 |
// | are met: | |
|
17 |
// | | |
|
18 |
// | Redistributions of source code must retain the above copyright | |
|
19 |
// | notice, this list of conditions and the following disclaimer. | |
|
20 |
// | | |
|
21 |
// | Redistributions in binary form must reproduce the above copyright | |
|
22 |
// | notice, this list of conditions and the following disclaimer in the | |
|
23 |
// | documentation and/or other materials provided with the distribution. | |
|
24 |
// | | |
|
25 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
26 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
27 |
// | or promote products derived from this software without specific prior| |
|
28 |
// | written permission. | |
|
29 |
// | | |
|
30 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
31 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
32 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
33 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
34 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
35 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
36 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
37 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
38 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
39 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
40 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
41 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
42 |
// +----------------------------------------------------------------------+ |
|
43 |
// | Author: Lukas Smith <smith@pooteeweet.org> | |
|
44 |
// +----------------------------------------------------------------------+ |
|
45 |
// |
|
46 |
// $Id: mysql.php,v 1.208 2008/03/13 03:31:55 afz Exp $ |
|
47 |
// |
|
48 |
|
|
49 |
/** |
|
50 |
* MDB2 MySQL driver |
|
51 |
* |
|
52 |
* @package MDB2 |
|
53 |
* @category Database |
|
54 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
55 |
*/ |
|
56 |
class MDB2_Driver_mysql extends MDB2_Driver_Common |
|
57 |
{ |
|
58 |
// {{{ properties |
|
59 |
|
|
60 |
var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\'); |
|
61 |
|
|
62 |
var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`'); |
|
63 |
|
|
64 |
var $sql_comments = array( |
|
65 |
array('start' => '-- ', 'end' => "\n", 'escape' => false), |
|
66 |
array('start' => '#', 'end' => "\n", 'escape' => false), |
|
67 |
array('start' => '/*', 'end' => '*/', 'escape' => false), |
|
68 |
); |
|
69 |
|
|
70 |
var $server_capabilities_checked = false; |
|
71 |
|
|
72 |
var $start_transaction = false; |
|
73 |
|
|
74 |
var $varchar_max_length = 255; |
|
75 |
|
|
76 |
// }}} |
|
77 |
// {{{ constructor |
|
78 |
|
|
79 |
/** |
|
80 |
* Constructor |
|
81 |
*/ |
|
82 |
function __construct() |
|
83 |
{ |
|
84 |
parent::__construct(); |
|
85 |
|
|
86 |
$this->phptype = 'mysql'; |
|
87 |
$this->dbsyntax = 'mysql'; |
|
88 |
|
|
89 |
$this->supported['sequences'] = 'emulated'; |
|
90 |
$this->supported['indexes'] = true; |
|
91 |
$this->supported['affected_rows'] = true; |
|
92 |
$this->supported['transactions'] = false; |
|
93 |
$this->supported['savepoints'] = false; |
|
94 |
$this->supported['summary_functions'] = true; |
|
95 |
$this->supported['order_by_text'] = true; |
|
96 |
$this->supported['current_id'] = 'emulated'; |
|
97 |
$this->supported['limit_queries'] = true; |
|
98 |
$this->supported['LOBs'] = true; |
|
99 |
$this->supported['replace'] = true; |
|
100 |
$this->supported['sub_selects'] = 'emulated'; |
|
101 |
$this->supported['triggers'] = false; |
|
102 |
$this->supported['auto_increment'] = true; |
|
103 |
$this->supported['primary_key'] = true; |
|
104 |
$this->supported['result_introspection'] = true; |
|
105 |
$this->supported['prepared_statements'] = 'emulated'; |
|
106 |
$this->supported['identifier_quoting'] = true; |
|
107 |
$this->supported['pattern_escaping'] = true; |
|
108 |
$this->supported['new_link'] = true; |
|
109 |
|
|
110 |
$this->options['DBA_username'] = false; |
|
111 |
$this->options['DBA_password'] = false; |
|
112 |
$this->options['default_table_type'] = ''; |
|
113 |
$this->options['max_identifiers_length'] = 64; |
|
114 |
|
|
115 |
$this->_reCheckSupportedOptions(); |
|
116 |
} |
|
117 |
|
|
118 |
// }}} |
|
119 |
// {{{ _reCheckSupportedOptions() |
|
120 |
|
|
121 |
/** |
|
122 |
* If the user changes certain options, other capabilities may depend |
|
123 |
* on the new settings, so we need to check them (again). |
|
124 |
* |
|
125 |
* @access private |
|
126 |
*/ |
|
127 |
function _reCheckSupportedOptions() |
|
128 |
{ |
|
129 |
$this->supported['transactions'] = $this->options['use_transactions']; |
|
130 |
$this->supported['savepoints'] = $this->options['use_transactions']; |
|
131 |
if ($this->options['default_table_type']) { |
|
132 |
switch (strtoupper($this->options['default_table_type'])) { |
|
133 |
case 'BLACKHOLE': |
|
134 |
case 'MEMORY': |
|
135 |
case 'ARCHIVE': |
|
136 |
case 'CSV': |
|
137 |
case 'HEAP': |
|
138 |
case 'ISAM': |
|
139 |
case 'MERGE': |
|
140 |
case 'MRG_ISAM': |
|
141 |
case 'ISAM': |
|
142 |
case 'MRG_MYISAM': |
|
143 |
case 'MYISAM': |
|
144 |
$this->supported['savepoints'] = false; |
|
145 |
$this->supported['transactions'] = false; |
|
146 |
$this->warnings[] = $this->options['default_table_type'] . |
|
147 |
' is not a supported default table type'; |
|
148 |
break; |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
// }}} |
|
154 |
// {{{ function setOption($option, $value) |
|
155 |
|
|
156 |
/** |
|
157 |
* set the option for the db class |
|
158 |
* |
|
159 |
* @param string option name |
|
160 |
* @param mixed value for the option |
|
161 |
* |
|
162 |
* @return mixed MDB2_OK or MDB2 Error Object |
|
163 |
* |
|
164 |
* @access public |
|
165 |
*/ |
|
166 |
function setOption($option, $value) |
|
167 |
{ |
|
168 |
$res = parent::setOption($option, $value); |
|
169 |
$this->_reCheckSupportedOptions(); |
|
170 |
} |
|
171 |
|
|
172 |
// }}} |
|
173 |
// {{{ errorInfo() |
|
174 |
|
|
175 |
/** |
|
176 |
* This method is used to collect information about an error |
|
177 |
* |
|
178 |
* @param integer $error |
|
179 |
* @return array |
|
180 |
* @access public |
|
181 |
*/ |
|
182 |
function errorInfo($error = null) |
|
183 |
{ |
|
184 |
if ($this->connection) { |
|
185 |
$native_code = @mysql_errno($this->connection); |
|
186 |
$native_msg = @mysql_error($this->connection); |
|
187 |
} else { |
|
188 |
$native_code = @mysql_errno(); |
|
189 |
$native_msg = @mysql_error(); |
|
190 |
} |
|
191 |
if (is_null($error)) { |
|
192 |
static $ecode_map; |
|
193 |
if (empty($ecode_map)) { |
|
194 |
$ecode_map = array( |
|
195 |
1000 => MDB2_ERROR_INVALID, //hashchk |
|
196 |
1001 => MDB2_ERROR_INVALID, //isamchk |
|
197 |
1004 => MDB2_ERROR_CANNOT_CREATE, |
|
198 |
1005 => MDB2_ERROR_CANNOT_CREATE, |
|
199 |
1006 => MDB2_ERROR_CANNOT_CREATE, |
|
200 |
1007 => MDB2_ERROR_ALREADY_EXISTS, |
|
201 |
1008 => MDB2_ERROR_CANNOT_DROP, |
|
202 |
1009 => MDB2_ERROR_CANNOT_DROP, |
|
203 |
1010 => MDB2_ERROR_CANNOT_DROP, |
|
204 |
1011 => MDB2_ERROR_CANNOT_DELETE, |
|
205 |
1022 => MDB2_ERROR_ALREADY_EXISTS, |
|
206 |
1029 => MDB2_ERROR_NOT_FOUND, |
|
207 |
1032 => MDB2_ERROR_NOT_FOUND, |
|
208 |
1044 => MDB2_ERROR_ACCESS_VIOLATION, |
|
209 |
1045 => MDB2_ERROR_ACCESS_VIOLATION, |
|
210 |
1046 => MDB2_ERROR_NODBSELECTED, |
|
211 |
1048 => MDB2_ERROR_CONSTRAINT, |
|
212 |
1049 => MDB2_ERROR_NOSUCHDB, |
|
213 |
1050 => MDB2_ERROR_ALREADY_EXISTS, |
|
214 |
1051 => MDB2_ERROR_NOSUCHTABLE, |
|
215 |
1054 => MDB2_ERROR_NOSUCHFIELD, |
|
216 |
1060 => MDB2_ERROR_ALREADY_EXISTS, |
|
217 |
1061 => MDB2_ERROR_ALREADY_EXISTS, |
|
218 |
1062 => MDB2_ERROR_ALREADY_EXISTS, |
|
219 |
1064 => MDB2_ERROR_SYNTAX, |
|
220 |
1067 => MDB2_ERROR_INVALID, |
|
221 |
1072 => MDB2_ERROR_NOT_FOUND, |
|
222 |
1086 => MDB2_ERROR_ALREADY_EXISTS, |
|
223 |
1091 => MDB2_ERROR_NOT_FOUND, |
|
224 |
1100 => MDB2_ERROR_NOT_LOCKED, |
|
225 |
1109 => MDB2_ERROR_NOT_FOUND, |
|
226 |
1125 => MDB2_ERROR_ALREADY_EXISTS, |
|
227 |
1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW, |
|
228 |
1138 => MDB2_ERROR_INVALID, |
|
229 |
1142 => MDB2_ERROR_ACCESS_VIOLATION, |
|
230 |
1143 => MDB2_ERROR_ACCESS_VIOLATION, |
|
231 |
1146 => MDB2_ERROR_NOSUCHTABLE, |
|
232 |
1149 => MDB2_ERROR_SYNTAX, |
|
233 |
1169 => MDB2_ERROR_CONSTRAINT, |
|
234 |
1176 => MDB2_ERROR_NOT_FOUND, |
|
235 |
1177 => MDB2_ERROR_NOSUCHTABLE, |
|
236 |
1213 => MDB2_ERROR_DEADLOCK, |
|
237 |
1216 => MDB2_ERROR_CONSTRAINT, |
|
238 |
1217 => MDB2_ERROR_CONSTRAINT, |
|
239 |
1227 => MDB2_ERROR_ACCESS_VIOLATION, |
|
240 |
1235 => MDB2_ERROR_CANNOT_CREATE, |
|
241 |
1299 => MDB2_ERROR_INVALID_DATE, |
|
242 |
1300 => MDB2_ERROR_INVALID, |
|
243 |
1304 => MDB2_ERROR_ALREADY_EXISTS, |
|
244 |
1305 => MDB2_ERROR_NOT_FOUND, |
|
245 |
1306 => MDB2_ERROR_CANNOT_DROP, |
|
246 |
1307 => MDB2_ERROR_CANNOT_CREATE, |
|
247 |
1334 => MDB2_ERROR_CANNOT_ALTER, |
|
248 |
1339 => MDB2_ERROR_NOT_FOUND, |
|
249 |
1356 => MDB2_ERROR_INVALID, |
|
250 |
1359 => MDB2_ERROR_ALREADY_EXISTS, |
|
251 |
1360 => MDB2_ERROR_NOT_FOUND, |
|
252 |
1363 => MDB2_ERROR_NOT_FOUND, |
|
253 |
1365 => MDB2_ERROR_DIVZERO, |
|
254 |
1451 => MDB2_ERROR_CONSTRAINT, |
|
255 |
1452 => MDB2_ERROR_CONSTRAINT, |
|
256 |
1542 => MDB2_ERROR_CANNOT_DROP, |
|
257 |
1546 => MDB2_ERROR_CONSTRAINT, |
|
258 |
1582 => MDB2_ERROR_CONSTRAINT, |
|
259 |
2003 => MDB2_ERROR_CONNECT_FAILED, |
|
260 |
2019 => MDB2_ERROR_INVALID, |
|
261 |
); |
|
262 |
} |
|
263 |
if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) { |
|
264 |
$ecode_map[1022] = MDB2_ERROR_CONSTRAINT; |
|
265 |
$ecode_map[1048] = MDB2_ERROR_CONSTRAINT_NOT_NULL; |
|
266 |
$ecode_map[1062] = MDB2_ERROR_CONSTRAINT; |
|
267 |
} else { |
|
268 |
// Doing this in case mode changes during runtime. |
|
269 |
$ecode_map[1022] = MDB2_ERROR_ALREADY_EXISTS; |
|
270 |
$ecode_map[1048] = MDB2_ERROR_CONSTRAINT; |
|
271 |
$ecode_map[1062] = MDB2_ERROR_ALREADY_EXISTS; |
|
272 |
} |
|
273 |
if (isset($ecode_map[$native_code])) { |
|
274 |
$error = $ecode_map[$native_code]; |
|
275 |
} |
|
276 |
} |
|
277 |
return array($error, $native_code, $native_msg); |
|
278 |
} |
|
279 |
|
|
280 |
// }}} |
|
281 |
// {{{ escape() |
|
282 |
|
|
283 |
/** |
|
284 |
* Quotes a string so it can be safely used in a query. It will quote |
|
285 |
* the text so it can safely be used within a query. |
|
286 |
* |
|
287 |
* @param string the input string to quote |
|
288 |
* @param bool escape wildcards |
|
289 |
* |
|
290 |
* @return string quoted string |
|
291 |
* |
|
292 |
* @access public |
|
293 |
*/ |
|
294 |
function escape($text, $escape_wildcards = false) |
|
295 |
{ |
|
296 |
if ($escape_wildcards) { |
|
297 |
$text = $this->escapePattern($text); |
|
298 |
} |
|
299 |
$connection = $this->getConnection(); |
|
300 |
if (PEAR::isError($connection)) { |
|
301 |
return $connection; |
|
302 |
} |
|
303 |
$text = @mysql_real_escape_string($text, $connection); |
|
304 |
return $text; |
|
305 |
} |
|
306 |
|
|
307 |
// }}} |
|
308 |
// {{{ beginTransaction() |
|
309 |
|
|
310 |
/** |
|
311 |
* Start a transaction or set a savepoint. |
|
312 |
* |
|
313 |
* @param string name of a savepoint to set |
|
314 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
315 |
* |
|
316 |
* @access public |
|
317 |
*/ |
|
318 |
function beginTransaction($savepoint = null) |
|
319 |
{ |
|
320 |
$this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); |
|
321 |
$this->_getServerCapabilities(); |
|
322 |
if (!is_null($savepoint)) { |
|
323 |
if (!$this->supports('savepoints')) { |
|
324 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
325 |
'savepoints are not supported', __FUNCTION__); |
|
326 |
} |
|
327 |
if (!$this->in_transaction) { |
|
328 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, |
|
329 |
'savepoint cannot be released when changes are auto committed', __FUNCTION__); |
|
330 |
} |
|
331 |
$query = 'SAVEPOINT '.$savepoint; |
|
332 |
return $this->_doQuery($query, true); |
|
333 |
} elseif ($this->in_transaction) { |
|
334 |
return MDB2_OK; //nothing to do |
|
335 |
} |
|
336 |
if (!$this->destructor_registered && $this->opened_persistent) { |
|
337 |
$this->destructor_registered = true; |
|
338 |
register_shutdown_function('MDB2_closeOpenTransactions'); |
|
339 |
} |
|
340 |
$query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 1'; |
|
341 |
$result =& $this->_doQuery($query, true); |
|
342 |
if (PEAR::isError($result)) { |
|
343 |
return $result; |
|
344 |
} |
|
345 |
$this->in_transaction = true; |
|
346 |
return MDB2_OK; |
|
347 |
} |
|
348 |
|
|
349 |
// }}} |
|
350 |
// {{{ commit() |
|
351 |
|
|
352 |
/** |
|
353 |
* Commit the database changes done during a transaction that is in |
|
354 |
* progress or release a savepoint. This function may only be called when |
|
355 |
* auto-committing is disabled, otherwise it will fail. Therefore, a new |
|
356 |
* transaction is implicitly started after committing the pending changes. |
|
357 |
* |
|
358 |
* @param string name of a savepoint to release |
|
359 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
360 |
* |
|
361 |
* @access public |
|
362 |
*/ |
|
363 |
function commit($savepoint = null) |
|
364 |
{ |
|
365 |
$this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); |
|
366 |
if (!$this->in_transaction) { |
|
367 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, |
|
368 |
'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__); |
|
369 |
} |
|
370 |
if (!is_null($savepoint)) { |
|
371 |
if (!$this->supports('savepoints')) { |
|
372 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
373 |
'savepoints are not supported', __FUNCTION__); |
|
374 |
} |
|
375 |
$server_info = $this->getServerVersion(); |
|
376 |
if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) { |
|
377 |
return MDB2_OK; |
|
378 |
} |
|
379 |
$query = 'RELEASE SAVEPOINT '.$savepoint; |
|
380 |
return $this->_doQuery($query, true); |
|
381 |
} |
|
382 |
|
|
383 |
if (!$this->supports('transactions')) { |
|
384 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
385 |
'transactions are not supported', __FUNCTION__); |
|
386 |
} |
|
387 |
|
|
388 |
$result =& $this->_doQuery('COMMIT', true); |
|
389 |
if (PEAR::isError($result)) { |
|
390 |
return $result; |
|
391 |
} |
|
392 |
if (!$this->start_transaction) { |
|
393 |
$query = 'SET AUTOCOMMIT = 0'; |
|
394 |
$result =& $this->_doQuery($query, true); |
|
395 |
if (PEAR::isError($result)) { |
|
396 |
return $result; |
|
397 |
} |
|
398 |
} |
|
399 |
$this->in_transaction = false; |
|
400 |
return MDB2_OK; |
|
401 |
} |
|
402 |
|
|
403 |
// }}} |
|
404 |
// {{{ rollback() |
|
405 |
|
|
406 |
/** |
|
407 |
* Cancel any database changes done during a transaction or since a specific |
|
408 |
* savepoint that is in progress. This function may only be called when |
|
409 |
* auto-committing is disabled, otherwise it will fail. Therefore, a new |
|
410 |
* transaction is implicitly started after canceling the pending changes. |
|
411 |
* |
|
412 |
* @param string name of a savepoint to rollback to |
|
413 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
414 |
* |
|
415 |
* @access public |
|
416 |
*/ |
|
417 |
function rollback($savepoint = null) |
|
418 |
{ |
|
419 |
$this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); |
|
420 |
if (!$this->in_transaction) { |
|
421 |
return $this->raiseError(MDB2_ERROR_INVALID, null, null, |
|
422 |
'rollback cannot be done changes are auto committed', __FUNCTION__); |
|
423 |
} |
|
424 |
if (!is_null($savepoint)) { |
|
425 |
if (!$this->supports('savepoints')) { |
|
426 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
427 |
'savepoints are not supported', __FUNCTION__); |
|
428 |
} |
|
429 |
$query = 'ROLLBACK TO SAVEPOINT '.$savepoint; |
|
430 |
return $this->_doQuery($query, true); |
|
431 |
} |
|
432 |
|
|
433 |
$query = 'ROLLBACK'; |
|
434 |
$result =& $this->_doQuery($query, true); |
|
435 |
if (PEAR::isError($result)) { |
|
436 |
return $result; |
|
437 |
} |
|
438 |
if (!$this->start_transaction) { |
|
439 |
$query = 'SET AUTOCOMMIT = 0'; |
|
440 |
$result =& $this->_doQuery($query, true); |
|
441 |
if (PEAR::isError($result)) { |
|
442 |
return $result; |
|
443 |
} |
|
444 |
} |
|
445 |
$this->in_transaction = false; |
|
446 |
return MDB2_OK; |
|
447 |
} |
|
448 |
|
|
449 |
// }}} |
|
450 |
// {{{ function setTransactionIsolation() |
|
451 |
|
|
452 |
/** |
|
453 |
* Set the transacton isolation level. |
|
454 |
* |
|
455 |
* @param string standard isolation level |
|
456 |
* READ UNCOMMITTED (allows dirty reads) |
|
457 |
* READ COMMITTED (prevents dirty reads) |
|
458 |
* REPEATABLE READ (prevents nonrepeatable reads) |
|
459 |
* SERIALIZABLE (prevents phantom reads) |
|
460 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
461 |
* |
|
462 |
* @access public |
|
463 |
* @since 2.1.1 |
|
464 |
*/ |
|
465 |
function setTransactionIsolation($isolation) |
|
466 |
{ |
|
467 |
$this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true)); |
|
468 |
if (!$this->supports('transactions')) { |
|
469 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
470 |
'transactions are not supported', __FUNCTION__); |
|
471 |
} |
|
472 |
switch ($isolation) { |
|
473 |
case 'READ UNCOMMITTED': |
|
474 |
case 'READ COMMITTED': |
|
475 |
case 'REPEATABLE READ': |
|
476 |
case 'SERIALIZABLE': |
|
477 |
break; |
|
478 |
default: |
|
479 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
480 |
'isolation level is not supported: '.$isolation, __FUNCTION__); |
|
481 |
} |
|
482 |
|
|
483 |
$query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation"; |
|
484 |
return $this->_doQuery($query, true); |
|
485 |
} |
|
486 |
|
|
487 |
// }}} |
|
488 |
// {{{ _doConnect() |
|
489 |
|
|
490 |
/** |
|
491 |
* do the grunt work of the connect |
|
492 |
* |
|
493 |
* @return connection on success or MDB2 Error Object on failure |
|
494 |
* @access protected |
|
495 |
*/ |
|
496 |
function _doConnect($username, $password, $persistent = false) |
|
497 |
{ |
|
498 |
if (!PEAR::loadExtension($this->phptype)) { |
|
499 |
return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
500 |
'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__); |
|
501 |
} |
|
502 |
|
|
503 |
$params = array(); |
|
504 |
if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') { |
|
505 |
$params[0] = ':' . $this->dsn['socket']; |
|
506 |
} else { |
|
507 |
$params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec'] |
|
508 |
: 'localhost'; |
|
509 |
if ($this->dsn['port']) { |
|
510 |
$params[0].= ':' . $this->dsn['port']; |
|
511 |
} |
|
512 |
} |
|
513 |
$params[] = $username ? $username : null; |
|
514 |
$params[] = $password ? $password : null; |
|
515 |
if (!$persistent) { |
|
516 |
if (isset($this->dsn['new_link']) |
|
517 |
&& ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true) |
|
518 |
) { |
|
519 |
$params[] = true; |
|
520 |
} else { |
|
521 |
$params[] = false; |
|
522 |
} |
|
523 |
} |
|
524 |
if (version_compare(phpversion(), '4.3.0', '>=')) { |
|
525 |
$params[] = isset($this->dsn['client_flags']) |
|
526 |
? $this->dsn['client_flags'] : null; |
|
527 |
} |
|
528 |
$connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect'; |
|
529 |
|
|
530 |
$connection = @call_user_func_array($connect_function, $params); |
|
531 |
if (!$connection) { |
|
532 |
if (($err = @mysql_error()) != '') { |
|
533 |
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null, |
|
534 |
$err, __FUNCTION__); |
|
535 |
} else { |
|
536 |
return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null, |
|
537 |
'unable to establish a connection', __FUNCTION__); |
|
538 |
} |
|
539 |
} |
|
540 |
|
|
541 |
if (!empty($this->dsn['charset'])) { |
|
542 |
$result = $this->setCharset($this->dsn['charset'], $connection); |
|
543 |
if (PEAR::isError($result)) { |
|
544 |
$this->disconnect(false); |
|
545 |
return $result; |
|
546 |
} |
|
547 |
} |
|
548 |
|
|
549 |
return $connection; |
|
550 |
} |
|
551 |
|
|
552 |
// }}} |
|
553 |
// {{{ connect() |
|
554 |
|
|
555 |
/** |
|
556 |
* Connect to the database |
|
557 |
* |
|
558 |
* @return MDB2_OK on success, MDB2 Error Object on failure |
|
559 |
* @access public |
|
560 |
*/ |
|
561 |
function connect() |
|
562 |
{ |
|
563 |
if (is_resource($this->connection)) { |
|
564 |
//if (count(array_diff($this->connected_dsn, $this->dsn)) == 0 |
|
565 |
if (MDB2::areEquals($this->connected_dsn, $this->dsn) |
|
566 |
&& $this->opened_persistent == $this->options['persistent'] |
|
567 |
) { |
|
568 |
return MDB2_OK; |
|
569 |
} |
|
570 |
$this->disconnect(false); |
|
571 |
} |
|
572 |
|
|
573 |
$connection = $this->_doConnect( |
|
574 |
$this->dsn['username'], |
|
575 |
$this->dsn['password'], |
|
576 |
$this->options['persistent'] |
|
577 |
); |
|
578 |
if (PEAR::isError($connection)) { |
|
579 |
return $connection; |
|
580 |
} |
|
581 |
|
|
582 |
$this->connection = $connection; |
|
583 |
$this->connected_dsn = $this->dsn; |
|
584 |
$this->connected_database_name = ''; |
|
585 |
$this->opened_persistent = $this->options['persistent']; |
|
586 |
$this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype; |
|
587 |
|
|
588 |
if ($this->database_name) { |
|
589 |
if ($this->database_name != $this->connected_database_name) { |
|
590 |
if (!@mysql_select_db($this->database_name, $connection)) { |
|
591 |
$err = $this->raiseError(null, null, null, |
|
592 |
'Could not select the database: '.$this->database_name, __FUNCTION__); |
|
593 |
return $err; |
|
594 |
} |
|
595 |
$this->connected_database_name = $this->database_name; |
|
596 |
} |
|
597 |
} |
|
598 |
|
|
599 |
$this->_getServerCapabilities(); |
|
600 |
|
|
601 |
return MDB2_OK; |
|
602 |
} |
|
603 |
|
|
604 |
// }}} |
|
605 |
// {{{ setCharset() |
|
606 |
|
|
607 |
/** |
|
608 |
* Set the charset on the current connection |
|
609 |
* |
|
610 |
* @param string charset (or array(charset, collation)) |
|
611 |
* @param resource connection handle |
|
612 |
* |
|
613 |
* @return true on success, MDB2 Error Object on failure |
|
614 |
*/ |
|
615 |
function setCharset($charset, $connection = null) |
|
616 |
{ |
|
617 |
if (is_null($connection)) { |
|
618 |
$connection = $this->getConnection(); |
|
619 |
if (PEAR::isError($connection)) { |
|
620 |
return $connection; |
|
621 |
} |
|
622 |
} |
|
623 |
$collation = null; |
|
624 |
if (is_array($charset) && 2 == count($charset)) { |
|
625 |
$collation = array_pop($charset); |
|
626 |
$charset = array_pop($charset); |
|
627 |
} |
|
628 |
$query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'"; |
|
629 |
if (!is_null($collation)) { |
|
630 |
$query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'"; |
|
631 |
} |
|
632 |
return $this->_doQuery($query, true, $connection); |
|
633 |
} |
|
634 |
|
|
635 |
// }}} |
|
636 |
// {{{ databaseExists() |
|
637 |
|
|
638 |
/** |
|
639 |
* check if given database name is exists? |
|
640 |
* |
|
641 |
* @param string $name name of the database that should be checked |
|
642 |
* |
|
643 |
* @return mixed true/false on success, a MDB2 error on failure |
|
644 |
* @access public |
|
645 |
*/ |
|
646 |
function databaseExists($name) |
|
647 |
{ |
|
648 |
$connection = $this->_doConnect($this->dsn['username'], |
|
649 |
$this->dsn['password'], |
|
650 |
$this->options['persistent']); |
|
651 |
if (PEAR::isError($connection)) { |
|
652 |
return $connection; |
|
653 |
} |
|
654 |
|
|
655 |
$result = @mysql_select_db($name, $connection); |
|
656 |
@mysql_close($connection); |
|
657 |
|
|
658 |
return $result; |
|
659 |
} |
|
660 |
|
|
661 |
// }}} |
|
662 |
// {{{ disconnect() |
|
663 |
|
|
664 |
/** |
|
665 |
* Log out and disconnect from the database. |
|
666 |
* |
|
667 |
* @param boolean $force if the disconnect should be forced even if the |
|
668 |
* connection is opened persistently |
|
669 |
* @return mixed true on success, false if not connected and error |
|
670 |
* object on error |
|
671 |
* @access public |
|
672 |
*/ |
|
673 |
function disconnect($force = true) |
|
674 |
{ |
|
675 |
if (is_resource($this->connection)) { |
|
676 |
if ($this->in_transaction) { |
|
677 |
$dsn = $this->dsn; |
|
678 |
$database_name = $this->database_name; |
|
679 |
$persistent = $this->options['persistent']; |
|
680 |
$this->dsn = $this->connected_dsn; |
|
681 |
$this->database_name = $this->connected_database_name; |
|
682 |
$this->options['persistent'] = $this->opened_persistent; |
|
683 |
$this->rollback(); |
|
684 |
$this->dsn = $dsn; |
|
685 |
$this->database_name = $database_name; |
|
686 |
$this->options['persistent'] = $persistent; |
|
687 |
} |
|
688 |
|
|
689 |
if (!$this->opened_persistent || $force) { |
|
690 |
@mysql_close($this->connection); |
|
691 |
} |
|
692 |
} |
|
693 |
return parent::disconnect($force); |
|
694 |
} |
|
695 |
|
|
696 |
// }}} |
|
697 |
// {{{ standaloneQuery() |
|
698 |
|
|
699 |
/** |
|
700 |
* execute a query as DBA |
|
701 |
* |
|
702 |
* @param string $query the SQL query |
|
703 |
* @param mixed $types array that contains the types of the columns in |
|
704 |
* the result set |
|
705 |
* @param boolean $is_manip if the query is a manipulation query |
|
706 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
707 |
* @access public |
|
708 |
*/ |
|
709 |
function &standaloneQuery($query, $types = null, $is_manip = false) |
|
710 |
{ |
|
711 |
$user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username']; |
|
712 |
$pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password']; |
|
713 |
$connection = $this->_doConnect($user, $pass, $this->options['persistent']); |
|
714 |
if (PEAR::isError($connection)) { |
|
715 |
return $connection; |
|
716 |
} |
|
717 |
|
|
718 |
$offset = $this->offset; |
|
719 |
$limit = $this->limit; |
|
720 |
$this->offset = $this->limit = 0; |
|
721 |
$query = $this->_modifyQuery($query, $is_manip, $limit, $offset); |
|
722 |
|
|
723 |
$result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name); |
|
724 |
if (!PEAR::isError($result)) { |
|
725 |
$result = $this->_affectedRows($connection, $result); |
|
726 |
} |
|
727 |
|
|
728 |
@mysql_close($connection); |
|
729 |
return $result; |
|
730 |
} |
|
731 |
|
|
732 |
// }}} |
|
733 |
// {{{ _doQuery() |
|
734 |
|
|
735 |
/** |
|
736 |
* Execute a query |
|
737 |
* @param string $query query |
|
738 |
* @param boolean $is_manip if the query is a manipulation query |
|
739 |
* @param resource $connection |
|
740 |
* @param string $database_name |
|
741 |
* @return result or error object |
|
742 |
* @access protected |
|
743 |
*/ |
|
744 |
function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null) |
|
745 |
{ |
|
746 |
$this->last_query = $query; |
|
747 |
$result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre')); |
|
748 |
if ($result) { |
|
749 |
if (PEAR::isError($result)) { |
|
750 |
return $result; |
|
751 |
} |
|
752 |
$query = $result; |
|
753 |
} |
|
754 |
if ($this->options['disable_query']) { |
|
755 |
$result = $is_manip ? 0 : null; |
|
756 |
return $result; |
|
757 |
} |
|
758 |
|
|
759 |
if (is_null($connection)) { |
|
760 |
$connection = $this->getConnection(); |
|
761 |
if (PEAR::isError($connection)) { |
|
762 |
return $connection; |
|
763 |
} |
|
764 |
} |
|
765 |
if (is_null($database_name)) { |
|
766 |
$database_name = $this->database_name; |
|
767 |
} |
|
768 |
|
|
769 |
if ($database_name) { |
|
770 |
if ($database_name != $this->connected_database_name) { |
|
771 |
if (!@mysql_select_db($database_name, $connection)) { |
|
772 |
$err = $this->raiseError(null, null, null, |
|
773 |
'Could not select the database: '.$database_name, __FUNCTION__); |
|
774 |
return $err; |
|
775 |
} |
|
776 |
$this->connected_database_name = $database_name; |
|
777 |
} |
|
778 |
} |
|
779 |
|
|
780 |
$function = $this->options['result_buffering'] |
|
781 |
? 'mysql_query' : 'mysql_unbuffered_query'; |
|
782 |
$result = @$function($query, $connection); |
|
783 |
if (!$result) { |
|
784 |
$err =& $this->raiseError(null, null, null, |
|
785 |
'Could not execute statement', __FUNCTION__); |
|
786 |
return $err; |
|
787 |
} |
|
788 |
|
|
789 |
$this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result)); |
|
790 |
return $result; |
|
791 |
} |
|
792 |
|
|
793 |
// }}} |
|
794 |
// {{{ _affectedRows() |
|
795 |
|
|
796 |
/** |
|
797 |
* Returns the number of rows affected |
|
798 |
* |
|
799 |
* @param resource $result |
|
800 |
* @param resource $connection |
|
801 |
* @return mixed MDB2 Error Object or the number of rows affected |
|
802 |
* @access private |
|
803 |
*/ |
|
804 |
function _affectedRows($connection, $result = null) |
|
805 |
{ |
|
806 |
if (is_null($connection)) { |
|
807 |
$connection = $this->getConnection(); |
|
808 |
if (PEAR::isError($connection)) { |
|
809 |
return $connection; |
|
810 |
} |
|
811 |
} |
|
812 |
return @mysql_affected_rows($connection); |
|
813 |
} |
|
814 |
|
|
815 |
// }}} |
|
816 |
// {{{ _modifyQuery() |
|
817 |
|
|
818 |
/** |
|
819 |
* Changes a query string for various DBMS specific reasons |
|
820 |
* |
|
821 |
* @param string $query query to modify |
|
822 |
* @param boolean $is_manip if it is a DML query |
|
823 |
* @param integer $limit limit the number of rows |
|
824 |
* @param integer $offset start reading from given offset |
|
825 |
* @return string modified query |
|
826 |
* @access protected |
|
827 |
*/ |
|
828 |
function _modifyQuery($query, $is_manip, $limit, $offset) |
|
829 |
{ |
|
830 |
if ($this->options['portability'] & MDB2_PORTABILITY_DELETE_COUNT) { |
|
831 |
// "DELETE FROM table" gives 0 affected rows in MySQL. |
|
832 |
// This little hack lets you know how many rows were deleted. |
|
833 |
if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) { |
|
834 |
$query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', |
|
835 |
'DELETE FROM \1 WHERE 1=1', $query); |
|
836 |
} |
|
837 |
} |
|
838 |
if ($limit > 0 |
|
839 |
&& !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query) |
|
840 |
) { |
|
841 |
$query = rtrim($query); |
|
842 |
if (substr($query, -1) == ';') { |
|
843 |
$query = substr($query, 0, -1); |
|
844 |
} |
|
845 |
|
|
846 |
// LIMIT doesn't always come last in the query |
|
847 |
// @see http://dev.mysql.com/doc/refman/5.0/en/select.html |
|
848 |
$after = ''; |
|
849 |
if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) { |
|
850 |
$after = $matches[0]; |
|
851 |
$query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query); |
|
852 |
} elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) { |
|
853 |
$after = $matches[0]; |
|
854 |
$query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query); |
|
855 |
} elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) { |
|
856 |
$after = $matches[0]; |
|
857 |
$query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query); |
|
858 |
} |
|
859 |
|
|
860 |
if ($is_manip) { |
|
861 |
return $query . " LIMIT $limit" . $after; |
|
862 |
} else { |
|
863 |
return $query . " LIMIT $offset, $limit" . $after; |
|
864 |
} |
|
865 |
} |
|
866 |
return $query; |
|
867 |
} |
|
868 |
|
|
869 |
// }}} |
|
870 |
// {{{ getServerVersion() |
|
871 |
|
|
872 |
/** |
|
873 |
* return version information about the server |
|
874 |
* |
|
875 |
* @param bool $native determines if the raw version string should be returned |
|
876 |
* @return mixed array/string with version information or MDB2 error object |
|
877 |
* @access public |
|
878 |
*/ |
|
879 |
function getServerVersion($native = false) |
|
880 |
{ |
|
881 |
$connection = $this->getConnection(); |
|
882 |
if (PEAR::isError($connection)) { |
|
883 |
return $connection; |
|
884 |
} |
|
885 |
if ($this->connected_server_info) { |
|
886 |
$server_info = $this->connected_server_info; |
|
887 |
} else { |
|
888 |
$server_info = @mysql_get_server_info($connection); |
|
889 |
} |
|
890 |
if (!$server_info) { |
|
891 |
return $this->raiseError(null, null, null, |
|
892 |
'Could not get server information', __FUNCTION__); |
|
893 |
} |
|
894 |
// cache server_info |
|
895 |
$this->connected_server_info = $server_info; |
|
896 |
if (!$native) { |
|
897 |
$tmp = explode('.', $server_info, 3); |
|
898 |
if (isset($tmp[2]) && strpos($tmp[2], '-')) { |
|
899 |
$tmp2 = explode('-', @$tmp[2], 2); |
|
900 |
} else { |
|
901 |
$tmp2[0] = isset($tmp[2]) ? $tmp[2] : null; |
|
902 |
$tmp2[1] = null; |
|
903 |
} |
|
904 |
$server_info = array( |
|
905 |
'major' => isset($tmp[0]) ? $tmp[0] : null, |
|
906 |
'minor' => isset($tmp[1]) ? $tmp[1] : null, |
|
907 |
'patch' => $tmp2[0], |
|
908 |
'extra' => $tmp2[1], |
|
909 |
'native' => $server_info, |
|
910 |
); |
|
911 |
} |
|
912 |
return $server_info; |
|
913 |
} |
|
914 |
|
|
915 |
// }}} |
|
916 |
// {{{ _getServerCapabilities() |
|
917 |
|
|
918 |
/** |
|
919 |
* Fetch some information about the server capabilities |
|
920 |
* (transactions, subselects, prepared statements, etc). |
|
921 |
* |
|
922 |
* @access private |
|
923 |
*/ |
|
924 |
function _getServerCapabilities() |
|
925 |
{ |
|
926 |
if (!$this->server_capabilities_checked) { |
|
927 |
$this->server_capabilities_checked = true; |
|
928 |
|
|
929 |
//set defaults |
|
930 |
$this->supported['sub_selects'] = 'emulated'; |
|
931 |
$this->supported['prepared_statements'] = 'emulated'; |
|
932 |
$this->supported['triggers'] = false; |
|
933 |
$this->start_transaction = false; |
|
934 |
$this->varchar_max_length = 255; |
|
935 |
|
|
936 |
$server_info = $this->getServerVersion(); |
|
937 |
if (is_array($server_info)) { |
|
938 |
$server_version = $server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch']; |
|
939 |
|
|
940 |
if (!version_compare($server_version, '4.1.0', '<')) { |
|
941 |
$this->supported['sub_selects'] = true; |
|
942 |
$this->supported['prepared_statements'] = true; |
|
943 |
} |
|
944 |
|
|
945 |
// SAVEPOINTs were introduced in MySQL 4.0.14 and 4.1.1 (InnoDB) |
|
946 |
if (version_compare($server_version, '4.1.0', '>=')) { |
|
947 |
if (version_compare($server_version, '4.1.1', '<')) { |
|
948 |
$this->supported['savepoints'] = false; |
|
949 |
} |
|
950 |
} elseif (version_compare($server_version, '4.0.14', '<')) { |
|
951 |
$this->supported['savepoints'] = false; |
|
952 |
} |
|
953 |
|
|
954 |
if (!version_compare($server_version, '4.0.11', '<')) { |
|
955 |
$this->start_transaction = true; |
|
956 |
} |
|
957 |
|
|
958 |
if (!version_compare($server_version, '5.0.3', '<')) { |
|
959 |
$this->varchar_max_length = 65532; |
|
960 |
} |
|
961 |
|
|
962 |
if (!version_compare($server_version, '5.0.2', '<')) { |
|
963 |
$this->supported['triggers'] = true; |
|
964 |
} |
|
965 |
} |
|
966 |
} |
|
967 |
} |
|
968 |
|
|
969 |
// }}} |
|
970 |
// {{{ function _skipUserDefinedVariable($query, $position) |
|
971 |
|
|
972 |
/** |
|
973 |
* Utility method, used by prepare() to avoid misinterpreting MySQL user |
|
974 |
* defined variables (SELECT @x:=5) for placeholders. |
|
975 |
* Check if the placeholder is a false positive, i.e. if it is an user defined |
|
976 |
* variable instead. If so, skip it and advance the position, otherwise |
|
977 |
* return the current position, which is valid |
|
978 |
* |
|
979 |
* @param string $query |
|
980 |
* @param integer $position current string cursor position |
|
981 |
* @return integer $new_position |
|
982 |
* @access protected |
|
983 |
*/ |
|
984 |
function _skipUserDefinedVariable($query, $position) |
|
985 |
{ |
|
986 |
$found = strpos(strrev(substr($query, 0, $position)), '@'); |
|
987 |
if ($found === false) { |
|
988 |
return $position; |
|
989 |
} |
|
990 |
$pos = strlen($query) - strlen(substr($query, $position)) - $found - 1; |
|
991 |
$substring = substr($query, $pos, $position - $pos + 2); |
|
992 |
if (preg_match('/^@\w+\s*:=$/', $substring)) { |
|
993 |
return $position + 1; //found an user defined variable: skip it |
|
994 |
} |
|
995 |
return $position; |
|
996 |
} |
|
997 |
|
|
998 |
// }}} |
|
999 |
// {{{ prepare() |
|
1000 |
|
|
1001 |
/** |
|
1002 |
* Prepares a query for multiple execution with execute(). |
|
1003 |
* With some database backends, this is emulated. |
|
1004 |
* prepare() requires a generic query as string like |
|
1005 |
* 'INSERT INTO numbers VALUES(?,?)' or |
|
1006 |
* 'INSERT INTO numbers VALUES(:foo,:bar)'. |
|
1007 |
* The ? and :name and are placeholders which can be set using |
|
1008 |
* bindParam() and the query can be sent off using the execute() method. |
|
1009 |
* The allowed format for :name can be set with the 'bindname_format' option. |
|
1010 |
* |
|
1011 |
* @param string $query the query to prepare |
|
1012 |
* @param mixed $types array that contains the types of the placeholders |
|
1013 |
* @param mixed $result_types array that contains the types of the columns in |
|
1014 |
* the result set or MDB2_PREPARE_RESULT, if set to |
|
1015 |
* MDB2_PREPARE_MANIP the query is handled as a manipulation query |
|
1016 |
* @param mixed $lobs key (field) value (parameter) pair for all lob placeholders |
|
1017 |
* @return mixed resource handle for the prepared query on success, a MDB2 |
|
1018 |
* error on failure |
|
1019 |
* @access public |
|
1020 |
* @see bindParam, execute |
|
1021 |
*/ |
|
1022 |
function &prepare($query, $types = null, $result_types = null, $lobs = array()) |
|
1023 |
{ |
|
1024 |
if ($this->options['emulate_prepared'] |
|
1025 |
|| $this->supported['prepared_statements'] !== true |
|
1026 |
) { |
|
1027 |
$obj =& parent::prepare($query, $types, $result_types, $lobs); |
|
1028 |
return $obj; |
|
1029 |
} |
|
1030 |
$is_manip = ($result_types === MDB2_PREPARE_MANIP); |
|
1031 |
$offset = $this->offset; |
|
1032 |
$limit = $this->limit; |
|
1033 |
$this->offset = $this->limit = 0; |
|
1034 |
$query = $this->_modifyQuery($query, $is_manip, $limit, $offset); |
|
1035 |
$result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre')); |
|
1036 |
if ($result) { |
|
1037 |
if (PEAR::isError($result)) { |
|
1038 |
return $result; |
|
1039 |
} |
|
1040 |
$query = $result; |
|
1041 |
} |
|
1042 |
$placeholder_type_guess = $placeholder_type = null; |
|
1043 |
$question = '?'; |
|
1044 |
$colon = ':'; |
|
1045 |
$positions = array(); |
|
1046 |
$position = 0; |
|
1047 |
while ($position < strlen($query)) { |
|
1048 |
$q_position = strpos($query, $question, $position); |
|
1049 |
$c_position = strpos($query, $colon, $position); |
|
1050 |
if ($q_position && $c_position) { |
|
1051 |
$p_position = min($q_position, $c_position); |
|
1052 |
} elseif ($q_position) { |
|
1053 |
$p_position = $q_position; |
|
1054 |
} elseif ($c_position) { |
|
1055 |
$p_position = $c_position; |
|
1056 |
} else { |
|
1057 |
break; |
|
1058 |
} |
|
1059 |
if (is_null($placeholder_type)) { |
|
1060 |
$placeholder_type_guess = $query[$p_position]; |
|
1061 |
} |
|
1062 |
|
|
1063 |
$new_pos = $this->_skipDelimitedStrings($query, $position, $p_position); |
|
1064 |
if (PEAR::isError($new_pos)) { |
|
1065 |
return $new_pos; |
|
1066 |
} |
|
1067 |
if ($new_pos != $position) { |
|
1068 |
$position = $new_pos; |
|
1069 |
continue; //evaluate again starting from the new position |
|
1070 |
} |
|
1071 |
|
|
1072 |
//make sure this is not part of an user defined variable |
|
1073 |
$new_pos = $this->_skipUserDefinedVariable($query, $position); |
|
1074 |
if ($new_pos != $position) { |
|
1075 |
$position = $new_pos; |
|
1076 |
continue; //evaluate again starting from the new position |
|
1077 |
} |
|
1078 |
|
|
1079 |
if ($query[$position] == $placeholder_type_guess) { |
|
1080 |
if (is_null($placeholder_type)) { |
|
1081 |
$placeholder_type = $query[$p_position]; |
|
1082 |
$question = $colon = $placeholder_type; |
|
1083 |
} |
|
1084 |
if ($placeholder_type == ':') { |
|
1085 |
$regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s'; |
|
1086 |
$parameter = preg_replace($regexp, '\\1', $query); |
|
1087 |
if ($parameter === '') { |
|
1088 |
$err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
1089 |
'named parameter name must match "bindname_format" option', __FUNCTION__); |
|
1090 |
return $err; |
|
1091 |
} |
|
1092 |
$positions[$p_position] = $parameter; |
|
1093 |
$query = substr_replace($query, '?', $position, strlen($parameter)+1); |
|
1094 |
} else { |
|
1095 |
$positions[$p_position] = count($positions); |
|
1096 |
} |
|
1097 |
$position = $p_position + 1; |
|
1098 |
} else { |
|
1099 |
$position = $p_position; |
|
1100 |
} |
|
1101 |
} |
|
1102 |
$connection = $this->getConnection(); |
|
1103 |
if (PEAR::isError($connection)) { |
|
1104 |
return $connection; |
|
1105 |
} |
|
1106 |
static $prep_statement_counter = 1; |
|
1107 |
$statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand())); |
|
1108 |
$statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']); |
|
1109 |
$query = "PREPARE $statement_name FROM ".$this->quote($query, 'text'); |
|
1110 |
$statement =& $this->_doQuery($query, true, $connection); |
|
1111 |
if (PEAR::isError($statement)) { |
|
1112 |
return $statement; |
|
1113 |
} |
|
1114 |
|
|
1115 |
$class_name = 'MDB2_Statement_'.$this->phptype; |
|
1116 |
$obj = new $class_name($this, $statement_name, $positions, $query, $types, $result_types, $is_manip, $limit, $offset); |
|
1117 |
$this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj)); |
|
1118 |
return $obj; |
|
1119 |
} |
|
1120 |
|
|
1121 |
// }}} |
|
1122 |
// {{{ replace() |
|
1123 |
|
|
1124 |
/** |
|
1125 |
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT |
|
1126 |
* query, except that if there is already a row in the table with the same |
|
1127 |
* key field values, the REPLACE query just updates its values instead of |
|
1128 |
* inserting a new row. |
|
1129 |
* |
|
1130 |
* The REPLACE type of query does not make part of the SQL standards. Since |
|
1131 |
* practically only MySQL implements it natively, this type of query is |
|
1132 |
* emulated through this method for other DBMS using standard types of |
|
1133 |
* queries inside a transaction to assure the atomicity of the operation. |
|
1134 |
* |
|
1135 |
* @access public |
|
1136 |
* |
|
1137 |
* @param string $table name of the table on which the REPLACE query will |
|
1138 |
* be executed. |
|
1139 |
* @param array $fields associative array that describes the fields and the |
|
1140 |
* values that will be inserted or updated in the specified table. The |
|
1141 |
* indexes of the array are the names of all the fields of the table. The |
|
1142 |
* values of the array are also associative arrays that describe the |
|
1143 |
* values and other properties of the table fields. |
|
1144 |
* |
|
1145 |
* Here follows a list of field properties that need to be specified: |
|
1146 |
* |
|
1147 |
* value: |
|
1148 |
* Value to be assigned to the specified field. This value may be |
|
1149 |
* of specified in database independent type format as this |
|
1150 |
* function can perform the necessary datatype conversions. |
|
1151 |
* |
|
1152 |
* Default: |
|
1153 |
* this property is required unless the Null property |
|
1154 |
* is set to 1. |
|
1155 |
* |
|
1156 |
* type |
|
1157 |
* Name of the type of the field. Currently, all types Metabase |
|
1158 |
* are supported except for clob and blob. |
|
1159 |
* |
|
1160 |
* Default: no type conversion |
|
1161 |
* |
|
1162 |
* null |
|
1163 |
* Boolean property that indicates that the value for this field |
|
1164 |
* should be set to null. |
|
1165 |
* |
|
1166 |
* The default value for fields missing in INSERT queries may be |
|
1167 |
* specified the definition of a table. Often, the default value |
|
1168 |
* is already null, but since the REPLACE may be emulated using |
|
1169 |
* an UPDATE query, make sure that all fields of the table are |
|
1170 |
* listed in this function argument array. |
|
1171 |
* |
|
1172 |
* Default: 0 |
|
1173 |
* |
|
1174 |
* key |
|
1175 |
* Boolean property that indicates that this field should be |
|
1176 |
* handled as a primary key or at least as part of the compound |
|
1177 |
* unique index of the table that will determine the row that will |
|
1178 |
* updated if it exists or inserted a new row otherwise. |
|
1179 |
* |
|
1180 |
* This function will fail if no key field is specified or if the |
|
1181 |
* value of a key field is set to null because fields that are |
|
1182 |
* part of unique index they may not be null. |
|
1183 |
* |
|
1184 |
* Default: 0 |
|
1185 |
* |
|
1186 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1187 |
*/ |
|
1188 |
function replace($table, $fields) |
|
1189 |
{ |
|
1190 |
$count = count($fields); |
|
1191 |
$query = $values = ''; |
|
1192 |
$keys = $colnum = 0; |
|
1193 |
for (reset($fields); $colnum < $count; next($fields), $colnum++) { |
|
1194 |
$name = key($fields); |
|
1195 |
if ($colnum > 0) { |
|
1196 |
$query .= ','; |
|
1197 |
$values.= ','; |
|
1198 |
} |
|
1199 |
$query.= $this->quoteIdentifier($name, true); |
|
1200 |
if (isset($fields[$name]['null']) && $fields[$name]['null']) { |
|
1201 |
$value = 'NULL'; |
|
1202 |
} else { |
|
1203 |
$type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null; |
|
1204 |
$value = $this->quote($fields[$name]['value'], $type); |
|
1205 |
if (PEAR::isError($value)) { |
|
1206 |
return $value; |
|
1207 |
} |
|
1208 |
} |
|
1209 |
$values.= $value; |
|
1210 |
if (isset($fields[$name]['key']) && $fields[$name]['key']) { |
|
1211 |
if ($value === 'NULL') { |
|
1212 |
return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null, |
|
1213 |
'key value '.$name.' may not be NULL', __FUNCTION__); |
|
1214 |
} |
|
1215 |
$keys++; |
|
1216 |
} |
|
1217 |
} |
|
1218 |
if ($keys == 0) { |
|
1219 |
return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null, |
|
1220 |
'not specified which fields are keys', __FUNCTION__); |
|
1221 |
} |
|
1222 |
|
|
1223 |
$connection = $this->getConnection(); |
|
1224 |
if (PEAR::isError($connection)) { |
|
1225 |
return $connection; |
|
1226 |
} |
|
1227 |
|
|
1228 |
$table = $this->quoteIdentifier($table, true); |
|
1229 |
$query = "REPLACE INTO $table ($query) VALUES ($values)"; |
|
1230 |
$result =& $this->_doQuery($query, true, $connection); |
|
1231 |
if (PEAR::isError($result)) { |
|
1232 |
return $result; |
|
1233 |
} |
|
1234 |
return $this->_affectedRows($connection, $result); |
|
1235 |
} |
|
1236 |
|
|
1237 |
// }}} |
|
1238 |
// {{{ nextID() |
|
1239 |
|
|
1240 |
/** |
|
1241 |
* Returns the next free id of a sequence |
|
1242 |
* |
|
1243 |
* @param string $seq_name name of the sequence |
|
1244 |
* @param boolean $ondemand when true the sequence is |
|
1245 |
* automatic created, if it |
|
1246 |
* not exists |
|
1247 |
* |
|
1248 |
* @return mixed MDB2 Error Object or id |
|
1249 |
* @access public |
|
1250 |
*/ |
|
1251 |
function nextID($seq_name, $ondemand = true) |
|
1252 |
{ |
|
1253 |
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); |
|
1254 |
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); |
|
1255 |
$query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)"; |
|
1256 |
$this->pushErrorHandling(PEAR_ERROR_RETURN); |
|
1257 |
$this->expectError(MDB2_ERROR_NOSUCHTABLE); |
|
1258 |
$result =& $this->_doQuery($query, true); |
|
1259 |
$this->popExpect(); |
|
1260 |
$this->popErrorHandling(); |
|
1261 |
if (PEAR::isError($result)) { |
|
1262 |
if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { |
|
1263 |
$this->loadModule('Manager', null, true); |
|
1264 |
$result = $this->manager->createSequence($seq_name); |
|
1265 |
if (PEAR::isError($result)) { |
|
1266 |
return $this->raiseError($result, null, null, |
|
1267 |
'on demand sequence '.$seq_name.' could not be created', __FUNCTION__); |
|
1268 |
} else { |
|
1269 |
return $this->nextID($seq_name, false); |
|
1270 |
} |
|
1271 |
} |
|
1272 |
return $result; |
|
1273 |
} |
|
1274 |
$value = $this->lastInsertID(); |
|
1275 |
if (is_numeric($value)) { |
|
1276 |
$query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; |
|
1277 |
$result =& $this->_doQuery($query, true); |
|
1278 |
if (PEAR::isError($result)) { |
|
1279 |
$this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; |
|
1280 |
} |
|
1281 |
} |
|
1282 |
return $value; |
|
1283 |
} |
|
1284 |
|
|
1285 |
// }}} |
|
1286 |
// {{{ lastInsertID() |
|
1287 |
|
|
1288 |
/** |
|
1289 |
* Returns the autoincrement ID if supported or $id or fetches the current |
|
1290 |
* ID in a sequence called: $table.(empty($field) ? '' : '_'.$field) |
|
1291 |
* |
|
1292 |
* @param string $table name of the table into which a new row was inserted |
|
1293 |
* @param string $field name of the field into which a new row was inserted |
|
1294 |
* @return mixed MDB2 Error Object or id |
|
1295 |
* @access public |
|
1296 |
*/ |
|
1297 |
function lastInsertID($table = null, $field = null) |
|
1298 |
{ |
|
1299 |
// not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051 |
|
1300 |
return $this->queryOne('SELECT LAST_INSERT_ID()', 'integer'); |
|
1301 |
} |
|
1302 |
|
|
1303 |
// }}} |
|
1304 |
// {{{ currID() |
|
1305 |
|
|
1306 |
/** |
|
1307 |
* Returns the current id of a sequence |
|
1308 |
* |
|
1309 |
* @param string $seq_name name of the sequence |
|
1310 |
* @return mixed MDB2 Error Object or id |
|
1311 |
* @access public |
|
1312 |
*/ |
|
1313 |
function currID($seq_name) |
|
1314 |
{ |
|
1315 |
$sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true); |
|
1316 |
$seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true); |
|
1317 |
$query = "SELECT MAX($seqcol_name) FROM $sequence_name"; |
|
1318 |
return $this->queryOne($query, 'integer'); |
|
1319 |
} |
|
1320 |
} |
|
1321 |
|
|
1322 |
/** |
|
1323 |
* MDB2 MySQL result driver |
|
1324 |
* |
|
1325 |
* @package MDB2 |
|
1326 |
* @category Database |
|
1327 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
1328 |
*/ |
|
1329 |
class MDB2_Result_mysql extends MDB2_Result_Common |
|
1330 |
{ |
|
1331 |
// }}} |
|
1332 |
// {{{ fetchRow() |
|
1333 |
|
|
1334 |
/** |
|
1335 |
* Fetch a row and insert the data into an existing array. |
|
1336 |
* |
|
1337 |
* @param int $fetchmode how the array data should be indexed |
|
1338 |
* @param int $rownum number of the row where the data can be found |
|
1339 |
* @return int data array on success, a MDB2 error on failure |
|
1340 |
* @access public |
|
1341 |
*/ |
|
1342 |
function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null) |
|
1343 |
{ |
|
1344 |
if (!is_null($rownum)) { |
|
1345 |
$seek = $this->seek($rownum); |
|
1346 |
if (PEAR::isError($seek)) { |
|
1347 |
return $seek; |
|
1348 |
} |
|
1349 |
} |
|
1350 |
if ($fetchmode == MDB2_FETCHMODE_DEFAULT) { |
|
1351 |
$fetchmode = $this->db->fetchmode; |
|
1352 |
} |
|
1353 |
if ($fetchmode & MDB2_FETCHMODE_ASSOC) { |
|
1354 |
$row = @mysql_fetch_assoc($this->result); |
|
1355 |
if (is_array($row) |
|
1356 |
&& $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE |
|
1357 |
) { |
|
1358 |
$row = array_change_key_case($row, $this->db->options['field_case']); |
|
1359 |
} |
|
1360 |
} else { |
|
1361 |
$row = @mysql_fetch_row($this->result); |
|
1362 |
} |
|
1363 |
|
|
1364 |
if (!$row) { |
|
1365 |
if ($this->result === false) { |
|
1366 |
$err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
1367 |
'resultset has already been freed', __FUNCTION__); |
|
1368 |
return $err; |
|
1369 |
} |
|
1370 |
$null = null; |
|
1371 |
return $null; |
|
1372 |
} |
|
1373 |
$mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL; |
|
1374 |
$rtrim = false; |
|
1375 |
if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) { |
|
1376 |
if (empty($this->types)) { |
|
1377 |
$mode += MDB2_PORTABILITY_RTRIM; |
|
1378 |
} else { |
|
1379 |
$rtrim = true; |
|
1380 |
} |
|
1381 |
} |
|
1382 |
if ($mode) { |
|
1383 |
$this->db->_fixResultArrayValues($row, $mode); |
|
1384 |
} |
|
1385 |
if (!empty($this->types)) { |
|
1386 |
$row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim); |
|
1387 |
} |
|
1388 |
if (!empty($this->values)) { |
|
1389 |
$this->_assignBindColumns($row); |
|
1390 |
} |
|
1391 |
if ($fetchmode === MDB2_FETCHMODE_OBJECT) { |
|
1392 |
$object_class = $this->db->options['fetch_class']; |
|
1393 |
if ($object_class == 'stdClass') { |
|
1394 |
$row = (object) $row; |
|
1395 |
} else { |
|
1396 |
$row = &new $object_class($row); |
|
1397 |
} |
|
1398 |
} |
|
1399 |
++$this->rownum; |
|
1400 |
return $row; |
|
1401 |
} |
|
1402 |
|
|
1403 |
// }}} |
|
1404 |
// {{{ _getColumnNames() |
|
1405 |
|
|
1406 |
/** |
|
1407 |
* Retrieve the names of columns returned by the DBMS in a query result. |
|
1408 |
* |
|
1409 |
* @return mixed Array variable that holds the names of columns as keys |
|
1410 |
* or an MDB2 error on failure. |
|
1411 |
* Some DBMS may not return any columns when the result set |
|
1412 |
* does not contain any rows. |
|
1413 |
* @access private |
|
1414 |
*/ |
|
1415 |
function _getColumnNames() |
|
1416 |
{ |
|
1417 |
$columns = array(); |
|
1418 |
$numcols = $this->numCols(); |
|
1419 |
if (PEAR::isError($numcols)) { |
|
1420 |
return $numcols; |
|
1421 |
} |
|
1422 |
for ($column = 0; $column < $numcols; $column++) { |
|
1423 |
$column_name = @mysql_field_name($this->result, $column); |
|
1424 |
$columns[$column_name] = $column; |
|
1425 |
} |
|
1426 |
if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
1427 |
$columns = array_change_key_case($columns, $this->db->options['field_case']); |
|
1428 |
} |
|
1429 |
return $columns; |
|
1430 |
} |
|
1431 |
|
|
1432 |
// }}} |
|
1433 |
// {{{ numCols() |
|
1434 |
|
|
1435 |
/** |
|
1436 |
* Count the number of columns returned by the DBMS in a query result. |
|
1437 |
* |
|
1438 |
* @return mixed integer value with the number of columns, a MDB2 error |
|
1439 |
* on failure |
|
1440 |
* @access public |
|
1441 |
*/ |
|
1442 |
function numCols() |
|
1443 |
{ |
|
1444 |
$cols = @mysql_num_fields($this->result); |
|
1445 |
if (is_null($cols)) { |
|
1446 |
if ($this->result === false) { |
|
1447 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
1448 |
'resultset has already been freed', __FUNCTION__); |
|
1449 |
} elseif (is_null($this->result)) { |
|
1450 |
return count($this->types); |
|
1451 |
} |
|
1452 |
return $this->db->raiseError(null, null, null, |
|
1453 |
'Could not get column count', __FUNCTION__); |
|
1454 |
} |
|
1455 |
return $cols; |
|
1456 |
} |
|
1457 |
|
|
1458 |
// }}} |
|
1459 |
// {{{ free() |
|
1460 |
|
|
1461 |
/** |
|
1462 |
* Free the internal resources associated with result. |
|
1463 |
* |
|
1464 |
* @return boolean true on success, false if result is invalid |
|
1465 |
* @access public |
|
1466 |
*/ |
|
1467 |
function free() |
|
1468 |
{ |
|
1469 |
if (is_resource($this->result) && $this->db->connection) { |
|
1470 |
$free = @mysql_free_result($this->result); |
|
1471 |
if ($free === false) { |
|
1472 |
return $this->db->raiseError(null, null, null, |
|
1473 |
'Could not free result', __FUNCTION__); |
|
1474 |
} |
|
1475 |
} |
|
1476 |
$this->result = false; |
|
1477 |
return MDB2_OK; |
|
1478 |
} |
|
1479 |
} |
|
1480 |
|
|
1481 |
/** |
|
1482 |
* MDB2 MySQL buffered result driver |
|
1483 |
* |
|
1484 |
* @package MDB2 |
|
1485 |
* @category Database |
|
1486 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
1487 |
*/ |
|
1488 |
class MDB2_BufferedResult_mysql extends MDB2_Result_mysql |
|
1489 |
{ |
|
1490 |
// }}} |
|
1491 |
// {{{ seek() |
|
1492 |
|
|
1493 |
/** |
|
1494 |
* Seek to a specific row in a result set |
|
1495 |
* |
|
1496 |
* @param int $rownum number of the row where the data can be found |
|
1497 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1498 |
* @access public |
|
1499 |
*/ |
|
1500 |
function seek($rownum = 0) |
|
1501 |
{ |
|
1502 |
if ($this->rownum != ($rownum - 1) && !@mysql_data_seek($this->result, $rownum)) { |
|
1503 |
if ($this->result === false) { |
|
1504 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
1505 |
'resultset has already been freed', __FUNCTION__); |
|
1506 |
} elseif (is_null($this->result)) { |
|
1507 |
return MDB2_OK; |
|
1508 |
} |
|
1509 |
return $this->db->raiseError(MDB2_ERROR_INVALID, null, null, |
|
1510 |
'tried to seek to an invalid row number ('.$rownum.')', __FUNCTION__); |
|
1511 |
} |
|
1512 |
$this->rownum = $rownum - 1; |
|
1513 |
return MDB2_OK; |
|
1514 |
} |
|
1515 |
|
|
1516 |
// }}} |
|
1517 |
// {{{ valid() |
|
1518 |
|
|
1519 |
/** |
|
1520 |
* Check if the end of the result set has been reached |
|
1521 |
* |
|
1522 |
* @return mixed true or false on sucess, a MDB2 error on failure |
|
1523 |
* @access public |
|
1524 |
*/ |
|
1525 |
function valid() |
|
1526 |
{ |
|
1527 |
$numrows = $this->numRows(); |
|
1528 |
if (PEAR::isError($numrows)) { |
|
1529 |
return $numrows; |
|
1530 |
} |
|
1531 |
return $this->rownum < ($numrows - 1); |
|
1532 |
} |
|
1533 |
|
|
1534 |
// }}} |
|
1535 |
// {{{ numRows() |
|
1536 |
|
|
1537 |
/** |
|
1538 |
* Returns the number of rows in a result object |
|
1539 |
* |
|
1540 |
* @return mixed MDB2 Error Object or the number of rows |
|
1541 |
* @access public |
|
1542 |
*/ |
|
1543 |
function numRows() |
|
1544 |
{ |
|
1545 |
$rows = @mysql_num_rows($this->result); |
|
1546 |
if (false === $rows) { |
|
1547 |
if (false === $this->result) { |
|
1548 |
return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
1549 |
'resultset has already been freed', __FUNCTION__); |
|
1550 |
} elseif (is_null($this->result)) { |
|
1551 |
return 0; |
|
1552 |
} |
|
1553 |
return $this->db->raiseError(null, null, null, |
|
1554 |
'Could not get row count', __FUNCTION__); |
|
1555 |
} |
|
1556 |
return $rows; |
|
1557 |
} |
|
1558 |
} |
|
1559 |
|
|
1560 |
/** |
|
1561 |
* MDB2 MySQL statement driver |
|
1562 |
* |
|
1563 |
* @package MDB2 |
|
1564 |
* @category Database |
|
1565 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
1566 |
*/ |
|
1567 |
class MDB2_Statement_mysql extends MDB2_Statement_Common |
|
1568 |
{ |
|
1569 |
// {{{ _execute() |
|
1570 |
|
|
1571 |
/** |
|
1572 |
* Execute a prepared query statement helper method. |
|
1573 |
* |
|
1574 |
* @param mixed $result_class string which specifies which result class to use |
|
1575 |
* @param mixed $result_wrap_class string which specifies which class to wrap results in |
|
1576 |
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure |
|
1577 |
* @access private |
|
1578 |
*/ |
|
1579 |
function &_execute($result_class = true, $result_wrap_class = false) |
|
1580 |
{ |
|
1581 |
if (is_null($this->statement)) { |
|
1582 |
$result =& parent::_execute($result_class, $result_wrap_class); |
|
1583 |
return $result; |
|
1584 |
} |
|
1585 |
$this->db->last_query = $this->query; |
|
1586 |
$this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values)); |
|
1587 |
if ($this->db->getOption('disable_query')) { |
|
1588 |
$result = $this->is_manip ? 0 : null; |
|
1589 |
return $result; |
|
1590 |
} |
|
1591 |
|
|
1592 |
$connection = $this->db->getConnection(); |
|
1593 |
if (PEAR::isError($connection)) { |
|
1594 |
return $connection; |
|
1595 |
} |
|
1596 |
|
|
1597 |
$query = 'EXECUTE '.$this->statement; |
|
1598 |
if (!empty($this->positions)) { |
|
1599 |
$parameters = array(); |
|
1600 |
foreach ($this->positions as $parameter) { |
|
1601 |
if (!array_key_exists($parameter, $this->values)) { |
|
1602 |
return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
1603 |
'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__); |
|
1604 |
} |
|
1605 |
$value = $this->values[$parameter]; |
|
1606 |
$type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null; |
|
1607 |
if (is_resource($value) || $type == 'clob' || $type == 'blob' && $this->db->options['lob_allow_url_include']) { |
|
1608 |
if (!is_resource($value) && preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) { |
|
1609 |
if ($match[1] == 'file://') { |
|
1610 |
$value = $match[2]; |
|
1611 |
} |
|
1612 |
$value = @fopen($value, 'r'); |
|
1613 |
$close = true; |
|
1614 |
} |
|
1615 |
if (is_resource($value)) { |
|
1616 |
$data = ''; |
|
1617 |
while (!@feof($value)) { |
|
1618 |
$data.= @fread($value, $this->db->options['lob_buffer_length']); |
|
1619 |
} |
|
1620 |
if ($close) { |
|
1621 |
@fclose($value); |
|
1622 |
} |
|
1623 |
$value = $data; |
|
1624 |
} |
|
1625 |
} |
|
1626 |
$quoted = $this->db->quote($value, $type); |
|
1627 |
if (PEAR::isError($quoted)) { |
|
1628 |
return $quoted; |
|
1629 |
} |
|
1630 |
$param_query = 'SET @'.$parameter.' = '.$quoted; |
|
1631 |
$result = $this->db->_doQuery($param_query, true, $connection); |
|
1632 |
if (PEAR::isError($result)) { |
|
1633 |
return $result; |
|
1634 |
} |
|
1635 |
} |
|
1636 |
$query.= ' USING @'.implode(', @', array_values($this->positions)); |
|
1637 |
} |
|
1638 |
|
|
1639 |
$result = $this->db->_doQuery($query, $this->is_manip, $connection); |
|
1640 |
if (PEAR::isError($result)) { |
|
1641 |
return $result; |
|
1642 |
} |
|
1643 |
|
|
1644 |
if ($this->is_manip) { |
|
1645 |
$affected_rows = $this->db->_affectedRows($connection, $result); |
|
1646 |
return $affected_rows; |
|
1647 |
} |
|
1648 |
|
|
1649 |
$result =& $this->db->_wrapResult($result, $this->result_types, |
|
1650 |
$result_class, $result_wrap_class, $this->limit, $this->offset); |
|
1651 |
$this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result)); |
|
1652 |
return $result; |
|
1653 |
} |
|
1654 |
|
|
1655 |
// }}} |
|
1656 |
// {{{ free() |
|
1657 |
|
|
1658 |
/** |
|
1659 |
* Release resources allocated for the specified prepared query. |
|
1660 |
* |
|
1661 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1662 |
* @access public |
|
1663 |
*/ |
|
1664 |
function free() |
|
1665 |
{ |
|
1666 |
if (is_null($this->positions)) { |
|
1667 |
return $this->db->raiseError(MDB2_ERROR, null, null, |
|
1668 |
'Prepared statement has already been freed', __FUNCTION__); |
|
1669 |
} |
|
1670 |
$result = MDB2_OK; |
|
1671 |
|
|
1672 |
if (!is_null($this->statement)) { |
|
1673 |
$connection = $this->db->getConnection(); |
|
1674 |
if (PEAR::isError($connection)) { |
|
1675 |
return $connection; |
|
1676 |
} |
|
1677 |
$query = 'DEALLOCATE PREPARE '.$this->statement; |
|
1678 |
$result = $this->db->_doQuery($query, true, $connection); |
|
1679 |
} |
|
1680 |
|
|
1681 |
parent::free(); |
|
1682 |
return $result; |
|
1683 |
} |
|
1684 |
} |
95ebbc
|
1685 |
?> |