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