Repository navigation
Apply the dialect when a statement is rendered - #261
Draft
simon-mundy wants to merge 4 commits into
Draft
simon-mundy wants to merge 4 commits into
simon-mundy wants to merge 4 commits into
Conversation
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 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theSql92default 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:On #259 with php-db/phpdb-mysql#89 the subselect renders
OFFSET 5with noLIMIT, 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
Sql::select(),insert(),update(),delete(),createTable(),alterTable()anddropTable()return the standard classes, the same objectsnewgives you.PlatformInterface::getStatementFactory()becomesgetDialect(): 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 withnew, and statements changed by a TableGateway event listener.PhpDb\Sql\Dialect:DialectInterface:clauseRenderers(SqlInterface $statement)andcolumnRenderer().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: aStringablea 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.Sql92returns it.Select::getLimit()andgetOffset(), so a renderer reads typed state.StatementFactoryInterface,Sql\Platform\AbstractPlatform,Sql\Platform\Platform,Sql::getStatementFactory()andAbstractSql::prepareSpecifications().Drivers no longer override
process*()or touch$specifications, so those could be marked@internaland 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.phpadds an end-to-end case: apreSelectlistener adds an offset and the test checks the SQL the driver receives.Checked
Not done