Skip to content

Commit

Permalink
Merge pull request #4039 from morozov/refactor-portability-statement
Browse files Browse the repository at this point in the history
Refactor portability statement into a functional composition
  • Loading branch information
morozov authored May 30, 2020
2 parents 9d22f7b + 7204a7d commit 90b68ab
Show file tree
Hide file tree
Showing 5 changed files with 812 additions and 172 deletions.
65 changes: 25 additions & 40 deletions src/Portability/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ class Connection extends \Doctrine\DBAL\Connection
public const PORTABILITY_SQLANYWHERE = 13;
public const PORTABILITY_SQLSRV = 13;

/** @var int */
private $portability = self::PORTABILITY_NONE;

/** @var int */
private $case;
/** @var Converter */
private $converter;

/**
* {@inheritdoc}
Expand All @@ -43,75 +40,63 @@ public function connect()
{
$ret = parent::connect();
if ($ret) {
$params = $this->getParams();
$params = $this->getParams();
$portability = self::PORTABILITY_NONE;

if (isset($params['portability'])) {
if ($this->getDatabasePlatform()->getName() === 'oracle') {
$params['portability'] &= self::PORTABILITY_ORACLE;
$portability = $params['portability'] & self::PORTABILITY_ORACLE;
} elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
$params['portability'] &= self::PORTABILITY_POSTGRESQL;
$portability = $params['portability'] & self::PORTABILITY_POSTGRESQL;
} elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
$params['portability'] &= self::PORTABILITY_SQLITE;
$portability = $params['portability'] & self::PORTABILITY_SQLITE;
} elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
$params['portability'] &= self::PORTABILITY_SQLANYWHERE;
$portability = $params['portability'] & self::PORTABILITY_SQLANYWHERE;
} elseif ($this->getDatabasePlatform()->getName() === 'db2') {
$params['portability'] &= self::PORTABILITY_DB2;
$portability = $params['portability'] & self::PORTABILITY_DB2;
} elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
$params['portability'] &= self::PORTABILITY_SQLSRV;
$portability = $params['portability'] & self::PORTABILITY_SQLSRV;
} else {
$params['portability'] &= self::PORTABILITY_OTHERVENDORS;
$portability = $params['portability'] & self::PORTABILITY_OTHERVENDORS;
}

$this->portability = $params['portability'];
}

if (isset($params['fetch_case']) && ($this->portability & self::PORTABILITY_FIX_CASE) !== 0) {
$case = null;

if (isset($params['fetch_case']) && ($portability & self::PORTABILITY_FIX_CASE) !== 0) {
if ($this->_conn instanceof PDOConnection) {
// make use of c-level support for case handling
$this->_conn->getWrappedConnection()->setAttribute(PDO::ATTR_CASE, $params['fetch_case']);
} else {
$this->case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
$case = $params['fetch_case'] === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;
}
}

$this->converter = new Converter(
($portability & self::PORTABILITY_EMPTY_TO_NULL) !== 0,
($portability & self::PORTABILITY_RTRIM) !== 0,
$case
);
}

return $ret;
}

/**
* @return int
*/
public function getPortability()
{
return $this->portability;
}

/**
* @return int
*/
public function getFetchCase()
{
return $this->case;
}

/**
* {@inheritdoc}
*/
public function executeQuery(string $query, array $params = [], $types = [], ?QueryCacheProfile $qcp = null) : ResultStatement
{
return new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
return new Statement(parent::executeQuery($query, $params, $types, $qcp), $this->converter);
}

public function prepare(string $sql) : DriverStatement
{
return new Statement(parent::prepare($sql), $this);
return new Statement(parent::prepare($sql), $this->converter);
}

public function query(string $sql) : ResultStatement
{
return new Statement(
$this->getWrappedConnection()
->query($sql),
$this
);
return new Statement(parent::query($sql), $this->converter);
}
}
247 changes: 247 additions & 0 deletions src/Portability/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Portability;

use function array_change_key_case;
use function array_map;
use function array_reduce;
use function is_string;
use function rtrim;

final class Converter
{
/** @var callable */
private $convertNumeric;

/** @var callable */
private $convertAssociative;

/** @var callable */
private $convertOne;

/** @var callable */
private $convertAllNumeric;

/** @var callable */
private $convertAllAssociative;

/** @var callable */
private $convertFirstColumn;

/**
* @param bool $convertEmptyStringToNull Whether each empty string should be converted to NULL
* @param bool $rightTrimString Whether each string should right-trimmed
* @param int|null $case Convert the case of the column names
* (one of {@link CASE_LOWER} and {@link CASE_UPPER})
*/
public function __construct(bool $convertEmptyStringToNull, bool $rightTrimString, ?int $case)
{
$id = static function ($value) {
return $value;
};

$convertValue = $this->createConvertValue($convertEmptyStringToNull, $rightTrimString);
$convertNumeric = $this->createConvertRow($convertValue, null);
$convertAssociative = $this->createConvertRow($convertValue, $case);

$this->convertNumeric = $this->createConvert($convertNumeric, $id);
$this->convertAssociative = $this->createConvert($convertAssociative, $id);
$this->convertOne = $this->createConvert($convertValue, $id);

$this->convertAllNumeric = $this->createConvertAll($convertNumeric, $id);
$this->convertAllAssociative = $this->createConvertAll($convertAssociative, $id);
$this->convertFirstColumn = $this->createConvertAll($convertValue, $id);
}

/**
* @param array<int,mixed>|false $row
*
* @return array<int,mixed>|false
*/
public function convertNumeric($row)
{
return ($this->convertNumeric)($row);
}

/**
* @param array<string,mixed>|false $row
*
* @return array<string,mixed>|false
*/
public function convertAssociative($row)
{
return ($this->convertAssociative)($row);
}

/**
* @param mixed|false $value
*
* @return mixed|false
*/
public function convertOne($value)
{
return ($this->convertOne)($value);
}

/**
* @param array<int,array<int,mixed>> $data
*
* @return array<int,array<int,mixed>>
*/
public function convertAllNumeric(array $data) : array
{
return ($this->convertAllNumeric)($data);
}

/**
* @param array<int,array<string,mixed>> $data
*
* @return array<int,array<string,mixed>>
*/
public function convertAllAssociative(array $data) : array
{
return ($this->convertAllAssociative)($data);
}

/**
* @param array<int,mixed> $data
*
* @return array<int,mixed>
*/
public function convertFirstColumn(array $data) : array
{
return ($this->convertFirstColumn)($data);
}

/**
* Creates a function that will convert each individual value retrieved from the database
*
* @param bool $convertEmptyStringToNull Whether each empty string should be converted to NULL
* @param bool $rightTrimString Whether each string should right-trimmed
*
* @return callable|null The resulting function or NULL if no conversion is needed
*/
private function createConvertValue(bool $convertEmptyStringToNull, bool $rightTrimString) : ?callable
{
$functions = [];

if ($convertEmptyStringToNull) {
$functions[] = static function ($value) {
if ($value === '') {
return null;
}

return $value;
};
}

if ($rightTrimString) {
$functions[] = static function ($value) {
if (! is_string($value)) {
return $value;
}

return rtrim($value);
};
}

return $this->compose(...$functions);
}

/**
* Creates a function that will convert each array-row retrieved from the database
*
* @param callable|null $function The function that will convert each value
* @param int|null $case Column name case
*
* @return callable|null The resulting function or NULL if no conversion is needed
*/
private function createConvertRow(?callable $function, ?int $case) : ?callable
{
$functions = [];

if ($function !== null) {
$functions[] = $this->createMapper($function);
}

if ($case !== null) {
$functions[] = static function (array $row) use ($case) : array {
return array_change_key_case($row, $case);
};
}

return $this->compose(...$functions);
}

/**
* Creates a function that will be applied to the return value of Statement::fetch*()
* or an identity function if no conversion is needed
*
* @param callable|null $function The function that will convert each tow
* @param callable $id Identity function
*/
private function createConvert(?callable $function, callable $id) : callable
{
if ($function === null) {
return $id;
}

return static function ($value) use ($function) {
if ($value === false) {
return false;
}

return $function($value);
};
}

/**
* Creates a function that will be applied to the return value of Statement::fetchAll*()
* or an identity function if no transformation is required
*
* @param callable|null $function The function that will transform each value
* @param callable $id Identity function
*/
private function createConvertAll(?callable $function, callable $id) : callable
{
if ($function === null) {
return $id;
}

return $this->createMapper($function);
}

/**
* Creates a function that maps each value of the array using the given function
*
* @param callable $function The function that maps each value of the array
*/
private function createMapper(callable $function) : callable
{
return static function (array $array) use ($function) : array {
return array_map($function, $array);
};
}

/**
* Creates a composition of the given set of functions
*
* @param callable ...$functions The functions to compose
*
* @return callable|null The composition or NULL if an empty set is provided
*/
private function compose(callable ...$functions) : ?callable
{
return array_reduce($functions, static function (?callable $carry, callable $item) : callable {
if ($carry === null) {
return $item;
}

return static function ($value) use ($carry, $item) {
return $item($carry($value));
};
});
}
}
Loading

0 comments on commit 90b68ab

Please sign in to comment.