Class: Hold::Sequel::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/hold/sequel/query.rb

Overview

A query has a dataset and mappings constructed to select a particular set of properties on a particular Sequel::IdentitySetRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, properties) ⇒ Query

Repo: repository to query properties: mapping or array of properties to fetch. properties ::=

nil or true = fetch the default set of properties for the given repository
array of property names = fetch just these properties, each in their default version
hash of property names = fetch just these properties, each in the version given in the hash

can pass a block: {|dataset, property_columns| return dataset.messed_with}



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hold/sequel/query.rb', line 15

def initialize(repo, properties)
  @repository = repo

  if properties.is_a?(::Array)
    @property_versions = {}
    properties.each {|p,v| @property_versions[p] = v}
  else
    @property_versions = properties
  end

  @property_columns, @aliased_columns, @tables =
    @repository.columns_aliases_and_tables_for_properties(@property_versions.keys)

  @dataset = @repository.dataset_to_select_tables(*@tables)
  @dataset = yield @dataset, @property_columns if block_given?

  id_cols = @property_columns[@repository.identity_property]
  @count_dataset = @dataset.select(*id_cols)

  @dataset = @dataset.select(*@aliased_columns)
end

Instance Attribute Details

#aliased_columnsObject (readonly)

Returns the value of attribute aliased_columns.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def aliased_columns
  @aliased_columns
end

#count_datasetObject (readonly)

Returns the value of attribute count_dataset.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def count_dataset
  @count_dataset
end

#datasetObject (readonly)

Returns the value of attribute dataset.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def dataset
  @dataset
end

#property_columnsObject (readonly)

Returns the value of attribute property_columns.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def property_columns
  @property_columns
end

#property_versionsObject (readonly)

Returns the value of attribute property_versions.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def property_versions
  @property_versions
end

#tablesObject (readonly)

Returns the value of attribute tables.



5
6
7
# File 'lib/hold/sequel/query.rb', line 5

def tables
  @tables
end

Instance Method Details

#results(lazy = false) ⇒ Object Also known as: to_a



66
67
68
69
# File 'lib/hold/sequel/query.rb', line 66

def results(lazy=false)
  lazy_array = DatasetLazyArray.new(@dataset, @count_dataset) {|rows| load_from_rows(rows)}
  lazy ? lazy_array : lazy_array.to_a
end

#results_with_rowsObject

this one is useful if you add extra selected columns onto the dataset, and you want to get at those extra values on the underlying rows alongside the loaded entities.



75
76
77
# File 'lib/hold/sequel/query.rb', line 75

def results_with_rows
  load_from_rows(@dataset.all, true)
end

#single_resultObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hold/sequel/query.rb', line 79

def single_result
  row = Hold::Sequel.translate_exceptions {@dataset.first} or return

  id = @repository.identity_mapper.load_value(row)
  property_hash = {@repository.identity_property => id}

  @property_versions.each do |prop_name, prop_version|
    property_hash[prop_name] = @repository.mapper(prop_name).load_value(row, id, prop_version)
  end

  @repository.construct_entity(property_hash, row)
end