Skip to content

Commit

Permalink
wip, rowNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 18, 2024
1 parent b2d0855 commit 196f63e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/DatabaseTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function getPanel(): ?string
$cmd = is_string($this->explain)
? $this->explain
: 'EXPLAIN';
$explain = (new ResultSet($connection->getConnectionDriver()->query("$cmd $sql", $params)))->fetchAll();
$explain = (new ResultSet($connection, $connection->getConnectionDriver()->query("$cmd $sql", $params)))->fetchAll();
} catch (\PDOException) {
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class Connection
private ?SqlPreprocessor $preprocessor;

/** @var callable(array, ResultSet): array */
private $rowNormalizer = [Helpers::class, 'normalizeRow'];
private TypeConverter $typeConverter;

Check failure on line 33 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan

PHPDoc tag @var for property Nette\Database\Connection::$typeConverter with type callable is incompatible with native type Nette\Database\TypeConverter.
private ?string $sql = null;
private int $transactionDepth = 0;
Expand Down Expand Up @@ -136,10 +135,9 @@ public function getReflection(): Reflection
}


public function setRowNormalizer(?callable $normalizer): static
public function setRowNormalizer(): static
{
$this->rowNormalizer = $normalizer;
return $this;
throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated, use getTypeConverter() instead.');
}


Expand Down Expand Up @@ -229,7 +227,7 @@ public function query(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
$time = microtime(true);
$result = $this->connection->query($this->sql, $params);
$time = microtime(true) - $time;
$resultSet = new ResultSet($result, new SqlLiteral($this->sql, $params), $this->rowNormalizer, $time);
$resultSet = new ResultSet($this, $result, new SqlLiteral($this->sql, $params), $time);
} catch (DriverException $e) {
Arrays::invoke($this->onQuery, $this, $e);
throw $e;
Expand Down
13 changes: 0 additions & 13 deletions src/Database/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,6 @@ public static function dumpSql(string $sql, ?array $params = null, ?Connection $
}


/** @internal */
public static function normalizeRow(array $row, ResultSet $resultSet): array
{
foreach ($resultSet->resolveColumnConverters() as $key => $converter) {
$value = $row[$key];
$row[$key] = isset($value, $converter)
? $converter($value)
: $value;
}
return $row;
}


/**
* Import SQL dump from file - extremely fast.
* @param ?array<callable(int, ?float): void> $onProgress
Expand Down
27 changes: 13 additions & 14 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
class ResultSet implements \Iterator
{
/** @var callable(array, ResultSet): array */
private readonly mixed $normalizer;
private Row|false|null $lastRow = null;
private int $lastRowKey = -1;

Expand All @@ -28,19 +26,18 @@ class ResultSet implements \Iterator


public function __construct(
private readonly Connection $connection,
private readonly Drivers\Result $result,
private readonly ?SqlLiteral $query = null,
?callable $normalizer = null,
private ?float $time = null,
) {
$this->normalizer = $normalizer;
}


/** @deprecated */
public function getConnection(): Connection
{
throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated.');
return $this->connection;
}


Expand Down Expand Up @@ -77,18 +74,20 @@ public function getTime(): float
/** @internal */
public function normalizeRow(array $row): array
{
return $this->normalizer
? ($this->normalizer)($row, $this)
: $row;
$converters = $this->converters ??= $this->resolveColumnConverters();
foreach ($row as $key => $value) {
$converter = $converters[$key];
$row[$key] = isset($value, $converter)
? $converter($value)
: $value;
}

return $row;
}


public function resolveColumnConverters(): array
private function resolveColumnConverters(): array
{
if (isset($this->converters)) {
return $this->converters;
}

$res = [];
$engine = $this->connection->getDatabaseEngine();
$converter = $this->connection->getTypeConverter();
Expand All @@ -97,7 +96,7 @@ public function resolveColumnConverters(): array
? $engine->resolveColumnConverter($meta, $converter)
: null;
}
return $this->converters = $res;
return $res;
}


Expand Down
4 changes: 2 additions & 2 deletions src/Database/TypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class TypeConverter
* Heuristic column type detection.
* @return Type::*
*/
public static function detectType(string $nativeType): string
private function detectType(string $nativeType): string
{
static $cache;
if (!isset($cache[$nativeType])) {
Expand All @@ -51,7 +51,7 @@ public static function detectType(string $nativeType): string

public function resolve(string $nativeType): ?\Closure
{
return match (self::detectType($nativeType)) {
return match ($this->detectType($nativeType)) {
Type::Integer => $this->toInt(...),
Type::Decimal => $this->convertDecimal ? $this->toDecimal(...) : null,
Type::Float => $this->toFloat(...),
Expand Down
3 changes: 2 additions & 1 deletion tests/Database/ResultSet.customNormalizer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
$connection->query('UPDATE author SET born=?', new DateTime('2022-01-23'));


// TODO
test('disabled normalization', function () use ($connection) {
$driverName = $GLOBALS['driverName'];

$connection->setRowNormalizer(null);
$connection->getTypeConverter()->convertDateTime = false;
$res = $connection->query('SELECT * FROM author');
$asInt = $driverName === 'pgsql' || ($driverName !== 'sqlsrv' && PHP_VERSION_ID >= 80100);
Assert::same([
Expand Down

0 comments on commit 196f63e

Please sign in to comment.