sequel 3.21.0 → 3.32.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.
Files changed (234) hide show
  1. data/CHANGELOG +539 -3
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +20 -6
  4. data/Rakefile +37 -19
  5. data/bin/sequel +1 -5
  6. data/doc/advanced_associations.rdoc +17 -0
  7. data/doc/association_basics.rdoc +293 -43
  8. data/doc/dataset_basics.rdoc +4 -4
  9. data/doc/dataset_filtering.rdoc +8 -0
  10. data/doc/mass_assignment.rdoc +54 -0
  11. data/doc/migration.rdoc +15 -538
  12. data/doc/model_hooks.rdoc +72 -27
  13. data/doc/opening_databases.rdoc +89 -50
  14. data/doc/prepared_statements.rdoc +40 -13
  15. data/doc/reflection.rdoc +8 -2
  16. data/doc/release_notes/3.22.0.txt +39 -0
  17. data/doc/release_notes/3.23.0.txt +172 -0
  18. data/doc/release_notes/3.24.0.txt +420 -0
  19. data/doc/release_notes/3.25.0.txt +88 -0
  20. data/doc/release_notes/3.26.0.txt +88 -0
  21. data/doc/release_notes/3.27.0.txt +82 -0
  22. data/doc/release_notes/3.28.0.txt +304 -0
  23. data/doc/release_notes/3.29.0.txt +459 -0
  24. data/doc/release_notes/3.30.0.txt +135 -0
  25. data/doc/release_notes/3.31.0.txt +146 -0
  26. data/doc/release_notes/3.32.0.txt +202 -0
  27. data/doc/schema_modification.rdoc +547 -0
  28. data/doc/sharding.rdoc +7 -1
  29. data/doc/testing.rdoc +129 -0
  30. data/doc/transactions.rdoc +137 -0
  31. data/doc/virtual_rows.rdoc +2 -2
  32. data/lib/sequel/adapters/ado/mssql.rb +18 -6
  33. data/lib/sequel/adapters/ado.rb +40 -18
  34. data/lib/sequel/adapters/amalgalite.rb +17 -9
  35. data/lib/sequel/adapters/db2.rb +175 -86
  36. data/lib/sequel/adapters/dbi.rb +15 -15
  37. data/lib/sequel/adapters/do/mysql.rb +5 -7
  38. data/lib/sequel/adapters/do/postgres.rb +0 -5
  39. data/lib/sequel/adapters/do/sqlite.rb +0 -5
  40. data/lib/sequel/adapters/do.rb +17 -36
  41. data/lib/sequel/adapters/firebird.rb +27 -208
  42. data/lib/sequel/adapters/ibmdb.rb +448 -0
  43. data/lib/sequel/adapters/informix.rb +6 -23
  44. data/lib/sequel/adapters/jdbc/as400.rb +16 -34
  45. data/lib/sequel/adapters/jdbc/db2.rb +52 -0
  46. data/lib/sequel/adapters/jdbc/derby.rb +260 -0
  47. data/lib/sequel/adapters/jdbc/firebird.rb +29 -0
  48. data/lib/sequel/adapters/jdbc/h2.rb +66 -30
  49. data/lib/sequel/adapters/jdbc/hsqldb.rb +177 -0
  50. data/lib/sequel/adapters/jdbc/informix.rb +26 -0
  51. data/lib/sequel/adapters/jdbc/jtds.rb +34 -0
  52. data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
  53. data/lib/sequel/adapters/jdbc/mysql.rb +11 -11
  54. data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
  55. data/lib/sequel/adapters/jdbc/postgresql.rb +9 -13
  56. data/lib/sequel/adapters/jdbc/sqlite.rb +2 -6
  57. data/lib/sequel/adapters/jdbc/sqlserver.rb +71 -0
  58. data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
  59. data/lib/sequel/adapters/jdbc.rb +231 -78
  60. data/lib/sequel/adapters/mock.rb +339 -0
  61. data/lib/sequel/adapters/mysql.rb +85 -184
  62. data/lib/sequel/adapters/mysql2.rb +26 -25
  63. data/lib/sequel/adapters/odbc/db2.rb +17 -0
  64. data/lib/sequel/adapters/odbc/mssql.rb +1 -6
  65. data/lib/sequel/adapters/odbc.rb +28 -9
  66. data/lib/sequel/adapters/openbase.rb +3 -5
  67. data/lib/sequel/adapters/oracle.rb +355 -53
  68. data/lib/sequel/adapters/postgres.rb +194 -33
  69. data/lib/sequel/adapters/shared/access.rb +32 -9
  70. data/lib/sequel/adapters/shared/db2.rb +328 -0
  71. data/lib/sequel/adapters/shared/firebird.rb +221 -0
  72. data/lib/sequel/adapters/shared/informix.rb +53 -0
  73. data/lib/sequel/adapters/shared/mssql.rb +278 -107
  74. data/lib/sequel/adapters/shared/mysql.rb +203 -65
  75. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +149 -0
  76. data/lib/sequel/adapters/shared/oracle.rb +237 -46
  77. data/lib/sequel/adapters/shared/postgres.rb +214 -82
  78. data/lib/sequel/adapters/shared/progress.rb +5 -8
  79. data/lib/sequel/adapters/shared/sqlite.rb +200 -50
  80. data/lib/sequel/adapters/sqlite.rb +29 -21
  81. data/lib/sequel/adapters/swift/mysql.rb +12 -7
  82. data/lib/sequel/adapters/swift/postgres.rb +0 -5
  83. data/lib/sequel/adapters/swift/sqlite.rb +8 -6
  84. data/lib/sequel/adapters/swift.rb +5 -5
  85. data/lib/sequel/adapters/tinytds.rb +160 -19
  86. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
  87. data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
  88. data/lib/sequel/ast_transformer.rb +190 -0
  89. data/lib/sequel/connection_pool/threaded.rb +3 -2
  90. data/lib/sequel/connection_pool.rb +1 -1
  91. data/lib/sequel/core.rb +68 -5
  92. data/lib/sequel/database/connecting.rb +5 -6
  93. data/lib/sequel/database/dataset.rb +4 -4
  94. data/lib/sequel/database/dataset_defaults.rb +59 -1
  95. data/lib/sequel/database/logging.rb +1 -1
  96. data/lib/sequel/database/misc.rb +104 -19
  97. data/lib/sequel/database/query.rb +163 -59
  98. data/lib/sequel/database/schema_generator.rb +17 -7
  99. data/lib/sequel/database/schema_methods.rb +59 -29
  100. data/lib/sequel/dataset/actions.rb +240 -76
  101. data/lib/sequel/dataset/features.rb +91 -8
  102. data/lib/sequel/dataset/graph.rb +38 -11
  103. data/lib/sequel/dataset/misc.rb +50 -23
  104. data/lib/sequel/dataset/mutation.rb +12 -7
  105. data/lib/sequel/dataset/prepared_statements.rb +59 -27
  106. data/lib/sequel/dataset/query.rb +236 -43
  107. data/lib/sequel/dataset/sql.rb +676 -267
  108. data/lib/sequel/dataset.rb +8 -0
  109. data/lib/sequel/exceptions.rb +4 -0
  110. data/lib/sequel/extensions/columns_introspection.rb +61 -0
  111. data/lib/sequel/extensions/migration.rb +6 -4
  112. data/lib/sequel/extensions/named_timezones.rb +5 -0
  113. data/lib/sequel/extensions/query.rb +1 -1
  114. data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
  115. data/lib/sequel/extensions/to_dot.rb +95 -83
  116. data/lib/sequel/model/associations.rb +1081 -344
  117. data/lib/sequel/model/base.rb +420 -133
  118. data/lib/sequel/model/errors.rb +1 -1
  119. data/lib/sequel/model/exceptions.rb +5 -1
  120. data/lib/sequel/model.rb +20 -10
  121. data/lib/sequel/plugins/active_model.rb +13 -1
  122. data/lib/sequel/plugins/association_pks.rb +22 -4
  123. data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
  124. data/lib/sequel/plugins/composition.rb +43 -10
  125. data/lib/sequel/plugins/dataset_associations.rb +100 -0
  126. data/lib/sequel/plugins/defaults_setter.rb +58 -0
  127. data/lib/sequel/plugins/force_encoding.rb +6 -6
  128. data/lib/sequel/plugins/identity_map.rb +114 -7
  129. data/lib/sequel/plugins/many_through_many.rb +68 -7
  130. data/lib/sequel/plugins/nested_attributes.rb +65 -10
  131. data/lib/sequel/plugins/prepared_statements.rb +151 -0
  132. data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
  133. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  134. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  135. data/lib/sequel/plugins/rcte_tree.rb +29 -15
  136. data/lib/sequel/plugins/serialization.rb +98 -49
  137. data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
  138. data/lib/sequel/plugins/sharding.rb +21 -54
  139. data/lib/sequel/plugins/single_table_inheritance.rb +3 -1
  140. data/lib/sequel/plugins/typecast_on_load.rb +9 -12
  141. data/lib/sequel/plugins/update_primary_key.rb +2 -2
  142. data/lib/sequel/plugins/xml_serializer.rb +3 -3
  143. data/lib/sequel/sql.rb +109 -52
  144. data/lib/sequel/timezones.rb +57 -38
  145. data/lib/sequel/version.rb +1 -1
  146. data/spec/adapters/db2_spec.rb +146 -0
  147. data/spec/adapters/mssql_spec.rb +135 -30
  148. data/spec/adapters/mysql_spec.rb +244 -212
  149. data/spec/adapters/oracle_spec.rb +67 -77
  150. data/spec/adapters/postgres_spec.rb +267 -78
  151. data/spec/adapters/spec_helper.rb +6 -5
  152. data/spec/adapters/sqlite_spec.rb +35 -21
  153. data/spec/core/connection_pool_spec.rb +71 -92
  154. data/spec/core/core_sql_spec.rb +21 -30
  155. data/spec/core/database_spec.rb +789 -512
  156. data/spec/core/dataset_spec.rb +1176 -1061
  157. data/spec/core/expression_filters_spec.rb +159 -42
  158. data/spec/core/mock_adapter_spec.rb +419 -0
  159. data/spec/core/object_graph_spec.rb +86 -114
  160. data/spec/core/schema_generator_spec.rb +3 -3
  161. data/spec/core/schema_spec.rb +298 -38
  162. data/spec/core/spec_helper.rb +6 -48
  163. data/spec/extensions/active_model_spec.rb +13 -0
  164. data/spec/extensions/association_autoreloading_spec.rb +1 -10
  165. data/spec/extensions/association_dependencies_spec.rb +2 -12
  166. data/spec/extensions/association_pks_spec.rb +66 -32
  167. data/spec/extensions/caching_spec.rb +23 -50
  168. data/spec/extensions/class_table_inheritance_spec.rb +31 -83
  169. data/spec/extensions/columns_introspection_spec.rb +91 -0
  170. data/spec/extensions/composition_spec.rb +18 -13
  171. data/spec/extensions/dataset_associations_spec.rb +199 -0
  172. data/spec/extensions/defaults_setter_spec.rb +64 -0
  173. data/spec/extensions/hook_class_methods_spec.rb +65 -91
  174. data/spec/extensions/identity_map_spec.rb +114 -22
  175. data/spec/extensions/instance_filters_spec.rb +11 -21
  176. data/spec/extensions/instance_hooks_spec.rb +53 -0
  177. data/spec/extensions/json_serializer_spec.rb +4 -5
  178. data/spec/extensions/lazy_attributes_spec.rb +16 -20
  179. data/spec/extensions/list_spec.rb +17 -39
  180. data/spec/extensions/many_through_many_spec.rb +283 -250
  181. data/spec/extensions/migration_spec.rb +35 -32
  182. data/spec/extensions/named_timezones_spec.rb +23 -3
  183. data/spec/extensions/nested_attributes_spec.rb +97 -88
  184. data/spec/extensions/optimistic_locking_spec.rb +9 -20
  185. data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
  186. data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
  187. data/spec/extensions/prepared_statements_spec.rb +53 -0
  188. data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
  189. data/spec/extensions/pretty_table_spec.rb +1 -6
  190. data/spec/extensions/query_spec.rb +6 -0
  191. data/spec/extensions/rcte_tree_spec.rb +41 -43
  192. data/spec/extensions/schema_dumper_spec.rb +5 -8
  193. data/spec/extensions/schema_spec.rb +13 -21
  194. data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
  195. data/spec/extensions/serialization_spec.rb +81 -43
  196. data/spec/extensions/sharding_spec.rb +66 -140
  197. data/spec/extensions/single_table_inheritance_spec.rb +18 -29
  198. data/spec/extensions/spec_helper.rb +13 -61
  199. data/spec/extensions/sql_expr_spec.rb +20 -60
  200. data/spec/extensions/tactical_eager_loading_spec.rb +9 -19
  201. data/spec/extensions/thread_local_timezones_spec.rb +22 -2
  202. data/spec/extensions/timestamps_spec.rb +6 -6
  203. data/spec/extensions/to_dot_spec.rb +4 -7
  204. data/spec/extensions/touch_spec.rb +13 -14
  205. data/spec/extensions/tree_spec.rb +11 -26
  206. data/spec/extensions/typecast_on_load_spec.rb +1 -6
  207. data/spec/extensions/update_primary_key_spec.rb +30 -24
  208. data/spec/extensions/validation_class_methods_spec.rb +30 -51
  209. data/spec/extensions/validation_helpers_spec.rb +16 -35
  210. data/spec/extensions/xml_serializer_spec.rb +16 -4
  211. data/spec/integration/associations_test.rb +661 -21
  212. data/spec/integration/database_test.rb +4 -2
  213. data/spec/integration/dataset_test.rb +500 -67
  214. data/spec/integration/eager_loader_test.rb +19 -21
  215. data/spec/integration/model_test.rb +80 -1
  216. data/spec/integration/plugin_test.rb +367 -119
  217. data/spec/integration/prepared_statement_test.rb +222 -125
  218. data/spec/integration/schema_test.rb +217 -39
  219. data/spec/integration/spec_helper.rb +39 -30
  220. data/spec/integration/timezone_test.rb +38 -12
  221. data/spec/integration/transaction_test.rb +182 -5
  222. data/spec/integration/type_test.rb +17 -3
  223. data/spec/model/association_reflection_spec.rb +201 -7
  224. data/spec/model/associations_spec.rb +955 -662
  225. data/spec/model/base_spec.rb +275 -95
  226. data/spec/model/dataset_methods_spec.rb +7 -27
  227. data/spec/model/eager_loading_spec.rb +677 -676
  228. data/spec/model/hooks_spec.rb +349 -67
  229. data/spec/model/model_spec.rb +222 -168
  230. data/spec/model/plugins_spec.rb +24 -13
  231. data/spec/model/record_spec.rb +381 -218
  232. data/spec/model/spec_helper.rb +13 -71
  233. data/spec/model/validations_spec.rb +11 -0
  234. metadata +117 -73
