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)



208
209
210
211
212
213
214
# File 'lib/rasti/db/query.rb', line 208

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



72
73
74
# File 'lib/rasti/db/query.rb', line 72

def all
  build_models dataset.all
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



60
61
62
# File 'lib/rasti/db/query.rb', line 60

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

#any?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rasti/db/query.rb', line 110

def any?
  count > 0
end

#countObject



106
107
108
# File 'lib/rasti/db/query.rb', line 106

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



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

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



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

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)


114
115
116
# File 'lib/rasti/db/query.rb', line 114

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



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

def first
  build_model dataset.first
end

#graph(*relations) ⇒ Object



94
95
96
# File 'lib/rasti/db/query.rb', line 94

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

#graph_queries(queries) ⇒ Object



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

def graph_queries(queries)
  build_query relations_graph: relations_graph.merge(queries: queries)
end

#join(*relations) ⇒ Object



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

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

  ds = graph.add_joins(dataset).distinct

  build_query dataset: ds
end

#lastObject



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

def last
  build_model dataset.last
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 |inner_ds, name|
    collection_class.computed_attributes[name].apply_join inner_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



64
65
66
67
68
69
70
# File 'lib/rasti/db/query.rb', line 64

def select_computed_attributes(*computed_attributes)
  ds = computed_attributes.inject(dataset) do |inner_ds, name|
    computed_attribute = collection_class.computed_attributes[name]
    computed_attribute.apply_join(inner_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