1 <?xml version="1.0" encoding="utf-8"?>
3 <article version="5.0" xmlns="http://docbook.org/ns/docbook"
4 xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink">
6 <title>Grammar of JSON Queries</title>
11 <firstname>Scott</firstname>
12 <surname>McKellar</surname>
15 <orgname>Equinox Software, Inc.</orgname>
24 <holder>Creative Commons Attribution-Share Alike 3.0 United States License.</holder>
32 <title>Introduction</title>
33 <para> The format of this grammar approximates Extended Backus-Naur notation. However it is
34 intended as input to human beings, not to parser generators such as Lex or Yacc. Do not
35 expect formal rigor. Sometimes narrative text will explain things that are clumsy to
36 express in formal notation. More often, the text will restate or summarize the formal
38 <para> Conventions: </para>
41 <para>The grammar is a series of productions.</para>
44 <para>A production consists of a name, followed by "::=", followed by a definition
45 for the name. The name identifies a grammatical construct that can appear on the
46 right side of another production.</para>
49 <para>Literals (including punctuation) are enclosed in 'single quotes', or in
50 "double quotes" if case is not significant.</para>
53 <para>A single quotation mark within a literal is escaped with a preceding
54 backslash: 'dog\'s tail'.</para>
57 <para>If a construct can be defined more than one way, then the alternatives may
58 appear in separate productions; or, they may appear in the same production,
59 separated by pipe symbols. The choice between these representations is of only
60 cosmetic significance.</para>
63 <para>A construct enclosed within square brackets is optional.</para>
66 <para>A construct enclosed within curly braces may be repeated zero or more
70 <para>JSON allows arbitrary white space between tokens. To avoid ugly clutter, this
71 grammar ignores the optional white space. </para>
74 <para>In many cases a production defines a JSON object, i.e. a list of name-value
75 pairs, separated by commas. Since the order of these name/value pairs is not
76 significant, the grammar will not try to show all the possible sequences. In
77 general it will present the required pairs first, if any, followed by any
78 optional elements.</para>
82 <para> Since both EBNF and JSON use curly braces and square brackets, pay close attention to
83 whether these characters are in single quotes. If they're in single quotes, they are
84 literal elements of the JSON notation. Otherwise they are elements of the EBNF notation.
89 <title>Primitives</title>
90 <para> We'll start by defining some primitives, to get them out of the way. They're mostly
91 just what you would expect. </para>
94 <production xml:id="ebnf.string">
96 <rhs> '"' chars '"' </rhs>
99 <production xml:id="ebnf.chars">
101 <rhs> any valid sequence of UTF-8 characters, with certain special characters
102 escaped according to JSON rules </rhs>
105 <production xml:id="ebnf.int_literal">
106 <lhs> integer_literal </lhs>
107 <rhs> [ sign ] digit { digit } </rhs>
110 <production xml:id="ebnf.sign">
112 <rhs> '+' | '-' </rhs>
115 <production xml:id="ebnf.digits">
117 <rhs>digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'</rhs>
120 <production xml:id="ebnf.int_string">
121 <lhs> integer_string </lhs>
122 <rhs> '"' integer_literal '"' </rhs>
125 <production xml:id="ebnf.int">
127 <rhs> integer_literal | integer_string </rhs>
130 <production xml:id="ebnf.num">
132 <rhs> any valid character sequence that is numeric according to JSON rules </rhs>
137 <para> When json_query requires an integral value, it will usually accept a quoted string
138 and convert it to an integer by brute force – to zero if necessary. Likewise it may
139 truncate a floating point number to an integral value. Scientific notation will be
140 accepted but may not give the intended results. </para>
144 <production xml:id="ebnf.bool">
146 <rhs> 'true' | 'false' | string | number </rhs>
151 <para> The preferred way to encode a boolean is with the JSON reserved word true or false,
152 in lower case without quotation marks. The string <literal>true</literal>, in upper,
153 lower, or mixed case, is another way to encode true. Any other string evaluates to
155 <para> As an accommodation to perl, numbers may be used as booleans. A numeric value of 1
156 means true, and any other numeric value means false. </para>
157 <para> Any other valid JSON value, such as an array, will be accepted as a boolean but
158 interpreted as false. </para>
159 <para> The last couple of primitives aren't really very primitive, but we introduce them
160 here for convenience: </para>
164 <production xml:id="ebnf.classname">
165 <lhs> class_name </lhs>
171 <para> A class_name is a special case of a string: the name of a class as defined by the
172 IDL. The class may refer either to a database table or to a source_definition, which is
177 <production xml:id="ebnf.field_name">
178 <lhs> field_name </lhs>
184 <para> A field_name is another special case of a string: the name of a non-virtual field as
185 defined by the IDL. A field_name is also a column name for the table corresponding to
186 the relevant class. </para>
193 <para> The following production applies not only to the main query but also to most
198 <production xml:id="ebnf.query">
200 <rhs> '{'<sbr/> '"from"' ':' from_list<sbr/> [ ',' '"select"' ':' select_list
201 ]<sbr/> [ ',' '"where"' ':' where_condition ]<sbr/> [ ',' '"having"' ':'
202 where_condition ]<sbr/> [ ',' '"order_by"' ':' order_by_list ]<sbr/> [ ','
203 '"limit"' ':' integer ]<sbr/> [ ',' '"offset"' ':' integer ]<sbr/> [ ','
204 '"distinct"' ':' boolean ]<sbr/> [ ',' '"no_i18n"' ':' boolean ]<sbr/> '}'
210 <para> Except for the <literal>"distinct"</literal> and <literal>no_i18n</literal> entries,
211 each name/value pair represents a major clause of the SELECT statement. The name/value
212 pairs may appear in any order. </para>
213 <para> There is no name/value pair for the GROUP BY clause, because json_query generates it
214 automatically according to information encoded elsewhere. </para>
215 <para> The <literal>"distinct"</literal> entry, if present and true, tells json_query that
216 it may have to create a GROUP BY clause. If not present, it defaults to false. </para>
217 <para> The <literal>"no_i18n"</literal> entry, if present and true, tells json_query to
218 suppress internationalization. If not present, it defaults to false. (Note that
219 <literal>"no_i18n"</literal> contains the digit one, not the letter ell.) </para>
220 <para> The values for <literal>limit</literal> and <literal>offset</literal> provide the
221 arguments of the LIMIT and OFFSET clauses, respectively, of the SQL statement. Each
222 value should be non-negative, if present, or else the SQL won't work. </para>