Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Database/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ protected function hashColumns(QueryParameters $params, array $columns): string
{
$hash = '';
foreach ($columns as $column) {
if ($column instanceof SelectQuery) {
// A scalar subquery column: its parameters, not its interpolated values.
$hash .= 'sq_' . ($column->getPrefix() ?? '') . '(' . $this->hashSelectQuery($params, $column->getTokens()) . '),';
continue;
}
if ($column instanceof Expression || $column instanceof Fragment || $column instanceof SubQuery) {
foreach ($column->getTokens()['parameters'] as $param) {
$params->push($param);
Expand Down
10 changes: 9 additions & 1 deletion src/Database/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInt
protected array $queryCache = [];

private ?string $name = null;
private ?\DateTimeZone $timezone = null;
private ?string $timezoneName = null;
private bool $useCache = true;

protected function __construct(
Expand Down Expand Up @@ -132,7 +134,13 @@ public function getSource(): string

public function getTimezone(): \DateTimeZone
{
return new \DateTimeZone($this->config->timezone);
// DateTimeZone has no mutators: one instance per configured name.
if ($this->timezone === null || $this->timezoneName !== $this->config->timezone) {
$this->timezone = new \DateTimeZone($this->config->timezone);
$this->timezoneName = $this->config->timezone;
}

return $this->timezone;
}

public function getSchemaHandler(): HandlerInterface
Expand Down
22 changes: 16 additions & 6 deletions src/Database/Query/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,11 @@ public function runChunks(int $limit, callable $callback): void
}

/**
* Count number of rows in query. Limit, offset, order by, group by values will be ignored.
*
* @param non-empty-string $column Column to count by (every column by default).
* The query {@see count()} runs: a copy selecting only the count, without order or grouping.
*
* @return int<0, max>
* @param non-empty-string $column
*/
public function count(string $column = '*', bool $distinct = false): int
public function countQuery(string $column = '*', bool $distinct = false): self
{
$select = clone $this;

Expand All @@ -347,7 +345,19 @@ public function count(string $column = '*', bool $distinct = false): int
$select->orderBy = [];
$select->groupBy = [];

$st = $select->run();
return $select;
}

/**
* Count number of rows in query. Limit, offset, order by, group by values will be ignored.
*
* @param non-empty-string $column Column to count by (every column by default).
*
* @return int<0, max>
*/
public function count(string $column = '*', bool $distinct = false): int
{
$st = $this->countQuery($column, $distinct)->run();
try {
return (int) $st->fetchColumn();
} finally {
Expand Down
9 changes: 5 additions & 4 deletions src/ORM/Command/Database/StatementPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

/**
* Statements compiled once per table and column set, so repeated writes skip
* the query builder and the compiler. Only scalar and null values qualify.
* the query builder and the compiler. Only scalar, datetime and null values qualify
* (a datetime is bound as the driver formats it, like the query builder does).
*
* @internal
*/
Expand All @@ -41,15 +42,15 @@ public static function update(DatabaseInterface $db, string $table, array $value
$key .= "{$column}\0n\1";
continue;
}
if (!\is_scalar($value)) {
if (!\is_scalar($value) && !$value instanceof \DateTimeInterface) {
return null;
}
$key .= "{$column}\1";
$args[] = $value;
}
$key .= "\2";
foreach ($where as $column => $value) {
if (!\is_scalar($value)) {
if (!\is_scalar($value) && !$value instanceof \DateTimeInterface) {
return null;
}
$key .= "{$column}\1";
Expand Down Expand Up @@ -108,7 +109,7 @@ public static function insert(DatabaseInterface $db, string $table, array $value
$key .= "{$column}\0n\1";
continue;
}
if (!\is_scalar($value)) {
if (!\is_scalar($value) && !$value instanceof \DateTimeInterface) {
return [false, null];
}
$key .= "{$column}\1";
Expand Down
35 changes: 28 additions & 7 deletions src/ORM/Parser/Typecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ final class Typecast implements CastableInterface, UncastableInterface
*/
private array $plain = [];

/** @var array<non-empty-string, true> Fields cast by the datetime rule. */
private array $datetimes = [];

private ?\DateTimeZone $timezone = null;

/**
* @param non-empty-string $role The role of the entity being typecasted
* @param DatabaseInterface $database The database instance used for typecasting
Expand All @@ -67,19 +72,16 @@ public function __construct(

public function setRules(array $rules): array
{
$timezone = null;
foreach ($rules as $key => $rule) {
unset($this->datetimes[$key]);
// Static rules
if (\in_array($rule, self::RULES, true)) {
$this->casters[$key] = match ($rule) {
'int' => static fn(mixed $value): int => (int) $value,
'bool' => static fn(mixed $value): bool => (bool) $value,
'float' => static fn(mixed $value): float => (float) $value,
// The driver timezone is fixed per connection: resolved once, not per value.
'datetime' => function (mixed $value) use (&$timezone): \DateTimeImmutable {
$timezone ??= $this->database->getDriver()->getTimezone();
return new \DateTimeImmutable($value, $timezone);
},
'datetime' => fn (mixed $value): \DateTimeImmutable => new \DateTimeImmutable($value, $this->timezone()),
// Mostly an array, but can also be a scalar type.
'json' => static fn(mixed $value): mixed => \json_decode(
$value,
Expand All @@ -92,8 +94,10 @@ public function setRules(array $rules): array
if ($rule === 'int' || $rule === 'bool' || $rule === 'float') {
$this->plain[$key] = $rule;
} elseif ($rule === 'datetime') {
// Database timestamps always parse: the maker calls the caster directly.
// Database timestamps always parse: compiled makers construct them inline
// in the driver timezone (see plainRules()).
$this->plain[$key] = $this->casters[$key];
$this->datetimes[$key] = true;
} else {
unset($this->plain[$key]);
}
Expand Down Expand Up @@ -175,7 +179,24 @@ public function setRules(array $rules): array
*/
public function plainRules(): ?array
{
return \count($this->plain) === \count($this->casters) ? $this->plain : null;
if (\count($this->plain) !== \count($this->casters)) {
return null;
}
$rules = $this->plain;
foreach ($this->datetimes as $key => $_) {
// A datetime field: the zone its values are constructed in.
$rules[$key] = $this->timezone();
}

return $rules;
}

/**
* The driver timezone is fixed per connection: resolved once, not per value.
*/
private function timezone(): \DateTimeZone
{
return $this->timezone ??= $this->database->getDriver()->getTimezone();
}

public function cast(array $data): array
Expand Down
81 changes: 70 additions & 11 deletions src/ORM/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,61 @@ public function orWhereJsonLength(string $path, int $length, string $operator =
public function count(?string $column = null): int
{
$query = $this->buildQuery();
if ($column === null) {
$pk = (array) $this->loader->getPK();
$column = \count($pk) > 1 || self::rowsAreUnique($query)
? '*'
: \sprintf('DISTINCT(%s)', \reset($pk));

return (int) $this->builder->withQuery($query)->count($column ?? $this->countColumn($query));
}

/**
* One page of entities and the number of rows of the whole query, as {@see count()} gives.
* For a select of a cached shape one statement reads both: the count is a scalar subquery
* column of the page query. Otherwise, or when the page is past the last row, the count
* is read by its own statement.
*
* @param int<0, max> $limit
* @param int<0, max> $offset
*
* @return array{list<TEntity>, int<0, max>}
*/
public function fetchPage(int $limit, int $offset): array
{
if (!$this->customized && $this->limit === 0 && $this->offset === 0) {
$page = (clone $this)->limit($limit)->offset($offset);
$key = $page->shape . "\3count";
$plan = $this->shapes[$key] ?? null;
if ($plan === null && \count($this->shapes) < 1024) {
$plan = $this->shapes[$key] = $page->shapePlan($this);
}
if ($plan) {
[$driver, $sql, $values, $slots, $maker] = $plan;
foreach ($slots as $index => $i) {
$values[$index] = new Parameter($this->shapeValues[$i]);
}
$statement = $driver->query($sql, $values);
$rows = $statement->fetchAll(StatementInterface::FETCH_NUM);
$statement->close();
if ($rows !== []) {
return [$maker($rows), (int) $rows[0][\array_key_last($rows[0])]];
}

return [[], (clone $this)->count()];
}
}

return (int) $this->builder->withQuery($query)->count($column);
$items = (clone $this)->limit($limit)->offset($offset)->fetchAll();

return [\is_array($items) ? \array_values($items) : \iterator_to_array($items, false), (clone $this)->count()];
}

/**
* COUNT(*) when every row is a distinct entity, COUNT(DISTINCT pk) otherwise.
*/
private function countColumn(SelectQuery $query): string
{
$pk = (array) $this->loader->getPK();

return \count($pk) > 1 || self::rowsAreUnique($query)
? '*'
: \sprintf('DISTINCT(%s)', \reset($pk));
}

/**
Expand Down Expand Up @@ -1180,7 +1227,7 @@ private function fetchShape(): ?array
return null;
}
[$driver, $sql, $values, $slots, $maker] = $plan;
foreach ($slots as $i => $index) {
foreach ($slots as $index => $i) {
$values[$index] = new Parameter($this->shapeValues[$i]);
}
$statement = $driver->query($sql, $values);
Expand All @@ -1194,7 +1241,7 @@ private function fetchShape(): ?array
* Compiles the recorded shape once with sentinel values and finds where each
* value lands in the statement parameters. False when the shape can't be cached.
*/
private function shapePlan(): array|false
private function shapePlan(?self $countOf = null): array|false
{
// A scope adds its own values (a tenant, a soft-delete flag) when the query is built;
// a cached statement would keep the first ones.
Expand All @@ -1215,23 +1262,35 @@ private function shapePlan(): array|false
return false;
}
$query = $copy->addGroupByPK()->loader->buildQuery();
if ($countOf !== null) {
// The count of the select without limit and offset, with the same values.
$counted = clone $countOf;
if (\count($counted->shapeValues) !== \count($sentinels)) {
return false;
}
$counted->shapeValues = $copy->shapeValues;
$counted->applyRecorded();
$countQuery = $counted->buildQuery();
$query->columns([...$query->getColumns(), $countQuery->countQuery($counted->countColumn($countQuery))]);
}
$parameters = new QueryParameters();
$sql = $query->sqlStatement($parameters);
$values = $parameters->getParameters();
// Statement parameter index => shape value; a value may land in more than one place.
$slots = [];
foreach ($values as $index => $value) {
$raw = $value instanceof ParameterInterface ? $value->getValue() : $value;
if (!\is_string($raw) || !\str_contains($raw, "\0arcano-value-")) {
continue;
}
$slot = $sentinels[$raw] ?? null;
if ($slot === null || isset($slots[$slot]) || !$value instanceof Parameter) {
if ($slot === null || !$value instanceof Parameter) {
return false;
}
$slots[$slot] = $index;
$slots[$index] = $slot;
}

return \count($slots) === \count($sentinels) ? [$query->getDriver(), $sql, $values, $slots, $maker] : false;
return \count(\array_unique($slots)) === \count($sentinels) ? [$query->getDriver(), $sql, $values, $slots, $maker] : false;
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/ORM/Service/Implementation/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,22 @@ private function compileDataMaker(string $role): ?\Closure

$items = [];
$casters = [];
$zones = [];
foreach (\array_intersect_key($rules, \array_flip($fields)) as $field => $rule) {
$key = \var_export($field, true);
if ($rule instanceof \DateTimeZone) {
$zones[$field] = $rule;
$items[] = "\$d[{$key}] === null or \$d[{$key}] = new \\DateTimeImmutable(\$d[{$key}], \$zones[{$key}]);";
continue;
}
if ($rule instanceof \Closure) {
$casters[$field] = $rule;
$items[] = "\$d[{$key}] === null or \$d[{$key}] = \$casters[{$key}](\$d[{$key}]);";
continue;
}
$items[] = "\$d[{$key}] === null or \$d[{$key}] = ({$rule}) \$d[{$key}];";
}
$cast = eval("declare(strict_types=1);\nreturn static function (array \$d) use (\$casters): array {\n" . \implode("\n", $items) . "\nreturn \$d;\n};");
$cast = eval("declare(strict_types=1);\nreturn static function (array \$d) use (\$casters, \$zones): array {\n" . \implode("\n", $items) . "\nreturn \$d;\n};");
$flip = \array_fill_keys($fields, true);
$count = \count($fields);
$heap = $this->heap;
Expand Down Expand Up @@ -276,9 +282,24 @@ private function compileRowMaker(string $role, array $columns, ?string $groupBy,
// Typecast compiled into the array literal: null stays null, as in Typecast::cast().
$items = [];
$casters = [];
$zones = [];
$prototypes = [];
foreach ($columns as $i => $field) {
$key = \var_export($field, true);
$rule = $rules[$field] ?? null;
if ($rule instanceof \DateTimeZone) {
// The datetime rule ({@see \Arcano\ORM\Parser\Typecast}) without a call per value.
$zones[$field] = $rule;
$construct = "new \\DateTimeImmutable(\$row[{$i}], \$zones[{$key}])";
if ($rule->getName() === 'UTC' && \function_exists('pam_native_utc_datetime')) {
// On the PAM runtime a UTC database timestamp is a filled-in clone of a
// prototype: the same object the constructor gives, without its parser.
$prototypes[$field] = new \DateTimeImmutable('2000-01-01 00:00:00', $rule);
$construct = "(\\is_string(\$row[{$i}]) && (\$t = \\pam_native_utc_datetime(\$row[{$i}], \$prototypes[{$key}])) !== null ? \$t : {$construct})";
}
$items[] = "{$key} => \$row[{$i}] === null ? null : {$construct}";
continue;
}
if ($rule instanceof \Closure) {
$casters[$field] = $rule;
$items[] = "{$key} => \$row[{$i}] === null ? null : \$casters[{$key}](\$row[{$i}])";
Expand Down Expand Up @@ -351,7 +372,7 @@ private function compileRowMaker(string $role, array $columns, ?string $groupBy,
// $nodes the heap node of each result.
$maker = eval(<<<PHP
declare(strict_types=1);
return static function (iterable \$rows, ?array &\$fresh = null, ?array &\$nodes = null, ?array &\$groups = null, ?array \$only = null) use (\$heap, \$make, \$prototype, \$relMap, \$nodePrototype, \$casters): array {
return static function (iterable \$rows, ?array &\$fresh = null, ?array &\$nodes = null, ?array &\$groups = null, ?array \$only = null) use (\$heap, \$make, \$prototype, \$relMap, \$nodePrototype, \$casters, \$zones, \$prototypes): array {
\$result = [];
{$seenInit} \$storage = \$heap->loadedStorage();
\$index = &\$heap->loadedIndex({$roleKey});
Expand Down
Loading
Loading