sequel 3.21.0 → 3.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +413 -3
- data/README.rdoc +20 -6
- data/Rakefile +23 -16
- data/bin/sequel +1 -5
- data/doc/association_basics.rdoc +210 -43
- data/doc/dataset_basics.rdoc +4 -4
- data/doc/mass_assignment.rdoc +54 -0
- data/doc/migration.rdoc +15 -538
- data/doc/model_hooks.rdoc +72 -27
- data/doc/opening_databases.rdoc +86 -50
- data/doc/prepared_statements.rdoc +40 -13
- data/doc/reflection.rdoc +8 -2
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/schema_modification.rdoc +547 -0
- data/doc/sharding.rdoc +7 -1
- data/doc/testing.rdoc +115 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/virtual_rows.rdoc +2 -2
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +15 -7
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +0 -5
- data/lib/sequel/adapters/do/postgres.rb +0 -5
- data/lib/sequel/adapters/do/sqlite.rb +0 -5
- data/lib/sequel/adapters/do.rb +17 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +448 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +5 -31
- data/lib/sequel/adapters/jdbc/db2.rb +44 -0
- data/lib/sequel/adapters/jdbc/derby.rb +217 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +29 -0
- data/lib/sequel/adapters/jdbc/h2.rb +26 -17
- data/lib/sequel/adapters/jdbc/hsqldb.rb +166 -0
- data/lib/sequel/adapters/jdbc/informix.rb +26 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +0 -32
- data/lib/sequel/adapters/jdbc/mysql.rb +9 -10
- data/lib/sequel/adapters/jdbc/oracle.rb +67 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +6 -11
- data/lib/sequel/adapters/jdbc/sqlite.rb +0 -5
- data/lib/sequel/adapters/jdbc/sqlserver.rb +41 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +107 -38
- data/lib/sequel/adapters/mock.rb +315 -0
- data/lib/sequel/adapters/mysql.rb +78 -182
- data/lib/sequel/adapters/mysql2.rb +24 -23
- data/lib/sequel/adapters/odbc/db2.rb +17 -0
- data/lib/sequel/adapters/odbc/mssql.rb +0 -5
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +2 -4
- data/lib/sequel/adapters/oracle.rb +349 -51
- data/lib/sequel/adapters/postgres.rb +169 -21
- data/lib/sequel/adapters/shared/access.rb +21 -6
- data/lib/sequel/adapters/shared/db2.rb +288 -0
- data/lib/sequel/adapters/shared/firebird.rb +214 -0
- data/lib/sequel/adapters/shared/informix.rb +45 -0
- data/lib/sequel/adapters/shared/mssql.rb +108 -65
- data/lib/sequel/adapters/shared/mysql.rb +56 -13
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +149 -0
- data/lib/sequel/adapters/shared/oracle.rb +185 -34
- data/lib/sequel/adapters/shared/postgres.rb +107 -58
- data/lib/sequel/adapters/shared/progress.rb +0 -6
- data/lib/sequel/adapters/shared/sqlite.rb +168 -41
- data/lib/sequel/adapters/sqlite.rb +27 -18
- data/lib/sequel/adapters/swift/mysql.rb +9 -5
- data/lib/sequel/adapters/swift/postgres.rb +0 -5
- data/lib/sequel/adapters/swift/sqlite.rb +6 -4
- data/lib/sequel/adapters/swift.rb +5 -5
- data/lib/sequel/adapters/tinytds.rb +152 -17
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +63 -0
- data/lib/sequel/ast_transformer.rb +190 -0
- data/lib/sequel/connection_pool/threaded.rb +3 -2
- data/lib/sequel/connection_pool.rb +1 -1
- data/lib/sequel/core.rb +46 -5
- data/lib/sequel/database/connecting.rb +5 -6
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +59 -1
- data/lib/sequel/database/logging.rb +1 -1
- data/lib/sequel/database/misc.rb +100 -19
- data/lib/sequel/database/query.rb +153 -58
- data/lib/sequel/database/schema_generator.rb +8 -5
- data/lib/sequel/database/schema_methods.rb +59 -29
- data/lib/sequel/dataset/actions.rb +194 -57
- data/lib/sequel/dataset/features.rb +81 -8
- data/lib/sequel/dataset/graph.rb +8 -7
- data/lib/sequel/dataset/misc.rb +50 -23
- data/lib/sequel/dataset/mutation.rb +5 -6
- data/lib/sequel/dataset/prepared_statements.rb +32 -15
- data/lib/sequel/dataset/query.rb +223 -42
- data/lib/sequel/dataset/sql.rb +58 -61
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/columns_introspection.rb +61 -0
- data/lib/sequel/extensions/migration.rb +6 -4
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/extensions/to_dot.rb +95 -83
- data/lib/sequel/model/associations.rb +997 -331
- data/lib/sequel/model/base.rb +386 -129
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +5 -1
- data/lib/sequel/model.rb +15 -8
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +58 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/identity_map.rb +114 -7
- data/lib/sequel/plugins/many_through_many.rb +65 -7
- data/lib/sequel/plugins/prepared_statements.rb +151 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/serialization.rb +6 -1
- data/lib/sequel/plugins/serialization_modification_detection.rb +51 -0
- data/lib/sequel/plugins/sharding.rb +12 -25
- data/lib/sequel/plugins/single_table_inheritance.rb +3 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/xml_serializer.rb +3 -3
- data/lib/sequel/sql.rb +107 -51
- data/lib/sequel/timezones.rb +44 -35
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/mssql_spec.rb +65 -29
- data/spec/adapters/mysql_spec.rb +122 -123
- data/spec/adapters/oracle_spec.rb +48 -76
- data/spec/adapters/postgres_spec.rb +213 -61
- data/spec/adapters/spec_helper.rb +6 -5
- data/spec/adapters/sqlite_spec.rb +35 -21
- data/spec/core/connection_pool_spec.rb +71 -92
- data/spec/core/core_sql_spec.rb +20 -31
- data/spec/core/database_spec.rb +729 -508
- data/spec/core/dataset_spec.rb +980 -1044
- data/spec/core/expression_filters_spec.rb +159 -42
- data/spec/core/mock_adapter_spec.rb +378 -0
- data/spec/core/object_graph_spec.rb +48 -114
- data/spec/core/schema_generator_spec.rb +3 -3
- data/spec/core/schema_spec.rb +298 -38
- data/spec/core/spec_helper.rb +6 -48
- data/spec/extensions/association_pks_spec.rb +38 -0
- data/spec/extensions/class_table_inheritance_spec.rb +1 -1
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +64 -0
- data/spec/extensions/identity_map_spec.rb +162 -0
- data/spec/extensions/instance_hooks_spec.rb +71 -0
- data/spec/extensions/many_through_many_spec.rb +195 -20
- data/spec/extensions/migration_spec.rb +17 -17
- data/spec/extensions/named_timezones_spec.rb +22 -2
- data/spec/extensions/nested_attributes_spec.rb +4 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +126 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +79 -0
- data/spec/extensions/prepared_statements_spec.rb +72 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +38 -0
- data/spec/extensions/schema_dumper_spec.rb +2 -2
- data/spec/extensions/schema_spec.rb +13 -21
- data/spec/extensions/serialization_modification_detection_spec.rb +37 -0
- data/spec/extensions/serialization_spec.rb +5 -8
- data/spec/extensions/single_table_inheritance_spec.rb +11 -0
- data/spec/extensions/spec_helper.rb +7 -1
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/to_dot_spec.rb +3 -5
- data/spec/extensions/typecast_on_load_spec.rb +1 -6
- data/spec/extensions/xml_serializer_spec.rb +16 -4
- data/spec/integration/associations_test.rb +522 -21
- data/spec/integration/database_test.rb +4 -2
- data/spec/integration/dataset_test.rb +375 -62
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/model_test.rb +80 -1
- data/spec/integration/plugin_test.rb +304 -116
- data/spec/integration/prepared_statement_test.rb +200 -120
- data/spec/integration/schema_test.rb +161 -30
- data/spec/integration/spec_helper.rb +39 -30
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +172 -5
- data/spec/integration/type_test.rb +17 -3
- data/spec/model/association_reflection_spec.rb +174 -7
- data/spec/model/associations_spec.rb +849 -661
- data/spec/model/base_spec.rb +255 -95
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +640 -676
- data/spec/model/hooks_spec.rb +309 -67
- data/spec/model/model_spec.rb +207 -167
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +321 -218
- data/spec/model/spec_helper.rb +13 -71
- data/spec/model/validations_spec.rb +11 -0
- metadata +95 -38
|
@@ -79,6 +79,27 @@ module Sequel
|
|
|
79
79
|
true
|
|
80
80
|
end
|
|
81
81
|
|
|
82
|
+
# The eager limit strategy to use for this dataset.
|
|
83
|
+
def eager_limit_strategy
|
|
84
|
+
fetch(:_eager_limit_strategy) do
|
|
85
|
+
self[:_eager_limit_strategy] = if self[:limit]
|
|
86
|
+
case s = self.fetch(:eager_limit_strategy){self[:model].default_eager_limit_strategy || :ruby}
|
|
87
|
+
when true
|
|
88
|
+
ds = associated_class.dataset
|
|
89
|
+
if ds.supports_window_functions?
|
|
90
|
+
:window_function
|
|
91
|
+
else
|
|
92
|
+
:ruby
|
|
93
|
+
end
|
|
94
|
+
else
|
|
95
|
+
s
|
|
96
|
+
end
|
|
97
|
+
else
|
|
98
|
+
nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
82
103
|
# By default associations do not need to select a key in an associated table
|
|
83
104
|
# to eagerly load.
|
|
84
105
|
def eager_loading_use_associated_key?
|
|
@@ -92,11 +113,36 @@ module Sequel
|
|
|
92
113
|
true
|
|
93
114
|
end
|
|
94
115
|
|
|
116
|
+
# The limit and offset for this association (returned as a two element array).
|
|
117
|
+
def limit_and_offset
|
|
118
|
+
if (v = self[:limit]).is_a?(Array)
|
|
119
|
+
v
|
|
120
|
+
else
|
|
121
|
+
[v, nil]
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
95
125
|
# Whether the associated object needs a primary key to be added/removed,
|
|
96
126
|
# false by default.
|
|
97
127
|
def need_associated_primary_key?
|
|
98
128
|
false
|
|
99
129
|
end
|
|
130
|
+
|
|
131
|
+
# Qualify +col+ with the given table name. If +col+ is an array of columns,
|
|
132
|
+
# return an array of qualified columns.
|
|
133
|
+
def qualify(table, col)
|
|
134
|
+
transform(col){|k| SQL::QualifiedIdentifier.new(table, k)}
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Qualify col with the associated model's table name.
|
|
138
|
+
def qualify_assoc(col)
|
|
139
|
+
qualify(associated_class.table_name, col)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Qualify col with the current model's table name.
|
|
143
|
+
def qualify_cur(col)
|
|
144
|
+
qualify(self[:model].table_name, col)
|
|
145
|
+
end
|
|
100
146
|
|
|
101
147
|
# Returns the reciprocal association variable, if one exists. The reciprocal
|
|
102
148
|
# association is the association in the associated class that is the opposite
|
|
@@ -165,6 +211,14 @@ module Sequel
|
|
|
165
211
|
def setter_method
|
|
166
212
|
:"#{self[:name]}="
|
|
167
213
|
end
|
|
214
|
+
|
|
215
|
+
private
|
|
216
|
+
|
|
217
|
+
# If +s+ is an array, map +s+ over the block. Otherwise, just call the
|
|
218
|
+
# block with +s+.
|
|
219
|
+
def transform(s)
|
|
220
|
+
s.is_a?(Array) ? s.map(&Proc.new) : (yield s)
|
|
221
|
+
end
|
|
168
222
|
end
|
|
169
223
|
|
|
170
224
|
class ManyToOneAssociationReflection < AssociationReflection
|
|
@@ -193,6 +247,11 @@ module Sequel
|
|
|
193
247
|
self[:key].nil?
|
|
194
248
|
end
|
|
195
249
|
|
|
250
|
+
# many_to_one associations don't need an eager limit strategy
|
|
251
|
+
def eager_limit_strategy
|
|
252
|
+
nil
|
|
253
|
+
end
|
|
254
|
+
|
|
196
255
|
# The key to use for the key hash when eager loading
|
|
197
256
|
def eager_loader_key
|
|
198
257
|
self[:eager_loader_key] ||= self[:key]
|
|
@@ -208,6 +267,11 @@ module Sequel
|
|
|
208
267
|
self[:primary_keys] ||= Array(primary_key)
|
|
209
268
|
end
|
|
210
269
|
alias associated_object_keys primary_keys
|
|
270
|
+
|
|
271
|
+
# #primary_key qualified by the associated table
|
|
272
|
+
def qualified_primary_key
|
|
273
|
+
self[:qualified_primary_key] ||= self[:qualify] == false ? primary_key : qualify_assoc(primary_key)
|
|
274
|
+
end
|
|
211
275
|
|
|
212
276
|
# True only if the reciprocal is a one_to_many association.
|
|
213
277
|
def reciprocal_array?
|
|
@@ -248,7 +312,7 @@ module Sequel
|
|
|
248
312
|
def can_have_associated_objects?(obj)
|
|
249
313
|
!self[:primary_keys].any?{|k| obj.send(k).nil?}
|
|
250
314
|
end
|
|
251
|
-
|
|
315
|
+
|
|
252
316
|
# Default foreign key name symbol for key in associated table that points to
|
|
253
317
|
# current table's primary key.
|
|
254
318
|
def default_key
|
|
@@ -259,10 +323,21 @@ module Sequel
|
|
|
259
323
|
def eager_loader_key
|
|
260
324
|
self[:eager_loader_key] ||= primary_key
|
|
261
325
|
end
|
|
326
|
+
|
|
327
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))
|
|
328
|
+
def eager_loading_predicate_key
|
|
329
|
+
self[:eager_loading_predicate_key] ||= qualify_assoc(self[:key])
|
|
330
|
+
end
|
|
331
|
+
alias qualified_key eager_loading_predicate_key
|
|
262
332
|
|
|
263
333
|
# The column in the current table that the key in the associated table references.
|
|
264
334
|
def primary_key
|
|
265
|
-
self[:primary_key]
|
|
335
|
+
self[:primary_key]
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# #primary_key qualified by the current table
|
|
339
|
+
def qualified_primary_key
|
|
340
|
+
self[:qualified_primary_key] ||= qualify_cur(primary_key)
|
|
266
341
|
end
|
|
267
342
|
|
|
268
343
|
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
|
@@ -297,6 +372,31 @@ module Sequel
|
|
|
297
372
|
class OneToOneAssociationReflection < OneToManyAssociationReflection
|
|
298
373
|
ASSOCIATION_TYPES[:one_to_one] = self
|
|
299
374
|
|
|
375
|
+
# one_to_one associations don't use an eager limit strategy by default, but
|
|
376
|
+
# support both DISTINCT ON and window functions as strategies.
|
|
377
|
+
def eager_limit_strategy
|
|
378
|
+
fetch(:_eager_limit_strategy) do
|
|
379
|
+
self[:_eager_limit_strategy] = case s = self[:eager_limit_strategy]
|
|
380
|
+
when Symbol
|
|
381
|
+
s
|
|
382
|
+
when true
|
|
383
|
+
ds = associated_class.dataset
|
|
384
|
+
if ds.supports_ordered_distinct_on?
|
|
385
|
+
:distinct_on
|
|
386
|
+
elsif ds.supports_window_functions?
|
|
387
|
+
:window_function
|
|
388
|
+
end
|
|
389
|
+
else
|
|
390
|
+
nil
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# The limit and offset for this association (returned as a two element array).
|
|
396
|
+
def limit_and_offset
|
|
397
|
+
[1, nil]
|
|
398
|
+
end
|
|
399
|
+
|
|
300
400
|
# one_to_one associations return a single object, not an array
|
|
301
401
|
def returns_array?
|
|
302
402
|
false
|
|
@@ -316,13 +416,6 @@ module Sequel
|
|
|
316
416
|
self[:left_key]
|
|
317
417
|
end
|
|
318
418
|
|
|
319
|
-
# The table containing the column to use for the associated key when eagerly loading
|
|
320
|
-
def associated_key_table
|
|
321
|
-
self[:associated_key_table] ||= (
|
|
322
|
-
syms = associated_class.dataset.split_alias(self[:join_table]);
|
|
323
|
-
syms[1] || syms[0])
|
|
324
|
-
end
|
|
325
|
-
|
|
326
419
|
# Alias of right_primary_keys
|
|
327
420
|
def associated_object_keys
|
|
328
421
|
right_primary_keys
|
|
@@ -362,11 +455,42 @@ module Sequel
|
|
|
362
455
|
self[:eager_loader_key] ||= self[:left_primary_key]
|
|
363
456
|
end
|
|
364
457
|
|
|
458
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)).
|
|
459
|
+
# The left key qualified by the join table.
|
|
460
|
+
def eager_loading_predicate_key
|
|
461
|
+
self[:eager_loading_predicate_key] ||= qualify(join_table_alias, self[:left_key])
|
|
462
|
+
end
|
|
463
|
+
alias qualified_left_key eager_loading_predicate_key
|
|
464
|
+
|
|
465
|
+
# The right key qualified by the join table.
|
|
466
|
+
def qualified_right_key
|
|
467
|
+
self[:qualified_right_key] ||= qualify(join_table_alias, self[:right_key])
|
|
468
|
+
end
|
|
469
|
+
|
|
365
470
|
# many_to_many associations need to select a key in an associated table to eagerly load
|
|
366
471
|
def eager_loading_use_associated_key?
|
|
367
472
|
true
|
|
368
473
|
end
|
|
369
474
|
|
|
475
|
+
# The source of the join table. This is the join table itself, unless it
|
|
476
|
+
# is aliased, in which case it is the unaliased part.
|
|
477
|
+
def join_table_source
|
|
478
|
+
fetch(:join_table_source) do
|
|
479
|
+
split_join_table_alias
|
|
480
|
+
self[:join_table_source]
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# The join table itself, unless it is aliased, in which case this
|
|
485
|
+
# is the alias.
|
|
486
|
+
def join_table_alias
|
|
487
|
+
fetch(:join_table_alias) do
|
|
488
|
+
split_join_table_alias
|
|
489
|
+
self[:join_table_alias]
|
|
490
|
+
end
|
|
491
|
+
end
|
|
492
|
+
alias associated_key_table join_table_alias
|
|
493
|
+
|
|
370
494
|
# Whether the associated object needs a primary key to be added/removed,
|
|
371
495
|
# true for many_to_many associations.
|
|
372
496
|
def need_associated_primary_key?
|
|
@@ -388,6 +512,11 @@ module Sequel
|
|
|
388
512
|
end
|
|
389
513
|
self[:reciprocal] = nil
|
|
390
514
|
end
|
|
515
|
+
|
|
516
|
+
# #right_primary_key qualified by the associated table
|
|
517
|
+
def qualified_right_primary_key
|
|
518
|
+
self[:qualified_right_primary_key] ||= qualify_assoc(right_primary_key)
|
|
519
|
+
end
|
|
391
520
|
|
|
392
521
|
# The primary key column(s) to use in the associated table (can be symbol or array).
|
|
393
522
|
def right_primary_key
|
|
@@ -404,6 +533,15 @@ module Sequel
|
|
|
404
533
|
return self[:select] if include?(:select)
|
|
405
534
|
self[:select] ||= Sequel::SQL::ColumnAll.new(associated_class.table_name)
|
|
406
535
|
end
|
|
536
|
+
|
|
537
|
+
private
|
|
538
|
+
|
|
539
|
+
# Split the join table into source and alias parts.
|
|
540
|
+
def split_join_table_alias
|
|
541
|
+
s, a = associated_class.dataset.split_alias(self[:join_table])
|
|
542
|
+
self[:join_table_source] = s
|
|
543
|
+
self[:join_table_alias] = a || s
|
|
544
|
+
end
|
|
407
545
|
end
|
|
408
546
|
|
|
409
547
|
# This module contains methods added to all association datasets
|
|
@@ -457,6 +595,12 @@ module Sequel
|
|
|
457
595
|
# Project.association_reflection(:portfolio)
|
|
458
596
|
# => {:type => :many_to_one, :name => :portfolio, ...}
|
|
459
597
|
#
|
|
598
|
+
# Associations should not have the same names as any of the columns in the
|
|
599
|
+
# model's current table they reference. If you are dealing with an existing schema that
|
|
600
|
+
# has a column named status, you can't name the association status, you'd
|
|
601
|
+
# have to name it foo_status or something else. If you give an association the same name
|
|
602
|
+
# as a column, you will probably end up with an association that doesn't work, or a SystemStackError.
|
|
603
|
+
#
|
|
460
604
|
# For a more in depth general overview, as well as a reference guide,
|
|
461
605
|
# see the {Association Basics guide}[link:files/doc/association_basics_rdoc.html].
|
|
462
606
|
# For examples of advanced usage, see the {Advanced Associations guide}[link:files/doc/advanced_associations_rdoc.html].
|
|
@@ -464,171 +608,210 @@ module Sequel
|
|
|
464
608
|
# All association reflections defined for this model (default: {}).
|
|
465
609
|
attr_reader :association_reflections
|
|
466
610
|
|
|
611
|
+
# The default :eager_limit_strategy option to use for *_many associations (default: nil)
|
|
612
|
+
attr_accessor :default_eager_limit_strategy
|
|
613
|
+
|
|
467
614
|
# Array of all association reflections for this model class
|
|
468
615
|
def all_association_reflections
|
|
469
616
|
association_reflections.values
|
|
470
617
|
end
|
|
471
618
|
|
|
619
|
+
# Given an association reflection and a dataset, apply the
|
|
620
|
+
# :select, :conditions, :order, :eager, :distinct, and :eager_block
|
|
621
|
+
# association options to the given dataset and return the dataset
|
|
622
|
+
# or a modified copy of it.
|
|
623
|
+
def apply_association_dataset_opts(opts, ds)
|
|
624
|
+
ds = ds.select(*opts.select) if opts.select
|
|
625
|
+
if c = opts[:conditions]
|
|
626
|
+
ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
|
|
627
|
+
end
|
|
628
|
+
ds = ds.order(*opts[:order]) if opts[:order]
|
|
629
|
+
ds = ds.eager(opts[:eager]) if opts[:eager]
|
|
630
|
+
ds = ds.distinct if opts[:distinct]
|
|
631
|
+
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
632
|
+
ds
|
|
633
|
+
end
|
|
634
|
+
|
|
472
635
|
# Associates a related model with the current model. The following types are
|
|
473
636
|
# supported:
|
|
474
637
|
#
|
|
475
|
-
#
|
|
476
|
-
#
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
#
|
|
480
|
-
#
|
|
481
|
-
#
|
|
482
|
-
#
|
|
483
|
-
#
|
|
484
|
-
#
|
|
485
|
-
#
|
|
486
|
-
#
|
|
487
|
-
#
|
|
488
|
-
#
|
|
489
|
-
#
|
|
490
|
-
#
|
|
491
|
-
#
|
|
638
|
+
# :many_to_one :: Foreign key in current model's table points to
|
|
639
|
+
# associated model's primary key. Each associated model object can
|
|
640
|
+
# be associated with more than one current model objects. Each current
|
|
641
|
+
# model object can be associated with only one associated model object.
|
|
642
|
+
# :one_to_many :: Foreign key in associated model's table points to this
|
|
643
|
+
# model's primary key. Each current model object can be associated with
|
|
644
|
+
# more than one associated model objects. Each associated model object
|
|
645
|
+
# can be associated with only one current model object.
|
|
646
|
+
# :one_to_one :: Similar to one_to_many in terms of foreign keys, but
|
|
647
|
+
# only one object is associated to the current object through the
|
|
648
|
+
# association. The methods created are similar to many_to_one, except
|
|
649
|
+
# that the one_to_one setter method saves the passed object.
|
|
650
|
+
# :many_to_many :: A join table is used that has a foreign key that points
|
|
651
|
+
# to this model's primary key and a foreign key that points to the
|
|
652
|
+
# associated model's primary key. Each current model object can be
|
|
653
|
+
# associated with many associated model objects, and each associated
|
|
654
|
+
# model object can be associated with many current model objects.
|
|
492
655
|
#
|
|
493
656
|
# The following options can be supplied:
|
|
494
|
-
#
|
|
495
|
-
#
|
|
496
|
-
#
|
|
497
|
-
#
|
|
498
|
-
#
|
|
499
|
-
#
|
|
500
|
-
#
|
|
501
|
-
#
|
|
502
|
-
#
|
|
503
|
-
#
|
|
504
|
-
#
|
|
505
|
-
#
|
|
506
|
-
#
|
|
507
|
-
#
|
|
508
|
-
#
|
|
509
|
-
#
|
|
510
|
-
#
|
|
511
|
-
#
|
|
512
|
-
#
|
|
513
|
-
#
|
|
514
|
-
#
|
|
515
|
-
#
|
|
516
|
-
#
|
|
517
|
-
#
|
|
518
|
-
#
|
|
519
|
-
#
|
|
520
|
-
#
|
|
521
|
-
#
|
|
522
|
-
#
|
|
523
|
-
#
|
|
524
|
-
#
|
|
525
|
-
#
|
|
526
|
-
#
|
|
527
|
-
#
|
|
528
|
-
#
|
|
529
|
-
#
|
|
530
|
-
#
|
|
531
|
-
#
|
|
532
|
-
#
|
|
533
|
-
#
|
|
534
|
-
#
|
|
535
|
-
#
|
|
536
|
-
#
|
|
537
|
-
#
|
|
538
|
-
#
|
|
539
|
-
#
|
|
540
|
-
#
|
|
541
|
-
#
|
|
542
|
-
#
|
|
543
|
-
#
|
|
544
|
-
#
|
|
545
|
-
#
|
|
546
|
-
#
|
|
547
|
-
#
|
|
548
|
-
#
|
|
549
|
-
#
|
|
550
|
-
#
|
|
551
|
-
#
|
|
552
|
-
#
|
|
553
|
-
#
|
|
554
|
-
#
|
|
555
|
-
#
|
|
556
|
-
#
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
560
|
-
#
|
|
561
|
-
#
|
|
562
|
-
#
|
|
563
|
-
#
|
|
564
|
-
#
|
|
565
|
-
#
|
|
566
|
-
#
|
|
567
|
-
#
|
|
568
|
-
#
|
|
569
|
-
#
|
|
570
|
-
#
|
|
571
|
-
#
|
|
572
|
-
#
|
|
573
|
-
#
|
|
574
|
-
#
|
|
575
|
-
#
|
|
576
|
-
#
|
|
577
|
-
#
|
|
578
|
-
#
|
|
579
|
-
#
|
|
580
|
-
#
|
|
581
|
-
#
|
|
582
|
-
#
|
|
583
|
-
#
|
|
584
|
-
#
|
|
585
|
-
#
|
|
586
|
-
#
|
|
587
|
-
#
|
|
588
|
-
#
|
|
589
|
-
#
|
|
590
|
-
#
|
|
591
|
-
#
|
|
592
|
-
#
|
|
593
|
-
#
|
|
594
|
-
#
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
#
|
|
598
|
-
#
|
|
599
|
-
#
|
|
600
|
-
#
|
|
601
|
-
#
|
|
602
|
-
#
|
|
603
|
-
#
|
|
604
|
-
#
|
|
605
|
-
#
|
|
606
|
-
#
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
#
|
|
610
|
-
#
|
|
611
|
-
#
|
|
612
|
-
#
|
|
613
|
-
#
|
|
614
|
-
#
|
|
615
|
-
#
|
|
616
|
-
#
|
|
617
|
-
#
|
|
618
|
-
#
|
|
619
|
-
#
|
|
620
|
-
#
|
|
621
|
-
#
|
|
622
|
-
#
|
|
623
|
-
#
|
|
624
|
-
#
|
|
625
|
-
#
|
|
657
|
+
# === All Types
|
|
658
|
+
# :after_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
659
|
+
# after a new item is added to the association.
|
|
660
|
+
# :after_load :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
661
|
+
# after the associated record(s) have been retrieved from the database.
|
|
662
|
+
# :after_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
663
|
+
# after an item is removed from the association.
|
|
664
|
+
# :after_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
665
|
+
# after an item is set using the association setter method.
|
|
666
|
+
# :allow_eager :: If set to false, you cannot load the association eagerly
|
|
667
|
+
# via eager or eager_graph
|
|
668
|
+
# :before_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
669
|
+
# before a new item is added to the association.
|
|
670
|
+
# :before_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
671
|
+
# before an item is removed from the association.
|
|
672
|
+
# :before_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
673
|
+
# before an item is set using the association setter method.
|
|
674
|
+
# :cartesian_product_number :: the number of joins completed by this association that could cause more
|
|
675
|
+
# than one row for each row in the current table (default: 0 for
|
|
676
|
+
# many_to_one and one_to_one associations, 1 for one_to_many and
|
|
677
|
+
# many_to_many associations).
|
|
678
|
+
# :class :: The associated class or its name. If not
|
|
679
|
+
# given, uses the association's name, which is camelized (and
|
|
680
|
+
# singularized unless the type is :many_to_one or :one_to_one)
|
|
681
|
+
# :clone :: Merge the current options and block into the options and block used in defining
|
|
682
|
+
# the given association. Can be used to DRY up a bunch of similar associations that
|
|
683
|
+
# all share the same options such as :class and :key, while changing the order and block used.
|
|
684
|
+
# :conditions :: The conditions to use to filter the association, can be any argument passed to filter.
|
|
685
|
+
# :dataset :: A proc that is instance_evaled to get the base dataset
|
|
686
|
+
# to use for the _dataset method (before the other options are applied).
|
|
687
|
+
# :distinct :: Use the DISTINCT clause when selecting associating object, both when
|
|
688
|
+
# lazy loading and eager loading via .eager (but not when using .eager_graph).
|
|
689
|
+
# :eager :: The associations to eagerly load via +eager+ when loading the associated object(s).
|
|
690
|
+
# :eager_block :: If given, use the block instead of the default block when
|
|
691
|
+
# eagerly loading. To not use a block when eager loading (when one is used normally),
|
|
692
|
+
# set to nil.
|
|
693
|
+
# :eager_graph :: The associations to eagerly load via +eager_graph+ when loading the associated object(s).
|
|
694
|
+
# many_to_many associations with this option cannot be eagerly loaded via +eager+.
|
|
695
|
+
# :eager_grapher :: A proc to use to implement eager loading via +eager_graph+, overriding the default.
|
|
696
|
+
# Takes one or three arguments. If three arguments, they are a dataset, an alias to use for
|
|
697
|
+
# the table to graph for this association, and the alias that was used for the current table
|
|
698
|
+
# (since you can cascade associations). If one argument, is passed a hash with keys :self,
|
|
699
|
+
# :table_alias, and :implicit_qualifier, corresponding to the three arguments, and an optional
|
|
700
|
+
# additional key :eager_block, a callback accepting one argument, the associated dataset. This
|
|
701
|
+
# is used to customize the association at query time.
|
|
702
|
+
# Should return a copy of the dataset with the association graphed into it.
|
|
703
|
+
# :eager_limit_strategy :: Determines the strategy used for enforcing limits when eager loading associations via
|
|
704
|
+
# the +eager+ method. For one_to_one associations, no strategy is used by default, and
|
|
705
|
+
# for *_many associations, the :ruby strategy is used by default, which still retrieves
|
|
706
|
+
# all records but slices the resulting array after the association is retrieved. You
|
|
707
|
+
# can pass a +true+ value for this option to have Sequel pick what it thinks is the best
|
|
708
|
+
# choice for the database, or specify a specific symbol to manually select a strategy.
|
|
709
|
+
# one_to_one associations support :distinct_on, :window_function, and :correlated_subquery.
|
|
710
|
+
# *_many associations support :ruby, :window_function, and :correlated_subquery.
|
|
711
|
+
# :eager_loader :: A proc to use to implement eager loading, overriding the default. Takes one or three arguments.
|
|
712
|
+
# If three arguments, the first should be a key hash (used solely to enhance performance), the
|
|
713
|
+
# second an array of records, and the third a hash of dependent associations. If one argument, is
|
|
714
|
+
# passed a hash with keys :key_hash, :rows, and :associations, corresponding to the three
|
|
715
|
+
# arguments, and an additional key :self, which is the dataset doing the eager loading. In the
|
|
716
|
+
# proc, the associated records should be queried from the database and the associations cache for
|
|
717
|
+
# each record should be populated.
|
|
718
|
+
# :eager_loader_key :: A symbol for the key column to use to populate the key hash
|
|
719
|
+
# for the eager loader.
|
|
720
|
+
# :extend :: A module or array of modules to extend the dataset with.
|
|
721
|
+
# :graph_block :: The block to pass to join_table when eagerly loading
|
|
722
|
+
# the association via +eager_graph+.
|
|
723
|
+
# :graph_conditions :: The additional conditions to use on the SQL join when eagerly loading
|
|
724
|
+
# the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
|
|
725
|
+
# specified, the :conditions option is used if it is a hash or array of two element arrays.
|
|
726
|
+
# :graph_join_type :: The type of SQL join to use when eagerly loading the association via
|
|
727
|
+
# eager_graph. Defaults to :left_outer.
|
|
728
|
+
# :graph_only_conditions :: The conditions to use on the SQL join when eagerly loading
|
|
729
|
+
# the association via +eager_graph+, instead of the default conditions specified by the
|
|
730
|
+
# foreign/primary keys. This option causes the :graph_conditions option to be ignored.
|
|
731
|
+
# :graph_select :: A column or array of columns to select from the associated table
|
|
732
|
+
# when eagerly loading the association via +eager_graph+. Defaults to all
|
|
733
|
+
# columns in the associated table.
|
|
734
|
+
# :limit :: Limit the number of records to the provided value. Use
|
|
735
|
+
# an array with two elements for the value to specify a
|
|
736
|
+
# limit (first element) and an offset (second element).
|
|
737
|
+
# :methods_module :: The module that methods the association creates will be placed into. Defaults
|
|
738
|
+
# to the module containing the model's columns.
|
|
739
|
+
# :order :: the column(s) by which to order the association dataset. Can be a
|
|
740
|
+
# singular column symbol or an array of column symbols.
|
|
741
|
+
# :order_eager_graph :: Whether to add the association's order to the graphed dataset's order when graphing
|
|
742
|
+
# via +eager_graph+. Defaults to true, so set to false to disable.
|
|
743
|
+
# :read_only :: Do not add a setter method (for many_to_one or one_to_one associations),
|
|
744
|
+
# or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
|
|
745
|
+
# :reciprocal :: the symbol name of the reciprocal association,
|
|
746
|
+
# if it exists. By default, Sequel will try to determine it by looking at the
|
|
747
|
+
# associated model's assocations for a association that matches
|
|
748
|
+
# the current association's key(s). Set to nil to not use a reciprocal.
|
|
749
|
+
# :select :: the columns to select. Defaults to the associated class's
|
|
750
|
+
# table_name.* in a many_to_many association, which means it doesn't include the attributes from the
|
|
751
|
+
# join table. If you want to include the join table attributes, you can
|
|
752
|
+
# use this option, but beware that the join table attributes can clash with
|
|
753
|
+
# attributes from the model table, so you should alias any attributes that have
|
|
754
|
+
# the same name in both the join table and the associated table.
|
|
755
|
+
# :validate :: Set to false to not validate when implicitly saving any associated object.
|
|
756
|
+
# === :many_to_one
|
|
757
|
+
# :key :: foreign key in current model's table that references
|
|
758
|
+
# associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
|
|
759
|
+
# array of symbols for a composite key association.
|
|
760
|
+
# :primary_key :: column in the associated table that :key option references, as a symbol.
|
|
761
|
+
# Defaults to the primary key of the associated table. Can use an
|
|
762
|
+
# array of symbols for a composite key association.
|
|
763
|
+
# :qualify :: Whether to use qualifier primary keys when loading the association. The default
|
|
764
|
+
# is true, so you must set to false to not qualify. Qualification rarely causes
|
|
765
|
+
# problems, but it's necessary to disable in some cases, such as when you are doing
|
|
766
|
+
# a JOIN USING operation on the column on Oracle.
|
|
767
|
+
# === :one_to_many and :one_to_one
|
|
768
|
+
# :key :: foreign key in associated model's table that references
|
|
769
|
+
# current model's primary key, as a symbol. Defaults to
|
|
770
|
+
# :"#{self.name.underscore}_id". Can use an
|
|
771
|
+
# array of symbols for a composite key association.
|
|
772
|
+
# :primary_key :: column in the current table that :key option references, as a symbol.
|
|
773
|
+
# Defaults to primary key of the current table. Can use an
|
|
774
|
+
# array of symbols for a composite key association.
|
|
775
|
+
# === :many_to_many
|
|
776
|
+
# :graph_join_table_block :: The block to pass to +join_table+ for
|
|
777
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
778
|
+
# :graph_join_table_conditions :: The additional conditions to use on the SQL join for
|
|
779
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
780
|
+
# Should be a hash or an array of two element arrays.
|
|
781
|
+
# :graph_join_table_join_type :: The type of SQL join to use for the join table when eagerly
|
|
782
|
+
# loading the association via +eager_graph+. Defaults to the
|
|
783
|
+
# :graph_join_type option or :left_outer.
|
|
784
|
+
# :graph_join_table_only_conditions :: The conditions to use on the SQL join for the join
|
|
785
|
+
# table when eagerly loading the association via +eager_graph+,
|
|
786
|
+
# instead of the default conditions specified by the
|
|
787
|
+
# foreign/primary keys. This option causes the
|
|
788
|
+
# :graph_join_table_conditions option to be ignored.
|
|
789
|
+
# :join_table :: name of table that includes the foreign keys to both
|
|
790
|
+
# the current model and the associated model, as a symbol. Defaults to the name
|
|
791
|
+
# of current model and name of associated model, pluralized,
|
|
792
|
+
# underscored, sorted, and joined with '_'.
|
|
793
|
+
# :join_table_block :: proc that can be used to modify the dataset used in the add/remove/remove_all
|
|
794
|
+
# methods. Should accept a dataset argument and return a modified dataset if present.
|
|
795
|
+
# :left_key :: foreign key in join table that points to current model's
|
|
796
|
+
# primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
|
|
797
|
+
# Can use an array of symbols for a composite key association.
|
|
798
|
+
# :left_primary_key :: column in current table that :left_key points to, as a symbol.
|
|
799
|
+
# Defaults to primary key of current table. Can use an
|
|
800
|
+
# array of symbols for a composite key association.
|
|
801
|
+
# :right_key :: foreign key in join table that points to associated
|
|
802
|
+
# model's primary key, as a symbol. Defaults to Defaults to :"#{name.to_s.singularize}_id".
|
|
803
|
+
# Can use an array of symbols for a composite key association.
|
|
804
|
+
# :right_primary_key :: column in associated table that :right_key points to, as a symbol.
|
|
805
|
+
# Defaults to primary key of the associated table. Can use an
|
|
806
|
+
# array of symbols for a composite key association.
|
|
807
|
+
# :uniq :: Adds a after_load callback that makes the array of objects unique.
|
|
626
808
|
def associate(type, name, opts = {}, &block)
|
|
627
809
|
raise(Error, 'one_to_many association type with :one_to_one option removed, used one_to_one association type') if opts[:one_to_one] && type == :one_to_many
|
|
628
810
|
raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type]
|
|
629
811
|
raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name
|
|
630
812
|
raise(Error, ':eager_loader option must have an arity of 1 or 3') if opts[:eager_loader] && ![1, 3].include?(opts[:eager_loader].arity)
|
|
631
|
-
|
|
813
|
+
raise(Error, ':eager_grapher option must have an arity of 1 or 3') if opts[:eager_grapher] && ![1, 3].include?(opts[:eager_grapher].arity)
|
|
814
|
+
|
|
632
815
|
# dup early so we don't modify opts
|
|
633
816
|
orig_opts = opts.dup
|
|
634
817
|
orig_opts = association_reflection(opts[:clone])[:orig_opts].merge(orig_opts) if opts[:clone]
|
|
@@ -668,57 +851,120 @@ module Sequel
|
|
|
668
851
|
|
|
669
852
|
# Modify and return eager loading dataset based on association options.
|
|
670
853
|
def eager_loading_dataset(opts, ds, select, associations, eager_options={})
|
|
854
|
+
ds = apply_association_dataset_opts(opts, ds)
|
|
671
855
|
ds = ds.select(*select) if select
|
|
672
|
-
if c = opts[:conditions]
|
|
673
|
-
ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
|
|
674
|
-
end
|
|
675
|
-
ds = ds.order(*opts[:order]) if opts[:order]
|
|
676
|
-
ds = ds.eager(opts[:eager]) if opts[:eager]
|
|
677
|
-
ds = ds.distinct if opts[:distinct]
|
|
678
856
|
if opts[:eager_graph]
|
|
857
|
+
raise(Error, "cannot eagerly load a #{opts[:type]} association that uses :eager_graph") if opts.eager_loading_use_associated_key?
|
|
679
858
|
ds = ds.eager_graph(opts[:eager_graph])
|
|
680
|
-
|
|
681
|
-
|
|
859
|
+
end
|
|
860
|
+
ds = ds.eager(associations) unless Array(associations).empty?
|
|
861
|
+
ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
|
|
862
|
+
if opts.eager_loading_use_associated_key?
|
|
682
863
|
ds = if opts[:uses_left_composite_keys]
|
|
683
|
-
|
|
684
|
-
ds.select_more(*opts.associated_key_alias.zip(opts.associated_key_column).map{|a, c| SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(t, c), a)})
|
|
864
|
+
ds.select_append(*opts.associated_key_alias.zip(opts.eager_loading_predicate_key).map{|a, k| SQL::AliasedExpression.new(k, a)})
|
|
685
865
|
else
|
|
686
|
-
ds.
|
|
866
|
+
ds.select_append(SQL::AliasedExpression.new(opts.eager_loading_predicate_key, opts.associated_key_alias))
|
|
687
867
|
end
|
|
688
868
|
end
|
|
689
|
-
ds = ds.eager(associations) unless Array(associations).empty?
|
|
690
|
-
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
691
869
|
ds
|
|
692
870
|
end
|
|
693
871
|
|
|
694
872
|
# Copy the association reflections to the subclass
|
|
695
873
|
def inherited(subclass)
|
|
696
874
|
super
|
|
697
|
-
subclass.instance_variable_set(:@association_reflections,
|
|
875
|
+
subclass.instance_variable_set(:@association_reflections, association_reflections.dup)
|
|
876
|
+
subclass.default_eager_limit_strategy = default_eager_limit_strategy
|
|
698
877
|
end
|
|
699
878
|
|
|
700
|
-
# Shortcut for adding a many_to_many association, see associate
|
|
879
|
+
# Shortcut for adding a many_to_many association, see #associate
|
|
701
880
|
def many_to_many(name, opts={}, &block)
|
|
702
881
|
associate(:many_to_many, name, opts, &block)
|
|
703
882
|
end
|
|
704
883
|
|
|
705
|
-
# Shortcut for adding a many_to_one association, see associate
|
|
884
|
+
# Shortcut for adding a many_to_one association, see #associate
|
|
706
885
|
def many_to_one(name, opts={}, &block)
|
|
707
886
|
associate(:many_to_one, name, opts, &block)
|
|
708
887
|
end
|
|
709
888
|
|
|
710
|
-
# Shortcut for adding a one_to_many association, see associate
|
|
889
|
+
# Shortcut for adding a one_to_many association, see #associate
|
|
711
890
|
def one_to_many(name, opts={}, &block)
|
|
712
891
|
associate(:one_to_many, name, opts, &block)
|
|
713
892
|
end
|
|
714
893
|
|
|
715
|
-
# Shortcut for adding a one_to_one association, see associate.
|
|
894
|
+
# Shortcut for adding a one_to_one association, see #associate.
|
|
716
895
|
def one_to_one(name, opts={}, &block)
|
|
717
896
|
associate(:one_to_one, name, opts, &block)
|
|
718
897
|
end
|
|
719
898
|
|
|
720
899
|
private
|
|
721
900
|
|
|
901
|
+
# Use a correlated subquery to limit the results of the eager loading dataset.
|
|
902
|
+
def apply_correlated_subquery_eager_limit_strategy(ds, opts)
|
|
903
|
+
klass = opts.associated_class
|
|
904
|
+
kds = klass.dataset
|
|
905
|
+
dsa = ds.send(:dataset_alias, 1)
|
|
906
|
+
raise Error, "can't use a correlated subquery if the associated class (#{opts.associated_class.inspect}) does not have a primary key" unless pk = klass.primary_key
|
|
907
|
+
pka = Array(pk)
|
|
908
|
+
raise Error, "can't use a correlated subquery if the associated class (#{opts.associated_class.inspect}) has a composite primary key and the database does not support multiple column IN" if pka.length > 1 && !ds.supports_multiple_column_in?
|
|
909
|
+
table = kds.opts[:from]
|
|
910
|
+
raise Error, "can't use a correlated subquery unless the associated class (#{opts.associated_class.inspect}) uses a single FROM table" unless table && table.length == 1
|
|
911
|
+
table = table.first
|
|
912
|
+
if order = ds.opts[:order]
|
|
913
|
+
oproc = lambda do |x|
|
|
914
|
+
case x
|
|
915
|
+
when Symbol
|
|
916
|
+
t, c, a = ds.send(:split_symbol, x)
|
|
917
|
+
if t && t.to_sym == table
|
|
918
|
+
SQL::QualifiedIdentifier.new(dsa, c)
|
|
919
|
+
else
|
|
920
|
+
x
|
|
921
|
+
end
|
|
922
|
+
when SQL::QualifiedIdentifier
|
|
923
|
+
if x.table == table
|
|
924
|
+
SQL::QualifiedIdentifier.new(dsa, x.column)
|
|
925
|
+
else
|
|
926
|
+
x
|
|
927
|
+
end
|
|
928
|
+
when SQL::OrderedExpression
|
|
929
|
+
SQL::OrderedExpression.new(oproc.call(x.expression), x.descending, :nulls=>x.nulls)
|
|
930
|
+
else
|
|
931
|
+
x
|
|
932
|
+
end
|
|
933
|
+
end
|
|
934
|
+
order = order.map(&oproc)
|
|
935
|
+
end
|
|
936
|
+
limit, offset = opts.limit_and_offset
|
|
937
|
+
|
|
938
|
+
subquery = yield kds.
|
|
939
|
+
unlimited.
|
|
940
|
+
from(SQL::AliasedExpression.new(table, dsa)).
|
|
941
|
+
select(*pka.map{|k| SQL::QualifiedIdentifier.new(dsa, k)}).
|
|
942
|
+
order(*order).
|
|
943
|
+
limit(limit, offset)
|
|
944
|
+
|
|
945
|
+
pk = if pk.is_a?(Array)
|
|
946
|
+
pk.map{|k| SQL::QualifiedIdentifier.new(table, k)}
|
|
947
|
+
else
|
|
948
|
+
SQL::QualifiedIdentifier.new(table, pk)
|
|
949
|
+
end
|
|
950
|
+
ds.filter(pk=>subquery)
|
|
951
|
+
end
|
|
952
|
+
|
|
953
|
+
# Use a window function to limit the results of the eager loading dataset.
|
|
954
|
+
def apply_window_function_eager_limit_strategy(ds, opts)
|
|
955
|
+
rn = ds.row_number_column
|
|
956
|
+
limit, offset = opts.limit_and_offset
|
|
957
|
+
ds = ds.unordered.select_append{row_number(:over, :partition=>opts.eager_loading_predicate_key, :order=>ds.opts[:order]){}.as(rn)}.from_self
|
|
958
|
+
ds = if opts[:type] == :one_to_one
|
|
959
|
+
ds.where(rn => 1)
|
|
960
|
+
elsif offset
|
|
961
|
+
offset += 1
|
|
962
|
+
ds.where(rn => (offset...(offset+limit)))
|
|
963
|
+
else
|
|
964
|
+
ds.where{SQL::Identifier.new(rn) <= limit}
|
|
965
|
+
end
|
|
966
|
+
end
|
|
967
|
+
|
|
722
968
|
# The module to use for the association's methods. Defaults to
|
|
723
969
|
# the overridable_methods_module.
|
|
724
970
|
def association_module(opts={})
|
|
@@ -753,9 +999,20 @@ module Sequel
|
|
|
753
999
|
def_association_method(opts)
|
|
754
1000
|
end
|
|
755
1001
|
|
|
756
|
-
# Adds the association method to the association methods module.
|
|
757
|
-
|
|
758
|
-
|
|
1002
|
+
# Adds the association method to the association methods module. Be backwards
|
|
1003
|
+
# compatible with ruby 1.8.6, which doesn't support blocks taking block arguments.
|
|
1004
|
+
if RUBY_VERSION >= '1.8.7'
|
|
1005
|
+
class_eval <<-END, __FILE__, __LINE__+1
|
|
1006
|
+
def def_association_method(opts)
|
|
1007
|
+
association_module_def(opts.association_method, opts){|*dynamic_opts, &block| load_associated_objects(opts, dynamic_opts[0], &block)}
|
|
1008
|
+
end
|
|
1009
|
+
END
|
|
1010
|
+
else
|
|
1011
|
+
class_eval <<-END, __FILE__, __LINE__+1
|
|
1012
|
+
def def_association_method(opts)
|
|
1013
|
+
association_module_def(opts.association_method, opts){|*dynamic_opts| load_associated_objects(opts, dynamic_opts[0])}
|
|
1014
|
+
end
|
|
1015
|
+
END
|
|
759
1016
|
end
|
|
760
1017
|
|
|
761
1018
|
# Configures many_to_many association reflection and adds the related association methods
|
|
@@ -782,14 +1039,27 @@ module Sequel
|
|
|
782
1039
|
opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
|
|
783
1040
|
opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
|
|
784
1041
|
opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) + lcks.zip(lcpks.map{|k| send(k)}))}
|
|
785
|
-
|
|
786
|
-
|
|
1042
|
+
|
|
787
1043
|
opts[:eager_loader] ||= proc do |eo|
|
|
788
1044
|
h = eo[:key_hash][left_pk]
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1045
|
+
rows = eo[:rows]
|
|
1046
|
+
rows.each{|object| object.associations[name] = []}
|
|
1047
|
+
r = rcks.zip(opts.right_primary_keys)
|
|
1048
|
+
l = [[opts.qualify(opts.join_table_alias, left), h.keys]]
|
|
1049
|
+
ds = model.eager_loading_dataset(opts, opts.associated_class.inner_join(join_table, r + l), nil, eo[:associations], eo)
|
|
1050
|
+
case opts.eager_limit_strategy
|
|
1051
|
+
when :window_function
|
|
1052
|
+
delete_rn = true
|
|
1053
|
+
rn = ds.row_number_column
|
|
1054
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1055
|
+
when :correlated_subquery
|
|
1056
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1057
|
+
dsa = ds.send(:dataset_alias, 2)
|
|
1058
|
+
xds.inner_join(join_table, r + lcks.map{|k| [k, SQL::QualifiedIdentifier.new(opts.join_table_alias, k)]}, :table_alias=>dsa)
|
|
1059
|
+
end
|
|
1060
|
+
end
|
|
1061
|
+
ds.all do |assoc_record|
|
|
1062
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
793
1063
|
hash_key = if uses_lcks
|
|
794
1064
|
left_key_alias.map{|k| assoc_record.values.delete(k)}
|
|
795
1065
|
else
|
|
@@ -798,6 +1068,10 @@ module Sequel
|
|
|
798
1068
|
next unless objects = h[hash_key]
|
|
799
1069
|
objects.each{|object| object.associations[name].push(assoc_record)}
|
|
800
1070
|
end
|
|
1071
|
+
if opts.eager_limit_strategy == :ruby
|
|
1072
|
+
limit, offset = opts.limit_and_offset
|
|
1073
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1074
|
+
end
|
|
801
1075
|
end
|
|
802
1076
|
|
|
803
1077
|
join_type = opts[:graph_join_type]
|
|
@@ -810,9 +1084,10 @@ module Sequel
|
|
|
810
1084
|
jt_only_conditions = opts[:graph_join_table_only_conditions]
|
|
811
1085
|
jt_join_type = opts[:graph_join_table_join_type]
|
|
812
1086
|
jt_graph_block = opts[:graph_join_table_block]
|
|
813
|
-
opts[:eager_grapher] ||= proc do |
|
|
814
|
-
ds =
|
|
815
|
-
ds.graph(
|
|
1087
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1088
|
+
ds = eo[:self]
|
|
1089
|
+
ds = ds.graph(join_table, use_jt_only_conditions ? jt_only_conditions : lcks.zip(lcpks) + graph_jt_conds, :select=>false, :table_alias=>ds.unused_table_alias(join_table, [eo[:table_alias]]), :join_type=>jt_join_type, :implicit_qualifier=>eo[:implicit_qualifier], :from_self_alias=>ds.opts[:eager_graph][:master], &jt_graph_block)
|
|
1090
|
+
ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : opts.right_primary_keys.zip(rcks) + conditions, :select=>select, :table_alias=>eo[:table_alias], :join_type=>join_type, &graph_block)
|
|
816
1091
|
end
|
|
817
1092
|
|
|
818
1093
|
def_association_dataset_methods(opts)
|
|
@@ -843,15 +1118,17 @@ module Sequel
|
|
|
843
1118
|
opts[:key] = opts.default_key unless opts.include?(:key)
|
|
844
1119
|
key = opts[:key]
|
|
845
1120
|
cks = opts[:keys] = Array(opts[:key])
|
|
1121
|
+
opts[:qualified_key] = opts.qualify_cur(key)
|
|
846
1122
|
if opts[:primary_key]
|
|
847
1123
|
cpks = Array(opts[:primary_key])
|
|
848
1124
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
849
1125
|
end
|
|
850
1126
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
1127
|
+
qualify = opts[:qualify] != false
|
|
851
1128
|
opts[:cartesian_product_number] ||= 0
|
|
852
1129
|
opts[:dataset] ||= proc do
|
|
853
1130
|
klass = opts.associated_class
|
|
854
|
-
klass.filter(opts.
|
|
1131
|
+
klass.filter(Array(opts.qualified_primary_key).zip(cks.map{|k| send(k)}))
|
|
855
1132
|
end
|
|
856
1133
|
opts[:eager_loader] ||= proc do |eo|
|
|
857
1134
|
h = eo[:key_hash][key]
|
|
@@ -862,7 +1139,7 @@ module Sequel
|
|
|
862
1139
|
# Skip eager loading if no objects have a foreign key for this association
|
|
863
1140
|
unless keys.empty?
|
|
864
1141
|
klass = opts.associated_class
|
|
865
|
-
model.eager_loading_dataset(opts, klass.filter(
|
|
1142
|
+
model.eager_loading_dataset(opts, klass.filter(opts.qualified_primary_key=>keys), nil, eo[:associations], eo).all do |assoc_record|
|
|
866
1143
|
hash_key = uses_cks ? opts.primary_keys.map{|k| assoc_record.send(k)} : assoc_record.send(opts.primary_key)
|
|
867
1144
|
next unless objects = h[hash_key]
|
|
868
1145
|
objects.each{|object| object.associations[name] = assoc_record}
|
|
@@ -876,8 +1153,9 @@ module Sequel
|
|
|
876
1153
|
only_conditions = opts[:graph_only_conditions]
|
|
877
1154
|
conditions = opts[:graph_conditions]
|
|
878
1155
|
graph_block = opts[:graph_block]
|
|
879
|
-
opts[:eager_grapher] ||= proc do |
|
|
880
|
-
ds
|
|
1156
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1157
|
+
ds = eo[:self]
|
|
1158
|
+
ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : opts.primary_keys.zip(cks) + conditions, eo.merge(:select=>select, :join_type=>join_type, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
|
|
881
1159
|
end
|
|
882
1160
|
|
|
883
1161
|
def_association_dataset_methods(opts)
|
|
@@ -900,25 +1178,42 @@ module Sequel
|
|
|
900
1178
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
901
1179
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
902
1180
|
opts[:dataset] ||= proc do
|
|
903
|
-
|
|
904
|
-
klass.filter(cks.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}.zip(cpks.map{|k| send(k)}))
|
|
1181
|
+
opts.associated_class.filter(Array(opts.qualified_key).zip(cpks.map{|k| send(k)}))
|
|
905
1182
|
end
|
|
906
1183
|
opts[:eager_loader] ||= proc do |eo|
|
|
907
1184
|
h = eo[:key_hash][primary_key]
|
|
1185
|
+
rows = eo[:rows]
|
|
908
1186
|
if one_to_one
|
|
909
|
-
|
|
1187
|
+
rows.each{|object| object.associations[name] = nil}
|
|
910
1188
|
else
|
|
911
|
-
|
|
1189
|
+
rows.each{|object| object.associations[name] = []}
|
|
912
1190
|
end
|
|
913
1191
|
reciprocal = opts.reciprocal
|
|
914
1192
|
klass = opts.associated_class
|
|
915
|
-
|
|
1193
|
+
filter_keys = opts.eager_loading_predicate_key
|
|
1194
|
+
ds = model.eager_loading_dataset(opts, klass.filter(filter_keys=>h.keys), nil, eo[:associations], eo)
|
|
1195
|
+
case opts.eager_limit_strategy
|
|
1196
|
+
when :distinct_on
|
|
1197
|
+
ds = ds.distinct(*filter_keys).order_prepend(*filter_keys)
|
|
1198
|
+
when :window_function
|
|
1199
|
+
delete_rn = true
|
|
1200
|
+
rn = ds.row_number_column
|
|
1201
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1202
|
+
when :correlated_subquery
|
|
1203
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1204
|
+
xds.where(opts.associated_object_keys.map{|k| [SQL::QualifiedIdentifier.new(xds.first_source_alias, k), SQL::QualifiedIdentifier.new(xds.first_source_table, k)]})
|
|
1205
|
+
end
|
|
1206
|
+
end
|
|
1207
|
+
ds.all do |assoc_record|
|
|
1208
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
916
1209
|
hash_key = uses_cks ? cks.map{|k| assoc_record.send(k)} : assoc_record.send(key)
|
|
917
1210
|
next unless objects = h[hash_key]
|
|
918
1211
|
if one_to_one
|
|
919
1212
|
objects.each do |object|
|
|
920
|
-
object.associations[name]
|
|
921
|
-
|
|
1213
|
+
unless object.associations[name]
|
|
1214
|
+
object.associations[name] = assoc_record
|
|
1215
|
+
assoc_record.associations[reciprocal] = object if reciprocal
|
|
1216
|
+
end
|
|
922
1217
|
end
|
|
923
1218
|
else
|
|
924
1219
|
objects.each do |object|
|
|
@@ -927,6 +1222,10 @@ module Sequel
|
|
|
927
1222
|
end
|
|
928
1223
|
end
|
|
929
1224
|
end
|
|
1225
|
+
if opts.eager_limit_strategy == :ruby
|
|
1226
|
+
limit, offset = opts.limit_and_offset
|
|
1227
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1228
|
+
end
|
|
930
1229
|
end
|
|
931
1230
|
|
|
932
1231
|
join_type = opts[:graph_join_type]
|
|
@@ -936,10 +1235,11 @@ module Sequel
|
|
|
936
1235
|
conditions = opts[:graph_conditions]
|
|
937
1236
|
opts[:cartesian_product_number] ||= one_to_one ? 0 : 1
|
|
938
1237
|
graph_block = opts[:graph_block]
|
|
939
|
-
opts[:eager_grapher] ||= proc do |
|
|
940
|
-
ds =
|
|
1238
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1239
|
+
ds = eo[:self]
|
|
1240
|
+
ds = ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : cks.zip(cpks) + conditions, eo.merge(:select=>select, :join_type=>join_type, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
|
|
941
1241
|
# We only load reciprocals for one_to_many associations, as other reciprocals don't make sense
|
|
942
|
-
ds.opts[:eager_graph][:reciprocals][
|
|
1242
|
+
ds.opts[:eager_graph][:reciprocals][eo[:table_alias]] = opts.reciprocal
|
|
943
1243
|
ds
|
|
944
1244
|
end
|
|
945
1245
|
|
|
@@ -993,6 +1293,15 @@ module Sequel
|
|
|
993
1293
|
association_module_def(opts.remove_method, opts){|o,*args| remove_associated_object(opts, o, *args)}
|
|
994
1294
|
association_module_def(opts.remove_all_method, opts){|*args| remove_all_associated_objects(opts, *args)}
|
|
995
1295
|
end
|
|
1296
|
+
|
|
1297
|
+
# Return dataset to graph into given the association reflection, applying the :callback option if set.
|
|
1298
|
+
def eager_graph_dataset(opts, eager_options)
|
|
1299
|
+
ds = opts.associated_class.dataset
|
|
1300
|
+
if cb = eager_options[:callback]
|
|
1301
|
+
ds = cb.call(ds)
|
|
1302
|
+
end
|
|
1303
|
+
ds
|
|
1304
|
+
end
|
|
996
1305
|
end
|
|
997
1306
|
|
|
998
1307
|
# Instance methods used to implement the associations support.
|
|
@@ -1004,8 +1313,8 @@ module Sequel
|
|
|
1004
1313
|
@associations ||= {}
|
|
1005
1314
|
end
|
|
1006
1315
|
|
|
1007
|
-
#
|
|
1008
|
-
# an Error if the model has no primary key.
|
|
1316
|
+
# Formally used internally by the associations code, like pk but doesn't raise
|
|
1317
|
+
# an Error if the model has no primary key. Not used any longer, deprecated.
|
|
1009
1318
|
def pk_or_nil
|
|
1010
1319
|
key = primary_key
|
|
1011
1320
|
key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key]
|
|
@@ -1033,6 +1342,15 @@ module Sequel
|
|
|
1033
1342
|
ds
|
|
1034
1343
|
end
|
|
1035
1344
|
|
|
1345
|
+
# Return a dataset for the association after applying any dynamic callback.
|
|
1346
|
+
def _associated_dataset(opts, dynamic_opts)
|
|
1347
|
+
ds = send(opts.dataset_method)
|
|
1348
|
+
if callback = dynamic_opts[:callback]
|
|
1349
|
+
ds = callback.call(ds)
|
|
1350
|
+
end
|
|
1351
|
+
ds
|
|
1352
|
+
end
|
|
1353
|
+
|
|
1036
1354
|
# Return an association dataset for the given association reflection
|
|
1037
1355
|
def _dataset(opts)
|
|
1038
1356
|
raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
|
|
@@ -1041,17 +1359,18 @@ module Sequel
|
|
|
1041
1359
|
|
|
1042
1360
|
# Dataset for the join table of the given many to many association reflection
|
|
1043
1361
|
def _join_table_dataset(opts)
|
|
1044
|
-
ds = model.db.from(opts
|
|
1362
|
+
ds = model.db.from(opts.join_table_source)
|
|
1045
1363
|
opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
|
|
1046
1364
|
end
|
|
1047
1365
|
|
|
1048
|
-
# Return the associated objects from the dataset, without callbacks, reciprocals, and caching.
|
|
1049
|
-
|
|
1366
|
+
# Return the associated objects from the dataset, without association callbacks, reciprocals, and caching.
|
|
1367
|
+
# Still apply the dynamic callback if present.
|
|
1368
|
+
def _load_associated_objects(opts, dynamic_opts={})
|
|
1050
1369
|
if opts.returns_array?
|
|
1051
|
-
opts.can_have_associated_objects?(self) ?
|
|
1370
|
+
opts.can_have_associated_objects?(self) ? _associated_dataset(opts, dynamic_opts).all : []
|
|
1052
1371
|
else
|
|
1053
1372
|
if opts.can_have_associated_objects?(self)
|
|
1054
|
-
|
|
1373
|
+
_associated_dataset(opts, dynamic_opts).all.first
|
|
1055
1374
|
end
|
|
1056
1375
|
end
|
|
1057
1376
|
end
|
|
@@ -1113,13 +1432,20 @@ module Sequel
|
|
|
1113
1432
|
end
|
|
1114
1433
|
|
|
1115
1434
|
# Load the associated objects using the dataset, handling callbacks, reciprocals, and caching.
|
|
1116
|
-
def load_associated_objects(opts,
|
|
1435
|
+
def load_associated_objects(opts, dynamic_opts=nil)
|
|
1436
|
+
if dynamic_opts == true or dynamic_opts == false or dynamic_opts == nil
|
|
1437
|
+
dynamic_opts = {:reload=>dynamic_opts}
|
|
1438
|
+
elsif dynamic_opts.respond_to?(:call)
|
|
1439
|
+
dynamic_opts = {:callback=>dynamic_opts}
|
|
1440
|
+
end
|
|
1441
|
+
if block_given?
|
|
1442
|
+
dynamic_opts = dynamic_opts.merge(:callback=>Proc.new)
|
|
1443
|
+
end
|
|
1117
1444
|
name = opts[:name]
|
|
1118
|
-
if associations.include?(name) and !reload
|
|
1445
|
+
if associations.include?(name) and !dynamic_opts[:callback] and !dynamic_opts[:reload]
|
|
1119
1446
|
associations[name]
|
|
1120
1447
|
else
|
|
1121
|
-
objs = _load_associated_objects(opts)
|
|
1122
|
-
run_association_callbacks(opts, :after_load, objs)
|
|
1448
|
+
objs = _load_associated_objects(opts, dynamic_opts)
|
|
1123
1449
|
if opts.set_reciprocal_to_self?
|
|
1124
1450
|
if opts.returns_array?
|
|
1125
1451
|
objs.each{|o| add_reciprocal_object(opts, o)}
|
|
@@ -1128,6 +1454,8 @@ module Sequel
|
|
|
1128
1454
|
end
|
|
1129
1455
|
end
|
|
1130
1456
|
associations[name] = objs
|
|
1457
|
+
run_association_callbacks(opts, :after_load, objs)
|
|
1458
|
+
associations[name]
|
|
1131
1459
|
end
|
|
1132
1460
|
end
|
|
1133
1461
|
|
|
@@ -1238,7 +1566,7 @@ module Sequel
|
|
|
1238
1566
|
# time, as it loads associated records using one query per association. However,
|
|
1239
1567
|
# it does not allow you the ability to filter or order based on columns in associated tables. +eager_graph+ loads
|
|
1240
1568
|
# all records in a single query using JOINs, allowing you to filter or order based on columns in associated
|
|
1241
|
-
# tables. However, +eager_graph+
|
|
1569
|
+
# tables. However, +eager_graph+ is usually slower than +eager+, especially if multiple
|
|
1242
1570
|
# one_to_many or many_to_many associations are joined.
|
|
1243
1571
|
#
|
|
1244
1572
|
# You can cascade the eager loading (loading associations on associated objects)
|
|
@@ -1259,12 +1587,80 @@ module Sequel
|
|
|
1259
1587
|
# Artist.eager_graph(:albums=>:tracks).all
|
|
1260
1588
|
# Artist.eager(:albums=>{:tracks=>:genre}).all
|
|
1261
1589
|
# Artist.eager_graph(:albums=>{:tracks=>:genre}).all
|
|
1590
|
+
#
|
|
1591
|
+
# You can also pass a callback as a hash value in order to customize the dataset being
|
|
1592
|
+
# eager loaded at query time, analogous to the way the :eager_block association option
|
|
1593
|
+
# allows you to customize it at association definition time. For example,
|
|
1594
|
+
# if you wanted artists with their albums since 1990:
|
|
1595
|
+
#
|
|
1596
|
+
# Artist.eager(:albums => proc{|ds| ds.filter{year > 1990}})
|
|
1597
|
+
#
|
|
1598
|
+
# Or if you needed albums and their artist's name only, using a single query:
|
|
1599
|
+
#
|
|
1600
|
+
# Albums.eager_graph(:artist => proc{|ds| ds.select(:name)})
|
|
1601
|
+
#
|
|
1602
|
+
# To cascade eager loading while using a callback, you substitute the cascaded
|
|
1603
|
+
# associations with a single entry hash that has the proc callback as the key and
|
|
1604
|
+
# the cascaded associations as the value. This will load artists with their albums
|
|
1605
|
+
# since 1990, and also the tracks on those albums and the genre for those tracks:
|
|
1606
|
+
#
|
|
1607
|
+
# Artist.eager(:albums => {proc{|ds| ds.filter{year > 1990}}=>{:tracks => :genre}})
|
|
1262
1608
|
module DatasetMethods
|
|
1263
1609
|
# Add the <tt>eager!</tt> and <tt>eager_graph!</tt> mutation methods to the dataset.
|
|
1264
1610
|
def self.extended(obj)
|
|
1265
1611
|
obj.def_mutation_method(:eager, :eager_graph)
|
|
1266
1612
|
end
|
|
1267
1613
|
|
|
1614
|
+
# If the expression is in the form <tt>x = y</tt> where +y+ is a <tt>Sequel::Model</tt>
|
|
1615
|
+
# instance, array of <tt>Sequel::Model</tt> instances, or a <tt>Sequel::Model</tt> dataset,
|
|
1616
|
+
# assume +x+ is an association symbol and look up the association reflection
|
|
1617
|
+
# via the dataset's model. From there, return the appropriate SQL based on the type of
|
|
1618
|
+
# association and the values of the foreign/primary keys of +y+. For most association
|
|
1619
|
+
# types, this is a simple transformation, but for +many_to_many+ associations this
|
|
1620
|
+
# creates a subquery to the join table.
|
|
1621
|
+
def complex_expression_sql(op, args)
|
|
1622
|
+
r = args.at(1)
|
|
1623
|
+
if (((op == :'=' || op == :'!=') and r.is_a?(Sequel::Model)) ||
|
|
1624
|
+
(multiple = ((op == :IN || op == :'NOT IN') and ((is_ds = r.is_a?(Sequel::Dataset)) or r.all?{|x| x.is_a?(Sequel::Model)}))))
|
|
1625
|
+
l = args.at(0)
|
|
1626
|
+
if ar = model.association_reflections[l]
|
|
1627
|
+
if multiple
|
|
1628
|
+
klass = ar.associated_class
|
|
1629
|
+
if is_ds
|
|
1630
|
+
if r.respond_to?(:model)
|
|
1631
|
+
unless r.model <= klass
|
|
1632
|
+
# A dataset for a different model class, could be a valid regular query
|
|
1633
|
+
return super
|
|
1634
|
+
end
|
|
1635
|
+
else
|
|
1636
|
+
# Not a model dataset, could be a valid regular query
|
|
1637
|
+
return super
|
|
1638
|
+
end
|
|
1639
|
+
else
|
|
1640
|
+
unless r.all?{|x| x.is_a?(klass)}
|
|
1641
|
+
raise Sequel::Error, "invalid association class for one object for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{klass.inspect}"
|
|
1642
|
+
end
|
|
1643
|
+
end
|
|
1644
|
+
elsif !r.is_a?(ar.associated_class)
|
|
1645
|
+
raise Sequel::Error, "invalid association class #{r.class.inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}, expected class #{ar.associated_class.inspect}"
|
|
1646
|
+
end
|
|
1647
|
+
|
|
1648
|
+
if exp = association_filter_expression(op, ar, r)
|
|
1649
|
+
literal(exp)
|
|
1650
|
+
else
|
|
1651
|
+
raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1652
|
+
end
|
|
1653
|
+
elsif multiple && (is_ds || r.empty?)
|
|
1654
|
+
# Not a query designed for this support, could be a valid regular query
|
|
1655
|
+
super
|
|
1656
|
+
else
|
|
1657
|
+
raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1658
|
+
end
|
|
1659
|
+
else
|
|
1660
|
+
super
|
|
1661
|
+
end
|
|
1662
|
+
end
|
|
1663
|
+
|
|
1268
1664
|
# The preferred eager loading method. Loads all associated records using one
|
|
1269
1665
|
# query for each association.
|
|
1270
1666
|
#
|
|
@@ -1311,9 +1707,9 @@ module Sequel
|
|
|
1311
1707
|
# The secondary eager loading method. Loads all associations in a single query. This
|
|
1312
1708
|
# method should only be used if you need to filter or order based on columns in associated tables.
|
|
1313
1709
|
#
|
|
1314
|
-
# This method
|
|
1315
|
-
#
|
|
1316
|
-
# of model objects.
|
|
1710
|
+
# This method uses <tt>Dataset#graph</tt> to create appropriate aliases for columns in all the
|
|
1711
|
+
# tables. Then it uses the graph's metadata to build the associations from the single hash, and
|
|
1712
|
+
# finally replaces the array of hashes with an array model objects inside all.
|
|
1317
1713
|
#
|
|
1318
1714
|
# Be very careful when using this with multiple one_to_many or many_to_many associations, as you can
|
|
1319
1715
|
# create large cartesian products. If you must graph multiple one_to_many and many_to_many associations,
|
|
@@ -1327,18 +1723,16 @@ module Sequel
|
|
|
1327
1723
|
# all objects. You can use the :graph_* association options to modify the SQL query.
|
|
1328
1724
|
#
|
|
1329
1725
|
# Like +eager+, you need to call +all+ on the dataset for the eager loading to work. If you just
|
|
1330
|
-
# call +each+,
|
|
1331
|
-
# model object values).
|
|
1726
|
+
# call +each+, it will yield plain hashes, each containing all columns from all the tables.
|
|
1332
1727
|
def eager_graph(*associations)
|
|
1333
1728
|
ds = if @opts[:eager_graph]
|
|
1334
1729
|
self
|
|
1335
1730
|
else
|
|
1336
1731
|
# Each of the following have a symbol key for the table alias, with the following values:
|
|
1337
1732
|
# :reciprocals - the reciprocal instance variable to use for this association
|
|
1733
|
+
# :reflections - AssociationReflection instance related to this association
|
|
1338
1734
|
# :requirements - array of requirements for this association
|
|
1339
|
-
|
|
1340
|
-
# :alias_association_name_map - the name of the association for this association
|
|
1341
|
-
clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :alias_association_type_map=>{}, :alias_association_name_map=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
|
|
1735
|
+
clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :reflections=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
|
|
1342
1736
|
end
|
|
1343
1737
|
ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations)
|
|
1344
1738
|
end
|
|
@@ -1366,20 +1760,32 @@ module Sequel
|
|
|
1366
1760
|
# r :: association reflection for the current association
|
|
1367
1761
|
# *associations :: any associations dependent on this one
|
|
1368
1762
|
def eager_graph_association(ds, model, ta, requirements, r, *associations)
|
|
1369
|
-
klass = r.associated_class
|
|
1370
1763
|
assoc_name = r[:name]
|
|
1371
1764
|
assoc_table_alias = ds.unused_table_alias(assoc_name)
|
|
1372
|
-
|
|
1765
|
+
loader = r[:eager_grapher]
|
|
1766
|
+
if !associations.empty?
|
|
1767
|
+
if associations.first.respond_to?(:call)
|
|
1768
|
+
callback = associations.first
|
|
1769
|
+
associations = {}
|
|
1770
|
+
elsif associations.length == 1 && (assocs = associations.first).is_a?(Hash) && assocs.length == 1 && (pr_assoc = assocs.to_a.first) && pr_assoc.first.respond_to?(:call)
|
|
1771
|
+
callback, assoc = pr_assoc
|
|
1772
|
+
associations = assoc.is_a?(Array) ? assoc : [assoc]
|
|
1773
|
+
end
|
|
1774
|
+
end
|
|
1775
|
+
ds = if loader.arity == 1
|
|
1776
|
+
loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>ta, :callback=>callback)
|
|
1777
|
+
else
|
|
1778
|
+
loader.call(ds, assoc_table_alias, ta)
|
|
1779
|
+
end
|
|
1373
1780
|
ds = ds.order_more(*qualified_expression(r[:order], assoc_table_alias)) if r[:order] and r[:order_eager_graph]
|
|
1374
1781
|
eager_graph = ds.opts[:eager_graph]
|
|
1375
1782
|
eager_graph[:requirements][assoc_table_alias] = requirements.dup
|
|
1376
|
-
eager_graph[:
|
|
1377
|
-
eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array?
|
|
1783
|
+
eager_graph[:reflections][assoc_table_alias] = r
|
|
1378
1784
|
eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2
|
|
1379
1785
|
ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
|
|
1380
1786
|
ds
|
|
1381
1787
|
end
|
|
1382
|
-
|
|
1788
|
+
|
|
1383
1789
|
# Check the associations are valid for the given model.
|
|
1384
1790
|
# Call eager_graph_association on each association.
|
|
1385
1791
|
#
|
|
@@ -1406,20 +1812,220 @@ module Sequel
|
|
|
1406
1812
|
ds
|
|
1407
1813
|
end
|
|
1408
1814
|
|
|
1409
|
-
#
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1815
|
+
# Replace the array of plain hashes with an array of model objects will all eager_graphed
|
|
1816
|
+
# associations set in the associations cache for each object.
|
|
1817
|
+
def eager_graph_build_associations(hashes)
|
|
1818
|
+
hashes.replace(EagerGraphLoader.new(self).load(hashes))
|
|
1819
|
+
end
|
|
1820
|
+
|
|
1821
|
+
private
|
|
1822
|
+
|
|
1823
|
+
# Return an expression for filtering by the given association reflection and associated object.
|
|
1824
|
+
def association_filter_expression(op, ref, obj)
|
|
1825
|
+
meth = :"#{ref[:type]}_association_filter_expression"
|
|
1826
|
+
send(meth, op, ref, obj) if respond_to?(meth, true)
|
|
1827
|
+
end
|
|
1828
|
+
|
|
1829
|
+
# Handle inversion for association filters by returning an inverted expression,
|
|
1830
|
+
# plus also handling cases where the referenced columns are NULL.
|
|
1831
|
+
def association_filter_handle_inversion(op, exp, cols)
|
|
1832
|
+
if op == :'!=' || op == :'NOT IN'
|
|
1833
|
+
if exp == SQL::Constants::FALSE
|
|
1834
|
+
~exp
|
|
1835
|
+
else
|
|
1836
|
+
~exp | Sequel::SQL::BooleanExpression.from_value_pairs(cols.zip([]), :OR)
|
|
1837
|
+
end
|
|
1838
|
+
else
|
|
1839
|
+
exp
|
|
1840
|
+
end
|
|
1841
|
+
end
|
|
1842
|
+
|
|
1843
|
+
# Return an expression for making sure that the given keys match the value of
|
|
1844
|
+
# the given methods for either the single object given or for any of the objects
|
|
1845
|
+
# given if +obj+ is an array.
|
|
1846
|
+
def association_filter_key_expression(keys, meths, obj)
|
|
1847
|
+
vals = if obj.is_a?(Sequel::Dataset)
|
|
1848
|
+
{(keys.length == 1 ? keys.first : keys)=>obj.select(*meths).exclude(Sequel::SQL::BooleanExpression.from_value_pairs(meths.zip([]), :OR))}
|
|
1849
|
+
else
|
|
1850
|
+
vals = Array(obj).reject{|o| !meths.all?{|m| o.send(m)}}
|
|
1851
|
+
return SQL::Constants::FALSE if vals.empty?
|
|
1852
|
+
if obj.is_a?(Array)
|
|
1853
|
+
if keys.length == 1
|
|
1854
|
+
meth = meths.first
|
|
1855
|
+
{keys.first=>vals.map{|o| o.send(meth)}}
|
|
1856
|
+
else
|
|
1857
|
+
{keys=>vals.map{|o| meths.map{|m| o.send(m)}}}
|
|
1858
|
+
end
|
|
1859
|
+
else
|
|
1860
|
+
keys.zip(meths.map{|k| obj.send(k)})
|
|
1861
|
+
end
|
|
1862
|
+
end
|
|
1863
|
+
SQL::BooleanExpression.from_value_pairs(vals)
|
|
1864
|
+
end
|
|
1865
|
+
|
|
1866
|
+
# Make sure the association is valid for this model, and return the related AssociationReflection.
|
|
1867
|
+
def check_association(model, association)
|
|
1868
|
+
raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
|
|
1869
|
+
raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
|
|
1870
|
+
reflection
|
|
1871
|
+
end
|
|
1872
|
+
|
|
1873
|
+
# Eagerly load all specified associations
|
|
1874
|
+
def eager_load(a, eager_assoc=@opts[:eager])
|
|
1875
|
+
return if a.empty?
|
|
1876
|
+
# Key is foreign/primary key name symbol
|
|
1877
|
+
# Value is hash with keys being foreign/primary key values (generally integers)
|
|
1878
|
+
# and values being an array of current model objects with that
|
|
1879
|
+
# specific foreign/primary key
|
|
1880
|
+
key_hash = {}
|
|
1881
|
+
# Reflections for all associations to eager load
|
|
1882
|
+
reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
|
|
1883
|
+
|
|
1884
|
+
# Populate keys to monitor
|
|
1885
|
+
reflections.each{|reflection| key_hash[reflection.eager_loader_key] ||= Hash.new{|h,k| h[k] = []}}
|
|
1886
|
+
|
|
1887
|
+
# Associate each object with every key being monitored
|
|
1888
|
+
a.each do |rec|
|
|
1889
|
+
key_hash.each do |key, id_map|
|
|
1890
|
+
case key
|
|
1891
|
+
when Array
|
|
1892
|
+
id_map[key.map{|k| rec[k]}] << rec if key.all?{|k| rec[k]}
|
|
1893
|
+
when Symbol
|
|
1894
|
+
id_map[rec[key]] << rec if rec[key]
|
|
1895
|
+
end
|
|
1896
|
+
end
|
|
1897
|
+
end
|
|
1898
|
+
|
|
1899
|
+
reflections.each do |r|
|
|
1900
|
+
loader = r[:eager_loader]
|
|
1901
|
+
associations = eager_assoc[r[:name]]
|
|
1902
|
+
if associations.respond_to?(:call)
|
|
1903
|
+
eager_block = associations
|
|
1904
|
+
associations = {}
|
|
1905
|
+
elsif associations.is_a?(Hash) && associations.length == 1 && (pr_assoc = associations.to_a.first) && pr_assoc.first.respond_to?(:call)
|
|
1906
|
+
eager_block, associations = pr_assoc
|
|
1907
|
+
end
|
|
1908
|
+
if loader.arity == 1
|
|
1909
|
+
loader.call(:key_hash=>key_hash, :rows=>a, :associations=>associations, :self=>self, :eager_block=>eager_block)
|
|
1910
|
+
else
|
|
1911
|
+
loader.call(key_hash, a, associations)
|
|
1912
|
+
end
|
|
1913
|
+
a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
|
|
1914
|
+
end
|
|
1915
|
+
end
|
|
1916
|
+
|
|
1917
|
+
# Return plain hashes instead of calling the row_proc if eager_graph is being used.
|
|
1918
|
+
def graph_each(&block)
|
|
1919
|
+
@opts[:eager_graph] ? fetch_rows(select_sql, &block) : super
|
|
1920
|
+
end
|
|
1921
|
+
|
|
1922
|
+
# Return a subquery expression for filering by a many_to_many association
|
|
1923
|
+
def many_to_many_association_filter_expression(op, ref, obj)
|
|
1924
|
+
lpks, lks, rks = ref.values_at(:left_primary_keys, :left_keys, :right_keys)
|
|
1925
|
+
lpks = lpks.first if lpks.length == 1
|
|
1926
|
+
exp = association_filter_key_expression(rks, ref.right_primary_keys, obj)
|
|
1927
|
+
if exp == SQL::Constants::FALSE
|
|
1928
|
+
association_filter_handle_inversion(op, exp, Array(lpks))
|
|
1929
|
+
else
|
|
1930
|
+
association_filter_handle_inversion(op, SQL::BooleanExpression.from_value_pairs(lpks=>model.db.from(ref[:join_table]).select(*lks).where(exp).exclude(SQL::BooleanExpression.from_value_pairs(lks.zip([]), :OR))), Array(lpks))
|
|
1931
|
+
end
|
|
1932
|
+
end
|
|
1933
|
+
|
|
1934
|
+
# Return a simple equality expression for filering by a many_to_one association
|
|
1935
|
+
def many_to_one_association_filter_expression(op, ref, obj)
|
|
1936
|
+
keys = ref[:keys]
|
|
1937
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, ref.primary_keys, obj), keys)
|
|
1938
|
+
end
|
|
1939
|
+
|
|
1940
|
+
# Return a simple equality expression for filering by a one_to_* association
|
|
1941
|
+
def one_to_many_association_filter_expression(op, ref, obj)
|
|
1942
|
+
keys = ref[:primary_keys]
|
|
1943
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, ref[:keys], obj), keys)
|
|
1944
|
+
end
|
|
1945
|
+
alias one_to_one_association_filter_expression one_to_many_association_filter_expression
|
|
1946
|
+
|
|
1947
|
+
# Build associations from the graph if #eager_graph was used,
|
|
1948
|
+
# and/or load other associations if #eager was used.
|
|
1949
|
+
def post_load(all_records)
|
|
1950
|
+
eager_graph_build_associations(all_records) if @opts[:eager_graph]
|
|
1951
|
+
eager_load(all_records) if @opts[:eager]
|
|
1952
|
+
super
|
|
1953
|
+
end
|
|
1954
|
+
end
|
|
1955
|
+
|
|
1956
|
+
# This class is the internal implementation of eager_graph. It is responsible for taking an array of plain
|
|
1957
|
+
# hashes and returning an array of model objects with all eager_graphed associations already set in the
|
|
1958
|
+
# association cache.
|
|
1959
|
+
class EagerGraphLoader
|
|
1960
|
+
# Hash with table alias symbol keys and after_load hook values
|
|
1961
|
+
attr_reader :after_load_map
|
|
1962
|
+
|
|
1963
|
+
# Hash with table alias symbol keys and association name values
|
|
1964
|
+
attr_reader :alias_map
|
|
1965
|
+
|
|
1966
|
+
# Hash with table alias symbol keys and subhash values mapping column_alias symbols to the
|
|
1967
|
+
# symbol of the real name of the column
|
|
1968
|
+
attr_reader :column_maps
|
|
1969
|
+
|
|
1970
|
+
# Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
|
|
1971
|
+
attr_reader :dependency_map
|
|
1972
|
+
|
|
1973
|
+
# Hash with table alias symbol keys and [limit, offset] values
|
|
1974
|
+
attr_reader :limit_map
|
|
1975
|
+
|
|
1976
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
1977
|
+
# The table alias symbol for the primary model
|
|
1978
|
+
attr_reader :master
|
|
1979
|
+
|
|
1980
|
+
# Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for
|
|
1981
|
+
# composite key tables)
|
|
1982
|
+
attr_reader :primary_keys
|
|
1983
|
+
|
|
1984
|
+
# Hash with table alias symbol keys and reciprocal association symbol values,
|
|
1985
|
+
# used for setting reciprocals for one_to_many associations.
|
|
1986
|
+
attr_reader :reciprocal_map
|
|
1987
|
+
|
|
1988
|
+
# Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols)
|
|
1989
|
+
# to model instances. Used so that only a single model instance is created for each object.
|
|
1990
|
+
attr_reader :records_map
|
|
1991
|
+
|
|
1992
|
+
# Hash with table alias symbol keys and AssociationReflection values
|
|
1993
|
+
attr_reader :reflection_map
|
|
1994
|
+
|
|
1995
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
1996
|
+
attr_reader :row_procs
|
|
1997
|
+
|
|
1998
|
+
# Hash with table alias symbol keys and true/false values, where true means the
|
|
1999
|
+
# association represented by the table alias uses an array of values instead of
|
|
2000
|
+
# a single value (i.e. true => *_many, false => *_to_one).
|
|
2001
|
+
attr_reader :type_map
|
|
2002
|
+
|
|
2003
|
+
# Initialize all of the data structures used during loading.
|
|
2004
|
+
def initialize(dataset)
|
|
2005
|
+
opts = dataset.opts
|
|
2006
|
+
eager_graph = opts[:eager_graph]
|
|
2007
|
+
@master = eager_graph[:master]
|
|
1413
2008
|
requirements = eager_graph[:requirements]
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
2009
|
+
reflection_map = @reflection_map = eager_graph[:reflections]
|
|
2010
|
+
reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
|
|
2011
|
+
@unique = eager_graph[:cartesian_product_number] > 1
|
|
1417
2012
|
|
|
2013
|
+
alias_map = @alias_map = {}
|
|
2014
|
+
type_map = @type_map = {}
|
|
2015
|
+
after_load_map = @after_load_map = {}
|
|
2016
|
+
limit_map = @limit_map = {}
|
|
2017
|
+
reflection_map.each do |k, v|
|
|
2018
|
+
alias_map[k] = v[:name]
|
|
2019
|
+
type_map[k] = v.returns_array?
|
|
2020
|
+
after_load_map[k] = v[:after_load] unless v[:after_load].empty?
|
|
2021
|
+
limit_map[k] = v.limit_and_offset if v[:limit]
|
|
2022
|
+
end
|
|
2023
|
+
|
|
1418
2024
|
# Make dependency map hash out of requirements array for each association.
|
|
1419
2025
|
# This builds a tree of dependencies that will be used for recursion
|
|
1420
2026
|
# to ensure that all parts of the object graph are loaded into the
|
|
1421
2027
|
# appropriate subordinate association.
|
|
1422
|
-
dependency_map = {}
|
|
2028
|
+
@dependency_map = {}
|
|
1423
2029
|
# Sort the associations by requirements length, so that
|
|
1424
2030
|
# requirements are added to the dependency hash before their
|
|
1425
2031
|
# dependencies.
|
|
@@ -1439,139 +2045,199 @@ module Sequel
|
|
|
1439
2045
|
# This mapping is used to make sure that duplicate entries in the
|
|
1440
2046
|
# result set are mapped to a single record. For example, using a
|
|
1441
2047
|
# single one_to_many association with 10 associated records,
|
|
1442
|
-
# the main object
|
|
2048
|
+
# the main object column values appear in the object graph 10 times.
|
|
1443
2049
|
# We map by primary key, if available, or by the object's entire values,
|
|
1444
2050
|
# if not. The mapping must be per table, so create sub maps for each table
|
|
1445
2051
|
# alias.
|
|
1446
|
-
records_map = {master=>{}}
|
|
2052
|
+
records_map = {@master=>{}}
|
|
1447
2053
|
alias_map.keys.each{|ta| records_map[ta] = {}}
|
|
2054
|
+
@records_map = records_map
|
|
2055
|
+
|
|
2056
|
+
datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
|
|
2057
|
+
column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
|
|
2058
|
+
primary_keys = {}
|
|
2059
|
+
column_maps = {}
|
|
2060
|
+
models = {}
|
|
2061
|
+
row_procs = {}
|
|
2062
|
+
datasets.each do |ta, ds|
|
|
2063
|
+
models[ta] = ds.model
|
|
2064
|
+
primary_keys[ta] = []
|
|
2065
|
+
column_maps[ta] = {}
|
|
2066
|
+
row_procs[ta] = ds.row_proc
|
|
2067
|
+
end
|
|
2068
|
+
column_aliases.each do |col_alias, tc|
|
|
2069
|
+
ta, column = tc
|
|
2070
|
+
column_maps[ta][col_alias] = column
|
|
2071
|
+
end
|
|
2072
|
+
column_maps.each do |ta, h|
|
|
2073
|
+
pk = models[ta].primary_key
|
|
2074
|
+
if pk.is_a?(Array)
|
|
2075
|
+
primary_keys[ta] = []
|
|
2076
|
+
h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
|
|
2077
|
+
else
|
|
2078
|
+
h.select{|ca, c| primary_keys[ta] = ca if pk == c}
|
|
2079
|
+
end
|
|
2080
|
+
end
|
|
2081
|
+
@column_maps = column_maps
|
|
2082
|
+
@primary_keys = primary_keys
|
|
2083
|
+
@row_procs = row_procs
|
|
2084
|
+
|
|
2085
|
+
# For performance, create two special maps for the master table,
|
|
2086
|
+
# so you can skip a hash lookup.
|
|
2087
|
+
@master_column_map = column_maps[master]
|
|
2088
|
+
@master_primary_keys = primary_keys[master]
|
|
2089
|
+
|
|
2090
|
+
# Add a special hash mapping table alias symbols to 5 element arrays that just
|
|
2091
|
+
# contain the data in other data structures for that table alias. This is
|
|
2092
|
+
# used for performance, to get all values in one hash lookup instead of
|
|
2093
|
+
# separate hash lookups for each data structure.
|
|
2094
|
+
ta_map = {}
|
|
2095
|
+
alias_map.keys.each do |ta|
|
|
2096
|
+
ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
|
|
2097
|
+
end
|
|
2098
|
+
@ta_map = ta_map
|
|
2099
|
+
end
|
|
2100
|
+
|
|
2101
|
+
# Return an array of primary model instances with the associations cache prepopulated
|
|
2102
|
+
# for all model objects (both primary and associated).
|
|
2103
|
+
def load(hashes)
|
|
2104
|
+
master = master()
|
|
1448
2105
|
|
|
2106
|
+
# Assign to local variables for speed increase
|
|
2107
|
+
rp = row_procs[master]
|
|
2108
|
+
rm = records_map[master]
|
|
2109
|
+
dm = dependency_map
|
|
2110
|
+
|
|
1449
2111
|
# This will hold the final record set that we will be replacing the object graph with.
|
|
1450
2112
|
records = []
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
key =
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
2113
|
+
|
|
2114
|
+
hashes.each do |h|
|
|
2115
|
+
unless key = master_pk(h)
|
|
2116
|
+
key = hkey(master_hfor(h))
|
|
2117
|
+
end
|
|
2118
|
+
unless primary_record = rm[key]
|
|
2119
|
+
primary_record = rm[key] = rp.call(master_hfor(h))
|
|
1458
2120
|
# Only add it to the list of records to return if it is a new record
|
|
1459
2121
|
records.push(primary_record)
|
|
1460
2122
|
end
|
|
1461
2123
|
# Build all associations for the current object and it's dependencies
|
|
1462
|
-
|
|
2124
|
+
_load(dm, primary_record, h)
|
|
1463
2125
|
end
|
|
1464
2126
|
|
|
1465
2127
|
# Remove duplicate records from all associations if this graph could possibly be a cartesian product
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
2128
|
+
# Run after_load procs if there are any
|
|
2129
|
+
post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
|
|
2130
|
+
|
|
2131
|
+
records
|
|
1470
2132
|
end
|
|
1471
2133
|
|
|
1472
2134
|
private
|
|
1473
|
-
|
|
1474
|
-
#
|
|
1475
|
-
def
|
|
1476
|
-
raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
|
|
1477
|
-
raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
|
|
1478
|
-
reflection
|
|
1479
|
-
end
|
|
1480
|
-
|
|
1481
|
-
# Build associations for the current object. This is called recursively
|
|
1482
|
-
# to build all dependencies.
|
|
1483
|
-
def eager_graph_build_associations_graph(dependency_map, alias_map, type_map, reciprocal_map, records_map, current, record_graph)
|
|
1484
|
-
return if dependency_map.empty?
|
|
1485
|
-
# Don't clobber the cached association value for one_to_many and many_to_many associations if it has already been setup
|
|
1486
|
-
dependency_map.keys.each do |ta|
|
|
1487
|
-
assoc_name = alias_map[ta]
|
|
1488
|
-
current.associations[assoc_name] = type_map[ta] ? [] : nil unless current.associations.include?(assoc_name)
|
|
1489
|
-
end
|
|
2135
|
+
|
|
2136
|
+
# Recursive method that creates associated model objects and associates them to the current model object.
|
|
2137
|
+
def _load(dependency_map, current, h)
|
|
1490
2138
|
dependency_map.each do |ta, deps|
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
2139
|
+
unless key = pk(ta, h)
|
|
2140
|
+
ta_h = hfor(ta, h)
|
|
2141
|
+
unless ta_h.values.any?
|
|
2142
|
+
assoc_name = alias_map[ta]
|
|
2143
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2144
|
+
assoc[assoc_name] = type_map[ta] ? [] : nil
|
|
2145
|
+
end
|
|
2146
|
+
next
|
|
2147
|
+
end
|
|
2148
|
+
key = hkey(ta_h)
|
|
2149
|
+
end
|
|
2150
|
+
rm, rp, assoc_name, tm, rcm = @ta_map[ta]
|
|
2151
|
+
unless rec = rm[key]
|
|
2152
|
+
rec = rm[key] = rp.call(hfor(ta, h))
|
|
1497
2153
|
end
|
|
1498
|
-
|
|
1499
|
-
if
|
|
1500
|
-
current.associations
|
|
1501
|
-
|
|
1502
|
-
rec.associations[reciprocal] = current
|
|
2154
|
+
|
|
2155
|
+
if tm
|
|
2156
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2157
|
+
assoc[assoc_name] = []
|
|
1503
2158
|
end
|
|
2159
|
+
assoc[assoc_name].push(rec)
|
|
2160
|
+
rec.associations[rcm] = current if rcm
|
|
1504
2161
|
else
|
|
1505
|
-
current.associations[assoc_name]
|
|
2162
|
+
current.associations[assoc_name] ||= rec
|
|
1506
2163
|
end
|
|
1507
2164
|
# Recurse into dependencies of the current object
|
|
1508
|
-
|
|
2165
|
+
_load(deps, rec, h) unless deps.empty?
|
|
1509
2166
|
end
|
|
1510
2167
|
end
|
|
1511
2168
|
|
|
2169
|
+
# Return the subhash for the specific table alias +ta+ by parsing the values out of the main hash +h+
|
|
2170
|
+
def hfor(ta, h)
|
|
2171
|
+
out = {}
|
|
2172
|
+
@column_maps[ta].each{|ca, c| out[c] = h[ca]}
|
|
2173
|
+
out
|
|
2174
|
+
end
|
|
2175
|
+
|
|
2176
|
+
# Return a suitable hash key for any subhash +h+, which is an array of values by column order.
|
|
2177
|
+
# This is only used if the primary key cannot be used.
|
|
2178
|
+
def hkey(h)
|
|
2179
|
+
h.sort_by{|x| x[0].to_s}
|
|
2180
|
+
end
|
|
2181
|
+
|
|
2182
|
+
# Return the subhash for the master table by parsing the values out of the main hash +h+
|
|
2183
|
+
def master_hfor(h)
|
|
2184
|
+
out = {}
|
|
2185
|
+
@master_column_map.each{|ca, c| out[c] = h[ca]}
|
|
2186
|
+
out
|
|
2187
|
+
end
|
|
2188
|
+
|
|
2189
|
+
# Return a primary key value for the master table by parsing it out of the main hash +h+.
|
|
2190
|
+
def master_pk(h)
|
|
2191
|
+
x = @master_primary_keys
|
|
2192
|
+
if x.is_a?(Array)
|
|
2193
|
+
unless x == []
|
|
2194
|
+
x = x.map{|ca| h[ca]}
|
|
2195
|
+
x if x.all?
|
|
2196
|
+
end
|
|
2197
|
+
else
|
|
2198
|
+
h[x]
|
|
2199
|
+
end
|
|
2200
|
+
end
|
|
2201
|
+
|
|
2202
|
+
# Return a primary key value for the given table alias by parsing it out of the main hash +h+.
|
|
2203
|
+
def pk(ta, h)
|
|
2204
|
+
x = primary_keys[ta]
|
|
2205
|
+
if x.is_a?(Array)
|
|
2206
|
+
unless x == []
|
|
2207
|
+
x = x.map{|ca| h[ca]}
|
|
2208
|
+
x if x.all?
|
|
2209
|
+
end
|
|
2210
|
+
else
|
|
2211
|
+
h[x]
|
|
2212
|
+
end
|
|
2213
|
+
end
|
|
2214
|
+
|
|
1512
2215
|
# If the result set is the result of a cartesian product, then it is possible that
|
|
1513
2216
|
# there are multiple records for each association when there should only be one.
|
|
1514
2217
|
# In that case, for each object in all associations loaded via +eager_graph+, run
|
|
1515
2218
|
# uniq! on the association to make sure no duplicate records show up.
|
|
1516
2219
|
# Note that this can cause legitimate duplicate records to be removed.
|
|
1517
|
-
def
|
|
2220
|
+
def post_process(records, dependency_map)
|
|
1518
2221
|
records.each do |record|
|
|
1519
2222
|
dependency_map.each do |ta, deps|
|
|
1520
|
-
|
|
1521
|
-
list =
|
|
2223
|
+
assoc_name = alias_map[ta]
|
|
2224
|
+
list = record.send(assoc_name)
|
|
2225
|
+
rec_list = if type_map[ta]
|
|
1522
2226
|
list.uniq!
|
|
2227
|
+
if lo = limit_map[ta]
|
|
2228
|
+
limit, offset = lo
|
|
2229
|
+
list.replace(list[offset||0, limit])
|
|
2230
|
+
end
|
|
2231
|
+
list
|
|
2232
|
+
elsif list
|
|
2233
|
+
[list]
|
|
1523
2234
|
else
|
|
1524
|
-
[
|
|
1525
|
-
end
|
|
1526
|
-
# Recurse into dependencies
|
|
1527
|
-
eager_graph_make_associations_unique(list, deps, alias_map, type_map) if list
|
|
1528
|
-
end
|
|
1529
|
-
end
|
|
1530
|
-
end
|
|
1531
|
-
|
|
1532
|
-
# Eagerly load all specified associations
|
|
1533
|
-
def eager_load(a, eager_assoc=@opts[:eager])
|
|
1534
|
-
return if a.empty?
|
|
1535
|
-
# Key is foreign/primary key name symbol
|
|
1536
|
-
# Value is hash with keys being foreign/primary key values (generally integers)
|
|
1537
|
-
# and values being an array of current model objects with that
|
|
1538
|
-
# specific foreign/primary key
|
|
1539
|
-
key_hash = {}
|
|
1540
|
-
# Reflections for all associations to eager load
|
|
1541
|
-
reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
|
|
1542
|
-
|
|
1543
|
-
# Populate keys to monitor
|
|
1544
|
-
reflections.each{|reflection| key_hash[reflection.eager_loader_key] ||= Hash.new{|h,k| h[k] = []}}
|
|
1545
|
-
|
|
1546
|
-
# Associate each object with every key being monitored
|
|
1547
|
-
a.each do |rec|
|
|
1548
|
-
key_hash.each do |key, id_map|
|
|
1549
|
-
case key
|
|
1550
|
-
when Array
|
|
1551
|
-
id_map[key.map{|k| rec[k]}] << rec if key.all?{|k| rec[k]}
|
|
1552
|
-
when Symbol
|
|
1553
|
-
id_map[rec[key]] << rec if rec[key]
|
|
2235
|
+
[]
|
|
1554
2236
|
end
|
|
2237
|
+
record.send(:run_association_callbacks, reflection_map[ta], :after_load, list) if after_load_map[ta]
|
|
2238
|
+
post_process(rec_list, deps) if !rec_list.empty? && !deps.empty?
|
|
1555
2239
|
end
|
|
1556
2240
|
end
|
|
1557
|
-
|
|
1558
|
-
reflections.each do |r|
|
|
1559
|
-
loader = r[:eager_loader]
|
|
1560
|
-
if loader.arity == 1
|
|
1561
|
-
loader.call(:key_hash=>key_hash, :rows=>a, :associations=>eager_assoc[r[:name]], :self=>self)
|
|
1562
|
-
else
|
|
1563
|
-
loader.call(key_hash, a, eager_assoc[r[:name]])
|
|
1564
|
-
end
|
|
1565
|
-
a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
|
|
1566
|
-
end
|
|
1567
|
-
end
|
|
1568
|
-
|
|
1569
|
-
# Build associations from the graph if #eager_graph was used,
|
|
1570
|
-
# and/or load other associations if #eager was used.
|
|
1571
|
-
def post_load(all_records)
|
|
1572
|
-
eager_graph_build_associations(all_records) if @opts[:eager_graph]
|
|
1573
|
-
eager_load(all_records) if @opts[:eager]
|
|
1574
|
-
super
|
|
1575
2241
|
end
|
|
1576
2242
|
end
|
|
1577
2243
|
end
|