Packageflare.query
Classpublic class Query

Performs query processing over a collection of ActionScript objects. Queries can perform filtering, sorting, grouping, and aggregation operations over a data collection. Arbitrary data collections can be queried by providing a visitor function similar to the Array.forEach method to the query eval method.

The select and where methods in the flare.query.methods package are useful shorthands for helping to construct queries in code.

Here is an example of a query. It uses helper methods defined in the flare.query.methods package. For example, the sum method creates a Sum query operator and the _ method creates as a Literal expression for its input value.

  import flare.query.methods.
  
  var data:Array = [
   {cat:"a", val:1}, {cat:"a", val:2}, {cat:"b", val:3}, {cat:"b", val:4},
   {cat:"c", val:5}, {cat:"c", val:6}, {cat:"d", val:7}, {cat:"d", val:8}
  ];
  
  var r:Array = select("cat", {sum:sum("val")}) // sum of values
                .where(neq("cat", _("d"))       // exclude category "d"
                .groupby("cat")                 // group by category
                .eval(data);                    // evaluate with data array
  
  // r == [{cat:"a", sum:3}, {cat:"b", sum:7}, {cat:"c", sum:11}]
  



Public Methods
 MethodDefined by
  
Query(select:Array = null, where:Array = null, orderby:Array = null, groupby:* = null)
Creates a new Query.
Query
  
eval(input:*):Array
Evaluates this query on an object collection.
Query
  
groupby(... terms):Query
Sets the group by terms for aggregate queries.
Query
  
map(value:Boolean = true):Query
Sets whether or not aggregate functions will be mapped to all tuples in the data set.
Query
  
orderby(... terms):Query
Sets the sort order for query results.
Query
  
select(... terms):Query
Sets the select clauses used by this query.
Query
  
update(... terms):Query
Sets the select clauses used by this query to update the values of the input set.
Query
  
where(e:*):Query
Sets the where clause (filter conditions) used by this query.
Query
Constructor detail
Query()constructor
public function Query(select:Array = null, where:Array = null, orderby:Array = null, groupby:* = null)

Creates a new Query.

Parameters
select:Array (default = null) — an array of select clauses. A select clause consists of either a string representing the name of a variable to query or an object of the form {name:expr}, where name is the name of the query variable to include in query result objects and expr is an Expression for the actual query value. Expressions can be any legal expression, including aggregate operators.
 
where:Array (default = null) — a where expression for filtering an object collection
 
orderby:Array (default = null) — directives for sorting query results, using the format of the flare.util.Sort class methods.
 
groupby:* (default = null) — directives for grouping query results, using the format of the flare.util.Sort class methods.

See also

Method detail
eval()method
public function eval(input:*):Array

Evaluates this query on an object collection. The input argument can either be an array of objects or a visitor function that takes another function as input and applies it to all items in a collection.

Parameters
input:* — either an array of objects or a visitor function

Returns
Array — an array of processed query results
groupby()method 
public function groupby(... terms):Query

Sets the group by terms for aggregate queries.

Parameters
... terms — an ordered list of terms to group by.

Returns
Query — this query object
map()method 
public function map(value:Boolean = true):Query

Sets whether or not aggregate functions will be mapped to all tuples in the data set. This allows the results of aggregate operators to be applied for all tuples. For example, the map directive allows queries of this form:

   var q:Query = select({a:div(a,sum(a))}).map().eval(...);
   

The result include normalized a values for all tuples in the input data. Without the map directive, a "group-by" for all data would be assumed and only a single tuple would be returned in the result set (matching the normal behavior of a SQL database). Map can also be specified with a group-by clause, in which case the aggregate operators will be handled separately for each group, but the result set will still contain a result for every tuple in the input set.

The map directive has no effect on "update" queries, which already apply map semantics. It only effects "select" queries.

Parameters
value:Boolean (default = true) — if true (the default), aggregate operators will be applied (mapped) to all tuples; if false, normal group-by semantics will be used.

Returns
Query — this query object
orderby()method 
public function orderby(... terms):Query

Sets the sort order for query results.

Parameters
... terms — the sort terms as a list of field names to sort on. By default, fields are sorted in ascending order. Add the prefix "-" (negative sign) to the field name to sort in descending order.

Returns
Query — this query object
select()method 
public function select(... terms):Query

Sets the select clauses used by this query. A select clause consists of either a string representing the name of a variable to query or an object of the form {name:expr}, where name is the name of the query variable to include in query result objects and expr is an Expression for the actual query value.

Calling the select method will overwrite the effect of any previous calls to the select or update methods.

Parameters
... terms — a list of query terms (select clauses). If the first element is an array, it will be used as the term list.

Returns
Query — this query object
update()method 
public function update(... terms):Query

Sets the select clauses used by this query to update the values of the input set. An update clause consists of an object of the form {name:expr}, where name is the name of the data variable to set and expr is an Expression for computing the value. When eval is invoked for an update query, the values of the input objects are updated and the returned result set is an array containing these input objects.

Calling the update method will overwrite the effect of any previous calls to the select or update methods.

Parameters
... terms — a list of query terms (update clauses). If the first element is an array, it will be used as the term list.

Returns
Query — this query object
where()method 
public function where(e:*):Query

Sets the where clause (filter conditions) used by this query.

Parameters
e:* — the filter expression. This can be a string, a literal value, or an Expression instance. This input value will be run through the Expression.expr method.

Returns
Query — this query object