Skip to content

Build statements from the platform instead of decorating them - #258

Open
tyrsson wants to merge 5 commits into
0.6.xfrom
rfc/256-statement-factory
Open

tyrsson wants to merge 5 commits into
0.6.xfrom
rfc/256-statement-factory

Conversation

@tyrsson

@tyrsson tyrsson commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Replaces the decorator mechanism with a statement factory, so a statement carries its dialect from the moment it is created, and removes the runtime default platform.

  • StatementFactoryInterface puts statement construction on the platform. AbstractPlatform returns the standard DML and DDL classes; a driver returns its own subclasses where its dialect renders a statement differently.
  • Sql delegates select(), insert(), update() and delete() to the factory and gains createTable(), alterTable() and dropTable(). getSqlPlatform() becomes getStatementFactory().
  • PlatformInterface::getSqlPlatformDecorator() becomes getStatementFactory().
  • buildSqlString() and prepareStatementForSqlObject() render the object they are given.
  • getSqlString() requires a platform. Driverless value quoting is not supported: a platform asks its driver to quote a value, and one without a driver throws. Sql92 becomes @internal, and with no default left it is reachable only from the test suite; nothing in src selects it.
  • Removed: PlatformDecoratorInterface, setSubject(), AbstractSql::$subject, localizeVariables(), getTypeDecorator(), setTypeDecorator(), the name-keyed decorator map, and the decorator and subject exceptions.
  • AbstractSql::prepareSpecifications() replaces the state copy, and processSubSelect() renders the subselect directly instead of cloning a decorator over it.

Why: the dialect is known when the platform is selected, so it is known when a statement is created. The decorator layer only existed to apply dialect rendering to a statement after construction, which also meant copying state into a shared instance, nulling the subject part-way through the render, and narrowing the decorator again at each call site. The Sql92 fallback meant whether a statement rendered at all depended on whether it carried values.

A statement built with new Select() no longer receives platform rendering. Inside phpdb every statement comes from Sql or from the factory, including the four TableGateway and RowGateway construction sites, so the event features keep mutating the object that renders.

Companion implementation: php-db/phpdb-mysql#88. phpdb-sqlite and phpdb-pgsql need the same one-line accessor change and their own factory overrides.

The first commit brings the tree in line with Mago 1.61.

Mago 1.61 code changes

src/Adapter/ParameterContainer.php:314 unfulfilled-expect

-     *
-     * @mago-expect analysis:possibly-undefined-string-array-index
      */
     public function offsetSetReference(string|int $name, string|int $from): void

src/Adapter/Profiler/Profiler.php:44 redundant-null-coalesce

-        return null === $lastIndex ? null : $this->profiles[$lastIndex] ?? null;
+        return null === $lastIndex ? null : $this->profiles[$lastIndex];

src/Container/AdapterInterfaceFactory.php:36 docblock-type-mismatch, redundant-null-coalesce

-        /** @var array<array-key, mixed> $config */
-        $config = $container->get('config') ?? [];
+        /** @var mixed $config */
+        $config = $container->get('config');
+        if (! is_array($config)) {
+            $config = [];
+        }

src/Metadata/Source/AbstractSource.php:699 mixed-array-assignment

      * @mago-expect analysis:mixed-assignment
+     * @mago-expect analysis:mixed-array-assignment
      * @mago-expect lint:no-isset

src/ResultSet/AbstractResultSet.php:124 redundant-condition

+        // An internal class may implement Traversable without Iterator or IteratorAggregate,
+        // so the guard stays even though a userland Traversable narrows to Iterator here.
+        // @mago-expect analysis:redundant-condition
         if ($dataSource instanceof Iterator) {

src/Sql/AbstractSql.php:367 redundant-null-coalesce

-                $joinName = $joinName[$alias] ?? '';
+                $joinName = $joinName[$alias];

src/TableGateway/AbstractTableGateway.php:32 overly-wide-return-type

- * @property int $lastInsertValue
+ * @property string|int|false|null $lastInsertValue
tyrsson and others added 2 commits November 26, 2026 22:16
Formatting moves with the 1.61 formatter. The analyzer findings it raises are
answered in code: the Traversable guard in resolveIterator stays because an
internal class may implement Traversable without Iterator or IteratorAggregate,
the metadata hierarchy walk keeps its mixed-array access under an expect, and
AdapterInterfaceFactory narrows the config service before indexing it. The
@Property for lastInsertValue now matches the property it documents, and two
redundant null coalesces are gone.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The dialect is known when the platform is selected, so it is known when a statement is
created. StatementFactoryInterface puts the statement construction on the platform: the
standard SQL implementation returns the PhpDb\\Sql\\Ddl and DML classes, and a driver returns
its own subclasses where its dialect renders a statement differently. Sql delegates its four
DML factories to it and gains createTable(), alterTable() and dropTable().

With the statement carrying the dialect, the decoration machinery has nothing left to do, so
PlatformDecoratorInterface, setSubject(), AbstractSql::\, localizeVariables(),
getTypeDecorator(), setTypeDecorator(), the name-keyed decorator map and the two decorator
exceptions are removed. AbstractSql gains prepareSpecifications() in place of the state copy,
and processSubSelect() renders the subselect directly rather than cloning a decorator over it.

A statement constructed directly still renders with standard SQL rules.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

tyrsson and others added 2 commits November 26, 2026 22:30
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tyrsson
tyrsson force-pushed the rfc/256-statement-factory branch 2 times, most recently from 144398b to 954de7a Compare November 27, 2026 05:31
Driverless value quoting is no longer supported: a platform asks its driver to quote a value, and
one without a driver throws rather than guess. AbstractSql::getSqlString() still defaulted to a
driverless Sql92, so whether a statement rendered depended on whether it carried values.

The platform is now a required parameter on SqlInterface and AbstractSql. Sql92 itself is
unchanged, and with no default left it is reachable only from the test suite: nothing in src
selects it.

The adapter and SQL chapters state the quoting requirement, and the bare getSqlString() calls in
the DDL and statement tests now pass a trusting standard SQL platform.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant