The Game Panel Design record
The Game Panel
ADR-0017 Accepted

Produce SQL is produced by with a compiler, not by the query objects

Comparing
v1 to v2
Current
Read the document

Context

Every object in the query and schema builders produces its own SQL, per as RFC-0003 describes. Thirty-two classes each write their own, interpolating names names straight into the string and returning their their bound values separately.

Two consequences follow defects come from that shape rather than from any one class. Names are written unquoted in some places and quoted and quoted in others, because each class decides for itself. And an An insert computes works out its column list once for its SQL and and again for for its bound values, so the two can disagree and bind values to the wrong columns.

Fixing either one defect class by class means fixing it thirty-two times times, and trusting whoever writes the thirty-thirdthird class to do the same.

Decision

I will have SQL is produced by a compiler rather than by the objects being compiled. A : a query or or schema object is will be a node node describing what is wanted, and a the compiler resolved by node registered for the node's class turns will turn it into SQL. Compilation returns , compiling will return the SQL and and its bound values together, produced by together from the same pass, and every every name written into SQL goes will go through one identifier identifier value object that quotes and escapes it.

Alternatives

An identifier value object alone. Quoting was originally to be fixed by introducing that object and using it throughout. The object survives as part of this decision, but . I rejected it as the whole fix because on its own it leaves leaves thirty-two classes each each choosing whether to use it, and does nothing about SQL and bound values being produced produced separately.

Retrofitting quoting across the self-compiling classes. Each class would quote the names it writes, then dismantling as it writes them. I rejected this because the retrofit would have to be dismantled once a compiler arrived anyway.

Consequences

Easier:

  • A name becomes will become SQL in one place, so quoting and escaping are will be settled once rather than per class.
  • A placeholder and its value cannot will not be able to disagree, because both are produced by will come from the same pass over the same same list.
  • Everything a complete statement allows arrives will happen at one point before it becomes a string: logging it, explaining explaining it, , prefixing its tables, and refusing a statement that should not run.
  • A module can module will be able to add a construct by registering a node and its its compiler, rather than subclassing the engine's query query objects.

Harder:

  • The whole layer is will be restructured, every test asserting SQL is will be rewritten, and the connection needs a connection will need a compiler to to execute anything.
  • The layer's coverage and mutation score will have to be re-earned rather than preserved.

Constrained:

  • Being executable stops will stop meaning "has a method that returns SQL" and starts start meaning "a compiler is registered for this node", so a half-configured object cannot will not be executed executable by accident.
  • Identifiers are Names will be quoted unconditionally, which makes names will make them case sensitive exactly as written.

Sources

  • Issue #51, Engine - Database - PostgreSQL, : the architecture, ; that thirty-three classes each define, define their own SQL production, a count that includes the contract declaring the method alongside the thirty-two implementing it, which define their own SQL production, ; that this is the cause of both the unquoted names and the insert binding defect, the ; the node, compiler, catalogue and and compiled-result shape, ; what it buys buys, in order of value, ; and its cost. This is is created and decided.
  • Issue #52, Engine - Database - Compiler seam, , with no edits: the contracts, the compiled result carrying SQL and bindings together, the identifier value object with its quoting, qualification, wildcard and length rules, the literal quoter, and the hook where a complete statement can be refused.
  • Issue #41, Engine - Query Builder - Identifier quoting and escaping, , closed : the quoting and escaping that each class decided for itself. Its closing comment records the rejected retrofit across thirty-three self-compiling classes, which would have been built and then dismantled when the compiler arrived.
  • Issue #42, Engine - Query Builder - Binding and clause correctness, , closed : an insert's SQL and bound values computed separately and disagreeing. Its closing comment records the defect becoming unrepresentable once both come from one pass.
  • The identifier value object having originally been the whole intended fix for quoting: first written down on .

Source adr/0017-sql-compiler.md · commit 9444a99 · published