@@ -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,23 @@ module Sequel
208
267
  self[:primary_keys] ||= Array(primary_key)
209
268
  end
210
269
  alias associated_object_keys primary_keys
270
+
271
+ # The method symbol or array of method symbols to call on the associated object
272
+ # to get the value to use for the foreign keys.
273
+ def primary_key_method
274
+ self[:primary_key_method] ||= primary_key
275
+ end
276
+
277
+ # The array of method symbols to call on the associated object
278
+ # to get the value to use for the foreign keys.
279
+ def primary_key_methods
280
+ self[:primary_key_methods] ||= Array(primary_key_method)
281
+ end
282
+
283
+ # #primary_key qualified by the associated table
284
+ def qualified_primary_key
285
+ self[:qualified_primary_key] ||= self[:qualify] == false ? primary_key : qualify_assoc(primary_key)
286
+ end
211
287
 
212
288
  # True only if the reciprocal is a one_to_many association.
213
289
  def reciprocal_array?
@@ -248,7 +324,7 @@ module Sequel
248
324
  def can_have_associated_objects?(obj)
249
325
  !self[:primary_keys].any?{|k| obj.send(k).nil?}
250
326
  end
251
-
327
+
252
328
  # Default foreign key name symbol for key in associated table that points to
253
329
  # current table's primary key.
254
330
  def default_key
@@ -259,10 +335,21 @@ module Sequel
259
335
  def eager_loader_key
260
336
  self[:eager_loader_key] ||= primary_key
261
337
  end
338
+
339
+ # The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))
340
+ def eager_loading_predicate_key
341
+ self[:eager_loading_predicate_key] ||= qualify_assoc(self[:key])
342
+ end
343
+ alias qualified_key eager_loading_predicate_key
262
344
 
263
345
  # The column in the current table that the key in the associated table references.
264
346
  def primary_key
265
- self[:primary_key] ||= self[:model].primary_key
347
+ self[:primary_key]
348
+ end
349
+
350
+ # #primary_key qualified by the current table
351
+ def qualified_primary_key
352
+ self[:qualified_primary_key] ||= qualify_cur(primary_key)
266
353
  end
267
354
 
268
355
  # Whether the reciprocal of this association returns an array of objects instead of a single object,
@@ -297,6 +384,31 @@ module Sequel
297
384
  class OneToOneAssociationReflection < OneToManyAssociationReflection
298
385
  ASSOCIATION_TYPES[:one_to_one] = self
299
386
 
387
+ # one_to_one associations don't use an eager limit strategy by default, but
388
+ # support both DISTINCT ON and window functions as strategies.
389
+ def eager_limit_strategy
390
+ fetch(:_eager_limit_strategy) do
391
+ self[:_eager_limit_strategy] = case s = self[:eager_limit_strategy]
392
+ when Symbol
393
+ s
394
+ when true
395
+ ds = associated_class.dataset
396
+ if ds.supports_ordered_distinct_on?
397
+ :distinct_on
398
+ elsif ds.supports_window_functions?
399
+ :window_function
400
+ end
401
+ else
402
+ nil
403
+ end
404
+ end
405
+ end
406
+
407
+ # The limit and offset for this association (returned as a two element array).
408
+ def limit_and_offset
409
+ [1, nil]
410
+ end
411
+
300
412
  # one_to_one associations return a single object, not an array
301
413
  def returns_array?
302
414
  false
@@ -316,13 +428,6 @@ module Sequel
316
428
  self[:left_key]
317
429
  end
318
430
 
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
431
  # Alias of right_primary_keys
327
432
  def associated_object_keys
328
433
  right_primary_keys
@@ -362,11 +467,42 @@ module Sequel
362
467
  self[:eager_loader_key] ||= self[:left_primary_key]
363
468
  end
364
469
 
470
+ # The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)).
471
+ # The left key qualified by the join table.
472
+ def eager_loading_predicate_key
473
+ self[:eager_loading_predicate_key] ||= qualify(join_table_alias, self[:left_key])
474
+ end
475
+ alias qualified_left_key eager_loading_predicate_key
476
+
477
+ # The right key qualified by the join table.
478
+ def qualified_right_key
479
+ self[:qualified_right_key] ||= qualify(join_table_alias, self[:right_key])
480
+ end
481
+
365
482
  # many_to_many associations need to select a key in an associated table to eagerly load
366
483
  def eager_loading_use_associated_key?
367
484
  true
368
485
  end
369
486
 
487
+ # The source of the join table. This is the join table itself, unless it
488
+ # is aliased, in which case it is the unaliased part.
489
+ def join_table_source
490
+ fetch(:join_table_source) do
491
+ split_join_table_alias
492
+ self[:join_table_source]
493
+ end
494
+ end
495
+
496
+ # The join table itself, unless it is aliased, in which case this
497
+ # is the alias.
498
+ def join_table_alias
499
+ fetch(:join_table_alias) do
500
+ split_join_table_alias
501
+ self[:join_table_alias]
502
+ end
503
+ end
504
+ alias associated_key_table join_table_alias
505
+
370
506
  # Whether the associated object needs a primary key to be added/removed,
371
507
  # true for many_to_many associations.
372
508
  def need_associated_primary_key?
@@ -388,6 +524,11 @@ module Sequel
388
524
  end
389
525
  self[:reciprocal] = nil
390
526
  end
527
+
528
+ # #right_primary_key qualified by the associated table
529
+ def qualified_right_primary_key
530
+ self[:qualified_right_primary_key] ||= qualify_assoc(right_primary_key)
531
+ end
391
532
 
392
533
  # The primary key column(s) to use in the associated table (can be symbol or array).
393
534
  def right_primary_key
@@ -399,11 +540,32 @@ module Sequel
399
540
  self[:right_primary_keys] ||= Array(right_primary_key)
400
541
  end
401
542
 
543
+ # The method symbol or array of method symbols to call on the associated objects
544
+ # to get the foreign key values for the join table.
545
+ def right_primary_key_method
546
+ self[:right_primary_key_method] ||= right_primary_key
547
+ end
548
+
549
+ # The array of method symbols to call on the associated objects
550
+ # to get the foreign key values for the join table.
551
+ def right_primary_key_methods
552
+ self[:right_primary_key_methods] ||= Array(right_primary_key_method)
553
+ end
554
+
402
555
  # The columns to select when loading the association, associated_class.table_name.* by default.
403
556
  def select
404
557
  return self[:select] if include?(:select)
405
558
  self[:select] ||= Sequel::SQL::ColumnAll.new(associated_class.table_name)
406
559
  end
560
+
561
+ private
562
+
563
+ # Split the join table into source and alias parts.
564
+ def split_join_table_alias
565
+ s, a = associated_class.dataset.split_alias(self[:join_table])
566
+ self[:join_table_source] = s
567
+ self[:join_table_alias] = a || s
568
+ end
407
569
  end
408
570
 
409
571
  # This module contains methods added to all association datasets
@@ -457,6 +619,12 @@ module Sequel
457
619
  # Project.association_reflection(:portfolio)
458
620
  # => {:type => :many_to_one, :name => :portfolio, ...}
459
621
  #
622
+ # Associations should not have the same names as any of the columns in the
623
+ # model's current table they reference. If you are dealing with an existing schema that
624
+ # has a column named status, you can't name the association status, you'd
625
+ # have to name it foo_status or something else. If you give an association the same name
626
+ # as a column, you will probably end up with an association that doesn't work, or a SystemStackError.
627
+ #
460
628
  # For a more in depth general overview, as well as a reference guide,
461
629
  # see the {Association Basics guide}[link:files/doc/association_basics_rdoc.html].
462
630
  # For examples of advanced usage, see the {Advanced Associations guide}[link:files/doc/advanced_associations_rdoc.html].
@@ -464,171 +632,233 @@ module Sequel
464
632
  # All association reflections defined for this model (default: {}).
465
633
  attr_reader :association_reflections
466
634
 
635
+ # The default :eager_limit_strategy option to use for *_many associations (default: nil)
636
+ attr_accessor :default_eager_limit_strategy
637
+
467
638
  # Array of all association reflections for this model class
468
639
  def all_association_reflections
469
640
  association_reflections.values
470
641
  end
471
642
 
643
+ # Given an association reflection and a dataset, apply the
644
+ # :select, :conditions, :order, :eager, :distinct, and :eager_block
645
+ # association options to the given dataset and return the dataset
646
+ # or a modified copy of it.
647
+ def apply_association_dataset_opts(opts, ds)
648
+ ds = ds.select(*opts.select) if opts.select
649
+ if c = opts[:conditions]
650
+ ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
651
+ end
652
+ ds = ds.order(*opts[:order]) if opts[:order]
653
+ ds = ds.eager(opts[:eager]) if opts[:eager]
654
+ ds = ds.distinct if opts[:distinct]
655
+ ds = opts[:eager_block].call(ds) if opts[:eager_block]
656
+ ds
657
+ end
658
+
472
659
  # Associates a related model with the current model. The following types are
473
660
  # supported:
474
661
  #
475
- # * :many_to_one - Foreign key in current model's table points to
476
- # associated model's primary key. Each associated model object can
477
- # be associated with more than one current model objects. Each current
478
- # model object can be associated with only one associated model object.
479
- # * :one_to_many - Foreign key in associated model's table points to this
480
- # model's primary key. Each current model object can be associated with
481
- # more than one associated model objects. Each associated model object
482
- # can be associated with only one current model object.
483
- # * :one_to_one - Similar to one_to_many in terms of foreign keys, but
484
- # only one object is associated to the current object through the
485
- # association. The methods created are similar to many_to_one, except
486
- # that the one_to_one setter method saves the passed object.
487
- # * :many_to_many - A join table is used that has a foreign key that points
488
- # to this model's primary key and a foreign key that points to the
489
- # associated model's primary key. Each current model object can be
490
- # associated with many associated model objects, and each associated
491
- # model object can be associated with many current model objects.
662
+ # :many_to_one :: Foreign key in current model's table points to
663
+ # associated model's primary key. Each associated model object can
664
+ # be associated with more than one current model objects. Each current
665
+ # model object can be associated with only one associated model object.
666
+ # :one_to_many :: Foreign key in associated model's table points to this
667
+ # model's primary key. Each current model object can be associated with
668
+ # more than one associated model objects. Each associated model object
669
+ # can be associated with only one current model object.
670
+ # :one_to_one :: Similar to one_to_many in terms of foreign keys, but
671
+ # only one object is associated to the current object through the
672
+ # association. The methods created are similar to many_to_one, except
673
+ # that the one_to_one setter method saves the passed object.
674
+ # :many_to_many :: A join table is used that has a foreign key that points
675
+ # to this model's primary key and a foreign key that points to the
676
+ # associated model's primary key. Each current model object can be
677
+ # associated with many associated model objects, and each associated
678
+ # model object can be associated with many current model objects.
492
679
  #
