Query
Queries provide a controlled way to retrieve data in Rell.
Queries have the following characteristics:
- Can't modify the data in the database (compile-time verification)
- Must return a value
- If the return type isn't explicitly specified, it's implicitly deducted
- Parameter types and return types must be GTV-compatible
Short form
Compact syntax for straightforward queries:
query q(x: integer): integer = x * x;
Full form
More explicit syntax with a return statement:
query q(x: integer): integer {
return x * x;
}
Size constraint annotations for parameters
Query parameters support size constraint annotations. Available annotations:
@size(n)/@size(a, b): Exact size or range (shorthand for@min_sizeand@max_size)@min_size(n): Minimum size@max_size(n): Maximum size
Supported types: text and byte_array. For examples and details, see
Size constraint annotations for parameters.
Default parameter values
Queries can have parameters with default values, making them optional when called from the blockchain (external clients). This allows you to add new parameters without breaking existing client code.
query get_users(min_age: integer = 0, max_age: integer = 100): list<user> {
return user @* { .age >= min_age, .age <= max_age };
}
In this example, both min_age and max_age have default values, so they're optional when calling the query from the
blockchain. Clients can call get_users without any parameters, with just min_age, or with both parameters.