Skip to content

Commit

Permalink
Added conversion of DBAL constants to PDO ones for PDOStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jan 29, 2018
1 parent b24fe97 commit 4c80584
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use PDO;

/**
* The PDO implementation of the Statement interface.
Expand All @@ -29,6 +31,29 @@
*/
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.
*/
Expand All @@ -41,6 +66,8 @@ protected function __construct()
*/
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
Expand All @@ -65,6 +92,8 @@ public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
*/
public function bindValue($param, $value, $type = ParameterType::STRING)
{
$type = $this->convertParamType($type);

try {
return parent::bindValue($param, $value, $type);
} catch (\PDOException $exception) {
Expand All @@ -77,6 +106,8 @@ public function bindValue($param, $value, $type = ParameterType::STRING)
*/
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) {
Expand Down Expand Up @@ -115,6 +146,8 @@ public function execute($params = null)
*/
public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
{
$fetchMode = $this->convertFetchMode($fetchMode);

try {
if ($fetchMode === null && \PDO::FETCH_ORI_NEXT === $cursorOrientation && 0 === $cursorOffset) {
return parent::fetch();
Expand All @@ -139,6 +172,8 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
*/
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
{
$fetchMode = $this->convertFetchMode($fetchMode);

try {
if ($fetchMode === null && null === $fetchArgument && null === $ctorArgs) {
return parent::fetchAll();
Expand Down Expand Up @@ -169,4 +204,38 @@ public function fetchColumn($columnIndex = 0)
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];
}
}

2 comments on commit 4c80584

@Ocramius
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marek-l:

$result = $statement->fetchAll();

return array_combine(
    array_map(function(array $row) : string { return $row['key']; }, $result),
    array_map(function(array $row) : string { return $row['value']; }, $result)
);

We don't support all fetch modes by choice, and are removing support for some fetch modes as well: #3070

@marek-l
Copy link

@marek-l marek-l commented on 4c80584 Apr 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ocramius yes, sorry I already found it. Thank you for response.

Please sign in to comment.