493
680
  # The following options can be supplied:
494
- # * *ALL types*:
495
- # - :after_add - Symbol, Proc, or array of both/either specifying a callback to call
496
- # after a new item is added to the association.
497
- # - :after_load - Symbol, Proc, or array of both/either specifying a callback to call
498
- # after the associated record(s) have been retrieved from the database. Not called
499
- # when eager loading via eager_graph, but called when eager loading via eager.
500
- # - :after_remove - Symbol, Proc, or array of both/either specifying a callback to call
501
- # after an item is removed from the association.
502
- # - :after_set - Symbol, Proc, or array of both/either specifying a callback to call
503
- # after an item is set using the association setter method.
504
- # - :allow_eager - If set to false, you cannot load the association eagerly
505
- # via eager or eager_graph
506
- # - :before_add - Symbol, Proc, or array of both/either specifying a callback to call
507
- # before a new item is added to the association.
508
- # - :before_remove - Symbol, Proc, or array of both/either specifying a callback to call
509
- # before an item is removed from the association.
510
- # - :before_set - Symbol, Proc, or array of both/either specifying a callback to call
511
- # before an item is set using the association setter method.
512
- # - :cartesian_product_number - the number of joins completed by this association that could cause more
513
- # than one row for each row in the current table (default: 0 for many_to_one and one_to_one associations,
514
- # 1 for one_to_many and many_to_many associations).
515
- # - :class - The associated class or its name. If not
516
- # given, uses the association's name, which is camelized (and
517
- # singularized unless the type is :many_to_one or :one_to_one)
518
- # - :clone - Merge the current options and block into the options and block used in defining
519
- # the given association. Can be used to DRY up a bunch of similar associations that
520
- # all share the same options such as :class and :key, while changing the order and block used.
521
- # - :conditions - The conditions to use to filter the association, can be any argument passed to filter.
522
- # - :dataset - A proc that is instance_evaled to get the base dataset
523
- # to use for the _dataset method (before the other options are applied).
524
- # - :distinct - Use the DISTINCT clause when selecting associating object, both when
525
- # lazy loading and eager loading via .eager (but not when using .eager_graph).
526
- # - :eager - The associations to eagerly load via +eager+ when loading the associated object(s).
527
- # - :eager_block - If given, use the block instead of the default block when
528
- # eagerly loading. To not use a block when eager loading (when one is used normally),
529
- # set to nil.
530
- # - :eager_graph - The associations to eagerly load via +eager_graph+ when loading the associated object(s).
531
- # - :eager_grapher - A proc to use to implement eager loading via +eager_graph+, overriding the default.
532
- # Takes three arguments, a dataset, an alias to use for the table to graph for this association,
533
- # and the alias that was used for the current table (since you can cascade associations),
534
- # Should return a copy of the dataset with the association graphed into it.
535
- # - :eager_loader - A proc to use to implement eager loading, overriding the default. Takes one or three arguments.
536
- # If three arguments, the first should be a key hash (used solely to enhance performance), the second an array of records,
537
- # and the third a hash of dependent associations. If one argument, is passed a hash with keys :key_hash,
538
- # :rows, and :associations, corresponding to the three arguments, and an additional key :self, which is
539
- # the dataset doing the eager loading. In the proc, the associated records should
540
- # be queried from the database and the associations cache for each
541
- # record should be populated.
542
- # - :eager_loader_key - A symbol for the key column to use to populate the key hash
543
- # for the eager loader.
544
- # - :extend - A module or array of modules to extend the dataset with.
545
- # - :graph_block - The block to pass to join_table when eagerly loading
546
- # the association via +eager_graph+.
547
- # - :graph_conditions - The additional conditions to use on the SQL join when eagerly loading
548
- # the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
549
- # specified, the :conditions option is used if it is a hash or array of two element arrays.
550
- # - :graph_join_type - The type of SQL join to use when eagerly loading the association via
551
- # eager_graph. Defaults to :left_outer.
552
- # - :graph_only_conditions - The conditions to use on the SQL join when eagerly loading
553
- # the association via +eager_graph+, instead of the default conditions specified by the
554
- # foreign/primary keys. This option causes the :graph_conditions option to be ignored.
555
- # - :graph_select - A column or array of columns to select from the associated table
556
- # when eagerly loading the association via +eager_graph+. Defaults to all
557
- # columns in the associated table.
558
- # - :limit - Limit the number of records to the provided value. Use
559
- # an array with two elements for the value to specify a limit (first element) and an offset (second element).
560
- # - :methods_module - The module that methods the association creates will be placed into. Defaults
561
- # to the module containing the model's columns.
562
- # - :order - the column(s) by which to order the association dataset. Can be a
563
- # singular column symbol or an array of column symbols.
564
- # - :order_eager_graph - Whether to add the association's order to the graphed dataset's order when graphing
565
- # via +eager_graph+. Defaults to true, so set to false to disable.
566
- # - :read_only - Do not add a setter method (for many_to_one or one_to_one associations),
567
- # or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
568
- # - :reciprocal - the symbol name of the reciprocal association,
569
- # if it exists. By default, Sequel will try to determine it by looking at the
570
- # associated model's assocations for a association that matches
571
- # the current association's key(s). Set to nil to not use a reciprocal.
572
- # - :select - the columns to select. Defaults to the associated class's
573
- # table_name.* in a many_to_many association, which means it doesn't include the attributes from the
574
- # join table. If you want to include the join table attributes, you can
575
- # use this option, but beware that the join table attributes can clash with
576
- # attributes from the model table, so you should alias any attributes that have
577
- # the same name in both the join table and the associated table.
578
- # - :validate - Set to false to not validate when implicitly saving any associated object.
579
- # * :many_to_one:
580
- # - :key - foreign_key in current model's table that references
581
- # associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
582
- # array of symbols for a composite key association.
583
- # - :primary_key - column in the associated table that :key option references, as a symbol.
584
- # Defaults to the primary key of the associated table. Can use an
585
- # array of symbols for a composite key association.
586
- # * :one_to_many and :one_to_one:
587
- # - :key - foreign key in associated model's table that references
588
- # current model's primary key, as a symbol. Defaults to
589
- # :"#{self.name.underscore}_id". Can use an
590
- # array of symbols for a composite key association.
591
- # - :primary_key - column in the current table that :key option references, as a symbol.
592
- # Defaults to primary key of the current table. Can use an
593
- # array of symbols for a composite key association.
594
- # * :many_to_many:
595
- # - :graph_join_table_block - The block to pass to +join_table+ for
596
- # the join table when eagerly loading the association via +eager_graph+.
597
- # - :graph_join_table_conditions - The additional conditions to use on the SQL join for
598
- # the join table when eagerly loading the association via +eager_graph+. Should be a hash
599
- # or an array of two element arrays.
600
- # - :graph_join_table_join_type - The type of SQL join to use for the join table when eagerly
601
- # loading the association via +eager_graph+. Defaults to the :graph_join_type option or
602
- # :left_outer.
603
- # - :graph_join_table_only_conditions - The conditions to use on the SQL join for the join
604
- # table when eagerly loading the association via +eager_graph+, instead of the default
605
- # conditions specified by the foreign/primary keys. This option causes the
606
- # :graph_join_table_conditions option to be ignored.
607
- # - :join_table - name of table that includes the foreign keys to both
608
- # the current model and the associated model, as a symbol. Defaults to the name
609
- # of current model and name of associated model, pluralized,
610
- # underscored, sorted, and joined with '_'.
611
- # - :join_table_block - proc that can be used to modify the dataset used in the add/remove/remove_all
612
- # methods. Should accept a dataset argument and return a modified dataset if present.
613
- # - :left_key - foreign key in join table that points to current model's
614
- # primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
615
- # Can use an array of symbols for a composite key association.
616
- # - :left_primary_key - column in current table that :left_key points to, as a symbol.
617
- # Defaults to primary key of current table. Can use an
618
- # array of symbols for a composite key association.
619
- # - :right_key - foreign key in join table that points to associated
620
- # model's primary key, as a symbol. Defaults to Defaults to :"#{name.to_s.singularize}_id".
621
- # Can use an array of symbols for a composite key association.
622
- # - :right_primary_key - column in associated table that :right_key points to, as a symbol.
623
- # Defaults to primary key of the associated table. Can use an
624
- # array of symbols for a composite key association.
625
- # - :uniq - Adds a after_load callback that makes the array of objects unique.
681
+ # === All Types
682
+ # :after_add :: Symbol, Proc, or array of both/either specifying a callback to call
683
+ # after a new item is added to the association.
684
+ # :after_load :: Symbol, Proc, or array of both/either specifying a callback to call
685
+ # after the associated record(s) have been retrieved from the database.
686
+ # :after_remove :: Symbol, Proc, or array of both/either specifying a callback to call
687
+ # after an item is removed from the association.
688
+ # :after_set :: Symbol, Proc, or array of both/either specifying a callback to call
689
+ # after an item is set using the association setter method.
690
+ # :allow_eager :: If set to false, you cannot load the association eagerly
691
+ # via eager or eager_graph
692
+ # :before_add :: Symbol, Proc, or array of both/either specifying a callback to call
693
+ # before a new item is added to the association.
694
+ # :before_remove :: Symbol, Proc, or array of both/either specifying a callback to call
695
+ # before an item is removed from the association.
696
+ # :before_set :: Symbol, Proc, or array of both/either specifying a callback to call
697
+ # before an item is set using the association setter method.
698
+ # :cartesian_product_number :: the number of joins completed by this association that could cause more
699
+ # than one row for each row in the current table (default: 0 for
700
+ # many_to_one and one_to_one associations, 1 for one_to_many and
701
+ # many_to_many associations).
702
+ # :class :: The associated class or its name. If not
703
+ # given, uses the association's name, which is camelized (and
704
+ # singularized unless the type is :many_to_one or :one_to_one)
705
+ # :clone :: Merge the current options and block into the options and block used in defining
706
+ # the given association. Can be used to DRY up a bunch of similar associations that
707
+ # all share the same options such as :class and :key, while changing the order and block used.
708
+ # :conditions :: The conditions to use to filter the association, can be any argument passed to filter.
709
+ # :dataset :: A proc that is instance_evaled to get the base dataset
710
+ # to use for the _dataset method (before the other options are applied).
711
+ # :distinct :: Use the DISTINCT clause when selecting associating object, both when
712
+ # lazy loading and eager loading via .eager (but not when using .eager_graph).
713
+ # :eager :: The associations to eagerly load via +eager+ when loading the associated object(s).
714
+ # :eager_block :: If given, use the block instead of the default block when
715
+ # eagerly loading. To not use a block when eager loading (when one is used normally),
716
+ # set to nil.
717
+ # :eager_graph :: The associations to eagerly load via +eager_graph+ when loading the associated object(s).
718
+ # many_to_many associations with this option cannot be eagerly loaded via +eager+.
719
+ # :eager_grapher :: A proc to use to implement eager loading via +eager_graph+, overriding the default.
720
+ # Takes one or three arguments. If three arguments, they are a dataset, an alias to use for
721
+ # the table to graph for this association, and the alias that was used for the current table
722
+ # (since you can cascade associations). If one argument, is passed a hash with keys :self,
723
+ # :table_alias, and :implicit_qualifier, corresponding to the three arguments, and an optional
724
+ # additional key :eager_block, a callback accepting one argument, the associated dataset. This
725
+ # is used to customize the association at query time.
726
+ # Should return a copy of the dataset with the association graphed into it.
727
+ # :eager_limit_strategy :: Determines the strategy used for enforcing limits when eager loading associations via
728
+ # the +eager+ method. For one_to_one associations, no strategy is used by default, and
729
+ # for *_many associations, the :ruby strategy is used by default, which still retrieves
730
+ # all records but slices the resulting array after the association is retrieved. You
731
+ # can pass a +true+ value for this option to have Sequel pick what it thinks is the best
732
+ # choice for the database, or specify a specific symbol to manually select a strategy.
733
+ # one_to_one associations support :distinct_on, :window_function, and :correlated_subquery.
734
+ # *_many associations support :ruby, :window_function, and :correlated_subquery.
735
+ # :eager_loader :: A proc to use to implement eager loading, overriding the default. Takes one or three arguments.
736
+ # If three arguments, the first should be a key hash (used solely to enhance performance), the
737
+ # second an array of records, and the third a hash of dependent associations. If one argument, is
738
+ # passed a hash with keys :key_hash, :rows, and :associations, corresponding to the three
739
+ # arguments, and an additional key :self, which is the dataset doing the eager loading. In the
740
+ # proc, the associated records should be queried from the database and the associations cache for
741
+ # each record should be populated.
742
+ # :eager_loader_key :: A symbol for the key column to use to populate the key hash
743
+ # for the eager loader.
744
+ # :extend :: A module or array of modules to extend the dataset with.
745
+ # :graph_alias_base :: The base name to use for the table alias when eager graphing. Defaults to the name
746
+ # of the association. If the alias name has already been used in the query, Sequel will create
747
+ # a unique alias by appending a numeric suffix (e.g. alias_0, alias_1, ...) until the alias is
748
+ # unique.
749
+ # :graph_block :: The block to pass to join_table when eagerly loading
750
+ # the association via +eager_graph+.
751
+ # :graph_conditions :: The additional conditions to use on the SQL join when eagerly loading
752
+ # the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
753
+ # specified, the :conditions option is used if it is a hash or array of two element arrays.
754
+ # :graph_join_type :: The type of SQL join to use when eagerly loading the association via
755
+ # eager_graph. Defaults to :left_outer.
756
+ # :graph_only_conditions :: The conditions to use on the SQL join when eagerly loading
757
+ # the association via +eager_graph+, instead of the default conditions specified by the
758
+ # foreign/primary keys. This option causes the :graph_conditions option to be ignored.
759
+ # :graph_select :: A column or array of columns to select from the associated table
760
+ # when eagerly loading the association via +eager_graph+. Defaults to all
761
+ # columns in the associated table.
762
+ # :limit :: Limit the number of records to the provided value. Use
763
+ # an array with two elements for the value to specify a
764
+ # limit (first element) and an offset (second element).
765
+ # :methods_module :: The module that methods the association creates will be placed into. Defaults
766
+ # to the module containing the model's columns.
767
+ # :order :: the column(s) by which to order the association dataset. Can be a
768
+ # singular column symbol or an array of column symbols.
769
+ # :order_eager_graph :: Whether to add the association's order to the graphed dataset's order when graphing
770
+ # via +eager_graph+. Defaults to true, so set to false to disable.
771
+ # :read_only :: Do not add a setter method (for many_to_one or one_to_one associations),
772
+ # or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
773
+ # :reciprocal :: the symbol name of the reciprocal association,
774
+ # if it exists. By default, Sequel will try to determine it by looking at the
775
+ # associated model's assocations for a association that matches
776
+ # the current association's key(s). Set to nil to not use a reciprocal.
777
+ # :select :: the columns to select. Defaults to the associated class's
778
+ # table_name.* in a many_to_many association, which means it doesn't include the attributes from the
779
+ # join table. If you want to include the join table attributes, you can
780
+ # use this option, but beware that the join table attributes can clash with
781
+ # attributes from the model table, so you should alias any attributes that have
782
+ # the same name in both the join table and the associated table.
783
+ # :validate :: Set to false to not validate when implicitly saving any associated object.
784
+ # === :many_to_one
785
+ # :key :: foreign key in current model's table that references
786
+ # associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
787
+ # array of symbols for a composite key association.
788
+ # :key_column :: Similar to, and usually identical to, :key, but :key refers to the model method
789
+ # to call, where :key_column refers to the underlying column. Should only be
790
+ # used if the the model method differs from the foreign key column, in conjunction
791
+ # with defining a model alias method for the key column.
792
+ # :primary_key :: column in the associated table that :key option references, as a symbol.
793
+ # Defaults to the primary key of the associated table. Can use an
794
+ # array of symbols for a composite key association.
795
+ # :primary_key_method :: the method symbol or array of method symbols to call on the associated
796
+ # object to get the foreign key values. Defaults to :primary_key option.
797
+ # :qualify :: Whether to use qualifier primary keys when loading the association. The default
798
+ # is true, so you must set to false to not qualify. Qualification rarely causes
799
+ # problems, but it's necessary to disable in some cases, such as when you are doing
800
+ # a JOIN USING operation on the column on Oracle.
801
+ # === :one_to_many and :one_to_one
802
+ # :key :: foreign key in associated model's table that references
803
+ # current model's primary key, as a symbol. Defaults to
804
+ # :"#{self.name.underscore}_id". Can use an
805
+ # array of symbols for a composite key association.
806
+ # :key_method :: the method symbol or array of method symbols to call on the associated
807
+ # object to get the foreign key values. Defaults to :key option.
808
+ # :primary_key :: column in the current table that :key option references, as a symbol.
809
+ # Defaults to primary key of the current table. Can use an
810
+ # array of symbols for a composite key association.
811
+ # :primary_key_column :: Similar to, and usually identical to, :primary_key, but :primary_key refers
812
+ # to the model method call, where :primary_key_column refers to the underlying column.
813
+ # Should only be used if the the model method differs from the primary key column, in
814
+ # conjunction with defining a model alias method for the primary key column.
815
+ # === :many_to_many
816
+ # :graph_join_table_block :: The block to pass to +join_table+ for
817
+ # the join table when eagerly loading the association via +eager_graph+.
818
+ # :graph_join_table_conditions :: The additional conditions to use on the SQL join for
819
+ # the join table when eagerly loading the association via +eager_graph+.
820
+ # Should be a hash or an array of two element arrays.
821
+ # :graph_join_table_join_type :: The type of SQL join to use for the join table when eagerly
822
+ # loading the association via +eager_graph+. Defaults to the
823
+ # :graph_join_type option or :left_outer.
824
+ # :graph_join_table_only_conditions :: The conditions to use on the SQL join for the join
825
+ # table when eagerly loading the association via +eager_graph+,
826
+ # instead of the default conditions specified by the
827
+ # foreign/primary keys. This option causes the
828
+ # :graph_join_table_conditions option to be ignored.
829
+ # :join_table :: name of table that includes the foreign keys to both
830
+ # the current model and the associated model, as a symbol. Defaults to the name
831
+ # of current model and name of associated model, pluralized,
832
+ # underscored, sorted, and joined with '_'.
833
+ # :join_table_block :: proc that can be used to modify the dataset used in the add/remove/remove_all
834
+ # methods. Should accept a dataset argument and return a modified dataset if present.
835
+ # :left_key :: foreign key in join table that points to current model's
836
+ # primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
837
+ # Can use an array of symbols for a composite key association.
838
+ # :left_primary_key :: column in current table that :left_key points to, as a symbol.
839
+ # Defaults to primary key of current table. Can use an
840
+ # array of symbols for a composite key association.
841
+ # :left_primary_key_column :: Similar to, and usually identical to, :left_primary_key, but :left_primary_key refers to
842
+ # the model method to call, where :left_primary_key_column refers to the underlying column. Should only
843
+ # be used if the model method differs from the left primary key column, in conjunction
844
+ # with defining a model alias method for the left primary key column.
845
+ # :right_key :: foreign key in join table that points to associated
846
+ # model's primary key, as a symbol. Defaults to Defaults to :"#{name.to_s.singularize}_id".
847
+ # Can use an array of symbols for a composite key association.
848
+ # :right_primary_key :: column in associated table that :right_key points to, as a symbol.
849
+ # Defaults to primary key of the associated table. Can use an
850
+ # array of symbols for a composite key association.
851
+ # :right_primary_key_method :: the method symbol or array of method symbols to call on the associated
852
+ # object to get the foreign key values for the join table.
853
+ # Defaults to :right_primary_key option.
854
+ # :uniq :: Adds a after_load callback that makes the array of objects unique.
626
855
  def associate(type, name, opts = {}, &block)
