1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
5 <article xmlns="http://docbook.org/ns/docbook">
8 <title>Grammar of JSON Queries</title>
10 <firstname>Scott</firstname>
11 <surname>McKellar</surname>
15 <sect1><title>Introduction</title>
17 The format of this grammar approximates Extended Backus-Naur notation. However it
18 is intended as input to human beings, not to parser generators such as Lex or
19 Yacc. Do not expect formal rigor. Sometimes narrative text will explain things
20 that are clumsy to express in formal notation. More often, the text will restate
21 or summarize the formal productions.
28 The grammar is a series of productions.
31 A production consists of a name, followed by "::=", followed by a
32 definition for the name. The name identifies a grammatical construct that can
33 appear on the right side of another production.
36 Literals (including punctuation) are enclosed in single quotes, or in double
37 quotes if case is not significant.
40 A single quotation mark within a literal is escaped with a preceding backslash.
43 If a construct can be defined more than one way, then the alternatives may appear
44 in separate productions; or, they may appear in the same production, separated by
45 pipe symbols. The choice between these representations is of only cosmetic
49 A construct enclosed within square brackets is optional.
52 A construct enclosed within curly braces may be repeated zero or more times.
55 JSON allows arbitrary white space between tokens. To avoid ugly clutter, this
56 grammar ignores the optional white space.
59 In many cases a production defines a JSON object, i.e. a list of name-value pairs,
60 separated by commas. Since the order of these name/value pairs is not significant,
61 the grammar will not try to show all the possible sequences. In general it will
62 present the required pairs first, if any, followed by any optional elements.
67 Since both EBNF and JSON use curly braces and square brackets, pay close attention to
68 whether these characters are in single quotes. If they're in single quotes, they are
69 literal elements of the JSON notation. Otherwise they are elements of the EBNF notation.
73 <sect1><title>Primitives</title>
75 We'll start by defining some primitives, to get them out of the way. They're
76 mostly just what you would expect.
94 any valid sequence of UTF-8 characters, with certain special characters
95 escaped according to JSON rules
104 [ sign ] digit { digit }
121 digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
131 '”' integer_literal '”'
140 integer_literal | integer_string
149 any valid character sequence that is numeric according to JSON rules
156 When json_query requires an integral value, it will usually accept a quoted string and
157 convert it to an integer by brute force – to zero if necessary. Likewise it may
158 truncate a floating point number to an integral value. Scientific notation will be
159 accepted but may not give the intended results.
169 'true' | 'false' | string | number
176 The preferred way to encode a boolean is with the JSON reserved word true or false,
177 in lower case without quotation marks. The string “<literal>true</literal>”, in
178 upper, lower, or mixed case, is another way to encode true. Any other string
182 As an accommodation to perl, numbers may be used as booleans. A numeric value of 1
183 means true, and any other numeric value means false.
186 Any other valid JSON value, such as an array, will be accepted as a boolean but interpreted
190 The last couple of primitives aren't really very primitive, but we introduce them here
208 A class_name is a special case of a string: the name of a class as defined
209 by the IDL. The class may refer either to a database table or to a
210 source_definition, which is a subquery.
227 A field_name is another special case of a string: the name of a non-virtual
228 field as defined by the IDL. A field_name is also a column name for the
229 table corresponding to the relevant class.
234 <sect1><title>Query</title>
237 The following production applies not only to the main query but also to
249 '”from”' ':' from_list<sbr/>
250 [ ',' '”select”' ':' select_list ]<sbr/>
251 [ ',' '”where”' ':' where_condition ]<sbr/>
252 [ ',' '”having”' ':' where_condition ]<sbr/>
253 [ ',' '”order_by”' ':' order_by_list ]<sbr/>
254 [ ',' '”limit”' ':' integer ]<sbr/>
255 [ ',' '”offset”' ':' integer ]<sbr/>
256 [ ',' '”distinct”' ':' boolean ]<sbr/>
257 [ ',' '”no_i18n”' ':' boolean ]<sbr/>
265 Except for the <literal>“distinct”</literal> and <literal>“no_i18n”</literal>
266 entries, each name/value pair represents a major clause of the SELECT statement.
267 The name/value pairs may appear in any order.
270 There is no name/value pair for the GROUP BY clause, because json_query
271 generates it automatically according to information encoded elsewhere.
274 The <literal>“distinct”</literal> entry, if present and true, tells json_query
275 that it may have to create a GROUP BY clause. If not present, it defaults to false.
278 The <literal>“no_i18n”</literal> entry, if present and true, tells json_query to
279 suppress internationalization. If not present, it defaults to false. (Note that
280 <literal>“no_i18n”</literal> contains the digit one, not the letter ell.)
283 The values for <literal>“limit”</literal> and <literal>“offset”</literal>
284 provide the arguments of the LIMIT and OFFSET clauses, respectively, of the
285 SQL statement. Each value should be non-negative, if present, or else the