File: query.bnf

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (223 lines) | stat: -rw-r--r-- 10,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
optional delimiter = lang.bs.SDelimiter;
required delimiter = lang.bs.SRequiredDelimiter;

// Connection between a db connection and an sql statement.
lang.bs.SExpr => queryExpr(pos, block, c, query) : "WITH" #keyword ~ lang.bs.SExpr(block) c, ":", SQuery query;
lang.bs.SExpr => queryExpr(pos, block, c, query) : "with" #keyword ~ lang.bs.SExpr(block) c, ":", SQuery query;

// Allow WITH-blocks, both as expressions and as statements.
lang.bs.SStmt => x : SWithBlock(block) x;
lang.bs.SExpr => x : SWithBlock(block) x;

lang.bs.Expr SWithBlock(lang.bs.Block block);
SWithBlock => QueryBlock(pos, block, c) : "WITH" #keyword ~ lang.bs.SExpr(block) c, "{" [, (lang.bs.SBlockItem(me), )* ]+ "}";
SWithBlock => QueryBlock(pos, block, c) : "with" #keyword ~ lang.bs.SExpr(block) c, "{" [, (lang.bs.SBlockItem(me), )* ]+ "}";

// Allow queries as "regular" statements inside these blocks.
// Note: The priority is needed as queries like: SELECT * FROM y; can be parsed as a multiplication
// with a variable declaration.
SWithBlock..lang.bs.SExpr[20] => queryBlockExpr(pos, block, query) : SQuery query;

// Transaction blocks.
lang.bs.SStmt => TransactionBlock(pos, block, c) : "WITH" #keyword ~ lang.bs.SExpr(block) c, ":", STransaction(me);
lang.bs.SStmt => TransactionBlock(pos, block, c) : "with" #keyword ~ lang.bs.SExpr(block) c, ":", STransaction(me);
SWithBlock..lang.bs.SStmt => TransactionBlock(pos, block) : STransaction(me);

lang.bs.SExpr => TransactionBlock(pos, block, c) : "WITH" #keyword ~ lang.bs.SExpr(block) c, ":", STransaction(me);
lang.bs.SExpr => TransactionBlock(pos, block, c) : "with" #keyword ~ lang.bs.SExpr(block) c, ":", STransaction(me);
SWithBlock..lang.bs.SExpr => TransactionBlock(pos, block) : STransaction(me);

void STransaction(TransactionBlock block);
STransaction : "TRANSACTION" #keyword, "{" [, (lang.bs.SBlockItem(block.child), )* ]+ "}";

STransaction..lang.bs.SStmt => TransactionAbort(pos, block, false, result) : "ROLLBACK" #keyword - (~ lang.bs.SExpr(block) result)?, ";";
STransaction..lang.bs.SStmt => TransactionAbort(pos, block, true, result) : "COMMIT" #keyword - (~ lang.bs.SExpr(block) result)?, ";";

// Base of an SQL query.
Query SQuery();

// Insert:
SQuery => InsertQuery(pos, table, init, returning) : "INSERT" #keyword ~ ("INTO" #keyword ~)? [SName table ~ "VALUES" #keyword, "(", SExprList init, ")"]+ - SReturning returning;
SQuery => InsertQuery(pos, table, cols, init, returning) : "INSERT" #keyword ~ ("INTO" #keyword ~)? [SName table, "(", SNameList cols, ")", "VALUES" #keyword, "(", SExprList init, ")"]+ - SReturning returning;

// Update:
SQuery => UpdateQuery(pos, table, assign, condition, returning) : "UPDATE" #keyword ~ [SName table ~ "SET" #keyword ~ SAssignExprs assign - SWhere condition]+ - SReturning returning;

// Delete:
SQuery => DeleteQuery(pos, table, condition, returning) : "DELETE" #keyword ~ ("FROM" #keyword ~)? [SName table - SWhere condition]+ - SReturning returning;

// Select:
SQuery => SelectQuery(pos, table, cols, joins, condition, orderBy) : "SELECT" #keyword ~ [SSelectCols cols ~ "FROM" #keyword ~ STableName table - SJoins joins - SWhere condition - SOrderBy orderBy]+;

// Select one row (or zero):
SQuery[10] => SelectOneQuery(pos, table, cols, joins, condition, orderBy) : "SELECT" #keyword ~ "ONE" #keyword ~ [SSelectCols cols ~ "FROM" #keyword ~ STableName table - SJoins joins - SWhere condition - SOrderBy orderBy]+;

// Count rows:
SQuery => CountQuery(pos, table, joins, condition) : "COUNT" #keyword ~ "FROM" #keyword ~ [STableName table - SJoins joins - SWhere condition]+;

