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 -> 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/ LIMIT don't apply to it, matching plain SQL aggregate-without- GROUP-BY semantics. Mixing aggregate and plain columns, and generic (non-aggregate) function calls, are not yet supported.

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Engine

Returns a new instance of Engine.



19
20
21
# File 'lib/openehr/aql/engine.rb', line 19

def initialize(query)
  @query = query
end

Instance Method Details

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



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

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