miniorm.query_ast

Members

Aliases

ColumnName
alias ColumnName = Blob
Undocumented in source.
Expr
alias Expr = Blob
Undocumented in source.
InsertDefaultValue
alias InsertDefaultValue = Constant!"DEFAULT VALUES"
Undocumented in source.
None
alias None = Constant!(string.init)
Undocumented in source.
Star
alias Star = Constant!"*"
Undocumented in source.
Window
alias Window = Blob
Undocumented in source.

Enums

InsertOpt
enum InsertOpt

Based on those that are valid in SQLite.

OrderingTermSort
enum OrderingTermSort
Undocumented in source.
WhereOp
enum WhereOp
Undocumented in source.

Structs

Blob
struct Blob
Undocumented in source.
ColumnNames
struct ColumnNames
Undocumented in source.
Constant
struct Constant(string s)

A node representing a constant value.

Delete
struct Delete

# Insert END # Delete START

From
struct From
Undocumented in source.
Insert
struct Insert

# Select END # Insert START

InsertColumns
struct InsertColumns
Undocumented in source.
InsertValues
struct InsertValues
Undocumented in source.
Limit
struct Limit
Undocumented in source.
LimitOffset
struct LimitOffset
Undocumented in source.
OrderBy
struct OrderBy
Undocumented in source.
OrderingTerm
struct OrderingTerm
Undocumented in source.
Query
struct Query

A SQL query.

ResultColumn
struct ResultColumn
Undocumented in source.
ResultColumnExpr
struct ResultColumnExpr
Undocumented in source.
ResultColumns
struct ResultColumns
Undocumented in source.
SchemaName
struct SchemaName
Undocumented in source.
Select
struct Select

# Select START A Select statement.

Sql
struct Sql

A SQL statement.

TableAlias
struct TableAlias
Delete END
TableOrQuery
struct TableOrQuery
Undocumented in source.
TableOrSubQueries
struct TableOrSubQueries
Undocumented in source.
TableOrSubQuerySelect
struct TableOrSubQuerySelect
Undocumented in source.
TableRef
struct TableRef

Reference to a table with options to reference another schema and/or create an alias.

Value
struct Value
Undocumented in source.
Values
struct Values
Undocumented in source.
Where
struct Where
Undocumented in source.
WhereExpr
struct WhereExpr
Undocumented in source.

Meta

Authors

Joakim Brännström (joakim.brannstrom@gmx.com)

This module contains an AST representation of a database query. The intention is to encode the structure in the types so errors in the SQL statement are detected at compile time.

Grammar

The grammar is expressed in PEG form using the same terminology as the dub package Pegged. It has not been verified to be correct so may contain errors.

This is simplified to only contain those parts of the grammar that is needed to pretty print constructed SQL queries. It is not intended to be a grammar that can correctly parse SQL. Thus it contains "blobs of data" that the grammar do not care about. The important part is to keep the structure. Where to insert "blobs".

A secondary purpose is to give some compile time guarantees of the constructed SQL queries. If it compiles it is reasonably assured to be correct.

When not necessary for pretty printing as a valid SQL blanks and such are skipped in the grammar definition.

1 SQL         <- blank* Query (spacing / eoi)
2 
3 Query       <- Select
4 
5 # --- SELECT START ---
6 Select      <- "SELECT" :blank+ ResultColumns :blank+ From? Where? GroupBy? (Window :blank+)? OrderBy? Values? Limit?
7 
8 ResultColumns       <- ResultColumn ("," ResultColumn)*
9 ResultColumn        <- Star / ResultColumnExpr
10 ResultColumnExpr    <- Query / Blob
11 
12 From    <- :blank+ "FROM" :blank+ (TableOrSubQueries / Blob)
13 Where   <- :blank+ "WHERE" :blank+ WhereExpr*
14 GroupBy <- :blank+ "GROUP BY" :blank+ Expr ("," Expr)* (:blank+ "HAVING" Expr)?
15 Window  <- Blob
16 
17 WhereExpr   <- Expr (:blank+ WhereOp :blank+ Expr)?
18 WhereOp     <- "AND" / "OR"
19 
20 OrderBy         <- :blank+ "ORDER BY" :blank+ OrderingTerm ("," OrderingTerm)
21 OrderingTerm    <- Expr :blank+ OrderingTermSort?
22 OrderingTermSort <- "ASC" / "DESC" / ""
23 
24 Limit <- "LIMIT" :blank+ Expr :blank+ (("OFFSET" :blank+ Expr) / ("," Expr))?
25 
26 TableOrSubQueries       <- TableOrQuery ("," TableOrSubQuery)*
27 TableOrSubQuery         <- TableOrSubQuerySelect / ("(" TableOrSubQueries ")") / (TableRef Blob*) / Blob
28 TableOrSubQuerySelect   <- "(" Select ")" TableAlias?
29 
30 # --- SELECT END ---
31 
32 # --- INSERT START ---
33 Insert          <- InsertOpt :blank+ "INTO" :blank+ TableRef TableAlias? InsertColumns? InsertValues
34 InsertOpt       <- "INSERT" / "REPLACE" / "INSERT OR REPLACE" / "INSERT OR ROLLBACK" / "INSERT OR ABORT" / "INSERT OR FAIL" / "INSERT OR IGNORE"
35 InsertColumns    <- :blank+ "(" ColumnName ("," ColumnName)* ")"
36 InsertValues    <- :blank+ (Values / Select / "DEFAULT VALUES")
37 ColumnName      <- identifier
38 
39 # --- INSERT END ---
40 
41 # --- DELETE START ---
42 Delete          <- "DELETE FROM" :blank+ TableRef Where?
43 # --- DELETE END ---
44 
45 # Reference an existing table
46 TableRef            <- SchemaName? TableName TableAlias?
47 
48 Values  <- :blank+ "VALUES" "(" Value ")" ("(" Value ")")*
49 Value   <- Expr ("," Expr)*
50 
51 TableAlias  <- :blank+ "AS" :blank+ identifier
52 
53 Expr        <- Blob
54 # Not representable in the grammar because it can be anything without a formal
55 # terminator. Its purpose is to be an injection point of user data.
56 Blob        <- ""
57 
58 SchemaName  <- identifier "."
59 TableName   <- identifier
60 Star        <- "*"

Grammar Encoding

* SumType is used when possible. * None is the first member of a SumType when the node is optional. * Literals are used as is. * All nodes have a toString.