Product SiteDocumentation Site

Chapter 12. Prologs

12.1. Setters.
12.1.1. Default Ordering Mode
12.1.2. Default Ordering Behaviour for Empty Sequences
12.1.3. Default Decimal Format
12.2. Namespaces
12.3. Global Variables
12.4. User-Defined Functions
This section introduces prologs, which allow declaring functions and global variables that can then be used in the main query. A prolog also allows setting some default behaviour.
The prolog appears before the main query and is optional. It can contain setters and module imports, followed by function and variable declarations.
Module imports are explained in the next chapter.

12.1. Setters.

Setters allow to specify a default behaviour for various aspects of the language.

12.1.1. Default Ordering Mode

This specifies the default behaviour of for clauses, i.e., if they bind tuples in the order in which items occur in the binding sequence. It can be overriden with ordered and unordered expressions.
Example 12.1. A default ordering setter.
declare ordering unordered;
for $answer in collection("answers")
return {
  "owner" : $answer.owner.display_name,
  "score" : $answer.score
}
Results:
{
  "owner" : "Ubiguchi",
  "score" : 7
}
{
  "owner" : "Rob Wells",
  "score" : 4
}
{
  "owner" : "Victor Nicollet",
  "score" : 17
}
{
  "owner" : "descent89",
  "score" : 1
}
{
  "owner" : "JasonSmith",
  "score" : 34
}
{
  "owner" : "JasonSmith",
  "score" : 6
}
{
  "owner" : "JasonSmith",
  "score" : 0
}
{
  "owner" : "JasonSmith",
  "score" : 1
}

12.1.2. Default Ordering Behaviour for Empty Sequences

This specifies whether empty sequences come first or last in an ordering clause. It can be overriden by the corresponding directives in such clauses.
Example 12.2. A default ordering for empty sequences.
declare default order empty least;
for $x in ({ "foo" : "bar" }, {})
order by $x.foo
return $x
Results:
{
}
{
  "foo" : "bar"
}

12.1.3. Default Decimal Format

This specifies a default decimal format for the builtin function format-number().
Example 12.3. A default decimal format setter.
declare default decimal-format
    decimal-separator = ","
    grouping-separator = " ";
format-number(12345.67890, "# ###,##")
Results:
"12 345,68"