Class: CassandraModel::QueryBuilder
- Inherits:
-
Object
- Object
- CassandraModel::QueryBuilder
show all
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/cassandra_model/query_builder.rb
Constant Summary
collapse
- EMPTY_OPTION =
[].freeze
Instance Method Summary
collapse
Constructor Details
#initialize(record_klass, params = {}, options = {}, extra_options = {}) ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
8
9
10
11
12
13
|
# File 'lib/cassandra_model/query_builder.rb', line 8
def initialize(record_klass, params = {}, options = {}, = {})
@record_klass = record_klass
@params = params
@options = options
=
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
138
139
140
141
|
# File 'lib/cassandra_model/query_builder.rb', line 138
def method_missing(method, *args)
scope = record_klass.scopes[method]
scope ? instance_exec(*args, &scope) : super
end
|
Instance Method Details
#==(rhs) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/cassandra_model/query_builder.rb', line 130
def ==(rhs)
rhs.is_a?(QueryBuilder) &&
rhs.record_klass == record_klass &&
rhs.params == params &&
rhs.options == options &&
rhs. ==
end
|
#async ⇒ Object
15
16
17
|
# File 'lib/cassandra_model/query_builder.rb', line 15
def async
@record_klass.request_async(@params, @options)
end
|
#check_exists ⇒ Object
62
63
64
|
# File 'lib/cassandra_model/query_builder.rb', line 62
def check_exists
new_instance(@params, @options.merge(check_exists: true), )
end
|
#cluster(*columns) ⇒ Object
94
95
96
|
# File 'lib/cassandra_model/query_builder.rb', line 94
def cluster(*columns)
new_instance(@params, @options, .merge(cluster: columns))
end
|
#cluster_except(*columns) ⇒ Object
98
99
100
|
# File 'lib/cassandra_model/query_builder.rb', line 98
def cluster_except(*columns)
cluster(*(@record_klass.primary_key - columns))
end
|
#create(attributes = {}, create_options = {}) ⇒ Object
44
45
46
|
# File 'lib/cassandra_model/query_builder.rb', line 44
def create(attributes = {}, create_options = {})
@record_klass.create(@params.merge(attributes), @options.merge(create_options))
end
|
#create_async(attributes = {}, create_options = {}) ⇒ Object
40
41
42
|
# File 'lib/cassandra_model/query_builder.rb', line 40
def create_async(attributes = {}, create_options = {})
@record_klass.create_async(@params.merge(attributes), @options.merge(create_options))
end
|
#each(&block) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/cassandra_model/query_builder.rb', line 80
def each(&block)
if [:cluster]
enum = ResultChunker.new(async, [:cluster])
enum = if [:cluster_limit]
ResultLimiter.new(enum, [:cluster_limit])
else
enum
end
block_given? ? enum.each(&block) : enum
else
async.each(&block)
end
end
|
#each_slice(slice_size = nil, &block) ⇒ Object
75
76
77
78
|
# File 'lib/cassandra_model/query_builder.rb', line 75
def each_slice(slice_size = nil, &block)
raise NotImplementedError if [:cluster]
paginate(slice_size).async.each_slice(&block)
end
|
#first ⇒ Object
36
37
38
|
# File 'lib/cassandra_model/query_builder.rb', line 36
def first
@record_klass.first(@params, @options)
end
|
#first_async ⇒ Object
32
33
34
|
# File 'lib/cassandra_model/query_builder.rb', line 32
def first_async
@record_klass.first_async(@params, @options)
end
|
#first_or_new(attributes) ⇒ Object
58
59
60
|
# File 'lib/cassandra_model/query_builder.rb', line 58
def first_or_new(attributes)
first_or_new_async(attributes).get
end
|
#first_or_new_async(attributes) ⇒ Object
52
53
54
55
56
|
# File 'lib/cassandra_model/query_builder.rb', line 52
def first_or_new_async(attributes)
first_async.then do |result|
result || new(attributes)
end
end
|
#get ⇒ Object
19
20
21
|
# File 'lib/cassandra_model/query_builder.rb', line 19
def get
@record_klass.request(@params, @options)
end
|
#inspect ⇒ Object
27
28
29
30
|
# File 'lib/cassandra_model/query_builder.rb', line 27
def inspect
results = limit(@options[:limit] || 10).get
"#<#{self.class.to_s}: #{inspected_results(results)}>"
end
|
#limit(limit) ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/cassandra_model/query_builder.rb', line 114
def limit(limit)
if [:cluster]
new_instance(@params, @options, .merge(cluster_limit: limit))
else
new_instance(@params, @options.merge(limit: limit), )
end
end
|
#new(attributes) ⇒ Object
48
49
50
|
# File 'lib/cassandra_model/query_builder.rb', line 48
def new(attributes)
@record_klass.new(@params.merge(attributes))
end
|
#order(*columns) ⇒ Object
110
111
112
|
# File 'lib/cassandra_model/query_builder.rb', line 110
def order(*columns)
append_option(columns, :order_by)
end
|
#paginate(page_size) ⇒ Object
126
127
128
|
# File 'lib/cassandra_model/query_builder.rb', line 126
def paginate(page_size)
new_instance(@params, @options.merge(page_size: page_size), )
end
|
#pluck(*columns) ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/cassandra_model/query_builder.rb', line 66
def pluck(*columns)
query = select(*columns)
if columns.length == 1
query.map { |result| pluck_values(columns, result).first }
else
query.map { |result| pluck_values(columns, result) }
end
end
|
#select(*columns) ⇒ Object
106
107
108
|
# File 'lib/cassandra_model/query_builder.rb', line 106
def select(*columns)
append_option(columns, :select)
end
|
#to_cql ⇒ Object
23
24
25
|
# File 'lib/cassandra_model/query_builder.rb', line 23
def to_cql
@record_klass.request_meta(@params, @options).first
end
|
#trace(trace) ⇒ Object
122
123
124
|
# File 'lib/cassandra_model/query_builder.rb', line 122
def trace(trace)
new_instance(@params, @options.merge(trace: trace), )
end
|
#where(params) ⇒ Object
102
103
104
|
# File 'lib/cassandra_model/query_builder.rb', line 102
def where(params)
new_instance(@params.merge(params.symbolize_keys), @options, )
end
|