Class: Chicago::Query
- Inherits:
-
Object
- Object
- Chicago::Query
- Defined in:
- lib/chicago/query.rb
Overview
A query object, wrapping a raw AST and linked to a schema.
The raw AST looks like the following:
{
:table_name => "name",
:query_type => "fact",
:columns => [... column definitions ...],
:filters => [... filter definitions ...],
:order => [... order definitions ...]
}
Class Attribute Summary collapse
-
.column_parser ⇒ Object
Sets the column parser class to be used.
-
.default_db ⇒ Object
Sets the default Sequel::Database used by queries.
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns an array of Columns selected in this query.
-
#table ⇒ Object
readonly
Returns an the fact or dimension this query is based on.
Instance Method Summary collapse
-
#dataset(db = nil) ⇒ Object
Applies the query to a Sequel::Database and returns a Sequel::Dataset.
- #filter(*filters) ⇒ Object
-
#initialize(schema, ast) ⇒ Query
constructor
Creates a query over a schema from a raw query AST.
- #limit(limit) ⇒ Object
-
#order(*ordering) ⇒ Object
Order the results by the specified columns.
- #select(*columns) ⇒ Object
Constructor Details
#initialize(schema, ast) ⇒ Query
Creates a query over a schema from a raw query AST.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chicago/query.rb', line 40 def initialize(schema, ast) @column_parser = self.class.column_parser.new(schema) ast.symbolize_keys! @table_name = ast[:table_name].downcase.to_sym @query_type = ast[:query_type].downcase.to_sym @columns = [] @filters = [] @order = [] @table = schema.send(@query_type, @table_name) or raise MissingDefinitionError.new("#{@query_type} '#{@table_name}' is not in the schema") select(*(ast[:columns] || [])) filter(*(ast[:filters] || [])) order(*(ast[:order] || [])) end |
Class Attribute Details
.column_parser ⇒ Object
Sets the column parser class to be used.
By default Schema::ColumnParser
33 34 35 |
# File 'lib/chicago/query.rb', line 33 def column_parser @column_parser end |
.default_db ⇒ Object
Sets the default Sequel::Database used by queries.
28 29 30 |
# File 'lib/chicago/query.rb', line 28 def default_db @default_db end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns an array of Columns selected in this query.
24 25 26 |
# File 'lib/chicago/query.rb', line 24 def columns @columns end |
#table ⇒ Object (readonly)
Returns an the fact or dimension this query is based on.
21 22 23 |
# File 'lib/chicago/query.rb', line 21 def table @table end |
Instance Method Details
#dataset(db = nil) ⇒ Object
Applies the query to a Sequel::Database and returns a Sequel::Dataset.
99 100 101 102 103 104 105 106 107 |
# File 'lib/chicago/query.rb', line 99 def dataset(db=nil) db ||= self.class.default_db builder = Database::DatasetBuilder.new(db, self) builder.select(@columns) builder.filter(@filters) builder.order(@order) builder.limit(@limit) if @limit builder.dataset end |
#filter(*filters) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/chicago/query.rb', line 64 def filter(*filters) copied_filters = filters.dup copied_filters.each do |filter| filter.symbolize_keys! filter[:column] = @column_parser.parse(filter[:column]).first end @filters += copied_filters self end |
#limit(limit) ⇒ Object
90 91 92 93 |
# File 'lib/chicago/query.rb', line 90 def limit(limit) @limit = limit self end |
#order(*ordering) ⇒ Object
Order the results by the specified columns.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/chicago/query.rb', line 79 def order(*ordering) @order = ordering.map do |c| if c.kind_of?(String) {:column => c, :ascending => true} else c.symbolize_keys! end end self end |
#select(*columns) ⇒ Object
58 59 60 61 |
# File 'lib/chicago/query.rb', line 58 def select(*columns) @columns += columns.map {|c| @column_parser.parse(c) } self end |