Mysqli
2 years ago
Exception.php
2 years ago
Interface.php
2 years ago
Mysqli.php
1 year ago
Pdo.php
1 year ago
Pdo.php
418 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: Pdo.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Statement |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Statement.php'; |
| 28 | /** |
| 29 | * Proxy class to wrap a PDOStatement object. |
| 30 | * Matches the interface of PDOStatement. All methods simply proxy to the |
| 31 | * matching method in PDOStatement. PDOExceptions thrown by PDOStatement |
| 32 | * are re-thrown as Zend_Db_Statement_Exception. |
| 33 | * |
| 34 | * @category Zend |
| 35 | * @package Zend_Db |
| 36 | * @subpackage Statement |
| 37 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 38 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 39 | */ |
| 40 | class Zend_Db_Statement_Pdo extends \Zend_Db_Statement implements \IteratorAggregate |
| 41 | { |
| 42 | /** |
| 43 | * @var int |
| 44 | */ |
| 45 | protected $_fetchMode = \PDO::FETCH_ASSOC; |
| 46 | /** |
| 47 | * Prepare a string SQL statement and create a statement object. |
| 48 | * |
| 49 | * @param string $sql |
| 50 | * @return void |
| 51 | * @throws Zend_Db_Statement_Exception |
| 52 | */ |
| 53 | protected function _prepare($sql) |
| 54 | { |
| 55 | try { |
| 56 | $this->_stmt = $this->_adapter->getConnection()->prepare($sql); |
| 57 | } catch (\PDOException $e) { |
| 58 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 59 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 60 | } |
| 61 | } |
| 62 | /** |
| 63 | * Bind a column of the statement result set to a PHP variable. |
| 64 | * |
| 65 | * @param string $column Name the column in the result set, either by |
| 66 | * position or by name. |
| 67 | * @param mixed $param Reference to the PHP variable containing the value. |
| 68 | * @param mixed $type OPTIONAL |
| 69 | * @return bool |
| 70 | * @throws Zend_Db_Statement_Exception |
| 71 | */ |
| 72 | public function bindColumn($column, &$param, $type = null) |
| 73 | { |
| 74 | try { |
| 75 | if ($type === null) { |
| 76 | return $this->_stmt->bindColumn($column, $param); |
| 77 | } else { |
| 78 | return $this->_stmt->bindColumn($column, $param, $type); |
| 79 | } |
| 80 | } catch (\PDOException $e) { |
| 81 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 82 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 83 | } |
| 84 | } |
| 85 | /** |
| 86 | * Binds a parameter to the specified variable name. |
| 87 | * |
| 88 | * @param mixed $parameter Name the parameter, either integer or string. |
| 89 | * @param mixed $variable Reference to PHP variable containing the value. |
| 90 | * @param mixed $type OPTIONAL Datatype of SQL parameter. |
| 91 | * @param mixed $length OPTIONAL Length of SQL parameter. |
| 92 | * @param mixed $options OPTIONAL Other options. |
| 93 | * @return bool |
| 94 | * @throws Zend_Db_Statement_Exception |
| 95 | */ |
| 96 | protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
| 97 | { |
| 98 | try { |
| 99 | if ($type === null) { |
| 100 | if (\is_bool($variable)) { |
| 101 | $type = \PDO::PARAM_BOOL; |
| 102 | } elseif ($variable === null) { |
| 103 | $type = \PDO::PARAM_NULL; |
| 104 | } elseif (\is_integer($variable)) { |
| 105 | $type = \PDO::PARAM_INT; |
| 106 | } else { |
| 107 | $type = \PDO::PARAM_STR; |
| 108 | } |
| 109 | } |
| 110 | return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options); |
| 111 | } catch (\PDOException $e) { |
| 112 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 113 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 114 | } |
| 115 | } |
| 116 | /** |
| 117 | * Binds a value to a parameter. |
| 118 | * |
| 119 | * @param mixed $parameter Name the parameter, either integer or string. |
| 120 | * @param mixed $value Scalar value to bind to the parameter. |
| 121 | * @param mixed $type OPTIONAL Datatype of the parameter. |
| 122 | * @return bool |
| 123 | * @throws Zend_Db_Statement_Exception |
| 124 | */ |
| 125 | public function bindValue($parameter, $value, $type = null) |
| 126 | { |
| 127 | if (\is_string($parameter) && $parameter[0] != ':') { |
| 128 | $parameter = ":{$parameter}"; |
| 129 | } |
| 130 | $this->_bindParam[$parameter] = $value; |
| 131 | try { |
| 132 | if ($type === null) { |
| 133 | return $this->_stmt->bindValue($parameter, $value); |
| 134 | } else { |
| 135 | return $this->_stmt->bindValue($parameter, $value, $type); |
| 136 | } |
| 137 | } catch (\PDOException $e) { |
| 138 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 139 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 140 | } |
| 141 | } |
| 142 | /** |
| 143 | * Closes the cursor, allowing the statement to be executed again. |
| 144 | * |
| 145 | * @return bool |
| 146 | * @throws Zend_Db_Statement_Exception |
| 147 | */ |
| 148 | public function closeCursor() |
| 149 | { |
| 150 | try { |
| 151 | return $this->_stmt->closeCursor(); |
| 152 | } catch (\PDOException $e) { |
| 153 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 154 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 155 | } |
| 156 | } |
| 157 | /** |
| 158 | * Returns the number of columns in the result set. |
| 159 | * Returns null if the statement has no result set metadata. |
| 160 | * |
| 161 | * @return int The number of columns. |
| 162 | * @throws Zend_Db_Statement_Exception |
| 163 | */ |
| 164 | public function columnCount() |
| 165 | { |
| 166 | try { |
| 167 | return $this->_stmt->columnCount(); |
| 168 | } catch (\PDOException $e) { |
| 169 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 170 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 171 | } |
| 172 | } |
| 173 | /** |
| 174 | * Retrieves the error code, if any, associated with the last operation on |
| 175 | * the statement handle. |
| 176 | * |
| 177 | * @return string error code. |
| 178 | * @throws Zend_Db_Statement_Exception |
| 179 | */ |
| 180 | public function errorCode() |
| 181 | { |
| 182 | try { |
| 183 | return $this->_stmt->errorCode(); |
| 184 | } catch (\PDOException $e) { |
| 185 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 186 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 187 | } |
| 188 | } |
| 189 | /** |
| 190 | * Retrieves an array of error information, if any, associated with the |
| 191 | * last operation on the statement handle. |
| 192 | * |
| 193 | * @return array |
| 194 | * @throws Zend_Db_Statement_Exception |
| 195 | */ |
| 196 | public function errorInfo() |
| 197 | { |
| 198 | try { |
| 199 | return $this->_stmt->errorInfo(); |
| 200 | } catch (\PDOException $e) { |
| 201 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 202 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 203 | } |
| 204 | } |
| 205 | /** |
| 206 | * Executes a prepared statement. |
| 207 | * |
| 208 | * @param array $params OPTIONAL Values to bind to parameter placeholders. |
| 209 | * @return bool |
| 210 | * @throws Zend_Db_Statement_Exception |
| 211 | */ |
| 212 | public function _execute(?array $params = null) |
| 213 | { |
| 214 | try { |
| 215 | if ($params !== null) { |
| 216 | foreach (\array_values($params) as $index => $paramValue) { |
| 217 | $this->_stmt->bindValue($index + 1, $paramValue, $paramValue === null ? \PDO::PARAM_NULL : \PDO::PARAM_STR); |
| 218 | } |
| 219 | } |
| 220 | return $this->_stmt->execute(); |
| 221 | } catch (\PDOException $e) { |
| 222 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 223 | throw new \Zend_Db_Statement_Exception($e->getMessage(), (int) $e->getCode(), $e); |
| 224 | } |
| 225 | } |
| 226 | /** |
| 227 | * Fetches a row from the result set. |
| 228 | * |
| 229 | * @param int $style OPTIONAL Fetch mode for this fetch operation. |
| 230 | * @param int $cursor OPTIONAL Absolute, relative, or other. |
| 231 | * @param int $offset OPTIONAL Number for absolute or relative cursors. |
| 232 | * @return mixed Array, object, or scalar depending on fetch mode. |
| 233 | * @throws Zend_Db_Statement_Exception |
| 234 | */ |
| 235 | public function fetch($style = null, $cursor = \PDO::FETCH_ORI_NEXT, $offset = 0) |
| 236 | { |
| 237 | if ($style === null) { |
| 238 | $style = $this->_fetchMode; |
| 239 | } |
| 240 | try { |
| 241 | return $this->_stmt->fetch($style, $cursor, $offset); |
| 242 | } catch (\PDOException $e) { |
| 243 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 244 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 245 | } |
| 246 | } |
| 247 | /** |
| 248 | * Required by IteratorAggregate interface |
| 249 | * |
| 250 | * @return IteratorIterator |
| 251 | */ |
| 252 | public function getIterator() : \IteratorIterator |
| 253 | { |
| 254 | return new \IteratorIterator($this->_stmt); |
| 255 | } |
| 256 | /** |
| 257 | * Returns an array containing all of the result set rows. |
| 258 | * |
| 259 | * @param int $style OPTIONAL Fetch mode. |
| 260 | * @param int $col OPTIONAL Column number, if fetch mode is by column. |
| 261 | * @return array Collection of rows, each in a format by the fetch mode. |
| 262 | * @throws Zend_Db_Statement_Exception |
| 263 | */ |
| 264 | public function fetchAll($style = null, $col = null) |
| 265 | { |
| 266 | if ($style === null) { |
| 267 | $style = $this->_fetchMode; |
| 268 | } |
| 269 | try { |
| 270 | if ($style == \PDO::FETCH_COLUMN) { |
| 271 | if ($col === null) { |
| 272 | $col = 0; |
| 273 | } |
| 274 | return $this->_stmt->fetchAll($style, $col); |
| 275 | } else { |
| 276 | return $this->_stmt->fetchAll($style); |
| 277 | } |
| 278 | } catch (\PDOException $e) { |
| 279 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 280 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 281 | } |
| 282 | } |
| 283 | /** |
| 284 | * Returns a single column from the next row of a result set. |
| 285 | * |
| 286 | * @param int $col OPTIONAL Position of the column to fetch. |
| 287 | * @return string |
| 288 | * @throws Zend_Db_Statement_Exception |
| 289 | */ |
| 290 | public function fetchColumn($col = 0) |
| 291 | { |
| 292 | try { |
| 293 | return $this->_stmt->fetchColumn($col); |
| 294 | } catch (\PDOException $e) { |
| 295 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 296 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 297 | } |
| 298 | } |
| 299 | /** |
| 300 | * Fetches the next row and returns it as an object. |
| 301 | * |
| 302 | * @param string $class OPTIONAL Name of the class to create. |
| 303 | * @param array $config OPTIONAL Constructor arguments for the class. |
| 304 | * @return mixed One object instance of the specified class. |
| 305 | * @throws Zend_Db_Statement_Exception |
| 306 | */ |
| 307 | public function fetchObject($class = 'stdClass', array $config = array()) |
| 308 | { |
| 309 | try { |
| 310 | return $this->_stmt->fetchObject($class, $config); |
| 311 | } catch (\PDOException $e) { |
| 312 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 313 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 314 | } |
| 315 | } |
| 316 | /** |
| 317 | * Retrieve a statement attribute. |
| 318 | * |
| 319 | * @param integer $key Attribute name. |
| 320 | * @return mixed Attribute value. |
| 321 | * @throws Zend_Db_Statement_Exception |
| 322 | */ |
| 323 | public function getAttribute($key) |
| 324 | { |
| 325 | try { |
| 326 | return $this->_stmt->getAttribute($key); |
| 327 | } catch (\PDOException $e) { |
| 328 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 329 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 330 | } |
| 331 | } |
| 332 | /** |
| 333 | * Returns metadata for a column in a result set. |
| 334 | * |
| 335 | * @param int $column |
| 336 | * @return mixed |
| 337 | * @throws Zend_Db_Statement_Exception |
| 338 | */ |
| 339 | public function getColumnMeta($column) |
| 340 | { |
| 341 | try { |
| 342 | return $this->_stmt->getColumnMeta($column); |
| 343 | } catch (\PDOException $e) { |
| 344 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 345 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 346 | } |
| 347 | } |
| 348 | /** |
| 349 | * Retrieves the next rowset (result set) for a SQL statement that has |
| 350 | * multiple result sets. An example is a stored procedure that returns |
| 351 | * the results of multiple queries. |
| 352 | * |
| 353 | * @return bool |
| 354 | * @throws Zend_Db_Statement_Exception |
| 355 | */ |
| 356 | public function nextRowset() |
| 357 | { |
| 358 | try { |
| 359 | return $this->_stmt->nextRowset(); |
| 360 | } catch (\PDOException $e) { |
| 361 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 362 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 363 | } |
| 364 | } |
| 365 | /** |
| 366 | * Returns the number of rows affected by the execution of the |
| 367 | * last INSERT, DELETE, or UPDATE statement executed by this |
| 368 | * statement object. |
| 369 | * |
| 370 | * @return int The number of rows affected. |
| 371 | * @throws Zend_Db_Statement_Exception |
| 372 | */ |
| 373 | public function rowCount() |
| 374 | { |
| 375 | try { |
| 376 | return $this->_stmt->rowCount(); |
| 377 | } catch (\PDOException $e) { |
| 378 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 379 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 380 | } |
| 381 | } |
| 382 | /** |
| 383 | * Set a statement attribute. |
| 384 | * |
| 385 | * @param string $key Attribute name. |
| 386 | * @param mixed $val Attribute value. |
| 387 | * @return bool |
| 388 | * @throws Zend_Db_Statement_Exception |
| 389 | */ |
| 390 | public function setAttribute($key, $val) |
| 391 | { |
| 392 | try { |
| 393 | return $this->_stmt->setAttribute($key, $val); |
| 394 | } catch (\PDOException $e) { |
| 395 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 396 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 397 | } |
| 398 | } |
| 399 | /** |
| 400 | * Set the default fetch mode for this statement. |
| 401 | * |
| 402 | * @param int $mode The fetch mode. |
| 403 | * @return bool |
| 404 | * @throws Zend_Db_Statement_Exception |
| 405 | */ |
| 406 | public function setFetchMode($mode) |
| 407 | { |
| 408 | $this->_fetchMode = $mode; |
| 409 | try { |
| 410 | return $this->_stmt->setFetchMode($mode); |
| 411 | } catch (\PDOException $e) { |
| 412 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 413 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 |