Class: Rasti::DB::Query
- Inherits:
-
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
204
205
206
207
208
209
210
|
# File 'lib/rasti/db/query.rb', line 204
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
#all ⇒ Object
Also known as:
to_a
68
69
70
|
# File 'lib/rasti/db/query.rb', line 68
def all
build_models dataset.all
end
|
#all_attributes ⇒ Object
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
106
107
108
|
# File 'lib/rasti/db/query.rb', line 106
def any?
count > 0
end
|
#count ⇒ Object
102
103
104
|
# File 'lib/rasti/db/query.rb', line 102
def count
dataset.count
end
|
#detect(*args, &block) ⇒ Object
122
123
124
|
# File 'lib/rasti/db/query.rb', line 122
def detect(*args, &block)
where(*args, &block).first
end
|
#each(batch_size: nil, &block) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/rasti/db/query.rb', line 73
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
83
84
85
86
87
88
|
# File 'lib/rasti/db/query.rb', line 83
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
110
111
112
|
# File 'lib/rasti/db/query.rb', line 110
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
|
#first ⇒ Object
114
115
116
|
# File 'lib/rasti/db/query.rb', line 114
def first
build_model dataset.first
end
|
#graph(*relations) ⇒ Object
90
91
92
|
# File 'lib/rasti/db/query.rb', line 90
def graph(*relations)
build_query relations_graph: relations_graph.merge(relations: relations)
end
|
#join(*relations) ⇒ Object
94
95
96
97
98
99
100
|
# File 'lib/rasti/db/query.rb', line 94
def join(*relations)
graph = Relations::Graph.new environment, collection_class, relations
ds = graph.add_joins(dataset).distinct
build_query dataset: ds
end
|
#last ⇒ Object
118
119
120
|
# File 'lib/rasti/db/query.rb', line 118
def last
build_model dataset.last
end
|
#nql(filter_expression) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/rasti/db/query.rb', line 131
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_keys ⇒ Object
31
32
33
|
# File 'lib/rasti/db/query.rb', line 31
def primary_keys
pluck collection_class.primary_key
end
|
#raw ⇒ Object
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 |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_s ⇒ Object
Also known as:
inspect
126
127
128
|
# File 'lib/rasti/db/query.rb', line 126
def to_s
"#<#{self.class.name}: \"#{dataset.sql}\">"
end
|