sequel 3.21.0 → 3.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +855 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -7
- data/Rakefile +42 -24
- data/bin/sequel +26 -8
- data/doc/active_record.rdoc +1 -0
- data/doc/advanced_associations.rdoc +171 -11
- data/doc/association_basics.rdoc +293 -43
- data/doc/dataset_basics.rdoc +5 -5
- data/doc/dataset_filtering.rdoc +8 -0
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +85 -552
- data/doc/model_hooks.rdoc +78 -27
- data/doc/object_model.rdoc +541 -0
- data/doc/opening_databases.rdoc +97 -50
- data/doc/prepared_statements.rdoc +40 -14
- data/doc/querying.rdoc +28 -4
- data/doc/reflection.rdoc +30 -4
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/schema_modification.rdoc +572 -0
- data/doc/sharding.rdoc +95 -3
- data/doc/testing.rdoc +129 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/virtual_rows.rdoc +51 -2
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +17 -9
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +1 -57
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +18 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +453 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/db2.rb +47 -0
- data/lib/sequel/adapters/jdbc/derby.rb +287 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +73 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +58 -48
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +265 -94
- data/lib/sequel/adapters/mock.rb +361 -0
- data/lib/sequel/adapters/mysql.rb +118 -222
- data/lib/sequel/adapters/mysql2.rb +58 -57
- data/lib/sequel/adapters/odbc/mssql.rb +15 -7
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +3 -5
- data/lib/sequel/adapters/oracle.rb +349 -53
- data/lib/sequel/adapters/postgres.rb +356 -107
- data/lib/sequel/adapters/shared/access.rb +32 -9
- data/lib/sequel/adapters/shared/db2.rb +342 -0
- data/lib/sequel/adapters/shared/firebird.rb +221 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +335 -118
- data/lib/sequel/adapters/shared/mysql.rb +340 -76
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
- data/lib/sequel/adapters/shared/oracle.rb +243 -45
- data/lib/sequel/adapters/shared/postgres.rb +524 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +265 -89
- data/lib/sequel/adapters/sqlite.rb +101 -39
- data/lib/sequel/adapters/swift/mysql.rb +9 -12
- data/lib/sequel/adapters/swift/postgres.rb +9 -74
- data/lib/sequel/adapters/swift/sqlite.rb +8 -6
- data/lib/sequel/adapters/swift.rb +8 -7
- data/lib/sequel/adapters/tinytds.rb +161 -21
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +11 -1
- data/lib/sequel/connection_pool/sharded_threaded.rb +28 -2
- data/lib/sequel/connection_pool/single.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +38 -14
- data/lib/sequel/connection_pool.rb +10 -2
- data/lib/sequel/core.rb +118 -11
- data/lib/sequel/database/connecting.rb +38 -28
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +150 -46
- data/lib/sequel/database/query.rb +215 -65
- data/lib/sequel/database/schema_generator.rb +64 -20
- data/lib/sequel/database/schema_methods.rb +212 -54
- data/lib/sequel/dataset/actions.rb +322 -85
- data/lib/sequel/dataset/features.rb +96 -8
- data/lib/sequel/dataset/graph.rb +38 -11
- data/lib/sequel/dataset/misc.rb +50 -23
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +237 -44
- data/lib/sequel/dataset/sql.rb +698 -278
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +79 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +108 -0
- data/lib/sequel/extensions/columns_introspection.rb +61 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +16 -34
- data/lib/sequel/extensions/migration.rb +97 -11
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/null_dataset.rb +90 -0
- data/lib/sequel/extensions/pg_array.rb +460 -0
- data/lib/sequel/extensions/pg_array_ops.rb +220 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +169 -0
- data/lib/sequel/extensions/pg_hstore.rb +296 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +259 -0
- data/lib/sequel/extensions/pg_inet.rb +89 -0
- data/lib/sequel/extensions/pg_json.rb +178 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +316 -0
- data/lib/sequel/extensions/pretty_table.rb +5 -71
- data/lib/sequel/extensions/query.rb +1 -1
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +251 -37
- data/lib/sequel/extensions/select_remove.rb +35 -0
- data/lib/sequel/extensions/server_block.rb +139 -0
- data/lib/sequel/extensions/sql_expr.rb +4 -110
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/extensions/to_dot.rb +95 -83
- data/lib/sequel/model/associations.rb +1187 -393
- data/lib/sequel/model/base.rb +578 -170
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +5 -1
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +58 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +123 -8
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +58 -6
- data/lib/sequel/plugins/list.rb +12 -2
- data/lib/sequel/plugins/many_through_many.rb +79 -28
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +139 -63
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/prepared_statements.rb +167 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/schema.rb +7 -2
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +10 -2
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +1 -1
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +526 -59
- data/lib/sequel/timezones.rb +57 -38
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +1 -1
- data/spec/adapters/mssql_spec.rb +142 -40
- data/spec/adapters/mysql_spec.rb +380 -234
- data/spec/adapters/oracle_spec.rb +67 -77
- data/spec/adapters/postgres_spec.rb +1050 -212
- data/spec/adapters/spec_helper.rb +8 -5
- data/spec/adapters/sqlite_spec.rb +139 -27
- data/spec/core/connection_pool_spec.rb +196 -93
- data/spec/core/database_spec.rb +879 -510
- data/spec/core/dataset_spec.rb +1415 -1086
- data/spec/core/expression_filters_spec.rb +554 -109
- data/spec/core/mock_adapter_spec.rb +449 -0
- data/spec/core/object_graph_spec.rb +86 -114
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +482 -38
- data/spec/core/spec_helper.rb +6 -48
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +66 -32
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/{core/core_sql_spec.rb → extensions/core_extensions_spec.rb} +119 -139
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +64 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +69 -92
- data/spec/extensions/identity_map_spec.rb +131 -22
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -6
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +48 -39
- data/spec/extensions/many_through_many_spec.rb +283 -250
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +251 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +105 -0
- data/spec/extensions/pg_array_spec.rb +196 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +136 -0
- data/spec/extensions/pg_hstore_spec.rb +195 -0
- data/spec/extensions/pg_inet_spec.rb +44 -0
- data/spec/extensions/pg_json_spec.rb +101 -0
- data/spec/extensions/pg_statement_cache_spec.rb +209 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +168 -0
- data/spec/extensions/query_spec.rb +6 -0
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +429 -22
- data/spec/extensions/schema_spec.rb +25 -21
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +85 -43
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +23 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +22 -61
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +4 -7
- data/spec/extensions/touch_spec.rb +13 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +30 -51
- data/spec/extensions/validation_helpers_spec.rb +16 -35
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +9 -3
- data/spec/integration/dataset_test.rb +605 -91
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +454 -143
- data/spec/integration/prepared_statement_test.rb +263 -128
- data/spec/integration/schema_test.rb +309 -43
- data/spec/integration/spec_helper.rb +40 -30
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +18 -4
- data/spec/model/association_reflection_spec.rb +201 -7
- data/spec/model/associations_spec.rb +1067 -661
- data/spec/model/base_spec.rb +329 -95
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +677 -676
- data/spec/model/hooks_spec.rb +349 -67
- data/spec/model/model_spec.rb +240 -168
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +490 -232
- data/spec/model/spec_helper.rb +14 -71
- data/spec/model/validations_spec.rb +11 -0
- data/spec/rcov.opts +2 -0
- metadata +178 -80
|
@@ -54,7 +54,7 @@ module Sequel
|
|
|
54
54
|
|
|
55
55
|
# The class associated to the current model class via this association
|
|
56
56
|
def associated_class
|
|
57
|
-
|
|
57
|
+
cached_fetch(:class){constantize(self[:class_name])}
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
# Whether this association can have associated objects, given the current
|
|
@@ -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
|
+
cached_fetch(:_eager_limit_strategy) do
|
|
85
|
+
if self[:limit]
|
|
86
|
+
case s = cached_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
|
|
@@ -105,16 +151,19 @@ module Sequel
|
|
|
105
151
|
# to populate reciprocal associations. For example, when you do this_artist.add_album(album)
|
|
106
152
|
# it sets album.artist to this_artist.
|
|
107
153
|
def reciprocal
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
154
|
+
cached_fetch(:reciprocal) do
|
|
155
|
+
r_types = Array(reciprocal_type)
|
|
156
|
+
keys = self[:keys]
|
|
157
|
+
recip = nil
|
|
158
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
|
159
|
+
if r_types.include?(assoc_reflect[:type]) && assoc_reflect[:keys] == keys && assoc_reflect.associated_class == self[:model]
|
|
160
|
+
cached_set(:reciprocal_type, assoc_reflect[:type])
|
|
161
|
+
recip = assoc_reflect[:name]
|
|
162
|
+
break
|
|
163
|
+
end
|
|
115
164
|
end
|
|
165
|
+
recip
|
|
116
166
|
end
|
|
117
|
-
self[:reciprocal] = nil
|
|
118
167
|
end
|
|
119
168
|
|
|
120
169
|
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
|
@@ -165,6 +214,47 @@ module Sequel
|
|
|
165
214
|
def setter_method
|
|
166
215
|
:"#{self[:name]}="
|
|
167
216
|
end
|
|
217
|
+
|
|
218
|
+
private
|
|
219
|
+
|
|
220
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE != 'ruby'
|
|
221
|
+
# On non-GVL rubies, assume the need to synchronize access. Store the key
|
|
222
|
+
# in a special sub-hash that always uses this method to synchronize access.
|
|
223
|
+
def cached_fetch(key)
|
|
224
|
+
fetch(key) do
|
|
225
|
+
h = self[:cache]
|
|
226
|
+
Sequel.synchronize{return h[key] if h.has_key?(key)}
|
|
227
|
+
value = yield
|
|
228
|
+
Sequel.synchronize{h[key] = value}
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# Cache the value at the given key, synchronizing access.
|
|
233
|
+
def cached_set(key, value)
|
|
234
|
+
h = self[:cache]
|
|
235
|
+
Sequel.synchronize{h[key] = value}
|
|
236
|
+
end
|
|
237
|
+
else
|
|
238
|
+
# On MRI, use a plain fetch, since the GVL will synchronize access.
|
|
239
|
+
def cached_fetch(key)
|
|
240
|
+
fetch(key) do
|
|
241
|
+
h = self[:cache]
|
|
242
|
+
h.fetch(key){h[key] = yield}
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# On MRI, just set the value at the key in the cache, since the GVL
|
|
247
|
+
# will synchronize access.
|
|
248
|
+
def cached_set(key, value)
|
|
249
|
+
self[:cache][key] = value
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# If +s+ is an array, map +s+ over the block. Otherwise, just call the
|
|
254
|
+
# block with +s+.
|
|
255
|
+
def transform(s)
|
|
256
|
+
s.is_a?(Array) ? s.map(&Proc.new) : (yield s)
|
|
257
|
+
end
|
|
168
258
|
end
|
|
169
259
|
|
|
170
260
|
class ManyToOneAssociationReflection < AssociationReflection
|
|
@@ -193,21 +283,43 @@ module Sequel
|
|
|
193
283
|
self[:key].nil?
|
|
194
284
|
end
|
|
195
285
|
|
|
286
|
+
# many_to_one associations don't need an eager limit strategy
|
|
287
|
+
def eager_limit_strategy
|
|
288
|
+
nil
|
|
289
|
+
end
|
|
290
|
+
|
|
196
291
|
# The key to use for the key hash when eager loading
|
|
197
292
|
def eager_loader_key
|
|
198
|
-
|
|
293
|
+
cached_fetch(:eager_loader_key){self[:key]}
|
|
199
294
|
end
|
|
200
295
|
|
|
201
296
|
# The column(s) in the associated table that the key in the current table references (either a symbol or an array).
|
|
202
297
|
def primary_key
|
|
203
|
-
|
|
298
|
+
cached_fetch(:primary_key){associated_class.primary_key}
|
|
204
299
|
end
|
|
205
300
|
|
|
206
301
|
# The columns in the associated table that the key in the current table references (always an array).
|
|
207
302
|
def primary_keys
|
|
208
|
-
|
|
303
|
+
cached_fetch(:primary_keys){Array(primary_key)}
|
|
209
304
|
end
|
|
210
305
|
alias associated_object_keys primary_keys
|
|
306
|
+
|
|
307
|
+
# The method symbol or array of method symbols to call on the associated object
|
|
308
|
+
# to get the value to use for the foreign keys.
|
|
309
|
+
def primary_key_method
|
|
310
|
+
cached_fetch(:primary_key_method){primary_key}
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
# The array of method symbols to call on the associated object
|
|
314
|
+
# to get the value to use for the foreign keys.
|
|
315
|
+
def primary_key_methods
|
|
316
|
+
cached_fetch(:primary_key_methods){Array(primary_key_method)}
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# #primary_key qualified by the associated table
|
|
320
|
+
def qualified_primary_key
|
|
321
|
+
cached_fetch(:qualified_primary_key){self[:qualify] == false ? primary_key : qualify_assoc(primary_key)}
|
|
322
|
+
end
|
|
211
323
|
|
|
212
324
|
# True only if the reciprocal is a one_to_many association.
|
|
213
325
|
def reciprocal_array?
|
|
@@ -223,7 +335,7 @@ module Sequel
|
|
|
223
335
|
# True only if the reciprocal is a one_to_one association.
|
|
224
336
|
def set_reciprocal_to_self?
|
|
225
337
|
reciprocal
|
|
226
|
-
|
|
338
|
+
reciprocal_type == :one_to_one
|
|
227
339
|
end
|
|
228
340
|
|
|
229
341
|
private
|
|
@@ -231,7 +343,7 @@ module Sequel
|
|
|
231
343
|
# The reciprocal type of a many_to_one association is either
|
|
232
344
|
# a one_to_many or a one_to_one association.
|
|
233
345
|
def reciprocal_type
|
|
234
|
-
|
|
346
|
+
cached_fetch(:reciprocal_type){[:one_to_many, :one_to_one]}
|
|
235
347
|
end
|
|
236
348
|
end
|
|
237
349
|
|
|
@@ -248,7 +360,7 @@ module Sequel
|
|
|
248
360
|
def can_have_associated_objects?(obj)
|
|
249
361
|
!self[:primary_keys].any?{|k| obj.send(k).nil?}
|
|
250
362
|
end
|
|
251
|
-
|
|
363
|
+
|
|
252
364
|
# Default foreign key name symbol for key in associated table that points to
|
|
253
365
|
# current table's primary key.
|
|
254
366
|
def default_key
|
|
@@ -257,12 +369,23 @@ module Sequel
|
|
|
257
369
|
|
|
258
370
|
# The key to use for the key hash when eager loading
|
|
259
371
|
def eager_loader_key
|
|
260
|
-
|
|
372
|
+
cached_fetch(:eager_loader_key){primary_key}
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))
|
|
376
|
+
def eager_loading_predicate_key
|
|
377
|
+
cached_fetch(:eager_loading_predicate_key){qualify_assoc(self[:key])}
|
|
261
378
|
end
|
|
379
|
+
alias qualified_key eager_loading_predicate_key
|
|
262
380
|
|
|
263
381
|
# The column in the current table that the key in the associated table references.
|
|
264
382
|
def primary_key
|
|
265
|
-
self[:primary_key]
|
|
383
|
+
self[:primary_key]
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
# #primary_key qualified by the current table
|
|
387
|
+
def qualified_primary_key
|
|
388
|
+
cached_fetch(:qualified_primary_key){qualify_cur(primary_key)}
|
|
266
389
|
end
|
|
267
390
|
|
|
268
391
|
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
|
@@ -297,6 +420,31 @@ module Sequel
|
|
|
297
420
|
class OneToOneAssociationReflection < OneToManyAssociationReflection
|
|
298
421
|
ASSOCIATION_TYPES[:one_to_one] = self
|
|
299
422
|
|
|
423
|
+
# one_to_one associations don't use an eager limit strategy by default, but
|
|
424
|
+
# support both DISTINCT ON and window functions as strategies.
|
|
425
|
+
def eager_limit_strategy
|
|
426
|
+
cached_fetch(:_eager_limit_strategy) do
|
|
427
|
+
case s = self[:eager_limit_strategy]
|
|
428
|
+
when Symbol
|
|
429
|
+
s
|
|
430
|
+
when true
|
|
431
|
+
ds = associated_class.dataset
|
|
432
|
+
if ds.supports_ordered_distinct_on?
|
|
433
|
+
:distinct_on
|
|
434
|
+
elsif ds.supports_window_functions?
|
|
435
|
+
:window_function
|
|
436
|
+
end
|
|
437
|
+
else
|
|
438
|
+
nil
|
|
439
|
+
end
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
|
|
443
|
+
# The limit and offset for this association (returned as a two element array).
|
|
444
|
+
def limit_and_offset
|
|
445
|
+
[1, nil]
|
|
446
|
+
end
|
|
447
|
+
|
|
300
448
|
# one_to_one associations return a single object, not an array
|
|
301
449
|
def returns_array?
|
|
302
450
|
false
|
|
@@ -316,13 +464,6 @@ module Sequel
|
|
|
316
464
|
self[:left_key]
|
|
317
465
|
end
|
|
318
466
|
|
|
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
467
|
# Alias of right_primary_keys
|
|
327
468
|
def associated_object_keys
|
|
328
469
|
right_primary_keys
|
|
@@ -359,7 +500,19 @@ module Sequel
|
|
|
359
500
|
|
|
360
501
|
# The key to use for the key hash when eager loading
|
|
361
502
|
def eager_loader_key
|
|
362
|
-
|
|
503
|
+
cached_fetch(:eager_loader_key){self[:left_primary_key]}
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)).
|
|
507
|
+
# The left key qualified by the join table.
|
|
508
|
+
def eager_loading_predicate_key
|
|
509
|
+
cached_fetch(:eager_loading_predicate_key){qualify(join_table_alias, self[:left_key])}
|
|
510
|
+
end
|
|
511
|
+
alias qualified_left_key eager_loading_predicate_key
|
|
512
|
+
|
|
513
|
+
# The right key qualified by the join table.
|
|
514
|
+
def qualified_right_key
|
|
515
|
+
cached_fetch(:qualified_right_key){qualify(join_table_alias, self[:right_key])}
|
|
363
516
|
end
|
|
364
517
|
|
|
365
518
|
# many_to_many associations need to select a key in an associated table to eagerly load
|
|
@@ -367,6 +520,22 @@ module Sequel
|
|
|
367
520
|
true
|
|
368
521
|
end
|
|
369
522
|
|
|
523
|
+
# The source of the join table. This is the join table itself, unless it
|
|
524
|
+
# is aliased, in which case it is the unaliased part.
|
|
525
|
+
def join_table_source
|
|
526
|
+
cached_fetch(:join_table_source){split_join_table_alias[0]}
|
|
527
|
+
end
|
|
528
|
+
|
|
529
|
+
# The join table itself, unless it is aliased, in which case this
|
|
530
|
+
# is the alias.
|
|
531
|
+
def join_table_alias
|
|
532
|
+
cached_fetch(:join_table_alias) do
|
|
533
|
+
s, a = split_join_table_alias
|
|
534
|
+
a || s
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
alias associated_key_table join_table_alias
|
|
538
|
+
|
|
370
539
|
# Whether the associated object needs a primary key to be added/removed,
|
|
371
540
|
# true for many_to_many associations.
|
|
372
541
|
def need_associated_primary_key?
|
|
@@ -375,34 +544,60 @@ module Sequel
|
|
|
375
544
|
|
|
376
545
|
# Returns the reciprocal association symbol, if one exists.
|
|
377
546
|
def reciprocal
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
547
|
+
cached_fetch(:reciprocal) do
|
|
548
|
+
left_keys = self[:left_keys]
|
|
549
|
+
right_keys = self[:right_keys]
|
|
550
|
+
join_table = self[:join_table]
|
|
551
|
+
recip = nil
|
|
552
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
|
553
|
+
if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_keys] == right_keys &&
|
|
554
|
+
assoc_reflect[:right_keys] == left_keys && assoc_reflect[:join_table] == join_table &&
|
|
555
|
+
assoc_reflect.associated_class == self[:model]
|
|
556
|
+
recip = assoc_reflect[:name]
|
|
557
|
+
break
|
|
558
|
+
end
|
|
387
559
|
end
|
|
560
|
+
recip
|
|
388
561
|
end
|
|
389
|
-
|
|
562
|
+
end
|
|
563
|
+
|
|
564
|
+
# #right_primary_key qualified by the associated table
|
|
565
|
+
def qualified_right_primary_key
|
|
566
|
+
cached_fetch(:qualified_right_primary_key){qualify_assoc(right_primary_key)}
|
|
390
567
|
end
|
|
391
568
|
|
|
392
569
|
# The primary key column(s) to use in the associated table (can be symbol or array).
|
|
393
570
|
def right_primary_key
|
|
394
|
-
|
|
571
|
+
cached_fetch(:right_primary_key){associated_class.primary_key}
|
|
395
572
|
end
|
|
396
573
|
|
|
397
574
|
# The primary key columns to use in the associated table (always array).
|
|
398
575
|
def right_primary_keys
|
|
399
|
-
|
|
576
|
+
cached_fetch(:right_primary_keys){Array(right_primary_key)}
|
|
400
577
|
end
|
|
401
578
|
|
|
579
|
+
# The method symbol or array of method symbols to call on the associated objects
|
|
580
|
+
# to get the foreign key values for the join table.
|
|
581
|
+
def right_primary_key_method
|
|
582
|
+
cached_fetch(:right_primary_key_method){right_primary_key}
|
|
583
|
+
end
|
|
584
|
+
|
|
585
|
+
# The array of method symbols to call on the associated objects
|
|
586
|
+
# to get the foreign key values for the join table.
|
|
587
|
+
def right_primary_key_methods
|
|
588
|
+
cached_fetch(:right_primary_key_methods){Array(right_primary_key_method)}
|
|
589
|
+
end
|
|
590
|
+
|
|
402
591
|
# The columns to select when loading the association, associated_class.table_name.* by default.
|
|
403
592
|
def select
|
|
404
|
-
|
|
405
|
-
|
|
593
|
+
cached_fetch(:select){Sequel::SQL::ColumnAll.new(associated_class.table_name)}
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
private
|
|
597
|
+
|
|
598
|
+
# Split the join table into source and alias parts.
|
|
599
|
+
def split_join_table_alias
|
|
600
|
+
associated_class.dataset.split_alias(self[:join_table])
|
|
406
601
|
end
|
|
407
602
|
end
|
|
408
603
|
|
|
@@ -457,6 +652,12 @@ module Sequel
|
|
|
457
652
|
# Project.association_reflection(:portfolio)
|
|
458
653
|
# => {:type => :many_to_one, :name => :portfolio, ...}
|
|
459
654
|
#
|
|
655
|
+
# Associations should not have the same names as any of the columns in the
|
|
656
|
+
# model's current table they reference. If you are dealing with an existing schema that
|
|
657
|
+
# has a column named status, you can't name the association status, you'd
|
|
658
|
+
# have to name it foo_status or something else. If you give an association the same name
|
|
659
|
+
# as a column, you will probably end up with an association that doesn't work, or a SystemStackError.
|
|
660
|
+
#
|
|
460
661
|
# For a more in depth general overview, as well as a reference guide,
|
|
461
662
|
# see the {Association Basics guide}[link:files/doc/association_basics_rdoc.html].
|
|
462
663
|
# For examples of advanced usage, see the {Advanced Associations guide}[link:files/doc/advanced_associations_rdoc.html].
|
|
@@ -464,181 +665,244 @@ module Sequel
|
|
|
464
665
|
# All association reflections defined for this model (default: {}).
|
|
465
666
|
attr_reader :association_reflections
|
|
466
667
|
|
|
668
|
+
# The default :eager_limit_strategy option to use for *_many associations (default: nil)
|
|
669
|
+
attr_accessor :default_eager_limit_strategy
|
|
670
|
+
|
|
467
671
|
# Array of all association reflections for this model class
|
|
468
672
|
def all_association_reflections
|
|
469
673
|
association_reflections.values
|
|
470
674
|
end
|
|
471
675
|
|
|
676
|
+
# Given an association reflection and a dataset, apply the
|
|
677
|
+
# :select, :conditions, :order, :eager, :distinct, and :eager_block
|
|
678
|
+
# association options to the given dataset and return the dataset
|
|
679
|
+
# or a modified copy of it.
|
|
680
|
+
def apply_association_dataset_opts(opts, ds)
|
|
681
|
+
ds = ds.select(*opts.select) if opts.select
|
|
682
|
+
if c = opts[:conditions]
|
|
683
|
+
ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
|
|
684
|
+
end
|
|
685
|
+
ds = ds.order(*opts[:order]) if opts[:order]
|
|
686
|
+
ds = ds.eager(opts[:eager]) if opts[:eager]
|
|
687
|
+
ds = ds.distinct if opts[:distinct]
|
|
688
|
+
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
689
|
+
ds
|
|
690
|
+
end
|
|
691
|
+
|
|
472
692
|
# Associates a related model with the current model. The following types are
|
|
473
693
|
# supported:
|
|
474
694
|
#
|
|
475
|
-
#
|
|
476
|
-
#
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
#
|
|
480
|
-
#
|
|
481
|
-
#
|
|
482
|
-
#
|
|
483
|
-
#
|
|
484
|
-
#
|
|
485
|
-
#
|
|
486
|
-
#
|
|
487
|
-
#
|
|
488
|
-
#
|
|
489
|
-
#
|
|
490
|
-
#
|
|
491
|
-
#
|
|
695
|
+
# :many_to_one :: Foreign key in current model's table points to
|
|
696
|
+
# associated model's primary key. Each associated model object can
|
|
697
|
+
# be associated with more than one current model objects. Each current
|
|
698
|
+
# model object can be associated with only one associated model object.
|
|
699
|
+
# :one_to_many :: Foreign key in associated model's table points to this
|
|
700
|
+
# model's primary key. Each current model object can be associated with
|
|
701
|
+
# more than one associated model objects. Each associated model object
|
|
702
|
+
# can be associated with only one current model object.
|
|
703
|
+
# :one_to_one :: Similar to one_to_many in terms of foreign keys, but
|
|
704
|
+
# only one object is associated to the current object through the
|
|
705
|
+
# association. The methods created are similar to many_to_one, except
|
|
706
|
+
# that the one_to_one setter method saves the passed object.
|
|
707
|
+
# :many_to_many :: A join table is used that has a foreign key that points
|
|
708
|
+
# to this model's primary key and a foreign key that points to the
|
|
709
|
+
# associated model's primary key. Each current model object can be
|
|
710
|
+
# associated with many associated model objects, and each associated
|
|
711
|
+
# model object can be associated with many current model objects.
|
|
492
712
|
#
|
|
493
713
|
# The following options can be supplied:
|
|
494
|
-
#
|
|
495
|
-
#
|
|
496
|
-
#
|
|
497
|
-
#
|
|
498
|
-
#
|
|
499
|
-
#
|
|
500
|
-
#
|
|
501
|
-
#
|
|
502
|
-
#
|
|
503
|
-
#
|
|
504
|
-
#
|
|
505
|
-
#
|
|
506
|
-
#
|
|
507
|
-
#
|
|
508
|
-
#
|
|
509
|
-
#
|
|
510
|
-
#
|
|
511
|
-
#
|
|
512
|
-
#
|
|
513
|
-
#
|
|
514
|
-
#
|
|
515
|
-
#
|
|
516
|
-
#
|
|
517
|
-
#
|
|
518
|
-
#
|
|
519
|
-
#
|
|
520
|
-
#
|
|
521
|
-
#
|
|
522
|
-
#
|
|
523
|
-
#
|
|
524
|
-
#
|
|
525
|
-
#
|
|
526
|
-
#
|
|
527
|
-
#
|
|
528
|
-
#
|
|
529
|
-
#
|
|
530
|
-
#
|
|
531
|
-
#
|
|
532
|
-
#
|
|
533
|
-
#
|
|
534
|
-
#
|
|
535
|
-
#
|
|
536
|
-
#
|
|
537
|
-
#
|
|
538
|
-
#
|
|
539
|
-
#
|
|
540
|
-
#
|
|
541
|
-
#
|
|
542
|
-
#
|
|
543
|
-
#
|
|
544
|
-
#
|
|
545
|
-
#
|
|
546
|
-
#
|
|
547
|
-
#
|
|
548
|
-
#
|
|
549
|
-
#
|
|
550
|
-
#
|
|
551
|
-
#
|
|
552
|
-
#
|
|
553
|
-
#
|
|
554
|
-
#
|
|
555
|
-
#
|
|
556
|
-
#
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
560
|
-
#
|
|
561
|
-
#
|
|
562
|
-
#
|
|
563
|
-
#
|
|
564
|
-
#
|
|
565
|
-
#
|
|
566
|
-
#
|
|
567
|
-
#
|
|
568
|
-
#
|
|
569
|
-
#
|
|
570
|
-
#
|
|
571
|
-
#
|
|
572
|
-
#
|
|
573
|
-
#
|
|
574
|
-
#
|
|
575
|
-
#
|
|
576
|
-
#
|
|
577
|
-
#
|
|
578
|
-
#
|
|
579
|
-
#
|
|
580
|
-
#
|
|
581
|
-
#
|
|
582
|
-
#
|
|
583
|
-
#
|
|
584
|
-
#
|
|
585
|
-
#
|
|
586
|
-
#
|
|
587
|
-
#
|
|
588
|
-
#
|
|
589
|
-
#
|
|
590
|
-
#
|
|
591
|
-
#
|
|
592
|
-
#
|
|
593
|
-
#
|
|
594
|
-
#
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
#
|
|
598
|
-
#
|
|
599
|
-
#
|
|
600
|
-
#
|
|
601
|
-
#
|
|
602
|
-
#
|
|
603
|
-
#
|
|
604
|
-
#
|
|
605
|
-
#
|
|
606
|
-
#
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
#
|
|
610
|
-
#
|
|
611
|
-
#
|
|
612
|
-
#
|
|
613
|
-
#
|
|
614
|
-
#
|
|
615
|
-
#
|
|
616
|
-
#
|
|
617
|
-
#
|
|
618
|
-
#
|
|
619
|
-
#
|
|
620
|
-
#
|
|
621
|
-
#
|
|
622
|
-
#
|
|
623
|
-
#
|
|
624
|
-
#
|
|
625
|
-
#
|
|
714
|
+
# === All Types
|
|
715
|
+
# :after_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
716
|
+
# after a new item is added to the association.
|
|
717
|
+
# :after_load :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
718
|
+
# after the associated record(s) have been retrieved from the database.
|
|
719
|
+
# :after_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
720
|
+
# after an item is removed from the association.
|
|
721
|
+
# :after_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
722
|
+
# after an item is set using the association setter method.
|
|
723
|
+
# :allow_eager :: If set to false, you cannot load the association eagerly
|
|
724
|
+
# via eager or eager_graph
|
|
725
|
+
# :before_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
726
|
+
# before a new item is added to the association.
|
|
727
|
+
# :before_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
728
|
+
# before an item is removed from the association.
|
|
729
|
+
# :before_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
730
|
+
# before an item is set using the association setter method.
|
|
731
|
+
# :cartesian_product_number :: the number of joins completed by this association that could cause more
|
|
732
|
+
# than one row for each row in the current table (default: 0 for
|
|
733
|
+
# many_to_one and one_to_one associations, 1 for one_to_many and
|
|
734
|
+
# many_to_many associations).
|
|
735
|
+
# :class :: The associated class or its name. If not
|
|
736
|
+
# given, uses the association's name, which is camelized (and
|
|
737
|
+
# singularized unless the type is :many_to_one or :one_to_one)
|
|
738
|
+
# :clone :: Merge the current options and block into the options and block used in defining
|
|
739
|
+
# the given association. Can be used to DRY up a bunch of similar associations that
|
|
740
|
+
# all share the same options such as :class and :key, while changing the order and block used.
|
|
741
|
+
# :conditions :: The conditions to use to filter the association, can be any argument passed to filter.
|
|
742
|
+
# :dataset :: A proc that is instance_evaled to get the base dataset
|
|
743
|
+
# to use for the _dataset method (before the other options are applied).
|
|
744
|
+
# :distinct :: Use the DISTINCT clause when selecting associating object, both when
|
|
745
|
+
# lazy loading and eager loading via .eager (but not when using .eager_graph).
|
|
746
|
+
# :eager :: The associations to eagerly load via +eager+ when loading the associated object(s).
|
|
747
|
+
# :eager_block :: If given, use the block instead of the default block when
|
|
748
|
+
# eagerly loading. To not use a block when eager loading (when one is used normally),
|
|
749
|
+
# set to nil.
|
|
750
|
+
# :eager_graph :: The associations to eagerly load via +eager_graph+ when loading the associated object(s).
|
|
751
|
+
# many_to_many associations with this option cannot be eagerly loaded via +eager+.
|
|
752
|
+
# :eager_grapher :: A proc to use to implement eager loading via +eager_graph+, overriding the default.
|
|
753
|
+
# Takes one or three arguments. If three arguments, they are a dataset, an alias to use for
|
|
754
|
+
# the table to graph for this association, and the alias that was used for the current table
|
|
755
|
+
# (since you can cascade associations). If one argument, is passed a hash with keys :self,
|
|
756
|
+
# :table_alias, and :implicit_qualifier, corresponding to the three arguments, and an optional
|
|
757
|
+
# additional key :eager_block, a callback accepting one argument, the associated dataset. This
|
|
758
|
+
# is used to customize the association at query time.
|
|
759
|
+
# Should return a copy of the dataset with the association graphed into it.
|
|
760
|
+
# :eager_limit_strategy :: Determines the strategy used for enforcing limits when eager loading associations via
|
|
761
|
+
# the +eager+ method. For one_to_one associations, no strategy is used by default, and
|
|
762
|
+
# for *_many associations, the :ruby strategy is used by default, which still retrieves
|
|
763
|
+
# all records but slices the resulting array after the association is retrieved. You
|
|
764
|
+
# can pass a +true+ value for this option to have Sequel pick what it thinks is the best
|
|
765
|
+
# choice for the database, or specify a specific symbol to manually select a strategy.
|
|
766
|
+
# one_to_one associations support :distinct_on, :window_function, and :correlated_subquery.
|
|
767
|
+
# *_many associations support :ruby, :window_function, and :correlated_subquery.
|
|
768
|
+
# :eager_loader :: A proc to use to implement eager loading, overriding the default. Takes one or three arguments.
|
|
769
|
+
# If three arguments, the first should be a key hash (used solely to enhance performance), the
|
|
770
|
+
# second an array of records, and the third a hash of dependent associations. If one argument, is
|
|
771
|
+
# passed a hash with keys :key_hash, :rows, and :associations, corresponding to the three
|
|
772
|
+
# arguments, and an additional key :self, which is the dataset doing the eager loading. In the
|
|
773
|
+
# proc, the associated records should be queried from the database and the associations cache for
|
|
774
|
+
# each record should be populated.
|
|
775
|
+
# :eager_loader_key :: A symbol for the key column to use to populate the key hash
|
|
776
|
+
# for the eager loader.
|
|
777
|
+
# :extend :: A module or array of modules to extend the dataset with.
|
|
778
|
+
# :graph_alias_base :: The base name to use for the table alias when eager graphing. Defaults to the name
|
|
779
|
+
# of the association. If the alias name has already been used in the query, Sequel will create
|
|
780
|
+
# a unique alias by appending a numeric suffix (e.g. alias_0, alias_1, ...) until the alias is
|
|
781
|
+
# unique.
|
|
782
|
+
# :graph_block :: The block to pass to join_table when eagerly loading
|
|
783
|
+
# the association via +eager_graph+.
|
|
784
|
+
# :graph_conditions :: The additional conditions to use on the SQL join when eagerly loading
|
|
785
|
+
# the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
|
|
786
|
+
# specified, the :conditions option is used if it is a hash or array of two element arrays.
|
|
787
|
+
# :graph_join_type :: The type of SQL join to use when eagerly loading the association via
|
|
788
|
+
# eager_graph. Defaults to :left_outer.
|
|
789
|
+
# :graph_only_conditions :: The conditions to use on the SQL join when eagerly loading
|
|
790
|
+
# the association via +eager_graph+, instead of the default conditions specified by the
|
|
791
|
+
# foreign/primary keys. This option causes the :graph_conditions option to be ignored.
|
|
792
|
+
# :graph_select :: A column or array of columns to select from the associated table
|
|
793
|
+
# when eagerly loading the association via +eager_graph+. Defaults to all
|
|
794
|
+
# columns in the associated table.
|
|
795
|
+
# :limit :: Limit the number of records to the provided value. Use
|
|
796
|
+
# an array with two elements for the value to specify a
|
|
797
|
+
# limit (first element) and an offset (second element).
|
|
798
|
+
# :methods_module :: The module that methods the association creates will be placed into. Defaults
|
|
799
|
+
# to the module containing the model's columns.
|
|
800
|
+
# :order :: the column(s) by which to order the association dataset. Can be a
|
|
801
|
+
# singular column symbol or an array of column symbols.
|
|
802
|
+
# :order_eager_graph :: Whether to add the association's order to the graphed dataset's order when graphing
|
|
803
|
+
# via +eager_graph+. Defaults to true, so set to false to disable.
|
|
804
|
+
# :read_only :: Do not add a setter method (for many_to_one or one_to_one associations),
|
|
805
|
+
# or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
|
|
806
|
+
# :reciprocal :: the symbol name of the reciprocal association,
|
|
807
|
+
# if it exists. By default, Sequel will try to determine it by looking at the
|
|
808
|
+
# associated model's assocations for a association that matches
|
|
809
|
+
# the current association's key(s). Set to nil to not use a reciprocal.
|
|
810
|
+
# :select :: the columns to select. Defaults to the associated class's
|
|
811
|
+
# table_name.* in a many_to_many association, which means it doesn't include the attributes from the
|
|
812
|
+
# join table. If you want to include the join table attributes, you can
|
|
813
|
+
# use this option, but beware that the join table attributes can clash with
|
|
814
|
+
# attributes from the model table, so you should alias any attributes that have
|
|
815
|
+
# the same name in both the join table and the associated table.
|
|
816
|
+
# :validate :: Set to false to not validate when implicitly saving any associated object.
|
|
817
|
+
# === :many_to_one
|
|
818
|
+
# :key :: foreign key in current model's table that references
|
|
819
|
+
# associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
|
|
820
|
+
# array of symbols for a composite key association.
|
|
821
|
+
# :key_column :: Similar to, and usually identical to, :key, but :key refers to the model method
|
|
822
|
+
# to call, where :key_column refers to the underlying column. Should only be
|
|
823
|
+
# used if the the model method differs from the foreign key column, in conjunction
|
|
824
|
+
# with defining a model alias method for the key column.
|
|
825
|
+
# :primary_key :: column in the associated table that :key option references, as a symbol.
|
|
826
|
+
# Defaults to the primary key of the associated table. Can use an
|
|
827
|
+
# array of symbols for a composite key association.
|
|
828
|
+
# :primary_key_method :: the method symbol or array of method symbols to call on the associated
|
|
829
|
+
# object to get the foreign key values. Defaults to :primary_key option.
|
|
830
|
+
# :qualify :: Whether to use qualifier primary keys when loading the association. The default
|
|
831
|
+
# is true, so you must set to false to not qualify. Qualification rarely causes
|
|
832
|
+
# problems, but it's necessary to disable in some cases, such as when you are doing
|
|
833
|
+
# a JOIN USING operation on the column on Oracle.
|
|
834
|
+
# === :one_to_many and :one_to_one
|
|
835
|
+
# :key :: foreign key in associated model's table that references
|
|
836
|
+
# current model's primary key, as a symbol. Defaults to
|
|
837
|
+
# :"#{self.name.underscore}_id". Can use an
|
|
838
|
+
# array of symbols for a composite key association.
|
|
839
|
+
# :key_method :: the method symbol or array of method symbols to call on the associated
|
|
840
|
+
# object to get the foreign key values. Defaults to :key option.
|
|
841
|
+
# :primary_key :: column in the current table that :key option references, as a symbol.
|
|
842
|
+
# Defaults to primary key of the current table. Can use an
|
|
843
|
+
# array of symbols for a composite key association.
|
|
844
|
+
# :primary_key_column :: Similar to, and usually identical to, :primary_key, but :primary_key refers
|
|
845
|
+
# to the model method call, where :primary_key_column refers to the underlying column.
|
|
846
|
+
# Should only be used if the the model method differs from the primary key column, in
|
|
847
|
+
# conjunction with defining a model alias method for the primary key column.
|
|
848
|
+
# === :many_to_many
|
|
849
|
+
# :graph_join_table_block :: The block to pass to +join_table+ for
|
|
850
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
851
|
+
# :graph_join_table_conditions :: The additional conditions to use on the SQL join for
|
|
852
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
853
|
+
# Should be a hash or an array of two element arrays.
|
|
854
|
+
# :graph_join_table_join_type :: The type of SQL join to use for the join table when eagerly
|
|
855
|
+
# loading the association via +eager_graph+. Defaults to the
|
|
856
|
+
# :graph_join_type option or :left_outer.
|
|
857
|
+
# :graph_join_table_only_conditions :: The conditions to use on the SQL join for the join
|
|
858
|
+
# table when eagerly loading the association via +eager_graph+,
|
|
859
|
+
# instead of the default conditions specified by the
|
|
860
|
+
# foreign/primary keys. This option causes the
|
|
861
|
+
# :graph_join_table_conditions option to be ignored.
|
|
862
|
+
# :join_table :: name of table that includes the foreign keys to both
|
|
863
|
+
# the current model and the associated model, as a symbol. Defaults to the name
|
|
864
|
+
# of current model and name of associated model, pluralized,
|
|
865
|
+
# underscored, sorted, and joined with '_'.
|
|
866
|
+
# :join_table_block :: proc that can be used to modify the dataset used in the add/remove/remove_all
|
|
867
|
+
# methods. Should accept a dataset argument and return a modified dataset if present.
|
|
868
|
+
# :left_key :: foreign key in join table that points to current model's
|
|
869
|
+
# primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
|
|
870
|
+
# Can use an array of symbols for a composite key association.
|
|
871
|
+
# :left_primary_key :: column in current table that :left_key points to, as a symbol.
|
|
872
|
+
# Defaults to primary key of current table. Can use an
|
|
873
|
+
# array of symbols for a composite key association.
|
|
874
|
+
# :left_primary_key_column :: Similar to, and usually identical to, :left_primary_key, but :left_primary_key refers to
|
|
875
|
+
# the model method to call, where :left_primary_key_column refers to the underlying column. Should only
|
|
876
|
+
# be used if the model method differs from the left primary key column, in conjunction
|
|
877
|
+
# with defining a model alias method for the left primary key column.
|
|
878
|
+
# :right_key :: foreign key in join table that points to associated
|
|
879
|
+
# model's primary key, as a symbol. Defaults to :"#{name.to_s.singularize}_id".
|
|
880
|
+
# Can use an array of symbols for a composite key association.
|
|
881
|
+
# :right_primary_key :: column in associated table that :right_key points to, as a symbol.
|
|
882
|
+
# Defaults to primary key of the associated table. Can use an
|
|
883
|
+
# array of symbols for a composite key association.
|
|
884
|
+
# :right_primary_key_method :: the method symbol or array of method symbols to call on the associated
|
|
885
|
+
# object to get the foreign key values for the join table.
|
|
886
|
+
# Defaults to :right_primary_key option.
|
|
887
|
+
# :uniq :: Adds a after_load callback that makes the array of objects unique.
|
|
626
888
|
def associate(type, name, opts = {}, &block)
|
|
627
889
|
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
890
|
raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type]
|
|
629
891
|
raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name
|
|
630
892
|
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
|
-
|
|
893
|
+
raise(Error, ':eager_grapher option must have an arity of 1 or 3') if opts[:eager_grapher] && ![1, 3].include?(opts[:eager_grapher].arity)
|
|
894
|
+
|
|
632
895
|
# dup early so we don't modify opts
|
|
633
896
|
orig_opts = opts.dup
|
|
634
897
|
orig_opts = association_reflection(opts[:clone])[:orig_opts].merge(orig_opts) if opts[:clone]
|
|
635
|
-
opts = orig_opts.merge(:type => type, :name => name, :cache
|
|
898
|
+
opts = orig_opts.merge(:type => type, :name => name, :cache=>{}, :model => self)
|
|
636
899
|
opts[:block] = block if block
|
|
637
900
|
opts = assoc_class.new.merge!(opts)
|
|
638
901
|
opts[:eager_block] = block unless opts.include?(:eager_block)
|
|
639
902
|
opts[:graph_join_type] ||= :left_outer
|
|
640
903
|
opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph)
|
|
641
904
|
conds = opts[:conditions]
|
|
905
|
+
opts[:graph_alias_base] ||= name
|
|
642
906
|
opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds)
|
|
643
907
|
opts[:graph_conditions] = opts.fetch(:graph_conditions, []).to_a
|
|
644
908
|
opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select]
|
|
@@ -647,6 +911,9 @@ module Sequel
|
|
|
647
911
|
end
|
|
648
912
|
late_binding_class_option(opts, opts.returns_array? ? singularize(name) : name)
|
|
649
913
|
|
|
914
|
+
# Remove :class entry if it exists and is nil, to work with cached_fetch
|
|
915
|
+
opts.delete(:class) unless opts[:class]
|
|
916
|
+
|
|
650
917
|
send(:"def_#{type}", opts)
|
|
651
918
|
|
|
652
919
|
orig_opts.delete(:clone)
|
|
@@ -668,57 +935,120 @@ module Sequel
|
|
|
668
935
|
|
|
669
936
|
# Modify and return eager loading dataset based on association options.
|
|
670
937
|
def eager_loading_dataset(opts, ds, select, associations, eager_options={})
|
|
938
|
+
ds = apply_association_dataset_opts(opts, ds)
|
|
671
939
|
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
940
|
if opts[:eager_graph]
|
|
941
|
+
raise(Error, "cannot eagerly load a #{opts[:type]} association that uses :eager_graph") if opts.eager_loading_use_associated_key?
|
|
679
942
|
ds = ds.eager_graph(opts[:eager_graph])
|
|
680
|
-
|
|
681
|
-
|
|
943
|
+
end
|
|
944
|
+
ds = ds.eager(associations) unless Array(associations).empty?
|
|
945
|
+
ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
|
|
946
|
+
if opts.eager_loading_use_associated_key?
|
|
682
947
|
ds = if opts[:uses_left_composite_keys]
|
|
683
|
-
|
|
684
|
-
ds.select_more(*opts.associated_key_alias.zip(opts.associated_key_column).map{|a, c| SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(t, c), a)})
|
|
948
|
+
ds.select_append(*opts.associated_key_alias.zip(opts.eager_loading_predicate_key).map{|a, k| SQL::AliasedExpression.new(k, a)})
|
|
685
949
|
else
|
|
686
|
-
ds.
|
|
950
|
+
ds.select_append(SQL::AliasedExpression.new(opts.eager_loading_predicate_key, opts.associated_key_alias))
|
|
687
951
|
end
|
|
688
952
|
end
|
|
689
|
-
ds = ds.eager(associations) unless Array(associations).empty?
|
|
690
|
-
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
691
953
|
ds
|
|
692
954
|
end
|
|
693
955
|
|
|
694
956
|
# Copy the association reflections to the subclass
|
|
695
957
|
def inherited(subclass)
|
|
696
958
|
super
|
|
697
|
-
subclass.instance_variable_set(:@association_reflections,
|
|
959
|
+
subclass.instance_variable_set(:@association_reflections, association_reflections.dup)
|
|
960
|
+
subclass.default_eager_limit_strategy = default_eager_limit_strategy
|
|
698
961
|
end
|
|
699
962
|
|
|
700
|
-
# Shortcut for adding a many_to_many association, see associate
|
|
963
|
+
# Shortcut for adding a many_to_many association, see #associate
|
|
701
964
|
def many_to_many(name, opts={}, &block)
|
|
702
965
|
associate(:many_to_many, name, opts, &block)
|
|
703
966
|
end
|
|
704
967
|
|
|
705
|
-
# Shortcut for adding a many_to_one association, see associate
|
|
968
|
+
# Shortcut for adding a many_to_one association, see #associate
|
|
706
969
|
def many_to_one(name, opts={}, &block)
|
|
707
970
|
associate(:many_to_one, name, opts, &block)
|
|
708
971
|
end
|
|
709
972
|
|
|
710
|
-
# Shortcut for adding a one_to_many association, see associate
|
|
973
|
+
# Shortcut for adding a one_to_many association, see #associate
|
|
711
974
|
def one_to_many(name, opts={}, &block)
|
|
712
975
|
associate(:one_to_many, name, opts, &block)
|
|
713
976
|
end
|
|
714
977
|
|
|
715
|
-
# Shortcut for adding a one_to_one association, see associate.
|
|
978
|
+
# Shortcut for adding a one_to_one association, see #associate.
|
|
716
979
|
def one_to_one(name, opts={}, &block)
|
|
717
980
|
associate(:one_to_one, name, opts, &block)
|
|
718
981
|
end
|
|
719
982
|
|
|
720
983
|
private
|
|
721
984
|
|
|
985
|
+
# Use a correlated subquery to limit the results of the eager loading dataset.
|
|
986
|
+
def apply_correlated_subquery_eager_limit_strategy(ds, opts)
|
|
987
|
+
klass = opts.associated_class
|
|
988
|
+
kds = klass.dataset
|
|
989
|
+
dsa = ds.send(:dataset_alias, 1)
|
|
990
|
+
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
|
|
991
|
+
pka = Array(pk)
|
|
992
|
+
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?
|
|
993
|
+
table = kds.opts[:from]
|
|
994
|
+
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
|
|
995
|
+
table = table.first
|
|
996
|
+
if order = ds.opts[:order]
|
|
997
|
+
oproc = lambda do |x|
|
|
998
|
+
case x
|
|
999
|
+
when Symbol
|
|
1000
|
+
t, c, a = ds.send(:split_symbol, x)
|
|
1001
|
+
if t && t.to_sym == table
|
|
1002
|
+
SQL::QualifiedIdentifier.new(dsa, c)
|
|
1003
|
+
else
|
|
1004
|
+
x
|
|
1005
|
+
end
|
|
1006
|
+
when SQL::QualifiedIdentifier
|
|
1007
|
+
if x.table == table
|
|
1008
|
+
SQL::QualifiedIdentifier.new(dsa, x.column)
|
|
1009
|
+
else
|
|
1010
|
+
x
|
|
1011
|
+
end
|
|
1012
|
+
when SQL::OrderedExpression
|
|
1013
|
+
SQL::OrderedExpression.new(oproc.call(x.expression), x.descending, :nulls=>x.nulls)
|
|
1014
|
+
else
|
|
1015
|
+
x
|
|
1016
|
+
end
|
|
1017
|
+
end
|
|
1018
|
+
order = order.map(&oproc)
|
|
1019
|
+
end
|
|
1020
|
+
limit, offset = opts.limit_and_offset
|
|
1021
|
+
|
|
1022
|
+
subquery = yield kds.
|
|
1023
|
+
unlimited.
|
|
1024
|
+
from(SQL::AliasedExpression.new(table, dsa)).
|
|
1025
|
+
select(*pka.map{|k| SQL::QualifiedIdentifier.new(dsa, k)}).
|
|
1026
|
+
order(*order).
|
|
1027
|
+
limit(limit, offset)
|
|
1028
|
+
|
|
1029
|
+
pk = if pk.is_a?(Array)
|
|
1030
|
+
pk.map{|k| SQL::QualifiedIdentifier.new(table, k)}
|
|
1031
|
+
else
|
|
1032
|
+
SQL::QualifiedIdentifier.new(table, pk)
|
|
1033
|
+
end
|
|
1034
|
+
ds.filter(pk=>subquery)
|
|
1035
|
+
end
|
|
1036
|
+
|
|
1037
|
+
# Use a window function to limit the results of the eager loading dataset.
|
|
1038
|
+
def apply_window_function_eager_limit_strategy(ds, opts)
|
|
1039
|
+
rn = ds.row_number_column
|
|
1040
|
+
limit, offset = opts.limit_and_offset
|
|
1041
|
+
ds = ds.unordered.select_append{row_number(:over, :partition=>opts.eager_loading_predicate_key, :order=>ds.opts[:order]){}.as(rn)}.from_self
|
|
1042
|
+
ds = if opts[:type] == :one_to_one
|
|
1043
|
+
ds.where(rn => 1)
|
|
1044
|
+
elsif offset
|
|
1045
|
+
offset += 1
|
|
1046
|
+
ds.where(rn => (offset...(offset+limit)))
|
|
1047
|
+
else
|
|
1048
|
+
ds.where{SQL::Identifier.new(rn) <= limit}
|
|
1049
|
+
end
|
|
1050
|
+
end
|
|
1051
|
+
|
|
722
1052
|
# The module to use for the association's methods. Defaults to
|
|
723
1053
|
# the overridable_methods_module.
|
|
724
1054
|
def association_module(opts={})
|
|
@@ -753,9 +1083,9 @@ module Sequel
|
|
|
753
1083
|
def_association_method(opts)
|
|
754
1084
|
end
|
|
755
1085
|
|
|
756
|
-
# Adds the association method to the association methods module.
|
|
1086
|
+
# Adds the association method to the association methods module.
|
|
757
1087
|
def def_association_method(opts)
|
|
758
|
-
association_module_def(opts.association_method, opts){|*
|
|
1088
|
+
association_module_def(opts.association_method, opts){|*dynamic_opts, &block| load_associated_objects(opts, dynamic_opts[0], &block)}
|
|
759
1089
|
end
|
|
760
1090
|
|
|
761
1091
|
# Configures many_to_many association reflection and adds the related association methods
|
|
@@ -768,6 +1098,9 @@ module Sequel
|
|
|
768
1098
|
rcks = opts[:right_keys] = Array(right)
|
|
769
1099
|
left_pk = (opts[:left_primary_key] ||= self.primary_key)
|
|
770
1100
|
lcpks = opts[:left_primary_keys] = Array(left_pk)
|
|
1101
|
+
lpkc = opts[:left_primary_key_column] ||= left_pk
|
|
1102
|
+
lpkcs = opts[:left_primary_key_columns] ||= Array(lpkc)
|
|
1103
|
+
elk = opts.eager_loader_key
|
|
771
1104
|
raise(Error, "mismatched number of left composite keys: #{lcks.inspect} vs #{lcpks.inspect}") unless lcks.length == lcpks.length
|
|
772
1105
|
if opts[:right_primary_key]
|
|
773
1106
|
rcpks = Array(opts[:right_primary_key])
|
|
@@ -782,14 +1115,27 @@ module Sequel
|
|
|
782
1115
|
opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
|
|
783
1116
|
opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
|
|
784
1117
|
opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) + lcks.zip(lcpks.map{|k| send(k)}))}
|
|
785
|
-
|
|
786
|
-
|
|
1118
|
+
|
|
787
1119
|
opts[:eager_loader] ||= proc do |eo|
|
|
788
|
-
h = eo[:key_hash][
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1120
|
+
h = eo[:key_hash][elk]
|
|
1121
|
+
rows = eo[:rows]
|
|
1122
|
+
rows.each{|object| object.associations[name] = []}
|
|
1123
|
+
r = rcks.zip(opts.right_primary_keys)
|
|
1124
|
+
l = [[opts.qualify(opts.join_table_alias, left), h.keys]]
|
|
1125
|
+
ds = model.eager_loading_dataset(opts, opts.associated_class.inner_join(join_table, r + l), nil, eo[:associations], eo)
|
|
1126
|
+
case opts.eager_limit_strategy
|
|
1127
|
+
when :window_function
|
|
1128
|
+
delete_rn = true
|
|
1129
|
+
rn = ds.row_number_column
|
|
1130
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1131
|
+
when :correlated_subquery
|
|
1132
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1133
|
+
dsa = ds.send(:dataset_alias, 2)
|
|
1134
|
+
xds.inner_join(join_table, r + lcks.map{|k| [k, SQL::QualifiedIdentifier.new(opts.join_table_alias, k)]}, :table_alias=>dsa)
|
|
1135
|
+
end
|
|
1136
|
+
end
|
|
1137
|
+
ds.all do |assoc_record|
|
|
1138
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
793
1139
|
hash_key = if uses_lcks
|
|
794
1140
|
left_key_alias.map{|k| assoc_record.values.delete(k)}
|
|
795
1141
|
else
|
|
@@ -798,6 +1144,10 @@ module Sequel
|
|
|
798
1144
|
next unless objects = h[hash_key]
|
|
799
1145
|
objects.each{|object| object.associations[name].push(assoc_record)}
|
|
800
1146
|
end
|
|
1147
|
+
if opts.eager_limit_strategy == :ruby
|
|
1148
|
+
limit, offset = opts.limit_and_offset
|
|
1149
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1150
|
+
end
|
|
801
1151
|
end
|
|
802
1152
|
|
|
803
1153
|
join_type = opts[:graph_join_type]
|
|
@@ -810,9 +1160,10 @@ module Sequel
|
|
|
810
1160
|
jt_only_conditions = opts[:graph_join_table_only_conditions]
|
|
811
1161
|
jt_join_type = opts[:graph_join_table_join_type]
|
|
812
1162
|
jt_graph_block = opts[:graph_join_table_block]
|
|
813
|
-
opts[:eager_grapher] ||= proc do |
|
|
814
|
-
ds =
|
|
815
|
-
ds.graph(
|
|
1163
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1164
|
+
ds = eo[:self]
|
|
1165
|
+
ds = ds.graph(join_table, use_jt_only_conditions ? jt_only_conditions : lcks.zip(lpkcs) + graph_jt_conds, :select=>false, :table_alias=>ds.unused_table_alias(join_table, [eo[:table_alias]]), :join_type=>jt_join_type, :implicit_qualifier=>eo[:implicit_qualifier], :from_self_alias=>ds.opts[:eager_graph][:master], &jt_graph_block)
|
|
1166
|
+
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
1167
|
end
|
|
817
1168
|
|
|
818
1169
|
def_association_dataset_methods(opts)
|
|
@@ -822,11 +1173,11 @@ module Sequel
|
|
|
822
1173
|
association_module_private_def(opts._add_method, opts) do |o|
|
|
823
1174
|
h = {}
|
|
824
1175
|
lcks.zip(lcpks).each{|k, pk| h[k] = send(pk)}
|
|
825
|
-
rcks.zip(opts.
|
|
1176
|
+
rcks.zip(opts.right_primary_key_methods).each{|k, pk| h[k] = o.send(pk)}
|
|
826
1177
|
_join_table_dataset(opts).insert(h)
|
|
827
1178
|
end
|
|
828
1179
|
association_module_private_def(opts._remove_method, opts) do |o|
|
|
829
|
-
_join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)}) + rcks.zip(opts.
|
|
1180
|
+
_join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)}) + rcks.zip(opts.right_primary_key_methods.map{|k| o.send(k)})).delete
|
|
830
1181
|
end
|
|
831
1182
|
association_module_private_def(opts._remove_all_method, opts) do
|
|
832
1183
|
_join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)})).delete
|
|
@@ -841,20 +1192,27 @@ module Sequel
|
|
|
841
1192
|
name = opts[:name]
|
|
842
1193
|
model = self
|
|
843
1194
|
opts[:key] = opts.default_key unless opts.include?(:key)
|
|
844
|
-
key = opts[:key]
|
|
845
|
-
cks = opts[:keys] = Array(
|
|
1195
|
+
key_column = key = opts[:key]
|
|
1196
|
+
cks = opts[:graph_keys] = opts[:keys] = Array(key)
|
|
1197
|
+
if opts[:key_column]
|
|
1198
|
+
key_column = opts[:key_column]
|
|
1199
|
+
opts[:eager_loader_key] ||= key_column
|
|
1200
|
+
opts[:graph_keys] = Array(key_column)
|
|
1201
|
+
end
|
|
1202
|
+
opts[:qualified_key] = opts.qualify_cur(key)
|
|
846
1203
|
if opts[:primary_key]
|
|
847
1204
|
cpks = Array(opts[:primary_key])
|
|
848
1205
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
849
1206
|
end
|
|
850
1207
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
1208
|
+
qualify = opts[:qualify] != false
|
|
851
1209
|
opts[:cartesian_product_number] ||= 0
|
|
852
1210
|
opts[:dataset] ||= proc do
|
|
853
1211
|
klass = opts.associated_class
|
|
854
|
-
klass.filter(opts.
|
|
1212
|
+
klass.filter(Array(opts.qualified_primary_key).zip(cks.map{|k| send(k)}))
|
|
855
1213
|
end
|
|
856
1214
|
opts[:eager_loader] ||= proc do |eo|
|
|
857
|
-
h = eo[:key_hash][
|
|
1215
|
+
h = eo[:key_hash][key_column]
|
|
858
1216
|
keys = h.keys
|
|
859
1217
|
# Default the cached association to nil, so any object that doesn't have it
|
|
860
1218
|
# populated will have cached the negative lookup.
|
|
@@ -862,8 +1220,8 @@ module Sequel
|
|
|
862
1220
|
# Skip eager loading if no objects have a foreign key for this association
|
|
863
1221
|
unless keys.empty?
|
|
864
1222
|
klass = opts.associated_class
|
|
865
|
-
model.eager_loading_dataset(opts, klass.filter(
|
|
866
|
-
hash_key = uses_cks ? opts.
|
|
1223
|
+
model.eager_loading_dataset(opts, klass.filter(opts.qualified_primary_key=>keys), nil, eo[:associations], eo).all do |assoc_record|
|
|
1224
|
+
hash_key = uses_cks ? opts.primary_key_methods.map{|k| assoc_record.send(k)} : assoc_record.send(opts.primary_key_method)
|
|
867
1225
|
next unless objects = h[hash_key]
|
|
868
1226
|
objects.each{|object| object.associations[name] = assoc_record}
|
|
869
1227
|
end
|
|
@@ -876,15 +1234,17 @@ module Sequel
|
|
|
876
1234
|
only_conditions = opts[:graph_only_conditions]
|
|
877
1235
|
conditions = opts[:graph_conditions]
|
|
878
1236
|
graph_block = opts[:graph_block]
|
|
879
|
-
opts[:
|
|
880
|
-
|
|
1237
|
+
graph_cks = opts[:graph_keys]
|
|
1238
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1239
|
+
ds = eo[:self]
|
|
1240
|
+
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
1241
|
end
|
|
882
1242
|
|
|
883
1243
|
def_association_dataset_methods(opts)
|
|
884
1244
|
|
|
885
1245
|
return if opts[:read_only]
|
|
886
1246
|
|
|
887
|
-
association_module_private_def(opts._setter_method, opts){|o| cks.zip(opts.
|
|
1247
|
+
association_module_private_def(opts._setter_method, opts){|o| cks.zip(opts.primary_key_methods).each{|k, pk| send(:"#{k}=", (o.send(pk) if o))}}
|
|
888
1248
|
association_module_def(opts.setter_method, opts){|o| set_associated_object(opts, o)}
|
|
889
1249
|
end
|
|
890
1250
|
|
|
@@ -894,31 +1254,52 @@ module Sequel
|
|
|
894
1254
|
name = opts[:name]
|
|
895
1255
|
model = self
|
|
896
1256
|
key = (opts[:key] ||= opts.default_key)
|
|
1257
|
+
km = opts[:key_method] ||= opts[:key]
|
|
897
1258
|
cks = opts[:keys] = Array(key)
|
|
898
1259
|
primary_key = (opts[:primary_key] ||= self.primary_key)
|
|
899
1260
|
cpks = opts[:primary_keys] = Array(primary_key)
|
|
1261
|
+
pkc = opts[:primary_key_column] ||= primary_key
|
|
1262
|
+
pkcs = opts[:primary_key_columns] ||= Array(pkc)
|
|
1263
|
+
elk = opts.eager_loader_key
|
|
900
1264
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
901
1265
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
902
1266
|
opts[:dataset] ||= proc do
|
|
903
|
-
|
|
904
|
-
klass.filter(cks.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}.zip(cpks.map{|k| send(k)}))
|
|
1267
|
+
opts.associated_class.filter(Array(opts.qualified_key).zip(cpks.map{|k| send(k)}))
|
|
905
1268
|
end
|
|
906
1269
|
opts[:eager_loader] ||= proc do |eo|
|
|
907
|
-
h = eo[:key_hash][
|
|
1270
|
+
h = eo[:key_hash][elk]
|
|
1271
|
+
rows = eo[:rows]
|
|
908
1272
|
if one_to_one
|
|
909
|
-
|
|
1273
|
+
rows.each{|object| object.associations[name] = nil}
|
|
910
1274
|
else
|
|
911
|
-
|
|
1275
|
+
rows.each{|object| object.associations[name] = []}
|
|
912
1276
|
end
|
|
913
1277
|
reciprocal = opts.reciprocal
|
|
914
1278
|
klass = opts.associated_class
|
|
915
|
-
|
|
916
|
-
|
|
1279
|
+
filter_keys = opts.eager_loading_predicate_key
|
|
1280
|
+
ds = model.eager_loading_dataset(opts, klass.filter(filter_keys=>h.keys), nil, eo[:associations], eo)
|
|
1281
|
+
case opts.eager_limit_strategy
|
|
1282
|
+
when :distinct_on
|
|
1283
|
+
ds = ds.distinct(*filter_keys).order_prepend(*filter_keys)
|
|
1284
|
+
when :window_function
|
|
1285
|
+
delete_rn = true
|
|
1286
|
+
rn = ds.row_number_column
|
|
1287
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1288
|
+
when :correlated_subquery
|
|
1289
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1290
|
+
xds.where(opts.associated_object_keys.map{|k| [SQL::QualifiedIdentifier.new(xds.first_source_alias, k), SQL::QualifiedIdentifier.new(xds.first_source_table, k)]})
|
|
1291
|
+
end
|
|
1292
|
+
end
|
|
1293
|
+
ds.all do |assoc_record|
|
|
1294
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
1295
|
+
hash_key = uses_cks ? km.map{|k| assoc_record.send(k)} : assoc_record.send(km)
|
|
917
1296
|
next unless objects = h[hash_key]
|
|
918
1297
|
if one_to_one
|
|
919
1298
|
objects.each do |object|
|
|
920
|
-
object.associations[name]
|
|
921
|
-
|
|
1299
|
+
unless object.associations[name]
|
|
1300
|
+
object.associations[name] = assoc_record
|
|
1301
|
+
assoc_record.associations[reciprocal] = object if reciprocal
|
|
1302
|
+
end
|
|
922
1303
|
end
|
|
923
1304
|
else
|
|
924
1305
|
objects.each do |object|
|
|
@@ -927,6 +1308,10 @@ module Sequel
|
|
|
927
1308
|
end
|
|
928
1309
|
end
|
|
929
1310
|
end
|
|
1311
|
+
if opts.eager_limit_strategy == :ruby
|
|
1312
|
+
limit, offset = opts.limit_and_offset
|
|
1313
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1314
|
+
end
|
|
930
1315
|
end
|
|
931
1316
|
|
|
932
1317
|
join_type = opts[:graph_join_type]
|
|
@@ -936,10 +1321,11 @@ module Sequel
|
|
|
936
1321
|
conditions = opts[:graph_conditions]
|
|
937
1322
|
opts[:cartesian_product_number] ||= one_to_one ? 0 : 1
|
|
938
1323
|
graph_block = opts[:graph_block]
|
|
939
|
-
opts[:eager_grapher] ||= proc do |
|
|
940
|
-
ds =
|
|
1324
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1325
|
+
ds = eo[:self]
|
|
1326
|
+
ds = ds.graph(eager_graph_dataset(opts, eo), use_only_conditions ? only_conditions : cks.zip(pkcs) + conditions, eo.merge(:select=>select, :join_type=>join_type, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
|
|
941
1327
|
# We only load reciprocals for one_to_many associations, as other reciprocals don't make sense
|
|
942
|
-
ds.opts[:eager_graph][:reciprocals][
|
|
1328
|
+
ds.opts[:eager_graph][:reciprocals][eo[:table_alias]] = opts.reciprocal
|
|
943
1329
|
ds
|
|
944
1330
|
end
|
|
945
1331
|
|
|
@@ -993,6 +1379,15 @@ module Sequel
|
|
|
993
1379
|
association_module_def(opts.remove_method, opts){|o,*args| remove_associated_object(opts, o, *args)}
|
|
994
1380
|
association_module_def(opts.remove_all_method, opts){|*args| remove_all_associated_objects(opts, *args)}
|
|
995
1381
|
end
|
|
1382
|
+
|
|
1383
|
+
# Return dataset to graph into given the association reflection, applying the :callback option if set.
|
|
1384
|
+
def eager_graph_dataset(opts, eager_options)
|
|
1385
|
+
ds = opts.associated_class.dataset
|
|
1386
|
+
if cb = eager_options[:callback]
|
|
1387
|
+
ds = cb.call(ds)
|
|
1388
|
+
end
|
|
1389
|
+
ds
|
|
1390
|
+
end
|
|
996
1391
|
end
|
|
997
1392
|
|
|
998
1393
|
# Instance methods used to implement the associations support.
|
|
@@ -1003,9 +1398,17 @@ module Sequel
|
|
|
1003
1398
|
def associations
|
|
1004
1399
|
@associations ||= {}
|
|
1005
1400
|
end
|
|
1401
|
+
|
|
1402
|
+
# Freeze the associations cache when freezing the object. Note that
|
|
1403
|
+
# retrieving associations after freezing will still work in most cases,
|
|
1404
|
+
# but the associations will not be cached in the association cache.
|
|
1405
|
+
def freeze
|
|
1406
|
+
associations.freeze
|
|
1407
|
+
super
|
|
1408
|
+
end
|
|
1006
1409
|
|
|
1007
|
-
#
|
|
1008
|
-
# an Error if the model has no primary key.
|
|
1410
|
+
# Formally used internally by the associations code, like pk but doesn't raise
|
|
1411
|
+
# an Error if the model has no primary key. Not used any longer, deprecated.
|
|
1009
1412
|
def pk_or_nil
|
|
1010
1413
|
key = primary_key
|
|
1011
1414
|
key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key]
|
|
@@ -1033,6 +1436,15 @@ module Sequel
|
|
|
1033
1436
|
ds
|
|
1034
1437
|
end
|
|
1035
1438
|
|
|
1439
|
+
# Return a dataset for the association after applying any dynamic callback.
|
|
1440
|
+
def _associated_dataset(opts, dynamic_opts)
|
|
1441
|
+
ds = send(opts.dataset_method)
|
|
1442
|
+
if callback = dynamic_opts[:callback]
|
|
1443
|
+
ds = callback.call(ds)
|
|
1444
|
+
end
|
|
1445
|
+
ds
|
|
1446
|
+
end
|
|
1447
|
+
|
|
1036
1448
|
# Return an association dataset for the given association reflection
|
|
1037
1449
|
def _dataset(opts)
|
|
1038
1450
|
raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
|
|
@@ -1041,18 +1453,33 @@ module Sequel
|
|
|
1041
1453
|
|
|
1042
1454
|
# Dataset for the join table of the given many to many association reflection
|
|
1043
1455
|
def _join_table_dataset(opts)
|
|
1044
|
-
ds = model.db.from(opts
|
|
1456
|
+
ds = model.db.from(opts.join_table_source)
|
|
1045
1457
|
opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
|
|
1046
1458
|
end
|
|
1047
1459
|
|
|
1048
|
-
# Return the associated
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1460
|
+
# Return the associated single object for the given association reflection and dynamic options
|
|
1461
|
+
# (or nil if no associated object).
|
|
1462
|
+
def _load_associated_object(opts, dynamic_opts)
|
|
1463
|
+
_load_associated_object_array(opts, dynamic_opts).first
|
|
1464
|
+
end
|
|
1465
|
+
|
|
1466
|
+
# Load the associated objects for the given association reflection and dynamic options
|
|
1467
|
+
# as an array.
|
|
1468
|
+
def _load_associated_object_array(opts, dynamic_opts)
|
|
1469
|
+
_associated_dataset(opts, dynamic_opts).all
|
|
1470
|
+
end
|
|
1471
|
+
|
|
1472
|
+
# Return the associated objects from the dataset, without association callbacks, reciprocals, and caching.
|
|
1473
|
+
# Still apply the dynamic callback if present.
|
|
1474
|
+
def _load_associated_objects(opts, dynamic_opts={})
|
|
1475
|
+
if opts.can_have_associated_objects?(self)
|
|
1476
|
+
if opts.returns_array?
|
|
1477
|
+
_load_associated_object_array(opts, dynamic_opts)
|
|
1478
|
+
else
|
|
1479
|
+
_load_associated_object(opts, dynamic_opts)
|
|
1055
1480
|
end
|
|
1481
|
+
else
|
|
1482
|
+
[] if opts.returns_array?
|
|
1056
1483
|
end
|
|
1057
1484
|
end
|
|
1058
1485
|
|
|
@@ -1086,6 +1513,7 @@ module Sequel
|
|
|
1086
1513
|
|
|
1087
1514
|
# Add/Set the current object to/as the given object's reciprocal association.
|
|
1088
1515
|
def add_reciprocal_object(opts, o)
|
|
1516
|
+
return if o.frozen?
|
|
1089
1517
|
return unless reciprocal = opts.reciprocal
|
|
1090
1518
|
if opts.reciprocal_array?
|
|
1091
1519
|
if array = o.associations[reciprocal] and !array.include?(self)
|
|
@@ -1113,13 +1541,20 @@ module Sequel
|
|
|
1113
1541
|
end
|
|
1114
1542
|
|
|
1115
1543
|
# Load the associated objects using the dataset, handling callbacks, reciprocals, and caching.
|
|
1116
|
-
def load_associated_objects(opts,
|
|
1544
|
+
def load_associated_objects(opts, dynamic_opts=nil)
|
|
1545
|
+
if dynamic_opts == true or dynamic_opts == false or dynamic_opts == nil
|
|
1546
|
+
dynamic_opts = {:reload=>dynamic_opts}
|
|
1547
|
+
elsif dynamic_opts.respond_to?(:call)
|
|
1548
|
+
dynamic_opts = {:callback=>dynamic_opts}
|
|
1549
|
+
end
|
|
1550
|
+
if block_given?
|
|
1551
|
+
dynamic_opts = dynamic_opts.merge(:callback=>Proc.new)
|
|
1552
|
+
end
|
|
1117
1553
|
name = opts[:name]
|
|
1118
|
-
if associations.include?(name) and !reload
|
|
1554
|
+
if associations.include?(name) and !dynamic_opts[:callback] and !dynamic_opts[:reload]
|
|
1119
1555
|
associations[name]
|
|
1120
1556
|
else
|
|
1121
|
-
objs = _load_associated_objects(opts)
|
|
1122
|
-
run_association_callbacks(opts, :after_load, objs)
|
|
1557
|
+
objs = _load_associated_objects(opts, dynamic_opts)
|
|
1123
1558
|
if opts.set_reciprocal_to_self?
|
|
1124
1559
|
if opts.returns_array?
|
|
1125
1560
|
objs.each{|o| add_reciprocal_object(opts, o)}
|
|
@@ -1127,7 +1562,14 @@ module Sequel
|
|
|
1127
1562
|
add_reciprocal_object(opts, objs)
|
|
1128
1563
|
end
|
|
1129
1564
|
end
|
|
1130
|
-
|
|
1565
|
+
|
|
1566
|
+
# If the current object is frozen, you can't update the associations
|
|
1567
|
+
# cache. This can cause issues for after_load procs that expect
|
|
1568
|
+
# the objects to be already cached in the associations, but
|
|
1569
|
+
# unfortunately that case cannot be handled.
|
|
1570
|
+
associations[name] = objs unless frozen?
|
|
1571
|
+
run_association_callbacks(opts, :after_load, objs)
|
|
1572
|
+
frozen? ? objs : associations[name]
|
|
1131
1573
|
end
|
|
1132
1574
|
end
|
|
1133
1575
|
|
|
@@ -1202,32 +1644,36 @@ module Sequel
|
|
|
1202
1644
|
end
|
|
1203
1645
|
end
|
|
1204
1646
|
|
|
1205
|
-
# Set the given object as the associated object for the given
|
|
1206
|
-
def
|
|
1207
|
-
|
|
1647
|
+
# Set the given object as the associated object for the given *_to_one association reflection
|
|
1648
|
+
def _set_associated_object(opts, o)
|
|
1649
|
+
a = associations[opts[:name]]
|
|
1650
|
+
return if a && a == o && !set_associated_object_if_same?
|
|
1208
1651
|
run_association_callbacks(opts, :before_set, o)
|
|
1209
|
-
|
|
1210
|
-
remove_reciprocal_object(opts, a)
|
|
1211
|
-
end
|
|
1652
|
+
remove_reciprocal_object(opts, a) if a
|
|
1212
1653
|
send(opts._setter_method, o)
|
|
1213
1654
|
associations[opts[:name]] = o
|
|
1214
1655
|
add_reciprocal_object(opts, o) if o
|
|
1215
1656
|
run_association_callbacks(opts, :after_set, o)
|
|
1216
1657
|
o
|
|
1217
1658
|
end
|
|
1659
|
+
|
|
1660
|
+
# Whether run the associated object setter code if passed the same object as the one already
|
|
1661
|
+
# cached in the association. Usually not set (so nil), can be set on a per-object basis
|
|
1662
|
+
# if necessary.
|
|
1663
|
+
def set_associated_object_if_same?
|
|
1664
|
+
@set_associated_object_if_same
|
|
1665
|
+
end
|
|
1218
1666
|
|
|
1667
|
+
# Set the given object as the associated object for the given many_to_one association reflection
|
|
1668
|
+
def set_associated_object(opts, o)
|
|
1669
|
+
raise(Error, "associated object #{o.inspect} does not have a primary key") if o && !o.pk
|
|
1670
|
+
_set_associated_object(opts, o)
|
|
1671
|
+
end
|
|
1672
|
+
|
|
1219
1673
|
# Set the given object as the associated object for the given one_to_one association reflection
|
|
1220
1674
|
def set_one_to_one_associated_object(opts, o)
|
|
1221
1675
|
raise(Error, "object #{inspect} does not have a primary key") unless pk
|
|
1222
|
-
|
|
1223
|
-
if a = associations[opts[:name]]
|
|
1224
|
-
remove_reciprocal_object(opts, a)
|
|
1225
|
-
end
|
|
1226
|
-
send(opts._setter_method, o)
|
|
1227
|
-
associations[opts[:name]] = o
|
|
1228
|
-
add_reciprocal_object(opts, o) if o
|
|
1229
|
-
run_association_callbacks(opts, :after_set, o)
|
|
1230
|
-
o
|
|
1676
|
+
_set_associated_object(opts, o)
|
|
1231
1677
|
end
|
|
1232
1678
|
end
|
|
1233
1679
|
|
|
@@ -1238,7 +1684,7 @@ module Sequel
|
|
|
1238
1684
|
# time, as it loads associated records using one query per association. However,
|
|
1239
1685
|
# it does not allow you the ability to filter or order based on columns in associated tables. +eager_graph+ loads
|
|
1240
1686
|
# all records in a single query using JOINs, allowing you to filter or order based on columns in associated
|
|
1241
|
-
# tables. However, +eager_graph+
|
|
1687
|
+
# tables. However, +eager_graph+ is usually slower than +eager+, especially if multiple
|
|
1242
1688
|
# one_to_many or many_to_many associations are joined.
|
|
1243
1689
|
#
|
|
1244
1690
|
# You can cascade the eager loading (loading associations on associated objects)
|
|
@@ -1259,12 +1705,80 @@ module Sequel
|
|
|
1259
1705
|
# Artist.eager_graph(:albums=>:tracks).all
|
|
1260
1706
|
# Artist.eager(:albums=>{:tracks=>:genre}).all
|
|
1261
1707
|
# Artist.eager_graph(:albums=>{:tracks=>:genre}).all
|
|
1708
|
+
#
|
|
1709
|
+
# You can also pass a callback as a hash value in order to customize the dataset being
|
|
1710
|
+
# eager loaded at query time, analogous to the way the :eager_block association option
|
|
1711
|
+
# allows you to customize it at association definition time. For example,
|
|
1712
|
+
# if you wanted artists with their albums since 1990:
|
|
1713
|
+
#
|
|
1714
|
+
# Artist.eager(:albums => proc{|ds| ds.filter{year > 1990}})
|
|
1715
|
+
#
|
|
1716
|
+
# Or if you needed albums and their artist's name only, using a single query:
|
|
1717
|
+
#
|
|
1718
|
+
# Albums.eager_graph(:artist => proc{|ds| ds.select(:name)})
|
|
1719
|
+
#
|
|
1720
|
+
# To cascade eager loading while using a callback, you substitute the cascaded
|
|
1721
|
+
# associations with a single entry hash that has the proc callback as the key and
|
|
1722
|
+
# the cascaded associations as the value. This will load artists with their albums
|
|
1723
|
+
# since 1990, and also the tracks on those albums and the genre for those tracks:
|
|
1724
|
+
#
|
|
1725
|
+
# Artist.eager(:albums => {proc{|ds| ds.filter{year > 1990}}=>{:tracks => :genre}})
|
|
1262
1726
|
module DatasetMethods
|
|
1263
1727
|
# Add the <tt>eager!</tt> and <tt>eager_graph!</tt> mutation methods to the dataset.
|
|
1264
1728
|
def self.extended(obj)
|
|
1265
1729
|
obj.def_mutation_method(:eager, :eager_graph)
|
|
1266
1730
|
end
|
|
1267
1731
|
|
|
1732
|
+
# If the expression is in the form <tt>x = y</tt> where +y+ is a <tt>Sequel::Model</tt>
|
|
1733
|
+
# instance, array of <tt>Sequel::Model</tt> instances, or a <tt>Sequel::Model</tt> dataset,
|
|
1734
|
+
# assume +x+ is an association symbol and look up the association reflection
|
|
1735
|
+
# via the dataset's model. From there, return the appropriate SQL based on the type of
|
|
1736
|
+
# association and the values of the foreign/primary keys of +y+. For most association
|
|
1737
|
+
# types, this is a simple transformation, but for +many_to_many+ associations this
|
|
1738
|
+
# creates a subquery to the join table.
|
|
1739
|
+
def complex_expression_sql_append(sql, op, args)
|
|
1740
|
+
r = args.at(1)
|
|
1741
|
+
if (((op == :'=' || op == :'!=') and r.is_a?(Sequel::Model)) ||
|
|
1742
|
+
(multiple = ((op == :IN || op == :'NOT IN') and ((is_ds = r.is_a?(Sequel::Dataset)) or r.all?{|x| x.is_a?(Sequel::Model)}))))
|
|
1743
|
+
l = args.at(0)
|
|
1744
|
+
if ar = model.association_reflections[l]
|
|
1745
|
+
if multiple
|
|
1746
|
+
klass = ar.associated_class
|
|
1747
|
+
if is_ds
|
|
1748
|
+
if r.respond_to?(:model)
|
|
1749
|
+
unless r.model <= klass
|
|
1750
|
+
# A dataset for a different model class, could be a valid regular query
|
|
1751
|
+
return super
|
|
1752
|
+
end
|
|
1753
|
+
else
|
|
1754
|
+
# Not a model dataset, could be a valid regular query
|
|
1755
|
+
return super
|
|
1756
|
+
end
|
|
1757
|
+
else
|
|
1758
|
+
unless r.all?{|x| x.is_a?(klass)}
|
|
1759
|
+
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}"
|
|
1760
|
+
end
|
|
1761
|
+
end
|
|
1762
|
+
elsif !r.is_a?(ar.associated_class)
|
|
1763
|
+
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}"
|
|
1764
|
+
end
|
|
1765
|
+
|
|
1766
|
+
if exp = association_filter_expression(op, ar, r)
|
|
1767
|
+
literal_append(sql, exp)
|
|
1768
|
+
else
|
|
1769
|
+
raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1770
|
+
end
|
|
1771
|
+
elsif multiple && (is_ds || r.empty?)
|
|
1772
|
+
# Not a query designed for this support, could be a valid regular query
|
|
1773
|
+
super
|
|
1774
|
+
else
|
|
1775
|
+
raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1776
|
+
end
|
|
1777
|
+
else
|
|
1778
|
+
super
|
|
1779
|
+
end
|
|
1780
|
+
end
|
|
1781
|
+
|
|
1268
1782
|
# The preferred eager loading method. Loads all associated records using one
|
|
1269
1783
|
# query for each association.
|
|
1270
1784
|
#
|
|
@@ -1311,9 +1825,9 @@ module Sequel
|
|
|
1311
1825
|
# The secondary eager loading method. Loads all associations in a single query. This
|
|
1312
1826
|
# method should only be used if you need to filter or order based on columns in associated tables.
|
|
1313
1827
|
#
|
|
1314
|
-
# This method
|
|
1315
|
-
#
|
|
1316
|
-
# of model objects.
|
|
1828
|
+
# This method uses <tt>Dataset#graph</tt> to create appropriate aliases for columns in all the
|
|
1829
|
+
# tables. Then it uses the graph's metadata to build the associations from the single hash, and
|
|
1830
|
+
# finally replaces the array of hashes with an array model objects inside all.
|
|
1317
1831
|
#
|
|
1318
1832
|
# Be very careful when using this with multiple one_to_many or many_to_many associations, as you can
|
|
1319
1833
|
# create large cartesian products. If you must graph multiple one_to_many and many_to_many associations,
|
|
@@ -1327,18 +1841,18 @@ module Sequel
|
|
|
1327
1841
|
# all objects. You can use the :graph_* association options to modify the SQL query.
|
|
1328
1842
|
#
|
|
1329
1843
|
# Like +eager+, you need to call +all+ on the dataset for the eager loading to work. If you just
|
|
1330
|
-
# call +each+,
|
|
1331
|
-
# model object values).
|
|
1844
|
+
# call +each+, it will yield plain hashes, each containing all columns from all the tables.
|
|
1332
1845
|
def eager_graph(*associations)
|
|
1333
|
-
ds = if @opts[:eager_graph]
|
|
1334
|
-
|
|
1846
|
+
ds = if eg = @opts[:eager_graph]
|
|
1847
|
+
eg = eg.dup
|
|
1848
|
+
[:requirements, :reflections, :reciprocals].each{|k| eg[k] = eg[k].dup}
|
|
1849
|
+
clone(:eager_graph=>eg)
|
|
1335
1850
|
else
|
|
1336
1851
|
# Each of the following have a symbol key for the table alias, with the following values:
|
|
1337
1852
|
# :reciprocals - the reciprocal instance variable to use for this association
|
|
1853
|
+
# :reflections - AssociationReflection instance related to this association
|
|
1338
1854
|
# :requirements - array of requirements for this association
|
|
1339
|
-
|
|
1340
|
-
# :alias_association_name_map - the name of the association for this association
|
|
1341
|
-
clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :alias_association_type_map=>{}, :alias_association_name_map=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
|
|
1855
|
+
clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :reflections=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
|
|
1342
1856
|
end
|
|
1343
1857
|
ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations)
|
|
1344
1858
|
end
|
|
@@ -1366,20 +1880,32 @@ module Sequel
|
|
|
1366
1880
|
# r :: association reflection for the current association
|
|
1367
1881
|
# *associations :: any associations dependent on this one
|
|
1368
1882
|
def eager_graph_association(ds, model, ta, requirements, r, *associations)
|
|
1369
|
-
klass = r.associated_class
|
|
1370
1883
|
assoc_name = r[:name]
|
|
1371
|
-
assoc_table_alias = ds.unused_table_alias(
|
|
1372
|
-
|
|
1884
|
+
assoc_table_alias = ds.unused_table_alias(r[:graph_alias_base])
|
|
1885
|
+
loader = r[:eager_grapher]
|
|
1886
|
+
if !associations.empty?
|
|
1887
|
+
if associations.first.respond_to?(:call)
|
|
1888
|
+
callback = associations.first
|
|
1889
|
+
associations = {}
|
|
1890
|
+
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)
|
|
1891
|
+
callback, assoc = pr_assoc
|
|
1892
|
+
associations = assoc.is_a?(Array) ? assoc : [assoc]
|
|
1893
|
+
end
|
|
1894
|
+
end
|
|
1895
|
+
ds = if loader.arity == 1
|
|
1896
|
+
loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>ta, :callback=>callback)
|
|
1897
|
+
else
|
|
1898
|
+
loader.call(ds, assoc_table_alias, ta)
|
|
1899
|
+
end
|
|
1373
1900
|
ds = ds.order_more(*qualified_expression(r[:order], assoc_table_alias)) if r[:order] and r[:order_eager_graph]
|
|
1374
1901
|
eager_graph = ds.opts[:eager_graph]
|
|
1375
1902
|
eager_graph[:requirements][assoc_table_alias] = requirements.dup
|
|
1376
|
-
eager_graph[:
|
|
1377
|
-
eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array?
|
|
1903
|
+
eager_graph[:reflections][assoc_table_alias] = r
|
|
1378
1904
|
eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2
|
|
1379
1905
|
ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
|
|
1380
1906
|
ds
|
|
1381
1907
|
end
|
|
1382
|
-
|
|
1908
|
+
|
|
1383
1909
|
# Check the associations are valid for the given model.
|
|
1384
1910
|
# Call eager_graph_association on each association.
|
|
1385
1911
|
#
|
|
@@ -1406,20 +1932,228 @@ module Sequel
|
|
|
1406
1932
|
ds
|
|
1407
1933
|
end
|
|
1408
1934
|
|
|
1409
|
-
#
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1935
|
+
# Replace the array of plain hashes with an array of model objects will all eager_graphed
|
|
1936
|
+
# associations set in the associations cache for each object.
|
|
1937
|
+
def eager_graph_build_associations(hashes)
|
|
1938
|
+
hashes.replace(EagerGraphLoader.new(self).load(hashes))
|
|
1939
|
+
end
|
|
1940
|
+
|
|
1941
|
+
private
|
|
1942
|
+
|
|
1943
|
+
# Return an expression for filtering by the given association reflection and associated object.
|
|
1944
|
+
def association_filter_expression(op, ref, obj)
|
|
1945
|
+
meth = :"#{ref[:type]}_association_filter_expression"
|
|
1946
|
+
send(meth, op, ref, obj) if respond_to?(meth, true)
|
|
1947
|
+
end
|
|
1948
|
+
|
|
1949
|
+
# Handle inversion for association filters by returning an inverted expression,
|
|
1950
|
+
# plus also handling cases where the referenced columns are NULL.
|
|
1951
|
+
def association_filter_handle_inversion(op, exp, cols)
|
|
1952
|
+
if op == :'!=' || op == :'NOT IN'
|
|
1953
|
+
if exp == SQL::Constants::FALSE
|
|
1954
|
+
~exp
|
|
1955
|
+
else
|
|
1956
|
+
~exp | Sequel::SQL::BooleanExpression.from_value_pairs(cols.zip([]), :OR)
|
|
1957
|
+
end
|
|
1958
|
+
else
|
|
1959
|
+
exp
|
|
1960
|
+
end
|
|
1961
|
+
end
|
|
1962
|
+
|
|
1963
|
+
# Return an expression for making sure that the given keys match the value of
|
|
1964
|
+
# the given methods for either the single object given or for any of the objects
|
|
1965
|
+
# given if +obj+ is an array.
|
|
1966
|
+
def association_filter_key_expression(keys, meths, obj)
|
|
1967
|
+
vals = if obj.is_a?(Sequel::Dataset)
|
|
1968
|
+
{(keys.length == 1 ? keys.first : keys)=>obj.select(*meths).exclude(Sequel::SQL::BooleanExpression.from_value_pairs(meths.zip([]), :OR))}
|
|
1969
|
+
else
|
|
1970
|
+
vals = Array(obj).reject{|o| !meths.all?{|m| o.send(m)}}
|
|
1971
|
+
return SQL::Constants::FALSE if vals.empty?
|
|
1972
|
+
if obj.is_a?(Array)
|
|
1973
|
+
if keys.length == 1
|
|
1974
|
+
meth = meths.first
|
|
1975
|
+
{keys.first=>vals.map{|o| o.send(meth)}}
|
|
1976
|
+
else
|
|
1977
|
+
{keys=>vals.map{|o| meths.map{|m| o.send(m)}}}
|
|
1978
|
+
end
|
|
1979
|
+
else
|
|
1980
|
+
keys.zip(meths.map{|k| obj.send(k)})
|
|
1981
|
+
end
|
|
1982
|
+
end
|
|
1983
|
+
SQL::BooleanExpression.from_value_pairs(vals)
|
|
1984
|
+
end
|
|
1985
|
+
|
|
1986
|
+
# Make sure the association is valid for this model, and return the related AssociationReflection.
|
|
1987
|
+
def check_association(model, association)
|
|
1988
|
+
raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
|
|
1989
|
+
raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
|
|
1990
|
+
reflection
|
|
1991
|
+
end
|
|
1992
|
+
|
|
1993
|
+
# Eagerly load all specified associations
|
|
1994
|
+
def eager_load(a, eager_assoc=@opts[:eager])
|
|
1995
|
+
return if a.empty?
|
|
1996
|
+
# Key is foreign/primary key name symbol
|
|
1997
|
+
# Value is hash with keys being foreign/primary key values (generally integers)
|
|
1998
|
+
# and values being an array of current model objects with that
|
|
1999
|
+
# specific foreign/primary key
|
|
2000
|
+
key_hash = {}
|
|
2001
|
+
# Reflections for all associations to eager load
|
|
2002
|
+
reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
|
|
2003
|
+
|
|
2004
|
+
# Populate keys to monitor
|
|
2005
|
+
reflections.each{|reflection| key_hash[reflection.eager_loader_key] ||= Hash.new{|h,k| h[k] = []}}
|
|
2006
|
+
|
|
2007
|
+
# Associate each object with every key being monitored
|
|
2008
|
+
a.each do |rec|
|
|
2009
|
+
key_hash.each do |key, id_map|
|
|
2010
|
+
case key
|
|
2011
|
+
when Array
|
|
2012
|
+
id_map[key.map{|k| rec[k]}] << rec if key.all?{|k| rec[k]}
|
|
2013
|
+
when Symbol
|
|
2014
|
+
id_map[rec[key]] << rec if rec[key]
|
|
2015
|
+
end
|
|
2016
|
+
end
|
|
2017
|
+
end
|
|
2018
|
+
|
|
2019
|
+
reflections.each do |r|
|
|
2020
|
+
loader = r[:eager_loader]
|
|
2021
|
+
associations = eager_assoc[r[:name]]
|
|
2022
|
+
if associations.respond_to?(:call)
|
|
2023
|
+
eager_block = associations
|
|
2024
|
+
associations = {}
|
|
2025
|
+
elsif associations.is_a?(Hash) && associations.length == 1 && (pr_assoc = associations.to_a.first) && pr_assoc.first.respond_to?(:call)
|
|
2026
|
+
eager_block, associations = pr_assoc
|
|
2027
|
+
end
|
|
2028
|
+
if loader.arity == 1
|
|
2029
|
+
loader.call(:key_hash=>key_hash, :rows=>a, :associations=>associations, :self=>self, :eager_block=>eager_block)
|
|
2030
|
+
else
|
|
2031
|
+
loader.call(key_hash, a, associations)
|
|
2032
|
+
end
|
|
2033
|
+
a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
|
|
2034
|
+
end
|
|
2035
|
+
end
|
|
2036
|
+
|
|
2037
|
+
# Return plain hashes instead of calling the row_proc if eager_graph is being used.
|
|
2038
|
+
def graph_each(&block)
|
|
2039
|
+
@opts[:eager_graph] ? fetch_rows(select_sql, &block) : super
|
|
2040
|
+
end
|
|
2041
|
+
|
|
2042
|
+
# Return a subquery expression for filering by a many_to_many association
|
|
2043
|
+
def many_to_many_association_filter_expression(op, ref, obj)
|
|
2044
|
+
lpks, lks, rks = ref.values_at(:left_primary_keys, :left_keys, :right_keys)
|
|
2045
|
+
jt = ref.join_table_alias
|
|
2046
|
+
lpks = lpks.first if lpks.length == 1
|
|
2047
|
+
lpks = ref.qualify(model.table_name, lpks)
|
|
2048
|
+
meths = ref.right_primary_keys
|
|
2049
|
+
meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
|
|
2050
|
+
exp = association_filter_key_expression(ref.qualify(jt, rks), meths, obj)
|
|
2051
|
+
if exp == SQL::Constants::FALSE
|
|
2052
|
+
association_filter_handle_inversion(op, exp, Array(lpks))
|
|
2053
|
+
else
|
|
2054
|
+
association_filter_handle_inversion(op, SQL::BooleanExpression.from_value_pairs(lpks=>model.db.from(ref[:join_table]).select(*ref.qualify(jt, lks)).where(exp).exclude(SQL::BooleanExpression.from_value_pairs(ref.qualify(jt, lks).zip([]), :OR))), Array(lpks))
|
|
2055
|
+
end
|
|
2056
|
+
end
|
|
2057
|
+
|
|
2058
|
+
# Return a simple equality expression for filering by a many_to_one association
|
|
2059
|
+
def many_to_one_association_filter_expression(op, ref, obj)
|
|
2060
|
+
keys = ref.qualify(model.table_name, ref[:keys])
|
|
2061
|
+
meths = ref.primary_keys
|
|
2062
|
+
meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
|
|
2063
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
|
|
2064
|
+
end
|
|
2065
|
+
|
|
2066
|
+
# Return a simple equality expression for filering by a one_to_* association
|
|
2067
|
+
def one_to_many_association_filter_expression(op, ref, obj)
|
|
2068
|
+
keys = ref.qualify(model.table_name, ref[:primary_keys])
|
|
2069
|
+
meths = ref[:keys]
|
|
2070
|
+
meths = ref.qualify(obj.model.table_name, meths) if obj.is_a?(Sequel::Dataset)
|
|
2071
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
|
|
2072
|
+
end
|
|
2073
|
+
alias one_to_one_association_filter_expression one_to_many_association_filter_expression
|
|
2074
|
+
|
|
2075
|
+
# Build associations from the graph if #eager_graph was used,
|
|
2076
|
+
# and/or load other associations if #eager was used.
|
|
2077
|
+
def post_load(all_records)
|
|
2078
|
+
eager_graph_build_associations(all_records) if @opts[:eager_graph]
|
|
2079
|
+
eager_load(all_records) if @opts[:eager]
|
|
2080
|
+
super
|
|
2081
|
+
end
|
|
2082
|
+
end
|
|
2083
|
+
|
|
2084
|
+
# This class is the internal implementation of eager_graph. It is responsible for taking an array of plain
|
|
2085
|
+
# hashes and returning an array of model objects with all eager_graphed associations already set in the
|
|
2086
|
+
# association cache.
|
|
2087
|
+
class EagerGraphLoader
|
|
2088
|
+
# Hash with table alias symbol keys and after_load hook values
|
|
2089
|
+
attr_reader :after_load_map
|
|
2090
|
+
|
|
2091
|
+
# Hash with table alias symbol keys and association name values
|
|
2092
|
+
attr_reader :alias_map
|
|
2093
|
+
|
|
2094
|
+
# Hash with table alias symbol keys and subhash values mapping column_alias symbols to the
|
|
2095
|
+
# symbol of the real name of the column
|
|
2096
|
+
attr_reader :column_maps
|
|
2097
|
+
|
|
2098
|
+
# Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
|
|
2099
|
+
attr_reader :dependency_map
|
|
2100
|
+
|
|
2101
|
+
# Hash with table alias symbol keys and [limit, offset] values
|
|
2102
|
+
attr_reader :limit_map
|
|
2103
|
+
|
|
2104
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
2105
|
+
# The table alias symbol for the primary model
|
|
2106
|
+
attr_reader :master
|
|
2107
|
+
|
|
2108
|
+
# Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for
|
|
2109
|
+
# composite key tables)
|
|
2110
|
+
attr_reader :primary_keys
|
|
2111
|
+
|
|
2112
|
+
# Hash with table alias symbol keys and reciprocal association symbol values,
|
|
2113
|
+
# used for setting reciprocals for one_to_many associations.
|
|
2114
|
+
attr_reader :reciprocal_map
|
|
2115
|
+
|
|
2116
|
+
# Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols)
|
|
2117
|
+
# to model instances. Used so that only a single model instance is created for each object.
|
|
2118
|
+
attr_reader :records_map
|
|
2119
|
+
|
|
2120
|
+
# Hash with table alias symbol keys and AssociationReflection values
|
|
2121
|
+
attr_reader :reflection_map
|
|
2122
|
+
|
|
2123
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
2124
|
+
attr_reader :row_procs
|
|
2125
|
+
|
|
2126
|
+
# Hash with table alias symbol keys and true/false values, where true means the
|
|
2127
|
+
# association represented by the table alias uses an array of values instead of
|
|
2128
|
+
# a single value (i.e. true => *_many, false => *_to_one).
|
|
2129
|
+
attr_reader :type_map
|
|
2130
|
+
|
|
2131
|
+
# Initialize all of the data structures used during loading.
|
|
2132
|
+
def initialize(dataset)
|
|
2133
|
+
opts = dataset.opts
|
|
2134
|
+
eager_graph = opts[:eager_graph]
|
|
2135
|
+
@master = eager_graph[:master]
|
|
1413
2136
|
requirements = eager_graph[:requirements]
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
2137
|
+
reflection_map = @reflection_map = eager_graph[:reflections]
|
|
2138
|
+
reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
|
|
2139
|
+
@unique = eager_graph[:cartesian_product_number] > 1
|
|
1417
2140
|
|
|
2141
|
+
alias_map = @alias_map = {}
|
|
2142
|
+
type_map = @type_map = {}
|
|
2143
|
+
after_load_map = @after_load_map = {}
|
|
2144
|
+
limit_map = @limit_map = {}
|
|
2145
|
+
reflection_map.each do |k, v|
|
|
2146
|
+
alias_map[k] = v[:name]
|
|
2147
|
+
type_map[k] = v.returns_array?
|
|
2148
|
+
after_load_map[k] = v[:after_load] unless v[:after_load].empty?
|
|
2149
|
+
limit_map[k] = v.limit_and_offset if v[:limit]
|
|
2150
|
+
end
|
|
2151
|
+
|
|
1418
2152
|
# Make dependency map hash out of requirements array for each association.
|
|
1419
2153
|
# This builds a tree of dependencies that will be used for recursion
|
|
1420
2154
|
# to ensure that all parts of the object graph are loaded into the
|
|
1421
2155
|
# appropriate subordinate association.
|
|
1422
|
-
dependency_map = {}
|
|
2156
|
+
@dependency_map = {}
|
|
1423
2157
|
# Sort the associations by requirements length, so that
|
|
1424
2158
|
# requirements are added to the dependency hash before their
|
|
1425
2159
|
# dependencies.
|
|
@@ -1439,140 +2173,200 @@ module Sequel
|
|
|
1439
2173
|
# This mapping is used to make sure that duplicate entries in the
|
|
1440
2174
|
# result set are mapped to a single record. For example, using a
|
|
1441
2175
|
# single one_to_many association with 10 associated records,
|
|
1442
|
-
# the main object
|
|
2176
|
+
# the main object column values appear in the object graph 10 times.
|
|
1443
2177
|
# We map by primary key, if available, or by the object's entire values,
|
|
1444
2178
|
# if not. The mapping must be per table, so create sub maps for each table
|
|
1445
2179
|
# alias.
|
|
1446
|
-
records_map = {master=>{}}
|
|
2180
|
+
records_map = {@master=>{}}
|
|
1447
2181
|
alias_map.keys.each{|ta| records_map[ta] = {}}
|
|
2182
|
+
@records_map = records_map
|
|
2183
|
+
|
|
2184
|
+
datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
|
|
2185
|
+
column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
|
|
2186
|
+
primary_keys = {}
|
|
2187
|
+
column_maps = {}
|
|
2188
|
+
models = {}
|
|
2189
|
+
row_procs = {}
|
|
2190
|
+
datasets.each do |ta, ds|
|
|
2191
|
+
models[ta] = ds.model
|
|
2192
|
+
primary_keys[ta] = []
|
|
2193
|
+
column_maps[ta] = {}
|
|
2194
|
+
row_procs[ta] = ds.row_proc
|
|
2195
|
+
end
|
|
2196
|
+
column_aliases.each do |col_alias, tc|
|
|
2197
|
+
ta, column = tc
|
|
2198
|
+
column_maps[ta][col_alias] = column
|
|
2199
|
+
end
|
|
2200
|
+
column_maps.each do |ta, h|
|
|
2201
|
+
pk = models[ta].primary_key
|
|
2202
|
+
if pk.is_a?(Array)
|
|
2203
|
+
primary_keys[ta] = []
|
|
2204
|
+
h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
|
|
2205
|
+
else
|
|
2206
|
+
h.select{|ca, c| primary_keys[ta] = ca if pk == c}
|
|
2207
|
+
end
|
|
2208
|
+
end
|
|
2209
|
+
@column_maps = column_maps
|
|
2210
|
+
@primary_keys = primary_keys
|
|
2211
|
+
@row_procs = row_procs
|
|
2212
|
+
|
|
2213
|
+
# For performance, create two special maps for the master table,
|
|
2214
|
+
# so you can skip a hash lookup.
|
|
2215
|
+
@master_column_map = column_maps[master]
|
|
2216
|
+
@master_primary_keys = primary_keys[master]
|
|
2217
|
+
|
|
2218
|
+
# Add a special hash mapping table alias symbols to 5 element arrays that just
|
|
2219
|
+
# contain the data in other data structures for that table alias. This is
|
|
2220
|
+
# used for performance, to get all values in one hash lookup instead of
|
|
2221
|
+
# separate hash lookups for each data structure.
|
|
2222
|
+
ta_map = {}
|
|
2223
|
+
alias_map.keys.each do |ta|
|
|
2224
|
+
ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
|
|
2225
|
+
end
|
|
2226
|
+
@ta_map = ta_map
|
|
2227
|
+
end
|
|
2228
|
+
|
|
2229
|
+
# Return an array of primary model instances with the associations cache prepopulated
|
|
2230
|
+
# for all model objects (both primary and associated).
|
|
2231
|
+
def load(hashes)
|
|
2232
|
+
master = master()
|
|
1448
2233
|
|
|
2234
|
+
# Assign to local variables for speed increase
|
|
2235
|
+
rp = row_procs[master]
|
|
2236
|
+
rm = records_map[master]
|
|
2237
|
+
dm = dependency_map
|
|
2238
|
+
|
|
1449
2239
|
# This will hold the final record set that we will be replacing the object graph with.
|
|
1450
2240
|
records = []
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
key =
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
2241
|
+
|
|
2242
|
+
hashes.each do |h|
|
|
2243
|
+
unless key = master_pk(h)
|
|
2244
|
+
key = hkey(master_hfor(h))
|
|
2245
|
+
end
|
|
2246
|
+
unless primary_record = rm[key]
|
|
2247
|
+
primary_record = rm[key] = rp.call(master_hfor(h))
|
|
1458
2248
|
# Only add it to the list of records to return if it is a new record
|
|
1459
2249
|
records.push(primary_record)
|
|
1460
2250
|
end
|
|
1461
2251
|
# Build all associations for the current object and it's dependencies
|
|
1462
|
-
|
|
2252
|
+
_load(dm, primary_record, h)
|
|
1463
2253
|
end
|
|
1464
2254
|
|
|
1465
2255
|
# Remove duplicate records from all associations if this graph could possibly be a cartesian product
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
2256
|
+
# Run after_load procs if there are any
|
|
2257
|
+
post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
|
|
2258
|
+
|
|
2259
|
+
records
|
|
1470
2260
|
end
|
|
1471
2261
|
|
|
1472
2262
|
private
|
|
1473
|
-
|
|
1474
|
-
#
|
|
1475
|
-
def
|
|
1476
|
-
raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
|
|
1477
|
-
raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
|
|
1478
|
-
reflection
|
|
1479
|
-
end
|
|
1480
|
-
|
|
1481
|
-
# Build associations for the current object. This is called recursively
|
|
1482
|
-
# to build all dependencies.
|
|
1483
|
-
def eager_graph_build_associations_graph(dependency_map, alias_map, type_map, reciprocal_map, records_map, current, record_graph)
|
|
1484
|
-
return if dependency_map.empty?
|
|
1485
|
-
# Don't clobber the cached association value for one_to_many and many_to_many associations if it has already been setup
|
|
1486
|
-
dependency_map.keys.each do |ta|
|
|
1487
|
-
assoc_name = alias_map[ta]
|
|
1488
|
-
current.associations[assoc_name] = type_map[ta] ? [] : nil unless current.associations.include?(assoc_name)
|
|
1489
|
-
end
|
|
2263
|
+
|
|
2264
|
+
# Recursive method that creates associated model objects and associates them to the current model object.
|
|
2265
|
+
def _load(dependency_map, current, h)
|
|
1490
2266
|
dependency_map.each do |ta, deps|
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
2267
|
+
unless key = pk(ta, h)
|
|
2268
|
+
ta_h = hfor(ta, h)
|
|
2269
|
+
unless ta_h.values.any?
|
|
2270
|
+
assoc_name = alias_map[ta]
|
|
2271
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2272
|
+
assoc[assoc_name] = type_map[ta] ? [] : nil
|
|
2273
|
+
end
|
|
2274
|
+
next
|
|
2275
|
+
end
|
|
2276
|
+
key = hkey(ta_h)
|
|
1497
2277
|
end
|
|
1498
|
-
assoc_name =
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
2278
|
+
rm, rp, assoc_name, tm, rcm = @ta_map[ta]
|
|
2279
|
+
unless rec = rm[key]
|
|
2280
|
+
rec = rm[key] = rp.call(hfor(ta, h))
|
|
2281
|
+
end
|
|
2282
|
+
|
|
2283
|
+
if tm
|
|
2284
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2285
|
+
assoc[assoc_name] = []
|
|
1503
2286
|
end
|
|
2287
|
+
assoc[assoc_name].push(rec)
|
|
2288
|
+
rec.associations[rcm] = current if rcm
|
|
1504
2289
|
else
|
|
1505
|
-
current.associations[assoc_name]
|
|
2290
|
+
current.associations[assoc_name] ||= rec
|
|
1506
2291
|
end
|
|
1507
2292
|
# Recurse into dependencies of the current object
|
|
1508
|
-
|
|
2293
|
+
_load(deps, rec, h) unless deps.empty?
|
|
1509
2294
|
end
|
|
1510
2295
|
end
|
|
1511
2296
|
|
|
2297
|
+
# Return the subhash for the specific table alias +ta+ by parsing the values out of the main hash +h+
|
|
2298
|
+
def hfor(ta, h)
|
|
2299
|
+
out = {}
|
|
2300
|
+
@column_maps[ta].each{|ca, c| out[c] = h[ca]}
|
|
2301
|
+
out
|
|
2302
|
+
end
|
|
2303
|
+
|
|
2304
|
+
# Return a suitable hash key for any subhash +h+, which is an array of values by column order.
|
|
2305
|
+
# This is only used if the primary key cannot be used.
|
|
2306
|
+
def hkey(h)
|
|
2307
|
+
h.sort_by{|x| x[0].to_s}
|
|
2308
|
+
end
|
|
2309
|
+
|
|
2310
|
+
# Return the subhash for the master table by parsing the values out of the main hash +h+
|
|
2311
|
+
def master_hfor(h)
|
|
2312
|
+
out = {}
|
|
2313
|
+
@master_column_map.each{|ca, c| out[c] = h[ca]}
|
|
2314
|
+
out
|
|
2315
|
+
end
|
|
2316
|
+
|
|
2317
|
+
# Return a primary key value for the master table by parsing it out of the main hash +h+.
|
|
2318
|
+
def master_pk(h)
|
|
2319
|
+
x = @master_primary_keys
|
|
2320
|
+
if x.is_a?(Array)
|
|
2321
|
+
unless x == []
|
|
2322
|
+
x = x.map{|ca| h[ca]}
|
|
2323
|
+
x if x.all?
|
|
2324
|
+
end
|
|
2325
|
+
else
|
|
2326
|
+
h[x]
|
|
2327
|
+
end
|
|
2328
|
+
end
|
|
2329
|
+
|
|
2330
|
+
# Return a primary key value for the given table alias by parsing it out of the main hash +h+.
|
|
2331
|
+
def pk(ta, h)
|
|
2332
|
+
x = primary_keys[ta]
|
|
2333
|
+
if x.is_a?(Array)
|
|
2334
|
+
unless x == []
|
|
2335
|
+
x = x.map{|ca| h[ca]}
|
|
2336
|
+
x if x.all?
|
|
2337
|
+
end
|
|
2338
|
+
else
|
|
2339
|
+
h[x]
|
|
2340
|
+
end
|
|
2341
|
+
end
|
|
2342
|
+
|
|
1512
2343
|
# If the result set is the result of a cartesian product, then it is possible that
|
|
1513
2344
|
# there are multiple records for each association when there should only be one.
|
|
1514
2345
|
# In that case, for each object in all associations loaded via +eager_graph+, run
|
|
1515
2346
|
# uniq! on the association to make sure no duplicate records show up.
|
|
1516
2347
|
# Note that this can cause legitimate duplicate records to be removed.
|
|
1517
|
-
def
|
|
2348
|
+
def post_process(records, dependency_map)
|
|
1518
2349
|
records.each do |record|
|
|
1519
2350
|
dependency_map.each do |ta, deps|
|
|
1520
|
-
|
|
1521
|
-
list =
|
|
2351
|
+
assoc_name = alias_map[ta]
|
|
2352
|
+
list = record.send(assoc_name)
|
|
2353
|
+
rec_list = if type_map[ta]
|
|
1522
2354
|
list.uniq!
|
|
2355
|
+
if lo = limit_map[ta]
|
|
2356
|
+
limit, offset = lo
|
|
2357
|
+
list.replace(list[offset||0, limit])
|
|
2358
|
+
end
|
|
2359
|
+
list
|
|
2360
|
+
elsif list
|
|
2361
|
+
[list]
|
|
1523
2362
|
else
|
|
1524
|
-
[
|
|
2363
|
+
[]
|
|
1525
2364
|
end
|
|
1526
|
-
|
|
1527
|
-
|
|
2365
|
+
record.send(:run_association_callbacks, reflection_map[ta], :after_load, list) if after_load_map[ta]
|
|
2366
|
+
post_process(rec_list, deps) if !rec_list.empty? && !deps.empty?
|
|
1528
2367
|
end
|
|
1529
2368
|
end
|
|
1530
2369
|
end
|
|
1531
|
-
|
|
1532
|
-
# Eagerly load all specified associations
|
|
1533
|
-
def eager_load(a, eager_assoc=@opts[:eager])
|
|
1534
|
-
return if a.empty?
|
|
1535
|
-
# Key is foreign/primary key name symbol
|
|
1536
|
-
# Value is hash with keys being foreign/primary key values (generally integers)
|
|
1537
|
-
# and values being an array of current model objects with that
|
|
1538
|
-
# specific foreign/primary key
|
|
1539
|
-
key_hash = {}
|
|
1540
|
-
# Reflections for all associations to eager load
|
|
1541
|
-
reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
|
|
1542
|
-
|
|
1543
|
-
# Populate keys to monitor
|
|
1544
|
-
reflections.each{|reflection| key_hash[reflection.eager_loader_key] ||= Hash.new{|h,k| h[k] = []}}
|
|
1545
|
-
|
|
1546
|
-
# Associate each object with every key being monitored
|
|
1547
|
-
a.each do |rec|
|
|
1548
|
-
key_hash.each do |key, id_map|
|
|
1549
|
-
case key
|
|
1550
|
-
when Array
|
|
1551
|
-
id_map[key.map{|k| rec[k]}] << rec if key.all?{|k| rec[k]}
|
|
1552
|
-
when Symbol
|
|
1553
|
-
id_map[rec[key]] << rec if rec[key]
|
|
1554
|
-
end
|
|
1555
|
-
end
|
|
1556
|
-
end
|
|
1557
|
-
|
|
1558
|
-
reflections.each do |r|
|
|
1559
|
-
loader = r[:eager_loader]
|
|
1560
|
-
if loader.arity == 1
|
|
1561
|
-
loader.call(:key_hash=>key_hash, :rows=>a, :associations=>eager_assoc[r[:name]], :self=>self)
|
|
1562
|
-
else
|
|
1563
|
-
loader.call(key_hash, a, eager_assoc[r[:name]])
|
|
1564
|
-
end
|
|
1565
|
-
a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
|
|
1566
|
-
end
|
|
1567
|
-
end
|
|
1568
|
-
|
|
1569
|
-
# Build associations from the graph if #eager_graph was used,
|
|
1570
|
-
# and/or load other associations if #eager was used.
|
|
1571
|
-
def post_load(all_records)
|
|
1572
|
-
eager_graph_build_associations(all_records) if @opts[:eager_graph]
|
|
1573
|
-
eager_load(all_records) if @opts[:eager]
|
|
1574
|
-
super
|
|
1575
|
-
end
|
|
1576
2370
|
end
|
|
1577
2371
|
end
|
|
1578
2372
|
end
|