123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace Doctrine\DBAL\Driver;
- use Doctrine\DBAL\FetchMode;
- use Doctrine\DBAL\ParameterType;
- use PDO;
- use const E_USER_DEPRECATED;
- use function sprintf;
- use function trigger_error;
- /**
- * The PDO implementation of the Statement interface.
- * Used by all PDO-based drivers.
- */
- class PDOStatement extends \PDOStatement implements Statement
- {
- private const PARAM_TYPE_MAP = [
- ParameterType::NULL => PDO::PARAM_NULL,
- ParameterType::INTEGER => PDO::PARAM_INT,
- ParameterType::STRING => PDO::PARAM_STR,
- ParameterType::BINARY => PDO::PARAM_LOB,
- ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
- ParameterType::BOOLEAN => PDO::PARAM_BOOL,
- ];
- private const FETCH_MODE_MAP = [
- FetchMode::ASSOCIATIVE => PDO::FETCH_ASSOC,
- FetchMode::NUMERIC => PDO::FETCH_NUM,
- FetchMode::MIXED => PDO::FETCH_BOTH,
- FetchMode::STANDARD_OBJECT => PDO::FETCH_OBJ,
- FetchMode::COLUMN => PDO::FETCH_COLUMN,
- FetchMode::CUSTOM_OBJECT => PDO::FETCH_CLASS,
- ];
- /**
- * Protected constructor.
- */
- protected function __construct()
- {
- }
- /**
- * {@inheritdoc}
- */
- public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
- {
- $fetchMode = $this->convertFetchMode($fetchMode);
- // This thin wrapper is necessary to shield against the weird signature
- // of PDOStatement::setFetchMode(): even if the second and third
- // parameters are optional, PHP will not let us remove it from this
- // declaration.
- try {
- if ($arg2 === null && $arg3 === null) {
- return parent::setFetchMode($fetchMode);
- }
- if ($arg3 === null) {
- return parent::setFetchMode($fetchMode, $arg2);
- }
- return parent::setFetchMode($fetchMode, $arg2, $arg3);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function bindValue($param, $value, $type = ParameterType::STRING)
- {
- $type = $this->convertParamType($type);
- try {
- return parent::bindValue($param, $value, $type);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
- {
- $type = $this->convertParamType($type);
- try {
- return parent::bindParam($column, $variable, $type, $length, $driverOptions);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function closeCursor()
- {
- try {
- return parent::closeCursor();
- } catch (\PDOException $exception) {
- // Exceptions not allowed by the interface.
- // In case driver implementations do not adhere to the interface, silence exceptions here.
- return true;
- }
- }
- /**
- * {@inheritdoc}
- */
- public function execute($params = null)
- {
- try {
- return parent::execute($params);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
- {
- $fetchMode = $this->convertFetchMode($fetchMode);
- try {
- if ($fetchMode === null && $cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) {
- return parent::fetch();
- }
- if ($cursorOrientation === PDO::FETCH_ORI_NEXT && $cursorOffset === 0) {
- return parent::fetch($fetchMode);
- }
- if ($cursorOffset === 0) {
- return parent::fetch($fetchMode, $cursorOrientation);
- }
- return parent::fetch($fetchMode, $cursorOrientation, $cursorOffset);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
- {
- $fetchMode = $this->convertFetchMode($fetchMode);
- try {
- if ($fetchMode === null && $fetchArgument === null && $ctorArgs === null) {
- return parent::fetchAll();
- }
- if ($fetchArgument === null && $ctorArgs === null) {
- return parent::fetchAll($fetchMode);
- }
- if ($ctorArgs === null) {
- return parent::fetchAll($fetchMode, $fetchArgument);
- }
- return parent::fetchAll($fetchMode, $fetchArgument, $ctorArgs);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function fetchColumn($columnIndex = 0)
- {
- try {
- return parent::fetchColumn($columnIndex);
- } catch (\PDOException $exception) {
- throw new PDOException($exception);
- }
- }
- /**
- * Converts DBAL parameter type to PDO parameter type
- *
- * @param int $type Parameter type
- */
- private function convertParamType(int $type) : int
- {
- if (! isset(self::PARAM_TYPE_MAP[$type])) {
- // TODO: next major: throw an exception
- @trigger_error(sprintf(
- 'Using a PDO parameter type (%d given) is deprecated and will cause an error in Doctrine 3.0',
- $type
- ), E_USER_DEPRECATED);
- return $type;
- }
- return self::PARAM_TYPE_MAP[$type];
- }
- /**
- * Converts DBAL fetch mode to PDO fetch mode
- *
- * @param int|null $fetchMode Fetch mode
- */
- private function convertFetchMode(?int $fetchMode) : ?int
- {
- if ($fetchMode === null) {
- return null;
- }
- if (! isset(self::FETCH_MODE_MAP[$fetchMode])) {
- // TODO: next major: throw an exception
- @trigger_error(sprintf(
- 'Using a PDO fetch mode or their combination (%d given)' .
- ' is deprecated and will cause an error in Doctrine 3.0',
- $fetchMode
- ), E_USER_DEPRECATED);
- return $fetchMode;
- }
- return self::FETCH_MODE_MAP[$fetchMode];
- }
- }
|