// Create table (only untyped)
SQuery => CreateQuery(pos, false, table) : "CREATE" #keyword ~ "TABLE" #keyword ~ SCreateTail table;
SQuery => CreateQuery(pos, true, table) : "CREATE" #keyword ~ "TABLE" #keyword ~ "IF" #keyword ~ "NOT" #keyword ~ "EXISTS" #keyword ~ SCreateTail table;

// Add an index (only untyped)
SQuery => IndexQuery(pos, name, table, cols) : "CREATE" #keyword ~ "INDEX" #keyword ~ SName name ~ "ON" #keyword ~ SName table, "(", [SNameList cols]+, ")";
SQuery => IndexQuery(pos, table, cols) : "CREATE" #keyword ~ "INDEX" #keyword ~ "ON" #keyword ~ SName table, "(", [SNameList cols]+, ")";

// Drop table (only untyped)
SQuery => DropQuery(pos, name) : "DROP" #keyword ~ "TABLE" #keyword ~ SName name;

// Helper rule for CREATE TABLE.
Table SCreateTail();
SCreateTail => Table(name) : SName name, "(", [, STableContent(me), ]+ ")";

// An optional RETURNING clause.
Maybe<ReturningClause> SReturning();
SReturning => Maybe<ReturningClause>() : ;
SReturning => ReturningClause(pos, cols) : ~ "RETURNING" #keyword [ ~ SSelectCols cols ]+;

// A WHERE clause that might be missing.
Maybe<SQLExpr> SWhere();
SWhere => Maybe<SQLExpr>() : ;
SWhere => Maybe<SQLExpr>(x) : ~ "WHERE" #keyword ~ SExpr x;

// ORDER BY clauses
Array<SelectQuery.OrderBy> SOrderBy();
SOrderBy => Array<SelectQuery.OrderBy>() : ;
SOrderBy => Array<SelectQuery.OrderBy>() : ~ "ORDER" #keyword ~ "BY" #keyword ~ SOrderByItem -> push - (, ",", SOrderByItem -> push)*;

SelectQuery.OrderBy SOrderByItem();
SOrderByItem => SelectQuery.OrderBy(pos, table, column, asc) : (SName table, "\.",)? - SName column - SAscDesc asc;

Bool SAscDesc() #keyword;
SAscDesc => true : ;
SAscDesc => true : ~ "ASC";
SAscDesc => false : ~ "DESC";

// Possible columns for a SELECT query.
Array<SelectedColumn> SSelectCols();
SSelectCols => Array<SelectedColumn>() : SSelectCol -> push - (, ",", SSelectCol -> push)*;
SSelectCols => Array<SelectedColumn>() : "\*";

SelectedColumn SSelectCol();
SSelectCol => x : SSelectColI x;
// Note: The AS keyword is optional in many dialects. We require it as it easily leads
// to confusion when one mistypes a later part of a query. E.g. ... FROM foo OUTER JOIN bar
// would be the same as: ... FROM foo AS OUTER JOIN bar ... which was not intended.
SSelectCol => x : SSelectColI x ~ "AS" #keyword ~ SName -> setAs;

SelectedColumn SSelectColI();
SSelectColI => SelectedColumn(pos, column) : SName column;
SSelectColI => SelectedColumn(pos, table, column) : SName table, "\.", SName column;

// Join clauses for a table.
Array<SelectQuery.Join> SJoins();
SJoins => Array<SelectQuery.Join>() : (~ SJoin -> push)*;

SelectQuery.Join SJoin();
SJoin => SelectQuery.Join(type, table, condition) : SJoinType type ~ STableName table ~ "ON" #keyword ~ SExpr condition;

// Types of joins.
SelectQuery.JoinType SJoinType() #keyword;
SJoinType => SelectQuery.JoinType.left() : "LEFT" ~ "JOIN";
SJoinType => SelectQuery.JoinType.right() : "RIGHT" ~ "JOIN";
SJoinType => SelectQuery.JoinType.inner() : ("INNER" ~)? - "JOIN";
SJoinType => SelectQuery.JoinType.full() : "FULL" - (~ "OUTER")? ~ "JOIN";

// A list of SQL expressions.
Array<SQLExpr> SExprList();
SExprList => Array<SQLExpr>() : SExpr -> push - (, ",", SExpr -> push)*;

// An SQL expression that evaluates to some value. We might want to piggy-back on the expressions of Basic Storm here.
SQLExpr SExpr();
// Here we have operators ALL, ANY, BETWEEN, IN, LIKE, OR, SOME
SExpr => x : SExpr1 x;

SQLExpr SExpr1();
// Operator AND
SExpr1 => x : SExpr2 x;
SExpr1 => andOp(lhs, rhs, op) : SExpr1 lhs ~ "AND" @op #keyword~ SExpr2 rhs;

