Building a Query String
A query string is a fully formed OQL statement that can be passed to a query engine and executed against a data set. To build a query string, you combine supported keywords, expressions, and operators to create an expression that returns the information you require.
A query string follows the rules specified by the query language and grammar. It can include:
- Namescopes. For example, the IMPORT statement. See IMPORT Statement.
- Path expressions. For example, in the query
SELECT * FROM /exampleRegion,
/exampleRegionis a path expression. See FROM Clause.
- Attribute names. For example, in the query
SELECT DISTINCT * FROM /exampleRegion p WHERE p.position1.secId = '1', we access the
secIdattribute of the Position object. See WHERE Clause.
- Method invocations. For example, in the query
SELECT DISTINCT * FROM /exampleRegion p WHERE p.name.startsWith('Bo'), we invoke the
startsWithmethod on the Name object. See WHERE Clause.
- Operators. For example, comparison operators (=,<,>,<>), unary operators (NOT), logical operators (AND, OR) and so on. See Operators for a complete list.
- Literals. For example, boolean, date, time and so on. See Supported Literals for a complete list.
- Query bind parameters. For example, in the query
SELECT DISTINCT * FROM $1 p WHERE p.status = $2, $1 and $2 are parameters that can be passed to the query during runtime. See Using Query Bind Parameters for more details.
- Preset query functions. For example, ELEMENT(expr) and IS_DEFINED(expr). See SELECT Statement for other available functions.
- SELECT statements. For example, in the example queries above
SELECT DISTINCT *. See SELECT Statement for other available functions.
- Comments. OQL permits extra characters to accompany the query string without changing the string’s definition. Form a multi-line comment by enclosing the comment body within
*/delimiters; OQL does not permit nested comments. A single line comment body is all the characters to the right of
--(two hyphens) up to the end of the line.
The components listed above can all be part of the query string, but none of the components are required. At a minimum, a query string contains an expression that can be evaluated against specified data.
The following sections provide guidelines for the query language building blocks that are used when writing typical Geode queries.