Chapter 7 Working With the Query Language : Simple Continuous Query Example

Simple Continuous Query Example
The example provided in this section shows how a callback rule function is used to gather results generated by the query. The callback rule function is shown next:

 
MyRF(ID, isBatchEnd, hasEnded, row, closure)
 
if (hasEnded) {
    System.debugOut(ID + ": ========");
} else if (isBatchEnd) {
    System.debugOut(ID + ": --------");
} else {
    Object[] columns = row;
    String agent = columns[0];
    double sales = columns[1];
    String zip = columns[2];
    System.debugOut(id
        + ": Agent " + agent
        + " sold $" + sales
        + " in " + zip
        + ". " + closure);
}

 
Create the Query

 
Query.create("report853", "select agent_name, total_sales, zipcode from /Concepts/Sales");

 
Open and Execute the Query Statement

 
String id = requestEvent@extId;
String stmt = "S" + id;
String clbk = "C" + id;
Query.Statement.open("report853", stmt);
Query.Statement.executeWithCallback(
stmt, clbk, "/MyRF", true, "@@@@");

 
Where requestEvent is an event, and "/MyRF" is the path to the rule function shown at the beginning of the section. The true parameter indicates that this is a continuous query.
Sample Output
In the sample output below, each row of data (generated when a relevant change occurs in the cache) is one batch, because the query does not involve ordering or aggregation. The last line below indicates that the query has ended. For example, someone closed the statement (not shown in the code sample).

 
C123: Agent Mary Smith sold $15063.28 in 94304. @@@@
C123: --------
 
Time passes…
 
C123: Agent Robert Ng sold $14983.05 in 94304. @@@@
C123: --------
 
Time passes…
 
C123: Agent Jose Ortiz sold $16244.78 in 94304. @@@@
C123: --------
C123: ========

 
Function Calls
This section shows the parameter values for each function call.
As a reminder: the first Boolean indicates whether the batch has ended or not; the second Boolean indicates whether the execution has ended or not.
Mary Smith makes a sale.
 
MyRF(clbk, false, false, ["Mary Smith", 15063.28, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
 
Time passes… Robert Ng makes a sale.
 
MyRF(clbk, false, false, ["Robert Ng", 14983.05, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
 
Time passes… Jose Ortiz makes a sale.
 
MyRF(clbk, false, false, ["Jose Ortiz", 16244.78, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
MyRF(clbk, true, true, null, "@@@@")
Example Showing Batching of Return Values
This example is the same as the example above, with the addition of an order by clause in the query text, to show batching behavior. Only the output and function calls differ.
Create the Query

 
Query.create("report853", "select agent_name, total_sales, zipcode from /Concepts/Sales order by agent_name");

 
Sample Output

 
Mary Smith makes a sale.
 
C123: Agent Mary Smith sold $15063.28 in 94304. @@@@
C123: --------
 
Time passes… Robert Ng makes a sale.
 
C123: Agent Mary Smith sold $15063.28 in 94304. @@@@
C123: Agent Robert Ng sold $14983.05 in 94304. @@@@
C123: --------
 
Time passes… Jose Ortiz makes a sale.
 
C123: Agent Jose Ortiz sold $16244.78 in 94304. @@@@
C123: Agent Mary Smith sold $15063.28 in 94304. @@@@
C123: Agent Robert Ng sold $14983.05 in 94304. @@@@
C123: --------
C123: ========

 
Function Calls
This section shows the parameter values for each function call.
As a reminder: the first Boolean indicates whether the batch has ended or not; the second Boolean indicates whether the execution has ended or not.
Mary Smith makes a sale.
 
MyRF(clbk, false, false, ["Mary Smith", 15063.28, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
 
Time passes… Robert Ng makes a sale.
 
MyRF(clbk, false, false, ["Mary Smith", 15063.28, 94304], "@@@@")
MyRF(clbk, false, false, ["Robert Ng", 14983.05, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
 
Time passes… Jose Ortiz makes a sale.
 
MyRF(clbk, false, false, ["Jose Ortiz", 16244.78, 94304], "@@@@")
MyRF(clbk, false, false, ["Mary Smith", 15063.28, 94304], "@@@@")
MyRF(clbk, false, false, ["Robert Ng", 14983.05, 94304], "@@@@")
MyRF(clbk, true, false, null, "@@@@")
MyRF(clbk, true, true, null, "@@@@")