Skip to content

Commit

Permalink
Merge pull request #204 from homersimpsons/feature/automatic-integer-…
Browse files Browse the repository at this point in the history
…type

DbalUtils: Add integer type (MySQL 8 breaks on non integer for TIMESTAMP column)
  • Loading branch information
moufmouf authored Jun 12, 2020
2 parents fe88876 + b9a05eb commit 07360b6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/InnerResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function executeQuery(): void

$this->logger->debug('Running SQL request: '.$sql);

$this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateArrayTypes($this->parameters));
$this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateTypes($this->parameters));

$this->fetchStarted = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function executeCountQuery(): void
{
$sql = $this->magicQuery->buildPreparedStatement($this->queryFactory->getMagicSqlCount(), $this->parameters);
$this->logger->debug('Running count query: '.$sql);
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters, 0, DbalUtils::generateArrayTypes($this->parameters));
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters, 0, DbalUtils::generateTypes($this->parameters));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Utils/DbalUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\TDBM\Utils;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use function is_array;
use function is_int;

Expand All @@ -14,11 +15,12 @@ class DbalUtils
{
/**
* If a parameter is an array (used in a "IN" statement), we need to tell Doctrine about it.
* If it is an integer we have to tell DBAL (default is string)
* @see https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion
* @param array<string, mixed> $parameters
* @return array<string, int>
*/
public static function generateArrayTypes(array $parameters): array
public static function generateTypes(array $parameters): array
{
$types = [];
foreach ($parameters as $key => $value) {
Expand All @@ -30,6 +32,8 @@ public static function generateArrayTypes(array $parameters): array
}
}
$types[$key] = Connection::PARAM_INT_ARRAY;
} elseif (is_int($value)) {
$types[$key] = ParameterType::INTEGER;
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Utils/DbalUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
namespace TheCodingMachine\TDBM\Utils;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use PHPUnit\Framework\TestCase;

class DbalUtilsTest extends TestCase
{
public function testGenerateArrayTypes(): void
public function testGenerateTypes(): void
{
$params = [
'key1' => 'foo',
'key2' => [1,2,3],
'key3' => [1,2,'baz'],
'key4' => 1,
];

$this->assertSame(['key2'=>Connection::PARAM_INT_ARRAY, 'key3'=>Connection::PARAM_STR_ARRAY], DbalUtils::generateArrayTypes($params));
$this->assertSame(['key2'=>Connection::PARAM_INT_ARRAY, 'key3'=>Connection::PARAM_STR_ARRAY, 'key4'=>ParameterType::INTEGER], DbalUtils::generateTypes($params));
}
}

0 comments on commit 07360b6

Please sign in to comment.