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
becomeswill become SQL in one place, so quoting and escapingarewill be settled once rather than per class. - A placeholder and its value
cannotwill not be able to disagree, because bothare produced bywill come from the same pass over thesamesame list. - Everything a complete statement allows
arriveswill happen at one point before it becomes a string: logging it,explainingexplaining it,, prefixing its tables, and refusing a statement that should not run. - A
module canmodule will be able to add a construct by registering a node anditsits compiler, rather than subclassing the engine'squeryquery objects.
Harder:
- The whole layer
iswill be restructured, every test asserting SQLiswill be rewritten, andthe connection needsa connection will need a compilertoto execute anything. - The layer's coverage and mutation score will have to be re-earned rather than preserved.
Constrained:
- Being executable
stopswill stop meaning "has a method that returns SQL" andstartsstart meaning "a compiler is registered for this node", so a half-configured objectcannotwill not beexecutedexecutable by accident. Identifiers areNames will be quoted unconditionally, whichmakes nameswill make them case sensitive exactly as written.
Sources
- Issue #51, Engine - Database - PostgreSQL, : the architecture
,; that thirty-three classeseach 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, catalogueandand compiled-result shape,; what itbuysbuys, in order of value,; and its cost. Thisisiscreatedanddecided. - 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