Skip to content

Commit

Permalink
Fix fetch column type (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
bronek89 authored May 31, 2022
1 parent 8b38bdc commit 81f1db9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=8.0",
"doctrine/dbal": "^2.13",
"doctrine/dbal": "^2.13.9",
"gowork/values": "^0.6",
"gowork/safe": "^0.2.0",
"symfony/console": "^5.0|^6.0",
Expand All @@ -18,7 +18,7 @@
"nikic/php-parser": "^4.10"
},
"require-dev": {
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.7",
"phpunit/phpunit": "^9.5"
},
"autoload": {
Expand Down
31 changes: 25 additions & 6 deletions src/DatabaseSelectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GW\DQO;

use DateTimeImmutable;
use Dazet\TypeUtil\StringUtil;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Query\QueryBuilder;
Expand All @@ -12,7 +13,9 @@
use function array_merge;
use function get_class;
use function is_array;
use function is_int;
use function is_object;
use function is_string;

final class DatabaseSelectBuilder
{
Expand Down Expand Up @@ -155,15 +158,31 @@ function (string $field): string {
return $copy;
}

/**
* @return false|string
*/
public function fetchColumn(int $index = 0)
public function fetchColumn(int $index = 0): false|string
{
/** @var ResultStatement<mixed> $statement */
$statement = (clone $this->builder)->setMaxResults(1)->execute();

return $statement->fetchColumn($index);
if (is_int($statement) || is_string($statement)) {
throw new RuntimeException("Expected select query");
}

if ($index > 0) {
$row = $statement->fetchNumeric();

if ($row === false) {
return false;
}

return StringUtil::toString($row[$index]);
}

$value = $statement->fetchOne();

if ($value === false) {
return false;
}

return StringUtil::toString($value);
}

public function fetchDate(int $index = 0): ?DateTimeImmutable
Expand Down

0 comments on commit 81f1db9

Please sign in to comment.