627
856
  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
857
  raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type]
629
858
  raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name
630
859
  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
-
860
+ raise(Error, ':eager_grapher option must have an arity of 1 or 3') if opts[:eager_grapher] && ![1, 3].include?(opts[:eager_grapher].arity)
861
+
632
862
  # dup early so we don't modify opts
633
863
  orig_opts = opts.dup
634
864
  orig_opts = association_reflection(opts[:clone])[:orig_opts].merge(orig_opts) if opts[:clone]
@@ -639,6 +869,7 @@ module Sequel
639
869
  opts[:graph_join_type] ||= :left_outer
640
870
  opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph)
641
871
  conds = opts[:conditions]
872
+ opts[:graph_alias_base] ||= name
642
873
  opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds)
643
874
  opts[:graph_conditions] = opts.fetch(:graph_conditions, []).to_a
644
875
  opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select]
@@ -668,57 +899,120 @@ module Sequel
668
899
 
669
900
  # Modify and return eager loading dataset based on association options.
670
901
  def eager_loading_dataset(opts, ds, select, associations, eager_options={})
902
+ ds = apply_association_dataset_opts(opts, ds)
671
903
  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
904
  if opts[:eager_graph]
905
+ raise(Error, "cannot eagerly load a #{opts[:type]} association that uses :eager_graph") if opts.eager_loading_use_associated_key?
679
906
  ds = ds.eager_graph(opts[:eager_graph])
680
- ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key?
681
- elsif opts.eager_loading_use_associated_key?
907
+ end
908
+ ds = ds.eager(associations) unless Array(associations).empty?
909
+ ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
910
+ if opts.eager_loading_use_associated_key?
682
911
  ds = if opts[:uses_left_composite_keys]
683
- t = opts.associated_key_table
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)})
912
+ ds.select_append(*opts.associated_key_alias.zip(opts.eager_loading_predicate_key).map{|a, k| SQL::AliasedExpression.new(k, a)})
685
913
  else
686
- ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias))
914
+ ds.select_append(SQL::AliasedExpression.new(opts.eager_loading_predicate_key, opts.associated_key_alias))
687
915
  end
688
916
  end
689
- ds = ds.eager(associations) unless Array(associations).empty?
690
- ds = opts[:eager_block].call(ds) if opts[:eager_block]
691
917
  ds
692
918
  end
693
919
 
694
920
  # Copy the association reflections to the subclass
695
921
  def inherited(subclass)
696
922
  super
697
- subclass.instance_variable_set(:@association_reflections, @association_reflections.dup)
923
+ subclass.instance_variable_set(:@association_reflections, association_reflections.dup)
924
+ subclass.default_eager_limit_strategy = default_eager_limit_strategy
698
925
  end
699
926
 
700
- # Shortcut for adding a many_to_many association, see associate
927
+ # Shortcut for adding a many_to_many association, see #associate
701
928
  def many_to_many(name, opts={}, &block)
702
929
  associate(:many_to_many, name, opts, &block)
703
930
  end
704
931
 
705
- # Shortcut for adding a many_to_one association, see associate
932
+ # Shortcut for adding a many_to_one association, see #associate
706
933
  def many_to_one(name, opts={}, &block)
707
934
  associate(:many_to_one, name, opts, &block)
708
935
  end
709
936
 
710
- # Shortcut for adding a one_to_many association, see associate
937
+ # Shortcut for adding a one_to_many association, see #associate
711
938
  def one_to_many(name, opts={}, &block)
712
939
  associate(:one_to_many, name, opts, &block)
713
940
  end
714
941
 
715
- # Shortcut for adding a one_to_one association, see associate.
942
+ # Shortcut for adding a one_to_one association, see #associate.
716
943
  def one_to_one(name, opts={}, &block)
717
944
  associate(:one_to_one, name, opts, &block)
718
945
  end
719
946
 
720
947
  private
721
948
 
949
+ # Use a correlated subquery to limit the results of the eager loading dataset.
950
+ def apply_correlated_subquery_eager_limit_strategy(ds, opts)
951
+ klass = opts.associated_class
952
+ kds = klass.dataset
953
+ dsa = ds.send(:dataset_alias, 1)
954
+ 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
955
+ pka = Array(pk)
956
+ 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?
957
+ table = kds.opts[:from]
958
+ 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
959
+ table = table.first
960
+ if order = ds.opts[:order]
961
+ oproc = lambda do |x|
962
+ case x
963
+ when Symbol
964
+ t, c, a = ds.send(:split_symbol, x)
965
+ if t && t.to_sym == table
966
+ SQL::QualifiedIdentifier.new(dsa, c)
967
+ else
968
+ x
969
+ end
970
+ when SQL::QualifiedIdentifier
971
+ if x.table == table
972
+ SQL::QualifiedIdentifier.new(dsa, x.column)
973
+ else
974
+ x
975
+ end
976
+ when SQL::OrderedExpression
977
+ SQL::OrderedExpression.new(oproc.call(x.expression), x.descending, :nulls=>x.nulls)
978
+ else
979
+ x
980
+ end
981
+ end
982
+ order = order.map(&oproc)
983
+ end
984
+ limit, offset = opts.limit_and_offset
985
+
986
+ subquery = yield kds.
987
+ unlimited.
988
+ from(SQL::AliasedExpression.new(table, dsa)).
989
+ select(*pka.map{|k| SQL::QualifiedIdentifier.new(dsa, k)}).
990
+ order(*order).
991
+ limit(limit, offset)
992
+
993
+ pk = if pk.is_a?(Array)
994
+ pk.map{|k| SQL::QualifiedIdentifier.new(table, k)}
995
+ else
996
+ SQL::QualifiedIdentifier.new(table, pk)
997
+ end
998
+ ds.filter(pk=>subquery)
999
+ end
1000
+
1001
+ # Use a window function to limit the results of the eager loading dataset.
1002
+ def apply_window_function_eager_limit_strategy(ds, opts)
1003
+ rn = ds.row_number_column
1004
+ limit, offset = opts.limit_and_offset
1005
+ ds = ds.unordered.select_append{row_number(:over, :partition=>opts.eager_loading_predicate_key, :order=>ds.opts[:order]){}.as(rn)}.from_self
1006
+ ds = if opts[:type] == :one_to_one
1007
+ ds.where(rn => 1)
1008
+ elsif offset
1009
+ offset += 1
1010
+ ds.where(rn => (offset...(offset+limit)))
1011
+ else
1012
+ ds.where{SQL::Identifier.new(rn) <= limit}
1013
+ end
1014
+ end
1015
+
722
1016
  # The module to use for the association's methods. Defaults to
723
1017
  # the overridable_methods_module.
724
1018
  def association_module(opts={})
@@ -753,9 +1047,20 @@ module Sequel
753
1047
  def_association_method(opts)
754
1048
  end
755
1049
 
756
- # Adds the association method to the association methods module.
757
- def def_association_method(opts)
758
- association_module_def(opts.association_method, opts){|*reload| load_associated_objects(opts, reload[0])}
1050
+ # Adds the association method to the association methods module. Be backwards
1051
+ # compatible with ruby 1.8.6, which doesn't support blocks taking block arguments.
1052
+ if RUBY_VERSION >= '1.8.7'
1053
+ class_eval <<-END, __FILE__, __LINE__+1
1054
+ def def_association_method(opts)
1055
+ association_module_def(opts.association_method, opts){|*dynamic_opts, &block| load_associated_objects(opts, dynamic_opts[0], &block)}
1056
+ end
1057
+ END
1058
+ else
1059
+ class_eval <<-END, __FILE__, __LINE__+1
1060
+ def def_association_method(opts)
1061
+ association_module_def(opts.association_method, opts){|*dynamic_opts| load_associated_objects(opts, dynamic_opts[0])}
1062
+ end
1063
+ END
759
1064
  end
760
1065
 
761
1066
  # Configures many_to_many association reflection and adds the related association methods
@@ -768,6 +1073,9 @@ module Sequel
768
1073
  rcks = opts[:right_keys] = Array(right)
769
1074
  left_pk = (opts[:left_primary_key] ||= self.primary_key)
770
1075
  lcpks = opts[:left_primary_keys] = Array(left_pk)
1076
+ lpkc = opts[:left_primary_key_column] ||= left_pk
1077
+ lpkcs = opts[:left_primary_key_columns] ||= Array(lpkc)
1078
+ elk = opts.eager_loader_key
771
1079
  raise(Error, "mismatched number of left composite keys: #{lcks.inspect} vs #{lcpks.inspect}") unless lcks.length == lcpks.length
772
1080
  if opts[:right_primary_key]
773
1081
  rcpks = Array(opts[:right_primary_key])
@@ -782,14 +1090,27 @@ module Sequel
782
1090
  opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
783
1091
  opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
784
1092
  opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) + lcks.zip(lcpks.map{|k| send(k)}))}
785
- database = db
786
-
1093
+
787
1094
  opts[:eager_loader] ||= proc do |eo|
788
- h = eo[:key_hash][left_pk]
789
- eo[:rows].each{|object| object.associations[name] = []}
790
- r = uses_rcks ? rcks.zip(opts.right_primary_keys) : [[right, opts.right_primary_key]]
791
- l = uses_lcks ? [[lcks.map{|k| SQL::QualifiedIdentifier.new(join_table, k)}, h.keys]] : [[left, h.keys]]
792
- model.eager_loading_dataset(opts, opts.associated_class.inner_join(join_table, r + l), Array(opts.select), eo[:associations], eo).all do |assoc_record|
1095
+ h = eo[:key_hash][elk]
1096
+ rows = eo[:rows]
1097
+ rows.each{|object| object.associations[name] = []}
1098
+ r = rcks.zip(opts.right_primary_keys)
1099
+ l = [[opts.qualify(opts.join_table_alias, left), h.keys]]
1100
+ ds = model.eager_loading_dataset(opts, opts.associated_class.inner_join(join_table, r + l), nil, eo[:associations], eo)
1101
+ case opts.eager_limit_strategy
1102
+ when :window_function
1103
+ delete_rn = true
1104
+ rn = ds.row_number_column
1105
+ ds = apply_window_function_eager_limit_strategy(ds, opts)
1106
+ when :correlated_subquery
1107
+ ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
1108
+ dsa = ds.send(:dataset_alias, 2)
1109
+ xds.inner_join(join_table, r + lcks.map{|k| [k, SQL::QualifiedIdentifier.new(opts.join_table_alias, k)]}, :table_alias=>dsa)
1110
+ end
1111
+ end
1112
+ ds.all do |assoc_record|
1113
+ assoc_record.values.delete(rn) if delete_rn
793
1114
  hash_key = if uses_lcks
794
1115
  left_key_alias.map{|k| assoc_record.values.delete(k)}
795
1116
  else
@@ -798,6 +1119,10 @@ module Sequel
798
1119
  next unless objects = h[hash_key]
799
1120
  objects.each{|object| object.associations[name].push(assoc_record)}
800
1121
  end
1122
+ if opts.eager_limit_strategy == :ruby
1123
+ limit, offset = opts.limit_and_offset
1124
+ rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
1125
+ end
801
1126
  end
802
1127
 
803
1128
  join_type = opts[:graph_join_type]
@@ -810,9 +1135,10 @@ module Sequel
810
1135
  jt_only_conditions = opts[:graph_join_table_only_conditions]
811
1136
  jt_join_type = opts[:graph_join_table_join_type]
812
1137
  jt_graph_block = opts[:graph_join_table_block]
813
- opts[:eager_grapher] ||= proc do |ds, assoc_alias, table_alias|
814
- 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), :join_type=>jt_join_type, :implicit_qualifier=>table_alias, :from_self_alias=>ds.opts[:eager_graph][:master], &jt_graph_block)
815
- ds.graph(opts.associated_class, use_only_conditions ? only_conditions : opts.right_primary_keys.zip(rcks) + conditions, :select=>select, :table_alias=>assoc_alias, :join_type=>join_type, &graph_block)
1138
+ opts[:eager_grapher] ||= proc do |eo|
1139
+ ds = eo[:self]
1140
+ ds = ds.graph(join_table, use_jt_only_conditions ? jt_only_conditions : lcks.zip(lpkcs) + 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)
1141
+ 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
1142
  end
817
1143
 
818
1144
  def_association_dataset_methods(opts)
@@ -822,11 +1148,11 @@ module Sequel
822
1148
  association_module_private_def(opts._add_method, opts) do |o|
823
1149
  h = {}
824
1150
  lcks.zip(lcpks).each{|k, pk| h[k] = send(pk)}
825
- rcks.zip(opts.right_primary_keys).each{|k, pk| h[k] = o.send(pk)}
1151
+ rcks.zip(opts.right_primary_key_methods).each{|k, pk| h[k] = o.send(pk)}
826
1152
  _join_table_dataset(opts).insert(h)
827
1153
  end
828
1154
  association_module_private_def(opts._remove_method, opts) do |o|
829
- _join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)}) + rcks.zip(opts.right_primary_keys.map{|k| o.send(k)})).delete
1155
+ _join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)}) + rcks.zip(opts.right_primary_key_methods.map{|k| o.send(k)})).delete
830
1156
  end
831
1157
  association_module_private_def(opts._remove_all_method, opts) do
832
1158
  _join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)})).delete
@@ -841,20 +1167,27 @@ module Sequel
841
1167
  name = opts[:name]
842
1168
  model = self
843
1169
  opts[:key] = opts.default_key unless opts.include?(:key)
844
- key = opts[:key]
845
- cks = opts[:keys] = Array(opts[:key])
1170
+ key_column = key = opts[:key]
1171
+ cks = opts[:graph_keys] = opts[:keys] = Array(key)
1172
+ if opts[:key_column]
1173
+ key_column = opts[:key_column]
1174
+ opts[:eager_loader_key] ||= key_column
1175
+ opts[:graph_keys] = Array(key_column)
1176
+ end
1177
+ opts[:qualified_key] = opts.qualify_cur(key)
846
1178
  if opts[:primary_key]
847
1179
  cpks = Array(opts[:primary_key])
848
1180
  raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
849
1181
  end
850
1182
  uses_cks = opts[:uses_composite_keys] = cks.length > 1
1183
+ qualify = opts[:qualify] != false
851
1184
  opts[:cartesian_product_number] ||= 0
852
1185
  opts[:dataset] ||= proc do
853
1186
  klass = opts.associated_class
854
- klass.filter(opts.primary_keys.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}.zip(cks.map{|k| send(k)}))
1187
+ klass.filter(Array(opts.qualified_primary_key).zip(cks.map{|k| send(k)}))
855
1188
  end
856
1189
  opts[:eager_loader] ||= proc do |eo|
857
- h = eo[:key_hash][key]
1190
+ h = eo[:key_hash][key_column]
858
1191
  keys = h.keys
859
1192
  # Default the cached association to nil, so any object that doesn't have it
860
1193
  # populated will have cached the negative lookup.
@@ -862,8 +1195,8 @@ module Sequel
862
1195
  # Skip eager loading if no objects have a foreign key for this association
863
1196
  unless keys.empty?
864
1197
  klass = opts.associated_class
865
- model.eager_loading_dataset(opts, klass.filter(uses_cks ? {opts.primary_keys.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}=>keys} : {SQL::QualifiedIdentifier.new(klass.table_name, opts.primary_key)=>keys}), opts.select, eo[:associations], eo).all do |assoc_record|
866
- hash_key = uses_cks ? opts.primary_keys.map{|k| assoc_record.send(k)} : assoc_record.send(opts.primary_key)
1198
+ model.eager_loading_dataset(opts, klass.filter(opts.qualified_primary_key=>keys), nil, eo[:associations], eo).all do |assoc_record|
1199
+ hash_key = uses_cks ? opts.primary_key_methods.map{|k| assoc_record.send(k)} : assoc_record.send(opts.primary_key_method)
867
1200
  next unless objects = h[hash_key]
868
1201
  objects.each{|object| object.associations[name] = assoc_record}
869
1202
  end
@@ -876,15 +1209,17 @@ module Sequel
876
1209
  only_conditions = opts[:graph_only_conditions]
877
1210
  conditions = opts[:graph_conditions]
878
1211
  graph_block = opts[:graph_block]
879
- opts[:eager_grapher] ||= proc do |ds, assoc_alias, table_alias|
880
- ds.graph(opts.associated_class, use_only_conditions ? only_conditions : opts.primary_keys.zip(cks) + conditions, :select=>select, :table_alias=>assoc_alias, :join_type=>join_type, :implicit_qualifier=>table_alias, :from_self_alias=>ds.opts[:eager_graph][:master], &graph_block)
1212
+ graph_cks = opts[:graph_keys]
1213
+ opts[:eager_grapher] ||= proc do |eo|
1214
+ ds = eo[:self]
1215
+ ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : opts.primary_keys.zip(graph_cks) + conditions, eo.merge(:select=>select, :join_type=>join_type, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
881
1216
  end
882
1217
 
883
1218
  def_association_dataset_methods(opts)
884
1219
 
885
1220
  return if opts[:read_only]
886
1221
 
887
- association_module_private_def(opts._setter_method, opts){|o| cks.zip(opts.primary_keys).each{|k, pk| send(:"#{k}=", (o.send(pk) if o))}}
1222
+ association_module_private_def(opts._setter_method, opts){|o| cks.zip(opts.primary_key_methods).each{|k, pk| send(:"#{k}=", (o.send(pk) if o))}}
888
1223
  association_module_def(opts.setter_method, opts){|o| set_associated_object(opts, o)}
889
1224
  end
890
1225
 
@@ -894,31 +1229,52 @@ module Sequel
894
1229
  name = opts[:name]
895
1230
  model = self
896
1231
  key = (opts[:key] ||= opts.default_key)
1232
+ km = opts[:key_method] ||= opts[:key]
897
1233
  cks = opts[:keys] = Array(key)
898
1234
  primary_key = (opts[:primary_key] ||= self.primary_key)
899
1235
  cpks = opts[:primary_keys] = Array(primary_key)
1236
+ pkc = opts[:primary_key_column] ||= primary_key
1237
+ pkcs = opts[:primary_key_columns] ||= Array(pkc)
1238
+ elk = opts.eager_loader_key
900
1239
  raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
901
1240
  uses_cks = opts[:uses_composite_keys] = cks.length > 1
902
1241
  opts[:dataset] ||= proc do
903
- klass = opts.associated_class
904
- klass.filter(cks.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}.zip(cpks.map{|k| send(k)}))
1242
+ opts.associated_class.filter(Array(opts.qualified_key).zip(cpks.map{|k| send(k)}))
905
1243
  end
906
1244
  opts[:eager_loader] ||= proc do |eo|
907
- h = eo[:key_hash][primary_key]
1245
+ h = eo[:key_hash][elk]
1246
+ rows = eo[:rows]
908
1247
  if one_to_one
909
- eo[:rows].each{|object| object.associations[name] = nil}
1248
+ rows.each{|object| object.associations[name] = nil}
910
1249
  else
911
- eo[:rows].each{|object| object.associations[name] = []}
1250
+ rows.each{|object| object.associations[name] = []}
912
1251
  end
913
1252
  reciprocal = opts.reciprocal
914
1253
  klass = opts.associated_class
915
- model.eager_loading_dataset(opts, klass.filter(uses_cks ? {cks.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}=>h.keys} : {SQL::QualifiedIdentifier.new(klass.table_name, key)=>h.keys}), opts.select, eo[:associations], eo).all do |assoc_record|
916
- hash_key = uses_cks ? cks.map{|k| assoc_record.send(k)} : assoc_record.send(key)
1254
+ filter_keys = opts.eager_loading_predicate_key
1255
+ ds = model.eager_loading_dataset(opts, klass.filter(filter_keys=>h.keys), nil, eo[:associations], eo)
1256
+ case opts.eager_limit_strategy
1257
+ when :distinct_on
1258
+ ds = ds.distinct(*filter_keys).order_prepend(*filter_keys)
1259
+ when :window_function
1260
+ delete_rn = true
1261
+ rn = ds.row_number_column
1262
+ ds = apply_window_function_eager_limit_strategy(ds, opts)
1263
+ when :correlated_subquery
1264
+ ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
1265
+ xds.where(opts.associated_object_keys.map{|k| [SQL::QualifiedIdentifier.new(xds.first_source_alias, k), SQL::QualifiedIdentifier.new(xds.first_source_table, k)]})
1266
+ end
1267
+ end
1268
+ ds.all do |assoc_record|
1269
+ assoc_record.values.delete(rn) if delete_rn
1270
+ hash_key = uses_cks ? km.map{|k| assoc_record.send(k)} : assoc_record.send(km)
917
1271
  next unless objects = h[hash_key]
918
1272
  if one_to_one
919
1273
  objects.each do |object|
920
- object.associations[name] = assoc_record
921
- assoc_record.associations[reciprocal] = object if reciprocal
1274
+ unless object.associations[name]
1275
+ object.associations[name] = assoc_record
1276
+ assoc_record.associations[reciprocal] = object if reciprocal
1277
+ end
922
1278
  end
923
1279
  else
924
1280
  objects.each do |object|
@@ -927,6 +1283,10 @@ module Sequel
927
1283
  end
928
1284
  end
929
1285
  end
1286
+ if opts.eager_limit_strategy == :ruby
1287
+ limit, offset = opts.limit_and_offset
1288
+ rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
1289
+ end
930
1290
  end
931
1291
 
932
1292
  join_type = opts[:graph_join_type]
@@ -936,10 +1296,11 @@ module Sequel
936
1296
  conditions = opts[:graph_conditions]
937
1297
  opts[:cartesian_product_number] ||= one_to_one ? 0 : 1
938
1298
  graph_block = opts[:graph_block]
939
- opts[:eager_grapher] ||= proc do |ds, assoc_alias, table_alias|
940
- ds = ds.graph(opts.associated_class, use_only_conditions ? only_conditions : cks.zip(cpks) + conditions, :select=>select, :table_alias=>assoc_alias, :join_type=>join_type, :implicit_qualifier=>table_alias, :from_self_alias=>ds.opts[:eager_graph][:master], &graph_block)
1299
+ opts[:eager_grapher] ||= proc do |eo|
1300
+ ds = eo[:self]
1301
+ ds = ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : cks.zip(pkcs) + conditions, eo.merge(:select=>select, :join_type=>join_type, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
941
1302
  # We only load reciprocals for one_to_many associations, as other reciprocals don't make sense
942
- ds.opts[:eager_graph][:reciprocals][assoc_alias] = opts.reciprocal
1303
+ ds.opts[:eager_graph][:reciprocals][eo[:table_alias]] = opts.reciprocal
943
1304
  ds
944
1305
  end
945
1306
 
@@ -993,6 +1354,15 @@ module Sequel
993
1354
  association_module_def(opts.remove_method, opts){|o,*args| remove_associated_object(opts, o, *args)}
994
1355
  association_module_def(opts.remove_all_method, opts){|*args| remove_all_associated_objects(opts, *args)}
995
1356
  end
1357
+
1358
+ # Return dataset to graph into given the association reflection, applying the :callback option if set.
1359
+ def eager_graph_dataset(opts, eager_options)
1360
+ ds = opts.associated_class.dataset
1361
+ if cb = eager_options[:callback]
1362
+ ds = cb.call(ds)
1363
+ end
1364
+ ds
1365
+ end
996
1366
  end
997
1367
 
998
1368
  # Instance methods used to implement the associations support.
@@ -1004,8 +1374,8 @@ module Sequel
1004
1374
  @associations ||= {}
1005
1375
  end
1006
1376
 
1007
- # Used internally by the associations code, like pk but doesn't raise
1008
- # an Error if the model has no primary key.
1377
+ # Formally used internally by the associations code, like pk but doesn't raise
1378
+ # an Error if the model has no primary key. Not used any longer, deprecated.
1009
1379
  def pk_or_nil
1010
1380
  key = primary_key
1011
1381
  key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key]
@@ -1033,6 +1403,15 @@ module Sequel
1033
1403
  ds
1034
1404
  end
1035
1405
 
1406
+ # Return a dataset for the association after applying any dynamic callback.
1407
+ def _associated_dataset(opts, dynamic_opts)
1408
+ ds = send(opts.dataset_method)
1409
+ if callback = dynamic_opts[:callback]
1410
+ ds = callback.call(ds)
1411
+ end
1412
+ ds
1413
+ end
1414
+
1036
1415
  # Return an association dataset for the given association reflection
1037
1416
  def _dataset(opts)
1038
1417
  raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
@@ -1041,17 +1420,18 @@ module Sequel
1041
1420
 
1042
1421
  # Dataset for the join table of the given many to many association reflection
1043
1422
  def _join_table_dataset(opts)
1044
- ds = model.db.from(opts[:join_table])
1423
+ ds = model.db.from(opts.join_table_source)
1045
1424
  opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
1046
1425
  end
1047
1426
 
1048
- # Return the associated objects from the dataset, without callbacks, reciprocals, and caching.
1049
- def _load_associated_objects(opts)
1427
+ # Return the associated objects from the dataset, without association callbacks, reciprocals, and caching.
1428
+ # Still apply the dynamic callback if present.
1429
+ def _load_associated_objects(opts, dynamic_opts={})
1050
1430
  if opts.returns_array?
1051
- opts.can_have_associated_objects?(self) ? send(opts.dataset_method).all : []
1431
+ opts.can_have_associated_objects?(self) ? _associated_dataset(opts, dynamic_opts).all : []
1052
1432
  else
1053
1433
  if opts.can_have_associated_objects?(self)
1054
- send(opts.dataset_method).all.first
1434
+ _associated_dataset(opts, dynamic_opts).all.first
1055
1435
  end
1056
1436
  end
1057
1437
  end
@@ -1113,13 +1493,20 @@ module Sequel
1113
1493
  end
1114
1494
 
1115
1495
  # Load the associated objects using the dataset, handling callbacks, reciprocals, and caching.
1116
- def load_associated_objects(opts, reload=false)
1496
+ def load_associated_objects(opts, dynamic_opts=nil)
1497
+ if dynamic_opts == true or dynamic_opts == false or dynamic_opts == nil
1498
+ dynamic_opts = {:reload=>dynamic_opts}
1499
+ elsif dynamic_opts.respond_to?(:call)
1500
+ dynamic_opts = {:callback=>dynamic_opts}
1501
+ end
1502
+ if block_given?
1503
+ dynamic_opts = dynamic_opts.merge(:callback=>Proc.new)
1504
+ end
1117
1505
  name = opts[:name]
1118
- if associations.include?(name) and !reload
1506
+ if associations.include?(name) and !dynamic_opts[:callback] and !dynamic_opts[:reload]
1119
1507
  associations[name]
1120
1508
  else
1121
- objs = _load_associated_objects(opts)
1122
- run_association_callbacks(opts, :after_load, objs)
1509
+ objs = _load_associated_objects(opts, dynamic_opts)
1123
1510
  if opts.set_reciprocal_to_self?
1124
1511
  if opts.returns_array?
1125
1512
  objs.each{|o| add_reciprocal_object(opts, o)}
@@ -1128,6 +1515,8 @@ module Sequel
1128
1515
  end
1129
1516
  end
1130
1517
  associations[name] = objs
1518
+ run_association_callbacks(opts, :after_load, objs)
1519
+ associations[name]
1131
1520
  end
1132
1521
  end
1133
1522
 
@@ -1238,7 +1627,7 @@ module Sequel
1238
1627
  # time, as it loads associated records using one query per association. However,
1239
1628
  # it does not allow you the ability to filter or order based on columns in associated tables. +eager_graph+ loads
1240
1629
  # all records in a single query using JOINs, allowing you to filter or order based on columns in associated
1241
- # tables. However, +eager_graph+ can be slower than +eager+, especially if multiple
1630
+ # tables. However, +eager_graph+ is usually slower than +eager+, especially if multiple
1242
1631
  # one_to_many or many_to_many associations are joined.
1243
1632
  #
1244
1633
  # You can cascade the eager loading (loading associations on associated objects)
@@ -1259,12 +1648,80 @@ module Sequel
1259
1648
  # Artist.eager_graph(:albums=>:tracks).all
1260
1649
  # Artist.eager(:albums=>{:tracks=>:genre}).all
1261
1650
  # Artist.eager_graph(:albums=>{:tracks=>:genre}).all
1651
+ #
1652
+ # You can also pass a callback as a hash value in order to customize the dataset being
1653
+ # eager loaded at query time, analogous to the way the :eager_block association option
1654
+ # allows you to customize it at association definition time. For example,
1655
+ # if you wanted artists with their albums since 1990:
1656
+ #
1657
+ # Artist.eager(:albums => proc{|ds| ds.filter{year > 1990}})
1658
+ #
1659
+ # Or if you needed albums and their artist's name only, using a single query:
1660
+ #
1661
+ # Albums.eager_graph(:artist => proc{|ds| ds.select(:name)})
1662
+ #
1663
+ # To cascade eager loading while using a callback, you substitute the cascaded
1664
+ # associations with a single entry hash that has the proc callback as the key and
1665
+ # the cascaded associations as the value. This will load artists with their albums
1666
+ # since 1990, and also the tracks on those albums and the genre for those tracks:
1667
+ #
1668
+ # Artist.eager(:albums => {proc{|ds| ds.filter{year > 1990}}=>{:tracks => :genre}})
1262
1669
  module DatasetMethods
1263
1670
  # Add the <tt>eager!</tt> and <tt>eager_graph!</tt> mutation methods to the dataset.
1264
1671
  def self.extended(obj)
1265
1672
  obj.def_mutation_method(:eager, :eager_graph)
1266
1673
  end
1267
1674
 
1675
+ # If the expression is in the form <tt>x = y</tt> where +y+ is a <tt>Sequel::Model</tt>
1676
+ # instance, array of <tt>Sequel::Model</tt> instances, or a <tt>Sequel::Model</tt> dataset,
1677
+ # assume +x+ is an association symbol and look up the association reflection
1678
+ # via the dataset's model. From there, return the appropriate SQL based on the type of
1679
+ # association and the values of the foreign/primary keys of +y+. For most association
1680
+ # types, this is a simple transformation, but for +many_to_many+ associations this
1681
+ # creates a subquery to the join table.
1682
+ def complex_expression_sql_append(sql, op, args)
1683
+ r = args.at(1)
1684
+ if (((op == :'=' || op == :'!=') and r.is_a?(Sequel::Model)) ||
1685
+ (multiple = ((op == :IN || op == :'NOT IN') and ((is_ds = r.is_a?(Sequel::Dataset)) or r.all?{|x| x.is_a?(Sequel::Model)}))))
1686
+ l = args.at(0)
1687
+ if ar = model.association_reflections[l]
1688
+ if multiple
1689
+ klass = ar.associated_class
1690
+ if is_ds
1691
+ if r.respond_to?(:model)
1692
+ unless r.model <= klass
1693
+ # A dataset for a different model class, could be a valid regular query
1694
+ return super
1695
+ end
1696
+ else
1697
+ # Not a model dataset, could be a valid regular query
1698
+ return super
1699
+ end
1700
+ else
1701
+ unless r.all?{|x| x.is_a?(klass)}
1702
+ 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}"
1703
+ end
1704
+ end
1705
+ elsif !r.is_a?(ar.associated_class)
1706
+ 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}"
1707
+ end
1708
+
1709
+ if exp = association_filter_expression(op, ar, r)
1710
+ literal_append(sql, exp)
1711
+ else
1712
+ raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}"
1713
+ end
1714
+ elsif multiple && (is_ds || r.empty?)
1715
+ # Not a query designed for this support, could be a valid regular query
1716
+ super
1717
+ else
1718
+ raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}"
1719
+ end
1720
+ else
1721
+ super
1722
+ end
1723
+ end
1724
+
1268
1725
  # The preferred eager loading method. Loads all associated records using one
