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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/datasource/adapters/sequel.rb', line 100
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
202
203
204
205
206
207
|
# File 'lib/datasource/adapters/sequel.rb', line 202
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) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/datasource/adapters/sequel.rb', line 141
def get_assoc_eager_options(klass, name, assoc_select, append_select)
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))
}
}
else
{}
end
end
|
.get_final_scope(ds) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/datasource/adapters/sequel.rb', line 177
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))
end
scope = select_scope(ds)
if scope.respond_to?(:use_datasource)
scope = scope.clone.use_datasource(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
194
195
196
|
# File 'lib/datasource/adapters/sequel.rb', line 194
def get_rows(ds)
get_final_scope(ds).all
end
|
.get_sequel_select_values(values = nil) ⇒ Object
160
161
162
|
# File 'lib/datasource/adapters/sequel.rb', line 160
def get_sequel_select_values(values = nil)
values.map { |str| ::Sequel.lit(str) }
end
|
.get_table_name(klass) ⇒ Object
117
118
119
|
# File 'lib/datasource/adapters/sequel.rb', line 117
def get_table_name(klass)
klass.table_name
end
|
.has_attribute?(record, name) ⇒ Boolean
137
138
139
|
# File 'lib/datasource/adapters/sequel.rb', line 137
def has_attribute?(record, name)
record.values.key?(name.to_sym)
end
|
.is_scope?(obj) ⇒ Boolean
121
122
123
|
# File 'lib/datasource/adapters/sequel.rb', line 121
def is_scope?(obj)
obj.kind_of?(::Sequel::Dataset)
end
|
.primary_scope_table(ds) ⇒ Object
198
199
200
|
# File 'lib/datasource/adapters/sequel.rb', line 198
def primary_scope_table(ds)
ds.scope.first_source_alias.to_s
end
|
.scope_loaded?(scope) ⇒ Boolean
133
134
135
|
# File 'lib/datasource/adapters/sequel.rb', line 133
def scope_loaded?(scope)
false
end
|
.scope_to_class(scope) ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/datasource/adapters/sequel.rb', line 125
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
|
.select_scope(ds) ⇒ Object
168
169
170
|
# File 'lib/datasource/adapters/sequel.rb', line 168
def select_scope(ds)
ds.scope.select(*get_sequel_select_values(ds.get_select_values))
end
|
.to_query(ds) ⇒ Object
164
165
166
|
# File 'lib/datasource/adapters/sequel.rb', line 164
def to_query(ds)
ds.scope.sql
end
|
.upgrade_records(ds, records) ⇒ Object
172
173
174
175
|
# File 'lib/datasource/adapters/sequel.rb', line 172
def upgrade_records(ds, records)
get_final_scope(ds).send :post_load, records
ds.results(records)
end
|