2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
37 #include "ucx/string.h"
41 * Enumeration of possible statement types.
43 typedef enum {DAVQL_ERROR, DAVQL_SELECT, DAVQL_SET} davqltype_t;
46 * Enumeration of possible token classes.
49 DAVQL_TOKEN_INVALID, DAVQL_TOKEN_KEYWORD,
50 DAVQL_TOKEN_IDENTIFIER, DAVQL_TOKEN_FMTSPEC,
51 DAVQL_TOKEN_STRING, DAVQL_TOKEN_NUMBER, DAVQL_TOKEN_TIMESTAMP,
52 DAVQL_TOKEN_COMMA, DAVQL_TOKEN_OPENP, DAVQL_TOKEN_CLOSEP,
53 DAVQL_TOKEN_OPERATOR, DAVQL_TOKEN_END
57 * Enumeration of possible expression types.
61 DAVQL_NUMBER, DAVQL_STRING, DAVQL_TIMESTAMP, DAVQL_IDENTIFIER,
62 DAVQL_UNARY, DAVQL_BINARY, DAVQL_LOGICAL, DAVQL_FUNCCALL
66 * Enumeration of possible expression operators.
69 DAVQL_NOOP, DAVQL_CALL, DAVQL_ARGLIST, // internal representations
70 DAVQL_ADD, DAVQL_SUB, DAVQL_MUL, DAVQL_DIV,
71 DAVQL_AND, DAVQL_OR, DAVQL_XOR, DAVQL_NEG, // airthmetic
72 DAVQL_NOT, DAVQL_LAND, DAVQL_LOR, DAVQL_LXOR, // logical
73 DAVQL_EQ, DAVQL_NEQ, DAVQL_LT, DAVQL_GT, DAVQL_LE, DAVQL_GE,
74 DAVQL_LIKE, DAVQL_UNLIKE // comparisons
78 davqltokenclass_t tokenclass;
83 * An expression within a DAVQL query.
85 typedef struct _davqlexpr DavQLExpression;
88 * The structure for type DavQLExpression.
92 * The original expression text.
93 * Contains the literal value, if type is LITERAL.
97 * The expression type.
105 * Left or single operand.
106 * <code>NULL</code> for literals or identifiers.
108 DavQLExpression *left;
111 * <code>NULL</code> for literals, identifiers or unary expressions.
113 DavQLExpression *right;
117 * A tuple representing an order criterion.
123 DavQLExpression *column;
125 * True, if the result shall be sorted descending, false otherwise.
126 * Default is false (ascending).
129 } DavQLOrderCriterion;
132 * A tuple representing a field.
138 * <li>SELECT: the identifier or an alias name</li>
139 * <li>SET: the identifier</li>
144 * The field expression.
146 * <li>SELECT: the queried property (identifier) or an expression</li>
147 * <li>SET: the expression for the value to be set</li>
150 DavQLExpression *expr;
154 * Query statement object.
155 * Contains the binary information about the parsed query.
157 * The grammar for a DavQLStatement is:
160 * Keyword = "select" | "set" | "from" | "at" | "as"
161 * | "where" | "anywhere" | "like" | "unlike"
162 * | "and" | "or" | "not" | "xor" | "with" | "infinity"
163 * | "order" | "by" | "asc" | "desc";
165 * Expression = AddExpression;
166 * AddExpression = MultExpression, [AddOperator, AddExpression];
167 * MultExpression = BitwiseExpression, [MultOperator, MultExpression];
168 * BitwiseExpression = UnaryExpression, [BitwiseOperator, BitwiseExpression];
169 * UnaryExpression = [UnaryOperator], (ParExpression | AtomicExpression);
170 * AtomicExpression = FunctionCall | Identifier | Literal;
171 * ParExpression = "(", Expression, ")";
173 * BitwiseOperator = "&" | "|" | "^";
174 * MultOperator = "*" | "/";
175 * AddOperator = "+" | "-";
176 * UnaryOperator = "+" | "-" | "~";
178 * FunctionCall = Identifier, "(", [ArgumentList], ")";
179 * ArgumentList = Expression, {",", Expression};
180 * Identifier = IdentifierChar - ?Digit?, {IdentifierChar}
181 * | "`", ?Character? - "`", {?Character? - "`"}, "`";
182 * IdentifierChar = ?Character? - (" "|",");
183 * Literal = Number | String | Timestamp;
184 * Number = ?Digit?, {?Digit?} | "%d";
185 * String = "'", {?Character? - "'" | "'''"} , "'" | "%s";
186 * Timestamp = "%t"; // TODO: maybe introduce a real literal
188 * LogicalExpression = BooleanExpression, [LogicalOperator, LogicalExpression];
189 * BooleanExpression = "not ", BooleanExpression
190 * | "(", LogicalExpression, ")"
192 * BooleanPrimary = Expression, (" like " | " unlike "), String
193 * | Expression, Comparison, Expression
194 * | FunctionCall | Identifier;
196 * LogicalOperator = " and " | " or " | " xor ";
197 * Comparison = | "=" | "<" | ">" | "<=" | ">=" | "!=";
199 * FieldExpressions = "-"
200 * | "*", {",", NamedField}
201 * | FieldExpression, {",", FieldExpression};
202 * FieldExpression = NamedField | Identifier;
203 * NamedField = Expression, " as ", Identifier;
205 * Assignments = Assignment, {",", Assignment};
206 * Assignment = Identifier, "=", Expression;
209 * | "/", [PathNode, {"/", PathNode}], ["/"];
210 * PathNode = {{?Character? - "/"} - Keyword};
212 * WithClause = "depth", "=", (Number | "infinity");
214 * OrderByClause = OrderByCriterion, {",", OrderByCriterion};
215 * OrderByCriterion = (Identifier | Number), [" asc"|" desc"];
219 * Note: mandatory spaces are part of the grammar. But you may also insert an
220 * arbitrary amount of optional spaces between two symbols if they are not part
221 * of an literal, identifier or the path.
225 * SelectStatement = "select ", FieldExpressions,
227 * [" with ", WithClause],
228 * [(" where ", LogicalExpression) | " anywhere"],
229 * [" order by ", OrderByClause];
234 * SetStatement = "set ",Assignments,
236 * [" with ", WithClause],
237 * (" where ", LogicalExpression) | " anywhere";
243 * The original query text.
247 * The statement type.
251 * Error code, if any error occurred. Zero otherwise.
255 * Error message, if any error occurred.
259 * The list of DavQLFields.
263 * A string that denotes the queried path.
267 * Logical expression for selection.
268 * <code>NULL</code>, if there is no where clause.
270 DavQLExpression* where;
272 * The list of DavQLOrderCriterions.
273 * This is <code>NULL</code> for SET queries and may be <code>NULL</code>
274 * if the result doesn't need to be sorted.
278 * The recursion depth for the statement.
280 * Magic numbers are DAV_DEPTH_INFINITY for infinity and
281 * DAV_DEPTH_PLACEHOLDER for a placeholder.
285 * A list of all required arguments
290 /** Infinity recursion depth for a DavQLStatement. */
291 #define DAV_DEPTH_INFINITY -1
293 /** Depth needs to be specified at runtime. */
294 #define DAV_DEPTH_PLACEHOLDER -2
296 /** Unexpected token. */
297 #define DAVQL_ERROR_UNEXPECTED_TOKEN 1
299 /** A token has been found, for which no token class is applicable. */
300 #define DAVQL_ERROR_INVALID_TOKEN 2
302 /** A token that has been expected was not found. */
303 #define DAVQL_ERROR_MISSING_TOKEN 11
305 /** An expression has been expected, but was not found. */
306 #define DAVQL_ERROR_MISSING_EXPR 12
308 /** A closed parenthesis ')' is missing. */
309 #define DAVQL_ERROR_MISSING_PAR 13
311 /** An assignment operator '=' is missing. */
312 #define DAVQL_ERROR_MISSING_ASSIGN 14
314 /** The type of the expression could not be determined. */
315 #define DAVQL_ERROR_INVALID_EXPR 21
317 /** An operator has been found for an unary expression, but it is invalid. */
318 #define DAVQL_ERROR_INVALID_UNARY_OP 22
320 /** An operator has been found for a logical expression, but it is invalid. */
321 #define DAVQL_ERROR_INVALID_LOGICAL_OP 23
323 /** Invalid format specifier. */
324 #define DAVQL_ERROR_INVALID_FMTSPEC 24
326 /** A string has been expected. */
327 #define DAVQL_ERROR_INVALID_STRING 25
329 /** The order criterion is invalid (must be an identifier or field index). */
330 #define DAVQL_ERROR_INVALID_ORDER_CRITERION 26
332 /** The depth is invalid. */
333 #define DAVQL_ERROR_INVALID_DEPTH 101
335 /** Nothing about the statement seems legit. */
336 #define DAVQL_ERROR_INVALID -1
338 /** A call to malloc or calloc failed. */
339 #define DAVQL_ERROR_OUT_OF_MEMORY -2
342 * Starts an interactive debugger for a DavQLStatement.
344 * @param stmt the statement to debug
346 void dav_debug_statement(DavQLStatement *stmt);
349 * Parses a statement.
350 * @param stmt the sstr_t containing the statement
351 * @return a DavQLStatement object
353 DavQLStatement* dav_parse_statement(sstr_t stmt);
356 * Implicitly converts a cstr to a sstr_t and calls dav_parse_statement.
358 #define dav_parse_cstr_statement(stmt) dav_parse_statement(S(stmt))
361 * Frees a DavQLStatement.
362 * @param stmt the statement object to free
364 void dav_free_statement(DavQLStatement *stmt);
370 #endif /* DAVQLPARSER_H */