Added conversion of DBAL constants to PDO ones for PDOStatement

parent b24fe977
...@@ -19,7 +19,9 @@ ...@@ -19,7 +19,9 @@
namespace Doctrine\DBAL\Driver; namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use PDO;
/** /**
* The PDO implementation of the Statement interface. * The PDO implementation of the Statement interface.
...@@ -29,6 +31,29 @@ use Doctrine\DBAL\ParameterType; ...@@ -29,6 +31,29 @@ use Doctrine\DBAL\ParameterType;
*/ */
class PDOStatement extends \PDOStatement implements Statement class PDOStatement extends \PDOStatement implements Statement
{ {
/**
* @var int[]
*/
private const PARAM_TYPE_MAP = [
ParameterType::NULL => PDO::PARAM_NULL,
ParameterType::INTEGER => PDO::PARAM_INT,
ParameterType::STRING => PDO::PARAM_STR,
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
];
/**
* @var int[]
*/
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 constructor.
*/ */
...@@ -41,6 +66,8 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -41,6 +66,8 @@ class PDOStatement extends \PDOStatement implements Statement
*/ */
public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null) public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
{ {
$fetchMode = $this->convertFetchMode($fetchMode);
// This thin wrapper is necessary to shield against the weird signature // This thin wrapper is necessary to shield against the weird signature
// of PDOStatement::setFetchMode(): even if the second and third // of PDOStatement::setFetchMode(): even if the second and third
// parameters are optional, PHP will not let us remove it from this // parameters are optional, PHP will not let us remove it from this
...@@ -65,6 +92,8 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -65,6 +92,8 @@ class PDOStatement extends \PDOStatement implements Statement
*/ */
public function bindValue($param, $value, $type = ParameterType::STRING) public function bindValue($param, $value, $type = ParameterType::STRING)
{ {
$type = $this->convertParamType($type);
try { try {
return parent::bindValue($param, $value, $type); return parent::bindValue($param, $value, $type);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
...@@ -77,6 +106,8 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -77,6 +106,8 @@ class PDOStatement extends \PDOStatement implements Statement
*/ */
public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null) public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null, $driverOptions = null)
{ {
$type = $this->convertParamType($type);
try { try {
return parent::bindParam($column, $variable, $type, $length, $driverOptions); return parent::bindParam($column, $variable, $type, $length, $driverOptions);
} catch (\PDOException $exception) { } catch (\PDOException $exception) {
...@@ -115,6 +146,8 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -115,6 +146,8 @@ class PDOStatement extends \PDOStatement implements Statement
*/ */
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{ {
$fetchMode = $this->convertFetchMode($fetchMode);
try { try {
if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) { if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
return parent::fetch(); return parent::fetch();
...@@ -139,6 +172,8 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -139,6 +172,8 @@ class PDOStatement extends \PDOStatement implements Statement
*/ */
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{ {
$fetchMode = $this->convertFetchMode($fetchMode);
try { try {
if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) { if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
return parent::fetchAll(); return parent::fetchAll();
...@@ -169,4 +204,38 @@ class PDOStatement extends \PDOStatement implements Statement ...@@ -169,4 +204,38 @@ class PDOStatement extends \PDOStatement implements Statement
throw new PDOException($exception); throw new PDOException($exception);
} }
} }
/**
* Converts DBAL parameter type to PDO parameter type
*
* @param int $type Parameter type
* @return int
*/
private function convertParamType(int $type) : int
{
if ( ! isset(self::PARAM_TYPE_MAP[$type])) {
throw new \InvalidArgumentException('Invalid parameter type: ' . $type);
}
return self::PARAM_TYPE_MAP[$type];
}
/**
* Converts DBAL fetch mode to PDO fetch mode
*
* @param int|null $fetchMode Fetch mode
* @return int|null
*/
private function convertFetchMode(?int $fetchMode) : ?int
{
if ($fetchMode === null) {
return null;
}
if ( ! isset(self::FETCH_MODE_MAP[$fetchMode])) {
throw new \InvalidArgumentException('Invalid fetch mode: ' . $fetchMode);
}
return self::FETCH_MODE_MAP[$fetchMode];
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment