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