Creating a query definition is a distinct and separate step from opening and executing a query statement. Creating a query definition is the most expensive step in the process of making the query available for execution. Therefore it is often best done at engine startup.
Where $min is a bind variable whose value is provided at runtime.
If a query statement based on this definition is executed and returns a result set, the result set columns would be,
zipcode,
total_sales, and
agent_name, with rows of entity values that match the condition specified at the time the query was executed.
The query definition name references the query definition that contains the query text. The statement name defined here is used in other functions to identify this query statement.
Where S_Id is a string variable that contains the statement name. Names can be constructed in various ways, as shown in
Simple Snapshot Query Example.
If you used bind variables in the query definition, then you set the values after opening the query statement, and before executing it. This sequence is required. The functions need not be executed right after each other, however. For example, the
Query.Statement.open() function could be in a startup rule function and the
Query.Statement.setvar() function could be in a rule function called on assertion of an event, followed by the
Query.Statement.execute() function.
The Query.Statement.execute() function returns values in a
result set. The result set is a tabular form (with rows and columns) on which you can perform operations to return data. It is used for snapshot queries only. Execution is synchronous.
In the above example, S_Id is a string variable providing the name that was given in the
Query.Statement.open() function. The example shows use of the external ID of event
evt (evt@extId) as the result set name, as a way to ensure that each result set has a unique name.
After you have collected the data you need, close the result set. You can close the result set directly, or close it indirectly by closing a higher-level item such as the the statement or the query definition. To close the result set use the following function:
You can use Query.Statement.executeWithCallback(), for snapshot or continuous queries. If you set the
IsContinuous argument to
true, the query runs as a continuous query.
The format of the Query.Statement.executeWithCallback() function is shown below.
The ExecutionName parameter keeps results from different executions distinct from each other.
The CallbackUri parameter value provides the project path to the callback rule function.
The IsContinuous parameter defines if the query is a snapshot or continuous query.
The Closure parameter is stored during the execution of the query, and provided as a parameter to the callback function every time that function is called.
You can close or delete at different levels. You can delete a query definition to make room for new query definitions. You can also delete (close) the statement that is running, without deleting the query definition itself. Use the following functions as needed for your situation: