Skip to content

Apply the dialect when a statement is rendered - #261

Draft
simon-mundy wants to merge 4 commits into
php-db:rfc/256-platform-value-quotingfrom
simon-mundy:rfc/256-render-time-dialect
Draft

simon-mundy wants to merge 4 commits into
php-db:rfc/256-platform-value-quotingfrom
simon-mundy:rfc/256-render-time-dialect

Conversation

@simon-mundy

@simon-mundy simon-mundy commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Applies the dialect when a statement is rendered, instead of when it is created.

Depends on #258 and #259. This branch is stacked on #259 and changes nothing they delete: the decorators, $subject, the name-keyed registry and the Sql92 default all stay gone. The only thing that moves is where the dialect is applied.

Companion: php-db/phpdb-mysql#90

Why

With the statement factory, a statement gets its dialect from the factory that built it, so a statement built any other way has none. A subselect made with new Select() renders as standard SQL inside a dialect statement:

$sub    = (new Select('film_actor'))->columns(['film_id'])->offset(5);
$select = $sql->select('film');
$select->where->in('film_id', new Argument\Select($sub));

On #259 with php-db/phpdb-mysql#89 the subselect renders OFFSET 5 with no LIMIT, which MySQL rejects. On 0.6.x the decorators handled this case, so it is a change in behaviour. With this branch the output matches 0.6.x.

What changes

  • Statements are dialect-neutral. Sql::select(), insert(), update(), delete(), createTable(), alterTable() and dropTable() return the standard classes, the same objects new gives you.
  • PlatformInterface::getStatementFactory() becomes getDialect(): DialectInterface.
  • AbstractSql::buildSqlString() asks the platform's dialect which clauses it renders itself, each time a statement is rendered. That reaches every statement the platform renders: subselects, statements built with new, and statements changed by a TableGateway event listener.
  • New in PhpDb\Sql\Dialect:
    • DialectInterface: clauseRenderers(SqlInterface $statement) and columnRenderer().
    • ClauseRendererInterface: returns the finished clause, an empty string to leave it out, or null to render it as standard SQL.
    • ColumnRendererInterface: adjusts one DDL column definition; core keeps the layout of the column list.
    • RenderContext: what a renderer works through. It binds values under the statement's own parameter prefix (so a renderer inside a subselect binds :subselect1limit, not :limit) and renders expressions and subselects with the statement's numbering.
    • FixedClause: a Stringable a dialect can list in place of a renderer for a clause that never varies. It is used without building a context.
    • StandardDialect: renders nothing itself. Sql92 returns it.
  • Select::getLimit() and getOffset(), so a renderer reads typed state.
  • Removed: StatementFactoryInterface, Sql\Platform\AbstractPlatform, Sql\Platform\Platform, Sql::getStatementFactory() and AbstractSql::prepareSpecifications().

Drivers no longer override process*() or touch $specifications, so those could be marked @internal and the render pipeline changed later without breaking a driver.

EventFeature

A listener is always handed the object that renders, because there is only one object and it is never replaced. The existing EventFeature, TableGateway and RowGateway tests pass unchanged. test/integration/TableGateway/EventFeatureDialectTest.php adds an end-to-end case: a preSelect listener adds an offset and the test checks the SQL the driver receives.

Checked

Not done

  • phpdb-sqlite and phpdb-pgsql are not ported.
  • A dialect cannot change how a single expression renders. That would need a per-expression hook, which is left out.
A statement built by the platform's factory carried its dialect from creation, so a statement
built any other way did not. A subselect made with new Select() rendered as standard SQL inside
a dialect statement, which for MySQL is an OFFSET with no LIMIT.

Statements are now dialect-neutral, and Sql builds the standard classes again. The platform
names its dialect through PlatformInterface::getDialect(), and AbstractSql asks that dialect
which clauses it renders itself each time a statement is rendered. That reaches every statement
the platform renders, including subselects and statements changed by a TableGateway event
listener, and the listener is always handed the object that renders.

A clause renderer returns the finished clause, an empty string to leave it out, or null to have
it rendered as standard SQL. It works through a RenderContext the statement builds for each
render, which binds values under that statement's parameter prefix and renders expressions and
subselects with the statement's own numbering. DDL column definitions go through a column
renderer, so core keeps the layout of the column list.

Select gains getLimit() and getOffset() so a renderer reads typed state. The statement factory,
Sql::getStatementFactory() and AbstractSql::prepareSpecifications() are removed.
A dialect may now give a clause as a Stringable in place of a renderer. FixedClause is the one
core provides. The statement uses it as the finished clause without building a render context,
which is what a renderer that reads nothing from the render was paying for.

DialectInterface::clauseRenderers() documents that it is passed the statement so a dialect can
leave out a clause it would only hand back to standard SQL.

The DDL statements resolve the column renderer and its context once for a list of columns
instead of once for each column.
@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

A render context no longer creates two closures of its own. AbstractSql keeps one shared pair,
created on first use, and the context calls them with its statement.

The DDL statements return early for an empty column list, so an ALTER TABLE with no added or
changed columns does not ask the dialect for a column renderer, and the context for a column
list is built when its first column is rendered.
Nothing reads them: the methods that bind check the driver and parameter container themselves,
and Mago analyses the class without them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant