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