1269
1726
  # query for each association.
1270
1727
  #
@@ -1311,9 +1768,9 @@ module Sequel
1311
1768
  # The secondary eager loading method. Loads all associations in a single query. This
1312
1769
  # method should only be used if you need to filter or order based on columns in associated tables.
1313
1770
  #
1314
- # This method builds an object graph using <tt>Dataset#graph</tt>. Then it uses the graph
1315
- # to build the associations, and finally replaces the graph with a simple array
1316
- # of model objects.
1771
+ # This method uses <tt>Dataset#graph</tt> to create appropriate aliases for columns in all the
1772
+ # tables. Then it uses the graph's metadata to build the associations from the single hash, and
1773
+ # finally replaces the array of hashes with an array model objects inside all.
1317
1774
  #
1318
1775
  # Be very careful when using this with multiple one_to_many or many_to_many associations, as you can
1319
1776
  # create large cartesian products. If you must graph multiple one_to_many and many_to_many associations,
@@ -1327,18 +1784,18 @@ module Sequel
1327
1784
  # all objects. You can use the :graph_* association options to modify the SQL query.
1328
1785
  #
1329
1786
  # Like +eager+, you need to call +all+ on the dataset for the eager loading to work. If you just
1330
- # call +each+, you will get a normal graphed result back (a hash with table alias symbol keys and
1331
- # model object values).
1787
+ # call +each+, it will yield plain hashes, each containing all columns from all the tables.
1332
1788
  def eager_graph(*associations)
1333
- ds = if @opts[:eager_graph]
1334
- self
1789
+ ds = if eg = @opts[:eager_graph]
1790
+ eg = eg.dup
1791
+ [:requirements, :reflections, :reciprocals].each{|k| eg[k] = eg[k].dup}
1792
+ clone(:eager_graph=>eg)
1335
1793
  else
1336
1794
  # Each of the following have a symbol key for the table alias, with the following values:
1337
1795
  # :reciprocals - the reciprocal instance variable to use for this association
1796
+ # :reflections - AssociationReflection instance related to this association
1338
1797
  # :requirements - array of requirements for this association
1339
- # :alias_association_type_map - the type of association for this association
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})
1798
+ clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :reflections=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
1342
1799
  end
1343
1800
  ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations)
1344
1801
  end
@@ -1366,20 +1823,32 @@ module Sequel
1366
1823
  # r :: association reflection for the current association
1367
1824
  # *associations :: any associations dependent on this one
1368
1825
  def eager_graph_association(ds, model, ta, requirements, r, *associations)
1369
- klass = r.associated_class
1370
1826
  assoc_name = r[:name]
1371
- assoc_table_alias = ds.unused_table_alias(assoc_name)
1372
- ds = r[:eager_grapher].call(ds, assoc_table_alias, ta)
1827
+ assoc_table_alias = ds.unused_table_alias(r[:graph_alias_base])
1828
+ loader = r[:eager_grapher]
1829
+ if !associations.empty?
1830
+ if associations.first.respond_to?(:call)
1831
+ callback = associations.first
1832
+ associations = {}
1833
+ 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)
1834
+ callback, assoc = pr_assoc
1835
+ associations = assoc.is_a?(Array) ? assoc : [assoc]
1836
+ end
1837
+ end
1838
+ ds = if loader.arity == 1
1839
+ loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>ta, :callback=>callback)
1840
+ else
1841
+ loader.call(ds, assoc_table_alias, ta)
1842
+ end
1373
1843
  ds = ds.order_more(*qualified_expression(r[:order], assoc_table_alias)) if r[:order] and r[:order_eager_graph]
1374
1844
  eager_graph = ds.opts[:eager_graph]
1375
1845
  eager_graph[:requirements][assoc_table_alias] = requirements.dup
1376
- eager_graph[:alias_association_name_map][assoc_table_alias] = assoc_name
1377
- eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array?
1846
+ eager_graph[:reflections][assoc_table_alias] = r
1378
1847
  eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2
1379
1848
  ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
1380
1849
  ds
1381
1850
  end
1382
-
1851
+
1383
1852
  # Check the associations are valid for the given model.
1384
1853
  # Call eager_graph_association on each association.
1385
1854
  #
@@ -1406,20 +1875,228 @@ module Sequel
1406
1875
  ds
1407
1876
  end
1408
1877
 
1409
- # Build associations out of the array of returned object graphs.
1410
- def eager_graph_build_associations(record_graphs)
1411
- eager_graph = @opts[:eager_graph]
1412
- master = eager_graph[:master]
1878
+ # Replace the array of plain hashes with an array of model objects will all eager_graphed
1879
+ # associations set in the associations cache for each object.
1880
+ def eager_graph_build_associations(hashes)
1881
+ hashes.replace(EagerGraphLoader.new(self).load(hashes))
1882
+ end
1883
+
1884
+ private
1885
+
1886
+ # Return an expression for filtering by the given association reflection and associated object.
1887
+ def association_filter_expression(op, ref, obj)
1888
+ meth = :"#{ref[:type]}_association_filter_expression"
1889
+ send(meth, op, ref, obj) if respond_to?(meth, true)
1890
+ end
1891
+
1892
+ # Handle inversion for association filters by returning an inverted expression,
1893
+ # plus also handling cases where the referenced columns are NULL.
1894
+ def association_filter_handle_inversion(op, exp, cols)
1895
+ if op == :'!=' || op == :'NOT IN'
1896
+ if exp == SQL::Constants::FALSE
1897
+ ~exp
1898
+ else
1899
+ ~exp | Sequel::SQL::BooleanExpression.from_value_pairs(cols.zip([]), :OR)
1900
+ end
1901
+ else
1902
+ exp
1903
+ end
1904
+ end
1905
+
1906
+ # Return an expression for making sure that the given keys match the value of
1907
+ # the given methods for either the single object given or for any of the objects
1908
+ # given if +obj+ is an array.
1909
+ def association_filter_key_expression(keys, meths, obj)
1910
+ vals = if obj.is_a?(Sequel::Dataset)
1911
+ {(keys.length == 1 ? keys.first : keys)=>obj.select(*meths).exclude(Sequel::SQL::BooleanExpression.from_value_pairs(meths.zip([]), :OR))}
1912
+ else
1913
+ vals = Array(obj).reject{|o| !meths.all?{|m| o.send(m)}}
1914
+ return SQL::Constants::FALSE if vals.empty?
1915
+ if obj.is_a?(Array)
1916
+ if keys.length == 1
1917
+ meth = meths.first
1918
+ {keys.first=>vals.map{|o| o.send(meth)}}
1919
+ else
1920
+ {keys=>vals.map{|o| meths.map{|m| o.send(m)}}}
1921
+ end
1922
+ else
1923
+ keys.zip(meths.map{|k| obj.send(k)})
1924
+ end
1925
+ end
1926
+ SQL::BooleanExpression.from_value_pairs(vals)
1927
+ end
1928
+
1929
+ # Make sure the association is valid for this model, and return the related AssociationReflection.
1930
+ def check_association(model, association)
1931
+ raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
1932
+ raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
1933
+ reflection
1934
+ end
1935
+
1936
+ # Eagerly load all specified associations
1937
+ def eager_load(a, eager_assoc=@opts[:eager])
1938
+ return if a.empty?
1939
+ # Key is foreign/primary key name symbol
1940
+ # Value is hash with keys being foreign/primary key values (generally integers)
1941
+ # and values being an array of current model objects with that
1942
+ # specific foreign/primary key
1943
+ key_hash = {}
1944
+ # Reflections for all associations to eager load
1945
+ reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
1946
+
1947
+ # Populate keys to monitor
1948
+ reflections.each{|reflection| key_hash[reflection.eager_loader_key] ||= Hash.new{|h,k| h[k] = []}}
1949
+
1950
+ # Associate each object with every key being monitored
1951
+ a.each do |rec|
1952
+ key_hash.each do |key, id_map|
1953
+ case key
1954
+ when Array
1955
+ id_map[key.map{|k| rec[k]}] << rec if key.all?{|k| rec[k]}
1956
+ when Symbol
1957
+ id_map[rec[key]] << rec if rec[key]
1958
+ end
1959
+ end
1960
+ end
1961
+
1962
+ reflections.each do |r|
1963
+ loader = r[:eager_loader]
1964
+ associations = eager_assoc[r[:name]]
1965
+ if associations.respond_to?(:call)
1966
+ eager_block = associations
1967
+ associations = {}
1968
+ elsif associations.is_a?(Hash) && associations.length == 1 && (pr_assoc = associations.to_a.first) && pr_assoc.first.respond_to?(:call)
1969
+ eager_block, associations = pr_assoc
1970
+ end
1971
+ if loader.arity == 1
1972
+ loader.call(:key_hash=>key_hash, :rows=>a, :associations=>associations, :self=>self, :eager_block=>eager_block)
1973
+ else
1974
+ loader.call(key_hash, a, associations)
1975
+ end
1976
+ a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
1977
+ end
1978
+ end
1979
+
1980
+ # Return plain hashes instead of calling the row_proc if eager_graph is being used.
1981
+ def graph_each(&block)
1982
+ @opts[:eager_graph] ? fetch_rows(select_sql, &block) : super
1983
+ end
1984
+
1985
+ # Return a subquery expression for filering by a many_to_many association
1986
+ def many_to_many_association_filter_expression(op, ref, obj)
1987
+ lpks, lks, rks = ref.values_at(:left_primary_keys, :left_keys, :right_keys)
1988
+ jt = ref.join_table_alias
1989
+ lpks = lpks.first if lpks.length == 1
1990
+ lpks = ref.qualify(model.table_name, lpks)
1991
+ meths = ref.right_primary_keys
1992
+ meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
1993
+ exp = association_filter_key_expression(ref.qualify(jt, rks), meths, obj)
1994
+ if exp == SQL::Constants::FALSE
1995
+ association_filter_handle_inversion(op, exp, Array(lpks))
1996
+ else
1997
+ association_filter_handle_inversion(op, SQL::BooleanExpression.from_value_pairs(lpks=>model.db.from(ref[:join_table]).select(*ref.qualify(jt, lks)).where(exp).exclude(SQL::BooleanExpression.from_value_pairs(ref.qualify(jt, lks).zip([]), :OR))), Array(lpks))
1998
+ end
1999
+ end
2000
+
2001
+ # Return a simple equality expression for filering by a many_to_one association
2002
+ def many_to_one_association_filter_expression(op, ref, obj)
2003
+ keys = ref.qualify(model.table_name, ref[:keys])
2004
+ meths = ref.primary_keys
2005
+ meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
2006
+ association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
2007
+ end
2008
+
2009
+ # Return a simple equality expression for filering by a one_to_* association
2010
+ def one_to_many_association_filter_expression(op, ref, obj)
2011
+ keys = ref.qualify(model.table_name, ref[:primary_keys])
2012
+ meths = ref[:keys]
2013
+ meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
2014
+ association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
2015
+ end
2016
+ alias one_to_one_association_filter_expression one_to_many_association_filter_expression
2017
+
2018
+ # Build associations from the graph if #eager_graph was used,
2019
+ # and/or load other associations if #eager was used.
2020
+ def post_load(all_records)
2021
+ eager_graph_build_associations(all_records) if @opts[:eager_graph]
2022
+ eager_load(all_records) if @opts[:eager]
2023
+ super
2024
+ end
2025
+ end
2026
+
2027
+ # This class is the internal implementation of eager_graph. It is responsible for taking an array of plain
2028
+ # hashes and returning an array of model objects with all eager_graphed associations already set in the
2029
+ # association cache.
2030
+ class EagerGraphLoader
2031
+ # Hash with table alias symbol keys and after_load hook values
2032
+ attr_reader :after_load_map
2033
+
2034
+ # Hash with table alias symbol keys and association name values
2035
+ attr_reader :alias_map
2036
+
2037
+ # Hash with table alias symbol keys and subhash values mapping column_alias symbols to the
2038
+ # symbol of the real name of the column
2039
+ attr_reader :column_maps
2040
+
2041
+ # Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
2042
+ attr_reader :dependency_map
2043
+
2044
+ # Hash with table alias symbol keys and [limit, offset] values
2045
+ attr_reader :limit_map
2046
+
2047
+ # Hash with table alias symbol keys and callable values used to create model instances
2048
+ # The table alias symbol for the primary model
2049
+ attr_reader :master
2050
+
2051
+ # Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for
2052
+ # composite key tables)
2053
+ attr_reader :primary_keys
2054
+
2055
+ # Hash with table alias symbol keys and reciprocal association symbol values,
2056
+ # used for setting reciprocals for one_to_many associations.
2057
+ attr_reader :reciprocal_map
2058
+
2059
+ # Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols)
2060
+ # to model instances. Used so that only a single model instance is created for each object.
2061
+ attr_reader :records_map
2062
+
2063
+ # Hash with table alias symbol keys and AssociationReflection values
2064
+ attr_reader :reflection_map
2065
+
2066
+ # Hash with table alias symbol keys and callable values used to create model instances
2067
+ attr_reader :row_procs
2068
+
2069
+ # Hash with table alias symbol keys and true/false values, where true means the
2070
+ # association represented by the table alias uses an array of values instead of
2071
+ # a single value (i.e. true => *_many, false => *_to_one).
2072
+ attr_reader :type_map
2073
+
2074
+ # Initialize all of the data structures used during loading.
2075
+ def initialize(dataset)
2076
+ opts = dataset.opts
2077
+ eager_graph = opts[:eager_graph]
2078
+ @master = eager_graph[:master]
1413
2079
  requirements = eager_graph[:requirements]
