Adapter
5 years ago
Profiler
6 years ago
Select
6 years ago
Statement
5 years ago
Table
6 years ago
Exception.php
6 years ago
Expr.php
6 years ago
Profiler.php
6 years ago
Select.php
6 years ago
Statement.php
6 years ago
Table.php
6 years ago
Statement.php
486 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Zend Framework |
| 4 | * |
| 5 | * LICENSE |
| 6 | * |
| 7 | * This source file is subject to the new BSD license that is bundled |
| 8 | * with this package in the file LICENSE.txt. |
| 9 | * It is also available through the world-wide-web at this URL: |
| 10 | * http://framework.zend.com/license/new-bsd |
| 11 | * If you did not receive a copy of the license and are unable to |
| 12 | * obtain it through the world-wide-web, please send an email |
| 13 | * to license@zend.com so we can send you a copy immediately. |
| 14 | * |
| 15 | * @category Zend |
| 16 | * @package Zend_Db |
| 17 | * @subpackage Statement |
| 18 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 19 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 20 | * @version $Id: Statement.php 23775 2011-03-01 17:25:24Z ralph $ |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * @see Zend_Db |
| 25 | */ |
| 26 | // require_once 'Zend/Db.php'; |
| 27 | |
| 28 | /** |
| 29 | * @see Zend_Db_Statement_Interface |
| 30 | */ |
| 31 | // require_once 'Zend/Db/Statement/Interface.php'; |
| 32 | |
| 33 | /** |
| 34 | * Abstract class to emulate a PDOStatement for native database adapters. |
| 35 | * |
| 36 | * @category Zend |
| 37 | * @package Zend_Db |
| 38 | * @subpackage Statement |
| 39 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 40 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 41 | */ |
| 42 | abstract class Zend_Db_Statement implements Zend_Db_Statement_Interface |
| 43 | { |
| 44 | |
| 45 | /** |
| 46 | * @var resource|object The driver level statement object/resource |
| 47 | */ |
| 48 | protected $_stmt = null; |
| 49 | |
| 50 | /** |
| 51 | * @var Zend_Db_Adapter_Abstract |
| 52 | */ |
| 53 | protected $_adapter = null; |
| 54 | |
| 55 | /** |
| 56 | * The current fetch mode. |
| 57 | * |
| 58 | * @var integer |
| 59 | */ |
| 60 | protected $_fetchMode = Zend_Db::FETCH_ASSOC; |
| 61 | |
| 62 | /** |
| 63 | * Attributes. |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $_attribute = array(); |
| 68 | |
| 69 | /** |
| 70 | * Column result bindings. |
| 71 | * |
| 72 | * @var array |
| 73 | */ |
| 74 | protected $_bindColumn = array(); |
| 75 | |
| 76 | /** |
| 77 | * Query parameter bindings; covers bindParam() and bindValue(). |
| 78 | * |
| 79 | * @var array |
| 80 | */ |
| 81 | protected $_bindParam = array(); |
| 82 | |
| 83 | /** |
| 84 | * SQL string split into an array at placeholders. |
| 85 | * |
| 86 | * @var array |
| 87 | */ |
| 88 | protected $_sqlSplit = array(); |
| 89 | |
| 90 | /** |
| 91 | * Parameter placeholders in the SQL string by position in the split array. |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | protected $_sqlParam = array(); |
| 96 | |
| 97 | /** |
| 98 | * @var Zend_Db_Profiler_Query |
| 99 | */ |
| 100 | protected $_queryId = null; |
| 101 | |
| 102 | /** |
| 103 | * Constructor for a statement. |
| 104 | * |
| 105 | * @param Zend_Db_Adapter_Abstract $adapter |
| 106 | * @param mixed $sql Either a string or Zend_Db_Select. |
| 107 | */ |
| 108 | public function __construct($adapter, $sql) |
| 109 | { |
| 110 | $this->_adapter = $adapter; |
| 111 | if ($sql instanceof Zend_Db_Select) { |
| 112 | $sql = $sql->assemble(); |
| 113 | } |
| 114 | $this->_parseParameters($sql); |
| 115 | $this->_prepare($sql); |
| 116 | |
| 117 | $this->_queryId = $this->_adapter->getProfiler()->queryStart($sql); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Internal method called by abstract statment constructor to setup |
| 122 | * the driver level statement |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | protected function _prepare($sql) |
| 127 | { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @param string $sql |
| 133 | * @return void |
| 134 | */ |
| 135 | protected function _parseParameters($sql) |
| 136 | { |
| 137 | $sql = $this->_stripQuoted($sql); |
| 138 | |
| 139 | // split into text and params |
| 140 | $this->_sqlSplit = preg_split('/(\?|\:[a-zA-Z0-9_]+)/', |
| 141 | $sql, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
| 142 | |
| 143 | // map params |
| 144 | $this->_sqlParam = array(); |
| 145 | foreach ($this->_sqlSplit as $key => $val) { |
| 146 | if ($val == '?') { |
| 147 | if ($this->_adapter->supportsParameters('positional') === false) { |
| 148 | /** |
| 149 | * @see Zend_Db_Statement_Exception |
| 150 | */ |
| 151 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 152 | throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$val'"); |
| 153 | } |
| 154 | } else if ($val[0] == ':') { |
| 155 | if ($this->_adapter->supportsParameters('named') === false) { |
| 156 | /** |
| 157 | * @see Zend_Db_Statement_Exception |
| 158 | */ |
| 159 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 160 | throw new Zend_Db_Statement_Exception("Invalid bind-variable name '$val'"); |
| 161 | } |
| 162 | } |
| 163 | $this->_sqlParam[] = $val; |
| 164 | } |
| 165 | |
| 166 | // set up for binding |
| 167 | $this->_bindParam = array(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Remove parts of a SQL string that contain quoted strings |
| 172 | * of values or identifiers. |
| 173 | * |
| 174 | * @param string $sql |
| 175 | * @return string |
| 176 | */ |
| 177 | protected function _stripQuoted($sql) |
| 178 | { |
| 179 | // get the character for delimited id quotes, |
| 180 | // this is usually " but in MySQL is ` |
| 181 | $d = $this->_adapter->quoteIdentifier('a'); |
| 182 | $d = $d[0]; |
| 183 | |
| 184 | // get the value used as an escaped delimited id quote, |
| 185 | // e.g. \" or "" or \` |
| 186 | $de = $this->_adapter->quoteIdentifier($d); |
| 187 | $de = substr($de, 1, 2); |
| 188 | $de = str_replace('\\', '\\\\', $de); |
| 189 | |
| 190 | // get the character for value quoting |
| 191 | // this should be ' |
| 192 | $q = $this->_adapter->quote('a'); |
| 193 | $q = $q[0]; |
| 194 | |
| 195 | // get the value used as an escaped quote, |
| 196 | // e.g. \' or '' |
| 197 | $qe = $this->_adapter->quote($q); |
| 198 | $qe = substr($qe, 1, 2); |
| 199 | $qe = str_replace('\\', '\\\\', $qe); |
| 200 | |
| 201 | // get a version of the SQL statement with all quoted |
| 202 | // values and delimited identifiers stripped out |
| 203 | // remove "foo\"bar" |
| 204 | $sql = preg_replace("/$q($qe|\\\\{2}|[^$q])*$q/", '', $sql); |
| 205 | // remove 'foo\'bar' |
| 206 | if (!empty($q)) { |
| 207 | $sql = preg_replace("/$q($qe|[^$q])*$q/", '', $sql); |
| 208 | } |
| 209 | |
| 210 | return $sql; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Bind a column of the statement result set to a PHP variable. |
| 215 | * |
| 216 | * @param string $column Name the column in the result set, either by |
| 217 | * position or by name. |
| 218 | * @param mixed $param Reference to the PHP variable containing the value. |
| 219 | * @param mixed $type OPTIONAL |
| 220 | * @return bool |
| 221 | */ |
| 222 | public function bindColumn($column, &$param, $type = null) |
| 223 | { |
| 224 | $this->_bindColumn[$column] =& $param; |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Binds a parameter to the specified variable name. |
| 230 | * |
| 231 | * @param mixed $parameter Name the parameter, either integer or string. |
| 232 | * @param mixed $variable Reference to PHP variable containing the value. |
| 233 | * @param mixed $type OPTIONAL Datatype of SQL parameter. |
| 234 | * @param mixed $length OPTIONAL Length of SQL parameter. |
| 235 | * @param mixed $options OPTIONAL Other options. |
| 236 | * @return bool |
| 237 | */ |
| 238 | public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
| 239 | { |
| 240 | if (!is_int($parameter) && !is_string($parameter)) { |
| 241 | /** |
| 242 | * @see Zend_Db_Statement_Exception |
| 243 | */ |
| 244 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 245 | throw new Zend_Db_Statement_Exception('Invalid bind-variable position'); |
| 246 | } |
| 247 | |
| 248 | $position = null; |
| 249 | if (($intval = (int) $parameter) > 0 && $this->_adapter->supportsParameters('positional')) { |
| 250 | if ($intval >= 1 || $intval <= count($this->_sqlParam)) { |
| 251 | $position = $intval; |
| 252 | } |
| 253 | } else if ($this->_adapter->supportsParameters('named')) { |
| 254 | if ($parameter[0] != ':') { |
| 255 | $parameter = ':' . $parameter; |
| 256 | } |
| 257 | if (in_array($parameter, $this->_sqlParam) !== false) { |
| 258 | $position = $parameter; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if ($position === null) { |
| 263 | /** |
| 264 | * @see Zend_Db_Statement_Exception |
| 265 | */ |
| 266 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 267 | throw new Zend_Db_Statement_Exception("Invalid bind-variable position '$parameter'"); |
| 268 | } |
| 269 | |
| 270 | // Finally we are assured that $position is valid |
| 271 | $this->_bindParam[$position] =& $variable; |
| 272 | return $this->_bindParam($position, $variable, $type, $length, $options); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Binds a value to a parameter. |
| 277 | * |
| 278 | * @param mixed $parameter Name the parameter, either integer or string. |
| 279 | * @param mixed $value Scalar value to bind to the parameter. |
| 280 | * @param mixed $type OPTIONAL Datatype of the parameter. |
| 281 | * @return bool |
| 282 | */ |
| 283 | public function bindValue($parameter, $value, $type = null) |
| 284 | { |
| 285 | return $this->bindParam($parameter, $value, $type); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Executes a prepared statement. |
| 290 | * |
| 291 | * @param array $params OPTIONAL Values to bind to parameter placeholders. |
| 292 | * @return bool |
| 293 | */ |
| 294 | public function execute(array $params = null) |
| 295 | { |
| 296 | /* |
| 297 | * Simple case - no query profiler to manage. |
| 298 | */ |
| 299 | if ($this->_queryId === null) { |
| 300 | return $this->_execute($params); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Do the same thing, but with query profiler |
| 305 | * management before and after the execute. |
| 306 | */ |
| 307 | $prof = $this->_adapter->getProfiler(); |
| 308 | $qp = $prof->getQueryProfile($this->_queryId); |
| 309 | if ($qp->hasEnded()) { |
| 310 | $this->_queryId = $prof->queryClone($qp); |
| 311 | $qp = $prof->getQueryProfile($this->_queryId); |
| 312 | } |
| 313 | if ($params !== null) { |
| 314 | $qp->bindParams($params); |
| 315 | } else { |
| 316 | $qp->bindParams($this->_bindParam); |
| 317 | } |
| 318 | $qp->start($this->_queryId); |
| 319 | |
| 320 | $retval = $this->_execute($params); |
| 321 | |
| 322 | $prof->queryEnd($this->_queryId); |
| 323 | |
| 324 | return $retval; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns an array containing all of the result set rows. |
| 329 | * |
| 330 | * @param int $style OPTIONAL Fetch mode. |
| 331 | * @param int $col OPTIONAL Column number, if fetch mode is by column. |
| 332 | * @return array Collection of rows, each in a format by the fetch mode. |
| 333 | */ |
| 334 | public function fetchAll($style = null, $col = null) |
| 335 | { |
| 336 | $data = array(); |
| 337 | if ($style === Zend_Db::FETCH_COLUMN && $col === null) { |
| 338 | $col = 0; |
| 339 | } |
| 340 | if ($col === null) { |
| 341 | while ($row = $this->fetch($style)) { |
| 342 | $data[] = $row; |
| 343 | } |
| 344 | } else { |
| 345 | while (false !== ($val = $this->fetchColumn($col))) { |
| 346 | $data[] = $val; |
| 347 | } |
| 348 | } |
| 349 | return $data; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Returns a single column from the next row of a result set. |
| 354 | * |
| 355 | * @param int $col OPTIONAL Position of the column to fetch. |
| 356 | * @return string One value from the next row of result set, or false. |
| 357 | */ |
| 358 | public function fetchColumn($col = 0) |
| 359 | { |
| 360 | $data = array(); |
| 361 | $col = (int) $col; |
| 362 | $row = $this->fetch(Zend_Db::FETCH_NUM); |
| 363 | if (!is_array($row)) { |
| 364 | return false; |
| 365 | } |
| 366 | return $row[$col]; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Fetches the next row and returns it as an object. |
| 371 | * |
| 372 | * @param string $class OPTIONAL Name of the class to create. |
| 373 | * @param array $config OPTIONAL Constructor arguments for the class. |
| 374 | * @return mixed One object instance of the specified class, or false. |
| 375 | */ |
| 376 | public function fetchObject($class = 'stdClass', array $config = array()) |
| 377 | { |
| 378 | $obj = new $class($config); |
| 379 | $row = $this->fetch(Zend_Db::FETCH_ASSOC); |
| 380 | if (!is_array($row)) { |
| 381 | return false; |
| 382 | } |
| 383 | foreach ($row as $key => $val) { |
| 384 | $obj->$key = $val; |
| 385 | } |
| 386 | return $obj; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * Retrieve a statement attribute. |
| 391 | * |
| 392 | * @param string $key Attribute name. |
| 393 | * @return mixed Attribute value. |
| 394 | */ |
| 395 | public function getAttribute($key) |
| 396 | { |
| 397 | if (array_key_exists($key, $this->_attribute)) { |
| 398 | return $this->_attribute[$key]; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Set a statement attribute. |
| 404 | * |
| 405 | * @param string $key Attribute name. |
| 406 | * @param mixed $val Attribute value. |
| 407 | * @return bool |
| 408 | */ |
| 409 | public function setAttribute($key, $val) |
| 410 | { |
| 411 | $this->_attribute[$key] = $val; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Set the default fetch mode for this statement. |
| 416 | * |
| 417 | * @param int $mode The fetch mode. |
| 418 | * @return bool |
| 419 | * @throws Zend_Db_Statement_Exception |
| 420 | */ |
| 421 | public function setFetchMode($mode) |
| 422 | { |
| 423 | switch ($mode) { |
| 424 | case Zend_Db::FETCH_NUM: |
| 425 | case Zend_Db::FETCH_ASSOC: |
| 426 | case Zend_Db::FETCH_BOTH: |
| 427 | case Zend_Db::FETCH_OBJ: |
| 428 | $this->_fetchMode = $mode; |
| 429 | break; |
| 430 | case Zend_Db::FETCH_BOUND: |
| 431 | default: |
| 432 | $this->closeCursor(); |
| 433 | /** |
| 434 | * @see Zend_Db_Statement_Exception |
| 435 | */ |
| 436 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 437 | throw new Zend_Db_Statement_Exception('invalid fetch mode'); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Helper function to map retrieved row |
| 444 | * to bound column variables |
| 445 | * |
| 446 | * @param array $row |
| 447 | * @return bool True |
| 448 | */ |
| 449 | public function _fetchBound($row) |
| 450 | { |
| 451 | foreach ($row as $key => $value) { |
| 452 | // bindColumn() takes 1-based integer positions |
| 453 | // but fetch() returns 0-based integer indexes |
| 454 | if (is_int($key)) { |
| 455 | $key++; |
| 456 | } |
| 457 | // set results only to variables that were bound previously |
| 458 | if (isset($this->_bindColumn[$key])) { |
| 459 | $this->_bindColumn[$key] = $value; |
| 460 | } |
| 461 | } |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Gets the Zend_Db_Adapter_Abstract for this |
| 467 | * particular Zend_Db_Statement object. |
| 468 | * |
| 469 | * @return Zend_Db_Adapter_Abstract |
| 470 | */ |
| 471 | public function getAdapter() |
| 472 | { |
| 473 | return $this->_adapter; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Gets the resource or object setup by the |
| 478 | * _parse |
| 479 | * @return unknown_type |
| 480 | */ |
| 481 | public function getDriverStatement() |
| 482 | { |
| 483 | return $this->_stmt; |
| 484 | } |
| 485 | } |
| 486 |