Class: Rasti::DB::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rasti/db/query.rb

Constant Summary collapse

DATASET_CHAINED_METHODS =
[:where, :exclude, :or, :order, :reverse_order, :limit, :offset].freeze

Instance Method Summary collapse

Constructor Details

#initialize(environment:, collection_class:, dataset:, relations_graph: nil) ⇒ Query

Returns a new instance of Query.



9
10
11
12
13
14
# File 'lib/rasti/db/query.rb', line 9

def initialize(environment:, collection_class:, dataset:, relations_graph:nil)
  @environment = environment
  @collection_class = collection_class
  @dataset = dataset.qualify collection_class.collection_name
  @relations_graph = relations_graph || Relations::Graph.new(environment, collection_class)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



194
195
196
197
198
199
200
# File 'lib/rasti/db/query.rb', line 194

def method_missing(method, *args, &block)
  if collection_class.queries.key? method
    instance_exec(*args, &collection_class.queries.fetch(method))
  else
    super
  end
end

Instance Method Details

#allObject Also known as: to_a



68
69
70
71
72
# File 'lib/rasti/db/query.rb', line 68

def all
  with_graph(dataset.all).map do |row|
    collection_class.model.new row
  end
end

#all_attributesObject



44
45
46
# File 'lib/rasti/db/query.rb', line 44

def all_attributes
  build_query dataset: dataset.select_all(collection_class.collection_name)
end

#all_graph_attributes(*relations) ⇒ Object



56
57
58
# File 'lib/rasti/db/query.rb', line 56

def all_graph_attributes(*relations)
  build_query relations_graph: relations_graph.with_all_attributes_for(relations)
end

#any?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/rasti/db/query.rb', line 108

def any?
  count > 0
end

#countObject



104
105
106
# File 'lib/rasti/db/query.rb', line 104

def count
  dataset.count
end

#detect(*args, &block) ⇒ Object



126
127
128
# File 'lib/rasti/db/query.rb', line 126

def detect(*args, &block)
  where(*args, &block).first
end

#each(batch_size: nil, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/rasti/db/query.rb', line 75

def each(batch_size:nil, &block)
  if batch_size.nil?
    all.each(&block)
  else
    each_batch(size: batch_size) do |models|
      models.each { |model| block.call model }
    end
  end
end

#each_batch(size:, &block) ⇒ Object



85
86
87
88
89
90
# File 'lib/rasti/db/query.rb', line 85

def each_batch(size:, &block)
  primary_keys.each_slice(size) do |pks|
    query = where(collection_class.primary_key => pks)
    block.call query.all
  end
end

#empty?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/rasti/db/query.rb', line 112

def empty?
  !any?
end

#exclude_attributes(*excluded_attributes) ⇒ Object



39
40
41
42
# File 'lib/rasti/db/query.rb', line 39

def exclude_attributes(*excluded_attributes)
  attributes = collection_class.collection_attributes - excluded_attributes
  select_attributes(*attributes)
end

#exclude_graph_attributes(excluded_attributes) ⇒ Object



52
53
54
# File 'lib/rasti/db/query.rb', line 52

def exclude_graph_attributes(excluded_attributes)
  build_query relations_graph: relations_graph.merge(excluded_attributes: excluded_attributes)
end

#firstObject



116
117
118
119
# File 'lib/rasti/db/query.rb', line 116

def first
  row = dataset.first
  row ? build_model(row) : nil
end

#graph(*relations) ⇒ Object



92
93
94
# File 'lib/rasti/db/query.rb', line 92

def graph(*relations)
  build_query relations_graph: relations_graph.merge(relations: relations)
end

#join(*relations) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/rasti/db/query.rb', line 96

def join(*relations)
  graph = Relations::Graph.new environment, collection_class, relations

  ds = graph.add_joins(dataset).distinct

  build_query dataset: ds
end

#lastObject



121
122
123
124
# File 'lib/rasti/db/query.rb', line 121

def last
  row = dataset.last
  row ? build_model(row) : nil
end

#nql(filter_expression) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rasti/db/query.rb', line 135

def nql(filter_expression)
  sentence = nql_parser.parse filter_expression

  raise NQL::InvalidExpressionError.new(filter_expression) if sentence.nil?

  ds = sentence.computed_attributes(collection_class).inject(dataset) do |ds, name|
    collection_class.computed_attributes[name].apply_join ds, environment
  end
  query = build_query dataset: ds

  dependency_tables = sentence.dependency_tables
  query = query.join(*dependency_tables) unless dependency_tables.empty?

  query.where sentence.filter_condition(collection_class)
end

#pluck(*attributes) ⇒ Object



26
27
28
29
# File 'lib/rasti/db/query.rb', line 26

def pluck(*attributes)
  ds = dataset.select(*attributes.map { |a| Sequel[collection_class.collection_name][a] })
  attributes.count == 1 ? ds.map { |r| r[attributes.first] } : ds.map(&:values)
end

#primary_keysObject



31
32
33
# File 'lib/rasti/db/query.rb', line 31

def primary_keys
  pluck collection_class.primary_key
end

#rawObject



22
23
24
# File 'lib/rasti/db/query.rb', line 22

def raw
  dataset.all
end

#select_attributes(*attributes) ⇒ Object



35
36
37
# File 'lib/rasti/db/query.rb', line 35

def select_attributes(*attributes)
  build_query dataset: dataset.select(*attributes.map { |a| Sequel[collection_class.collection_name][a] })
end

#select_computed_attributes(*computed_attributes) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rasti/db/query.rb', line 60

def select_computed_attributes(*computed_attributes)
  ds = computed_attributes.inject(dataset) do |ds, name|
    computed_attribute = collection_class.computed_attributes[name]
    computed_attribute.apply_join(ds, environment).select_append(computed_attribute.identifier.as(name))
  end
  build_query dataset: ds
end

#select_graph_attributes(selected_attributes) ⇒ Object



48
49
50
# File 'lib/rasti/db/query.rb', line 48

def select_graph_attributes(selected_attributes)
  build_query relations_graph: relations_graph.merge(selected_attributes: selected_attributes)
end

#to_sObject Also known as: inspect



130
131
132
# File 'lib/rasti/db/query.rb', line 130

def to_s
  "#<#{self.class.name}: \"#{dataset.sql}\">"
end