Class: OpenEHR::AQL::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/openehr/aql/engine.rb

Overview

Orchestrates one Query execution against a Dataset, in the order CONTAINS -> WHERE -> ORDER BY -> SELECT -> DISTINCT -> TOP/LIMIT/OFFSET.

A SELECT clause made entirely of aggregate columns (Model::AggregateFunctionCall) collapses the whole (post-WHERE) binding set into a single summary row instead - ORDER BY/DISTINCT/ TOP/LIMIT don't apply to it, matching plain SQL aggregate-without- GROUP-BY semantics. A SELECT mixing aggregate and non-aggregate columns implicitly groups by every non-aggregate column's value (the standard SQL reading when no explicit GROUP BY is given) - zero surviving bindings therefore means zero groups/rows, unlike the all-aggregate case's single row of aggregate defaults. Generic (non-aggregate) function calls are not yet supported.

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Engine

Returns a new instance of Engine.



23
24
25
# File 'lib/openehr/aql/engine.rb', line 23

def initialize(query)
  @query = query
end

Instance Method Details

#execute(dataset, params: {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/openehr/aql/engine.rb', line 27

def execute(dataset, params: {})
  bindings = ContainsResolver.new(@query.from_clause, Dataset.wrap(dataset), params: params).each_binding
                              .select { |binding| where_matches?(binding, params) }
  rows = aggregate_query? ? aggregate_rows(bindings) : select_rows(bindings)
  ResultSet.new(columns: @query.select_clause.columns.map { |column| column_name(column) }, rows: rows)
end