SQLExpr SExpr2();
// Operator OR
SExpr2 => x : SExpr3 x;
SExpr2 => orOp(lhs, rhs, op) : SExpr2 lhs ~ "OR" @op #keyword ~ SExpr3 rhs;

SQLExpr SExpr3();
// Operator NOT
SExpr3 => x : SExpr4 x;
SExpr3 => NotOperator(pos, x) : "NOT" #keyword ~ SExpr4 x;

SQLExpr SExpr4();
// Comparison operators.
SExpr4 => x : SExpr5 x;
SExpr4 => equalsOp(l, r, op) : SExpr5 l, "==?" @op, SExpr5 r;
SExpr4 => CompareOperator(l, r, op) : SExpr5 l, "<" @op, SExpr5 r;
SExpr4 => CompareOperator(l, r, op) : SExpr5 l, ">" @op, SExpr5 r;
SExpr4 => CompareOperator(l, r, op) : SExpr5 l, "<=" @op, SExpr5 r;
SExpr4 => CompareOperator(l, r, op) : SExpr5 l, ">=" @op, SExpr5 r;
SExpr4 => CompareOperator(l, r, op) : SExpr5 l, "!=" @op, SExpr5 r;
SExpr4 => LikeOperator(pos, l, r) : SExpr5 l ~ "LIKE" #keyword ~ SExpr5 r;
// Note: Could possibly be generalized to the IS operator that checks for booleans. However,
// the variant for NULL seems to be a special case.
SExpr4 => NullCheckOperator(pos, x, false) : SExpr5 x ~ "IS" #keyword ~ "NULL" #keyword;
SExpr4 => NullCheckOperator(pos, x, true) : SExpr5 x ~ "IS" #keyword ~ "NOT" #keyword ~ "NULL" #keyword;

SQLExpr SExpr5();
// Addition, subtraction, bitwise operators, etc.
// Also unary + and -.
SExpr5 => x : SExpr6 x;
SExpr5 => NumOperator(l, r, op) : SExpr5 l, "+" @op, SExpr6 r;
SExpr5 => NumOperator(l, r, op) : SExpr5 l, "-" @op, SExpr6 r;
SExpr5 => StrConcatOp(l, r, op) : SExpr5 l, "||" @op, SExpr6 r;


SQLExpr SExpr6();
// Multiplication, division, modulus.
SExpr6 => x : SExpr7 x;
SExpr6 => NumOperator(l, r, op) : SExpr6 l, "*" @op, SExpr7 r;
SExpr6 => NumOperator(l, r, op) : SExpr6 l, "/" @op, SExpr7 r;
SExpr6 => NumOperator(l, r, op) : SExpr6 l, "%" @op, SExpr7 r;

SQLExpr SExpr7();
// Bitwise not.
SExpr7 => x : SAtom x;

// Atomic expressions.
SQLExpr SAtom();
SAtom => x : SLiteral x;
SAtom => SQLName(pos, name) : lang.bs.SName name #varName; // Identifiers in the surrounding code.
SAtom => SQLName(pos, name) : SName name; // SQL identifiers, if they allow some other representation.
SAtom => SQLName(pos, table, column) : SName table #varName, "\.", SName column; // Disambiguation
SAtom => x : "(", SExpr x, ")";

// Basic Storm expressions.
SAtom => Escaped(expr) : "$", "{", lang.bs.SExpr @expr, "}";

// Built-in functions.
SAtom[10] => currentDateTime(pos) : "CURRENT" #keyword ~ "DATETIME" #keyword;

// SQL literals.
SQLLiteral SLiteral();
SLiteral => SQLInt(pos, v) : "-?[0-9]+" v #constant; // Integer.
SLiteral => SQLFloat(pos, v) : "-?[0-9]+\.[0-9]+" v #constant; // Float.
SLiteral => SQLStr(pos, v) : lang.bs.SString v #string; // String.

// Assigned value in UPDATE statement.
AssignExpr SAssignExpr();
SAssignExpr => AssignExpr(col, expr) : SName col, "=", SExpr expr;

Array<AssignExpr> SAssignExprs();
SAssignExprs => Array<AssignExpr>() : SAssignExpr -> push - (, ",", SAssignExpr -> push)*;

// Table name. Allows optionally renaming the table in the query.
TableName STableName();
STableName => TableName(table) : SName table;
STableName => TableName(table, as) : SName table ~ ("AS" ~)? SName as;

// SQL name. We might want to extend this eventually.
SStr SName() #varName;
SName => s : "[A-Za-z_][A-Za-z0-9_]*" @s;

// List of names.
Array<SStr> SNameList();
SNameList => Array<SStr>() : SName -> push - (, ",", SName -> push)*;