Module: Datasource::Adapters::Sequel
- Defined in:
- lib/datasource/adapters/sequel.rb
Defined Under Namespace
Modules: DatasourceGenerator, Model, ScopeExtensions
Class Method Summary
collapse
Class Method Details
.association_reflection(klass, name) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/datasource/adapters/sequel.rb', line 132
def association_reflection(klass, name)
reflection = klass.association_reflections[name]
macro = case reflection[:type]
when :many_to_one then :belongs_to
when :one_to_many then :has_many
when :one_to_one then :has_one
else
fail Datasource::Error, "unimplemented association type #{reflection[:type]} - TODO"
end
{
klass: reflection[:cache][:class] || reflection[:class_name].constantize,
macro: macro,
foreign_key: reflection[:key].try!(:to_s)
}
end
|
.ensure_table_join!(ds, name, att) ⇒ Object
244
245
246
247
248
249
|
# File 'lib/datasource/adapters/sequel.rb', line 244
def ensure_table_join!(ds, name, att)
join_value = Hash(ds.scope.opts[:join]).find do |value|
(value.table_alias || value.table).to_s == att[:name]
end
fail Datasource::Error, "given scope does not join on #{name}, but it is required by #{att[:name]}" unless join_value
end
|
.get_assoc_eager_options(klass, name, assoc_select, append_select, params) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/datasource/adapters/sequel.rb', line 177
def get_assoc_eager_options(klass, name, assoc_select, append_select, params)
if reflection = association_reflection(klass, name)
self_append_select = []
Datasource::Base.reflection_select(reflection, append_select, self_append_select)
assoc_class = reflection[:klass]
datasource_class = assoc_class.default_datasource
{
name => ->(ds) {
ds.with_datasource(datasource_class)
.datasource_select(*(assoc_select + self_append_select))
.datasource_params(params)
}
}
else
{}
end
end
|
.get_final_scope(ds) ⇒ Object
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/datasource/adapters/sequel.rb', line 219
def get_final_scope(ds)
eager = {}
append_select = []
ds.expose_associations.each_pair do |assoc_name, assoc_select|
eager.merge!(
get_assoc_eager_options(ds.class.orm_klass, assoc_name.to_sym, assoc_select, append_select, ds.params))
end
scope = select_scope(ds)
if scope.respond_to?(:datasource_set)
scope = scope.clone.datasource_set(datasource_class: nil)
end
scope
.select_append(*get_sequel_select_values(append_select.map { |v| primary_scope_table(ds) + ".#{v}" }))
.eager(eager)
end
|
.get_rows(ds) ⇒ Object
236
237
238
|
# File 'lib/datasource/adapters/sequel.rb', line 236
def get_rows(ds)
get_final_scope(ds).all
end
|
.get_sequel_select_values(values = nil) ⇒ Object
197
198
199
|
# File 'lib/datasource/adapters/sequel.rb', line 197
def get_sequel_select_values(values = nil)
values.map { |str| ::Sequel.lit(str) }
end
|
.get_table_name(klass) ⇒ Object
149
150
151
|
# File 'lib/datasource/adapters/sequel.rb', line 149
def get_table_name(klass)
klass.table_name
end
|
.has_attribute?(record, name) ⇒ Boolean
173
174
175
|
# File 'lib/datasource/adapters/sequel.rb', line 173
def has_attribute?(record, name)
record.values.key?(name.to_sym)
end
|
.is_scope?(obj) ⇒ Boolean
153
154
155
|
# File 'lib/datasource/adapters/sequel.rb', line 153
def is_scope?(obj)
obj.kind_of?(::Sequel::Dataset)
end
|
.primary_scope_table(ds) ⇒ Object
240
241
242
|
# File 'lib/datasource/adapters/sequel.rb', line 240
def primary_scope_table(ds)
ds.scope.first_source_alias.to_s
end
|
.scope_loaded?(scope) ⇒ Boolean
165
166
167
|
# File 'lib/datasource/adapters/sequel.rb', line 165
def scope_loaded?(scope)
false
end
|
.scope_to_class(scope) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/datasource/adapters/sequel.rb', line 157
def scope_to_class(scope)
if scope.row_proc && scope.row_proc.ancestors.include?(::Sequel::Model)
scope.row_proc
else
fail Datasource::Error, "unable to determine model for scope"
end
end
|
.scope_to_records(scope) ⇒ Object
169
170
171
|
# File 'lib/datasource/adapters/sequel.rb', line 169
def scope_to_records(scope)
scope.all
end
|
.select_scope(ds) ⇒ Object
205
206
207
|
# File 'lib/datasource/adapters/sequel.rb', line 205
def select_scope(ds)
ds.scope.select(*get_sequel_select_values(ds.get_select_values))
end
|
.to_query(ds) ⇒ Object
201
202
203
|
# File 'lib/datasource/adapters/sequel.rb', line 201
def to_query(ds)
ds.scope.sql
end
|
.upgrade_records(ds, records) ⇒ Object
209
210
211
212
213
214
215
216
217
|
# File 'lib/datasource/adapters/sequel.rb', line 209
def upgrade_records(ds, records)
Datasource.logger.debug { "Upgrading records #{records.map(&:class).map(&:name).join(', ')}" }
assocs_loaded = records.all? { |record|
record._datasource_instance
}
get_final_scope(ds).send :post_load, records unless assocs_loaded
ds.results(records)
end
|