1414
- alias_map = eager_graph[:alias_association_name_map]
1415
- type_map = eager_graph[:alias_association_type_map]
1416
- reciprocal_map = eager_graph[:reciprocals]
2080
+ reflection_map = @reflection_map = eager_graph[:reflections]
2081
+ reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
2082
+ @unique = eager_graph[:cartesian_product_number] > 1
1417
2083
 
2084
+ alias_map = @alias_map = {}
2085
+ type_map = @type_map = {}
2086
+ after_load_map = @after_load_map = {}
2087
+ limit_map = @limit_map = {}
2088
+ reflection_map.each do |k, v|
2089
+ alias_map[k] = v[:name]
2090
+ type_map[k] = v.returns_array?
2091
+ after_load_map[k] = v[:after_load] unless v[:after_load].empty?
2092
+ limit_map[k] = v.limit_and_offset if v[:limit]
2093
+ end
2094
+
1418
2095
  # Make dependency map hash out of requirements array for each association.
1419
2096
  # This builds a tree of dependencies that will be used for recursion
1420
2097
  # to ensure that all parts of the object graph are loaded into the
1421
2098
  # appropriate subordinate association.
1422
- dependency_map = {}
2099
+ @dependency_map = {}
1423
2100
  # Sort the associations by requirements length, so that
1424
2101
  # requirements are added to the dependency hash before their
1425
2102
  # dependencies.
@@ -1439,140 +2116,200 @@ module Sequel
1439
2116
  # This mapping is used to make sure that duplicate entries in the
1440
2117
  # result set are mapped to a single record. For example, using a
1441
2118
  # single one_to_many association with 10 associated records,
1442
- # the main object will appear in the object graph 10 times.
2119
+ # the main object column values appear in the object graph 10 times.
1443
2120
  # We map by primary key, if available, or by the object's entire values,
1444
2121
  # if not. The mapping must be per table, so create sub maps for each table
1445
2122
  # alias.
1446
- records_map = {master=>{}}
2123
+ records_map = {@master=>{}}
1447
2124
  alias_map.keys.each{|ta| records_map[ta] = {}}
2125
+ @records_map = records_map
2126
+
2127
+ datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
2128
+ column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
2129
+ primary_keys = {}
2130
+ column_maps = {}
2131
+ models = {}
2132
+ row_procs = {}
2133
+ datasets.each do |ta, ds|
2134
+ models[ta] = ds.model
2135
+ primary_keys[ta] = []
2136
+ column_maps[ta] = {}
2137
+ row_procs[ta] = ds.row_proc
2138
+ end
2139
+ column_aliases.each do |col_alias, tc|
2140
+ ta, column = tc
2141
+ column_maps[ta][col_alias] = column
2142
+ end
2143
+ column_maps.each do |ta, h|
2144
+ pk = models[ta].primary_key
2145
+ if pk.is_a?(Array)
2146
+ primary_keys[ta] = []
2147
+ h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
2148
+ else
2149
+ h.select{|ca, c| primary_keys[ta] = ca if pk == c}
2150
+ end
2151
+ end
2152
+ @column_maps = column_maps
2153
+ @primary_keys = primary_keys
2154
+ @row_procs = row_procs
2155
+
2156
+ # For performance, create two special maps for the master table,
2157
+ # so you can skip a hash lookup.
2158
+ @master_column_map = column_maps[master]
2159
+ @master_primary_keys = primary_keys[master]
2160
+
2161
+ # Add a special hash mapping table alias symbols to 5 element arrays that just
2162
+ # contain the data in other data structures for that table alias. This is
2163
+ # used for performance, to get all values in one hash lookup instead of
2164
+ # separate hash lookups for each data structure.
2165
+ ta_map = {}
2166
+ alias_map.keys.each do |ta|
2167
+ ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
2168
+ end
2169
+ @ta_map = ta_map
2170
+ end
2171
+
2172
+ # Return an array of primary model instances with the associations cache prepopulated
2173
+ # for all model objects (both primary and associated).
2174
+ def load(hashes)
2175
+ master = master()
1448
2176
 
2177
+ # Assign to local variables for speed increase
2178
+ rp = row_procs[master]
2179
+ rm = records_map[master]
2180
+ dm = dependency_map
2181
+
1449
2182
  # This will hold the final record set that we will be replacing the object graph with.
1450
2183
  records = []
1451
- record_graphs.each do |record_graph|
1452
- primary_record = record_graph[master]
1453
- key = primary_record.pk_or_nil || primary_record.values.sort_by{|x| x[0].to_s}
1454
- if cached_pr = records_map[master][key]
1455
- primary_record = cached_pr
1456
- else
1457
- records_map[master][key] = primary_record
2184
+
2185
+ hashes.each do |h|
2186
+ unless key = master_pk(h)
2187
+ key = hkey(master_hfor(h))
2188
+ end
2189
+ unless primary_record = rm[key]
2190
+ primary_record = rm[key] = rp.call(master_hfor(h))
1458
2191
  # Only add it to the list of records to return if it is a new record
1459
2192
  records.push(primary_record)
1460
2193
  end
1461
2194
  # Build all associations for the current object and it's dependencies
1462
- eager_graph_build_associations_graph(dependency_map, alias_map, type_map, reciprocal_map, records_map, primary_record, record_graph)
2195
+ _load(dm, primary_record, h)
1463
2196
  end
1464
2197
 
1465
2198
  # Remove duplicate records from all associations if this graph could possibly be a cartesian product
1466
- eager_graph_make_associations_unique(records, dependency_map, alias_map, type_map) if eager_graph[:cartesian_product_number] > 1
1467
-
1468
- # Replace the array of object graphs with an array of model objects
1469
- record_graphs.replace(records)
2199
+ # Run after_load procs if there are any
2200
+ post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
2201
+
2202
+ records
1470
2203
  end
1471
2204
 
1472
2205
  private
1473
-
1474
- # Make sure the association is valid for this model, and return the related AssociationReflection.
1475
- def check_association(model, association)
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
2206
+
2207
+ # Recursive method that creates associated model objects and associates them to the current model object.
2208
+ def _load(dependency_map, current, h)
1490
2209
  dependency_map.each do |ta, deps|
1491
- next unless rec = record_graph[ta]
1492
- key = rec.pk_or_nil || rec.values.sort_by{|x| x[0].to_s}
1493
- if cached_rec = records_map[ta][key]
1494
- rec = cached_rec
1495
- else
1496
- records_map[ta][key] = rec
2210
+ unless key = pk(ta, h)
2211
+ ta_h = hfor(ta, h)
2212
+ unless ta_h.values.any?
2213
+ assoc_name = alias_map[ta]
2214
+ unless (assoc = current.associations).has_key?(assoc_name)
2215
+ assoc[assoc_name] = type_map[ta] ? [] : nil
2216
+ end
2217
+ next
2218
+ end
2219
+ key = hkey(ta_h)
2220
+ end
2221
+ rm, rp, assoc_name, tm, rcm = @ta_map[ta]
2222
+ unless rec = rm[key]
2223
+ rec = rm[key] = rp.call(hfor(ta, h))
1497
2224
  end
1498
- assoc_name = alias_map[ta]
1499
- if type_map[ta]
1500
- current.associations[assoc_name].push(rec)
1501
- if reciprocal = reciprocal_map[ta]
1502
- rec.associations[reciprocal] = current
2225
+
2226
+ if tm
2227
+ unless (assoc = current.associations).has_key?(assoc_name)
2228
+ assoc[assoc_name] = []
1503
2229
  end
2230
+ assoc[assoc_name].push(rec)
2231
+ rec.associations[rcm] = current if rcm
1504
2232
  else
1505
- current.associations[assoc_name] = rec
2233
+ current.associations[assoc_name] ||= rec
1506
2234
  end
1507
2235
  # Recurse into dependencies of the current object
1508
- eager_graph_build_associations_graph(deps, alias_map, type_map, reciprocal_map, records_map, rec, record_graph)
2236
+ _load(deps, rec, h) unless deps.empty?
1509
2237
  end
1510
2238
  end
1511
2239
 
2240
+ # Return the subhash for the specific table alias +ta+ by parsing the values out of the main hash +h+
2241
+ def hfor(ta, h)
2242
+ out = {}
2243
+ @column_maps[ta].each{|ca, c| out[c] = h[ca]}
2244
+ out
2245
+ end
2246
+
2247
+ # Return a suitable hash key for any subhash +h+, which is an array of values by column order.
2248
+ # This is only used if the primary key cannot be used.
2249
+ def hkey(h)
2250
+ h.sort_by{|x| x[0].to_s}
2251
+ end
2252
+
2253
+ # Return the subhash for the master table by parsing the values out of the main hash +h+
2254
+ def master_hfor(h)
2255
+ out = {}
2256
+ @master_column_map.each{|ca, c| out[c] = h[ca]}
2257
+ out
2258
+ end
2259
+
2260
+ # Return a primary key value for the master table by parsing it out of the main hash +h+.
2261
+ def master_pk(h)
2262
+ x = @master_primary_keys
2263
+ if x.is_a?(Array)
2264
+ unless x == []
2265
+ x = x.map{|ca| h[ca]}
2266
+ x if x.all?
2267
+ end
2268
+ else
2269
+ h[x]
2270
+ end
2271
+ end
2272
+
2273
+ # Return a primary key value for the given table alias by parsing it out of the main hash +h+.
2274
+ def pk(ta, h)
2275
+ x = primary_keys[ta]
2276
+ if x.is_a?(Array)
2277
+ unless x == []
2278
+ x = x.map{|ca| h[ca]}
2279
+ x if x.all?
2280
+ end
2281
+ else
2282
+ h[x]
2283
+ end
2284
+ end
2285
+
1512
2286
  # If the result set is the result of a cartesian product, then it is possible that
1513
2287
  # there are multiple records for each association when there should only be one.
1514
2288
  # In that case, for each object in all associations loaded via +eager_graph+, run
1515
2289
  # uniq! on the association to make sure no duplicate records show up.
1516
2290
  # Note that this can cause legitimate duplicate records to be removed.
1517
- def eager_graph_make_associations_unique(records, dependency_map, alias_map, type_map)
2291
+ def post_process(records, dependency_map)
1518
2292
  records.each do |record|
1519
2293
  dependency_map.each do |ta, deps|
1520
- list = record.send(alias_map[ta])
1521
- list = if type_map[ta]
2294
+ assoc_name = alias_map[ta]
2295
+ list = record.send(assoc_name)
2296
+ rec_list = if type_map[ta]
1522
2297
  list.uniq!
2298
+ if lo = limit_map[ta]
2299
+ limit, offset = lo
2300
+ list.replace(list[offset||0, limit])
2301
+ end
2302
+ list
2303
+ elsif list
2304
+ [list]
1523
2305
  else
1524
- [list] if list
2306
+ []
1525
2307
  end
1526
- # Recurse into dependencies
1527
- eager_graph_make_associations_unique(list, deps, alias_map, type_map) if list
2308
+ record.send(:run_association_callbacks, reflection_map[ta], :after_load, list) if after_load_map[ta]
2309
+ post_process(rec_list, deps) if !rec_list.empty? && !deps.empty?
1528
2310
  end
1529
2311
  end
1530
2312
  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]
1554
- end
1555
- end
1556
- 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
- end
1576
2313
  end
1577
2314
  end
1578
2315
  end