Skip to content

Commit

Permalink
propagate Bridge's Converter to Result and ResultRow
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Nov 29, 2023
1 parent 0965bed commit 72f4d45
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
12 changes: 4 additions & 8 deletions src/Bridge/AbstractBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

abstract class AbstractBridge extends DefaultQueryBuilder implements Bridge
{
private ?Converter $converter = null;
private ?ConverterPluginRegistry $converterPluginRegistry = null;
private ?Writer $writer = null;
private ?string $serverName = null;
Expand Down Expand Up @@ -236,15 +237,15 @@ protected function lookupServerVersion(): ?string
/**
* Create default writer based upon server name and version and driver.
*/
protected function createConverter(ConverterPluginRegistry $converterPluginRegistry): Converter
protected function getConverter(): Converter
{
$ret = match ($this->getServerFlavor()) {
Platform::MARIADB => new MySQLConverter(),
Platform::MYSQL => new MySQLConverter(),
default => new Converter(),
};

$ret->setConverterPluginRegistry($converterPluginRegistry);
$ret->setConverterPluginRegistry($this->getConverterPluginRegistry());

return $ret;
}
Expand Down Expand Up @@ -299,11 +300,6 @@ protected function createWriter(Escaper $escaper, Converter $converter): Writer
*/
public function getWriter(): Writer
{
return $this->writer ??= $this->createWriter(
$this->createEscaper(),
$this->createConverter(
$this->getConverterPluginRegistry(),
),
);
return $this->writer ??= $this->createWriter($this->createEscaper(), $this->getConverter());
}
}
5 changes: 4 additions & 1 deletion src/Bridge/Doctrine/DoctrineQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ protected function doExecuteQuery(string $expression, array $arguments = []): Re

$doctrineResult = $this->connection->executeQuery($expression, $arguments);

return new IterableResult($doctrineResult->iterateAssociative(), $doctrineResult->rowCount(), fn () => $doctrineResult->free());
$result = new IterableResult($doctrineResult->iterateAssociative(), $doctrineResult->rowCount(), fn () => $doctrineResult->free());
$result->setConverter($this->getConverter());

return $result;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Bridge/Pdo/PdoQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ protected function doExecuteQuery(string $expression, array $arguments = []): Re
$statement = $this->connection->prepare($expression);
$statement->execute($arguments);

return new IterableResult(
$result = new IterableResult(
$statement->getIterator(),
$statement->rowCount(),
fn () => $statement->closeCursor(),
);
$result->setConverter($this->getConverter());

return $result;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Platform/Writer/MySQLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use MakinaCorpus\QueryBuilder\Converter\Converter;
use MakinaCorpus\QueryBuilder\Error\QueryBuilderError;
use MakinaCorpus\QueryBuilder\Expression\Aggregate;
use MakinaCorpus\QueryBuilder\Expression\Cast;
use MakinaCorpus\QueryBuilder\Expression\Concat;
use MakinaCorpus\QueryBuilder\Expression\ConstantTable;
use MakinaCorpus\QueryBuilder\Expression\CurrentTimestamp;
Expand Down
17 changes: 15 additions & 2 deletions src/Result/AbstractResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MakinaCorpus\QueryBuilder\Result;

use MakinaCorpus\QueryBuilder\Converter\Converter;
use MakinaCorpus\QueryBuilder\Error\ResultAlreadyStartedError;
use MakinaCorpus\QueryBuilder\Error\ResultError;
use MakinaCorpus\QueryBuilder\Error\ResultLockedError;
Expand All @@ -24,11 +25,23 @@ abstract class AbstractResult implements Result, \IteratorAggregate
private bool $iterationCompleted = false;
private bool $justRewinded = false;
private bool $rewindable = false;
private ?Converter $converter = null;

public function __construct(
private bool $countIsReliable = true,
) {}

/**
* Set converter.
*
* @internal
* For bridge only.
*/
public function setConverter(Converter $converter): void
{
$this->converter = $converter;
}

/**
* Get row count from driver.
*/
Expand Down Expand Up @@ -131,7 +144,7 @@ public function setColumnTypes(array $columnTypes): static
public function fetchRow(): ?ResultRow
{
if ($row = $this->fetchNext()) {
return new DefaultResultRow($row);
return new DefaultResultRow($row, $this->converter);
}
return null;
}
Expand All @@ -151,7 +164,7 @@ public function fetchHydrated(): mixed
return null;
}

return ($this->hydrator)($this->hydratorUsesArray ? $row : new DefaultResultRow($row));
return ($this->hydrator)($this->hydratorUsesArray ? $row : new DefaultResultRow($row, $this->converter));
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Result/DefaultResultRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MakinaCorpus\QueryBuilder\Result;

use MakinaCorpus\QueryBuilder\Converter\Converter;
use MakinaCorpus\QueryBuilder\Error\ResultError;

class DefaultResultRow implements ResultRow
Expand All @@ -12,6 +13,7 @@ class DefaultResultRow implements ResultRow

public function __construct(
private array $rawValues,
private ?Converter $converter = null,
) {
$this->names = \array_keys($rawValues);
}
Expand All @@ -28,7 +30,10 @@ public function get(int|string $columnName, ?string $phpType = null): mixed
}

if ($phpType) {
throw new \Exception("Type conversion from SQL to PHP is not implemented yet.");
if (!$this->converter) {
$this->converter = new Converter();
}
$value = $this->converter->fromSql($phpType, $value);
}

return $value;
Expand Down
20 changes: 8 additions & 12 deletions src/Writer/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,22 @@
class Writer
{
private string $matchParametersRegex;
private ?Converter $converter = null;
protected Escaper $escaper;
protected Converter $converter;

public function __construct(?Escaper $escaper = null, ?Converter $converter = null)
{
$this->converter = $converter ?? $this->createConverter();
$this->converter = $converter;
$this->escaper = $escaper ?? new StandardEscaper();
$this->buildParameterRegex();
}

/**
* Create default converter.
*
* This is a fallback, during production runtime, the converter should have
* been injected by setup code, in order to inherit from the converter
* registry and user implementations.
* Get converter.
*/
protected function createConverter(): Converter
protected function getConverter(): Converter
{
return new Converter();
return $this->converter ??= new Converter();
}

/**
Expand All @@ -114,7 +110,7 @@ public function prepare(string|Expression|SqlString $sql): SqlString
// then $identifier = $sql->getIdentifier();
}

$context = new WriterContext($this->converter);
$context = new WriterContext($this->getConverter());
$rawSql = $this->format($sql, $context);

return new SqlString(
Expand Down Expand Up @@ -151,7 +147,7 @@ protected function guessTypeOf(Expression $expression): ?string
}

if ($expression instanceof Value) {
if ($type = $this->converter->guessInputType($expression->getValue())) {
if ($type = $this->getConverter()->guessInputType($expression->getValue())) {
// Cache guessed type to avoid doing it twice when converting
// values later at query time.
$expression->setType($type);
Expand Down Expand Up @@ -314,7 +310,7 @@ protected function parseExpression(Raw|RawQuery $expression, WriterContext $cont
return $asString;
}

$converter = $this->converter;
$converter = $this->getConverter();

// See https://stackoverflow.com/a/3735908 for the starting
// sequence explaination, the rest should be comprehensible.
Expand Down

0 comments on commit 72f4d45

Please sign in to comment.