sequel 3.21.0 → 3.41.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 +1131 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +106 -65
- data/Rakefile +59 -27
- data/bin/sequel +50 -26
- data/doc/active_record.rdoc +68 -52
- data/doc/advanced_associations.rdoc +201 -75
- data/doc/association_basics.rdoc +327 -79
- data/doc/bin_sequel.rdoc +144 -0
- data/doc/cheat_sheet.rdoc +21 -21
- data/doc/core_extensions.rdoc +374 -0
- data/doc/dataset_basics.rdoc +10 -10
- data/doc/dataset_filtering.rdoc +54 -42
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +89 -557
- data/doc/model_hooks.rdoc +80 -29
- data/doc/object_model.rdoc +576 -0
- data/doc/opening_databases.rdoc +121 -53
- data/doc/prepared_statements.rdoc +41 -15
- data/doc/querying.rdoc +137 -56
- data/doc/reflection.rdoc +36 -10
- 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/release_notes/3.37.0.txt +338 -0
- data/doc/release_notes/3.38.0.txt +234 -0
- data/doc/release_notes/3.39.0.txt +237 -0
- data/doc/release_notes/3.40.0.txt +73 -0
- data/doc/release_notes/3.41.0.txt +155 -0
- data/doc/schema_modification.rdoc +585 -0
- data/doc/sharding.rdoc +99 -8
- data/doc/sql.rdoc +154 -112
- data/doc/testing.rdoc +169 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/validations.rdoc +1 -1
- data/doc/virtual_rows.rdoc +78 -43
- data/lib/sequel/adapters/ado/access.rb +334 -0
- data/lib/sequel/adapters/ado/mssql.rb +18 -12
- data/lib/sequel/adapters/ado.rb +69 -21
- data/lib/sequel/adapters/amalgalite.rb +17 -14
- data/lib/sequel/adapters/cubrid.rb +143 -0
- data/lib/sequel/adapters/db2.rb +178 -85
- data/lib/sequel/adapters/dbi.rb +19 -21
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +2 -61
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +20 -39
- data/lib/sequel/adapters/firebird.rb +33 -210
- data/lib/sequel/adapters/ibmdb.rb +452 -0
- data/lib/sequel/adapters/informix.rb +6 -29
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/cubrid.rb +52 -0
- data/lib/sequel/adapters/jdbc/db2.rb +78 -0
- data/lib/sequel/adapters/jdbc/derby.rb +325 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +68 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +189 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +40 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +16 -28
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +77 -52
- 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 +72 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +308 -114
- data/lib/sequel/adapters/mock.rb +372 -0
- data/lib/sequel/adapters/mysql.rb +130 -230
- data/lib/sequel/adapters/mysql2.rb +64 -63
- data/lib/sequel/adapters/odbc/mssql.rb +17 -9
- data/lib/sequel/adapters/odbc.rb +29 -10
- data/lib/sequel/adapters/openbase.rb +7 -11
- data/lib/sequel/adapters/oracle.rb +364 -57
- data/lib/sequel/adapters/postgres.rb +379 -145
- data/lib/sequel/adapters/shared/access.rb +236 -11
- data/lib/sequel/adapters/shared/cubrid.rb +216 -0
- data/lib/sequel/adapters/shared/db2.rb +353 -0
- data/lib/sequel/adapters/shared/firebird.rb +231 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +412 -116
- data/lib/sequel/adapters/shared/mysql.rb +396 -89
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +166 -0
- data/lib/sequel/adapters/shared/oracle.rb +262 -45
- data/lib/sequel/adapters/shared/postgres.rb +688 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +317 -96
- data/lib/sequel/adapters/sqlite.rb +109 -46
- data/lib/sequel/adapters/swift/mysql.rb +10 -12
- data/lib/sequel/adapters/swift/postgres.rb +13 -79
- data/lib/sequel/adapters/swift/sqlite.rb +9 -6
- data/lib/sequel/adapters/swift.rb +31 -27
- data/lib/sequel/adapters/tinytds.rb +177 -25
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +77 -0
- data/lib/sequel/adapters/utils/pg_types.rb +81 -0
- data/lib/sequel/adapters/utils/split_alter_table.rb +36 -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 +23 -13
- data/lib/sequel/connection_pool/sharded_threaded.rb +64 -13
- data/lib/sequel/connection_pool/single.rb +11 -3
- data/lib/sequel/connection_pool/threaded.rb +69 -20
- data/lib/sequel/connection_pool.rb +20 -18
- data/lib/sequel/core.rb +143 -11
- data/lib/sequel/database/connecting.rb +66 -29
- 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 +192 -51
- data/lib/sequel/database/query.rb +299 -114
- data/lib/sequel/database/schema_generator.rb +103 -36
- data/lib/sequel/database/schema_methods.rb +310 -69
- data/lib/sequel/dataset/actions.rb +344 -90
- data/lib/sequel/dataset/features.rb +101 -8
- data/lib/sequel/dataset/graph.rb +42 -15
- data/lib/sequel/dataset/misc.rb +52 -25
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +322 -68
- data/lib/sequel/dataset/sql.rb +716 -293
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +83 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +109 -0
- data/lib/sequel/extensions/blank.rb +4 -0
- data/lib/sequel/extensions/columns_introspection.rb +72 -0
- data/lib/sequel/extensions/connection_validator.rb +109 -0
- data/lib/sequel/extensions/constraint_validations.rb +451 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +30 -46
- data/lib/sequel/extensions/eval_inspect.rb +173 -0
- data/lib/sequel/extensions/inflector.rb +4 -0
- data/lib/sequel/extensions/looser_typecasting.rb +5 -4
- data/lib/sequel/extensions/migration.rb +100 -11
- data/lib/sequel/extensions/named_timezones.rb +9 -0
- data/lib/sequel/extensions/null_dataset.rb +94 -0
- data/lib/sequel/extensions/pagination.rb +4 -0
- data/lib/sequel/extensions/pg_array.rb +539 -0
- data/lib/sequel/extensions/pg_array_ops.rb +263 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +175 -0
- data/lib/sequel/extensions/pg_hstore.rb +334 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +293 -0
- data/lib/sequel/extensions/pg_inet.rb +113 -0
- data/lib/sequel/extensions/pg_interval.rb +191 -0
- data/lib/sequel/extensions/pg_json.rb +212 -0
- data/lib/sequel/extensions/pg_range.rb +513 -0
- data/lib/sequel/extensions/pg_range_ops.rb +150 -0
- data/lib/sequel/extensions/pg_row.rb +576 -0
- data/lib/sequel/extensions/pg_row_ops.rb +182 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +317 -0
- data/lib/sequel/extensions/pretty_table.rb +17 -72
- data/lib/sequel/extensions/query.rb +8 -4
- 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 +282 -76
- data/lib/sequel/extensions/select_remove.rb +39 -0
- data/lib/sequel/extensions/server_block.rb +140 -0
- data/lib/sequel/extensions/split_array_nil.rb +65 -0
- data/lib/sequel/extensions/sql_expr.rb +8 -110
- data/lib/sequel/extensions/string_date_time.rb +4 -0
- data/lib/sequel/extensions/thread_local_timezones.rb +10 -4
- data/lib/sequel/extensions/to_dot.rb +99 -83
- data/lib/sequel/model/associations.rb +1263 -409
- data/lib/sequel/model/base.rb +624 -171
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +24 -2
- 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 +137 -16
- 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/constraint_validations.rb +198 -0
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +72 -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 +134 -15
- 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 +77 -6
- data/lib/sequel/plugins/list.rb +13 -2
- data/lib/sequel/plugins/many_through_many.rb +103 -51
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +150 -66
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/pg_row.rb +121 -0
- data/lib/sequel/plugins/pg_typecast_on_load.rb +65 -0
- data/lib/sequel/plugins/prepared_statements.rb +168 -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 +31 -17
- data/lib/sequel/plugins/schema.rb +8 -3
- 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 +57 -12
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +29 -3
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/touch.rb +18 -6
- data/lib/sequel/plugins/tree.rb +3 -3
- 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 +2 -1
- data/lib/sequel/plugins/validation_helpers.rb +57 -4
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +721 -141
- data/lib/sequel/timezones.rb +67 -40
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +53 -39
- data/spec/adapters/mssql_spec.rb +261 -66
- data/spec/adapters/mysql_spec.rb +597 -352
- data/spec/adapters/oracle_spec.rb +92 -117
- data/spec/adapters/postgres_spec.rb +2103 -341
- data/spec/adapters/spec_helper.rb +16 -6
- data/spec/adapters/sqlite_spec.rb +237 -76
- data/spec/core/connection_pool_spec.rb +270 -141
- data/spec/core/database_spec.rb +980 -524
- data/spec/core/dataset_spec.rb +1658 -1243
- data/spec/core/expression_filters_spec.rb +741 -334
- data/spec/core/mock_adapter_spec.rb +453 -0
- data/spec/core/object_graph_spec.rb +88 -129
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +556 -53
- data/spec/core/spec_helper.rb +7 -48
- data/spec/core_extensions_spec.rb +626 -0
- 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 +227 -33
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/boolean_readers_spec.rb +25 -25
- 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/extensions/connection_validator_spec.rb +118 -0
- data/spec/extensions/constraint_validations_plugin_spec.rb +196 -0
- data/spec/extensions/constraint_validations_spec.rb +316 -0
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +88 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/eval_inspect_spec.rb +67 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +97 -128
- data/spec/extensions/identity_map_spec.rb +142 -24
- 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 -12
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +49 -40
- data/spec/extensions/looser_typecasting_spec.rb +7 -7
- data/spec/extensions/many_through_many_spec.rb +368 -254
- 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 +268 -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 +112 -0
- data/spec/extensions/pg_array_spec.rb +324 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +140 -0
- data/spec/extensions/pg_hstore_spec.rb +200 -0
- data/spec/extensions/pg_inet_spec.rb +47 -0
- data/spec/extensions/pg_interval_spec.rb +72 -0
- data/spec/extensions/pg_json_spec.rb +99 -0
- data/spec/extensions/pg_range_ops_spec.rb +56 -0
- data/spec/extensions/pg_range_spec.rb +395 -0
- data/spec/extensions/pg_row_ops_spec.rb +58 -0
- data/spec/extensions/pg_row_plugin_spec.rb +49 -0
- data/spec/extensions/pg_row_spec.rb +340 -0
- data/spec/extensions/pg_statement_cache_spec.rb +208 -0
- data/spec/extensions/pg_typecast_on_load_spec.rb +58 -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 +167 -0
- data/spec/extensions/query_spec.rb +9 -3
- 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 +434 -49
- data/spec/extensions/schema_spec.rb +30 -49
- 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 +86 -48
- 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 +65 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +34 -67
- data/spec/extensions/split_array_nil_spec.rb +24 -0
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/subclasses_spec.rb +14 -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 +8 -11
- data/spec/extensions/touch_spec.rb +53 -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 +77 -68
- data/spec/extensions/validation_helpers_spec.rb +48 -37
- 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 +25 -5
- data/spec/integration/dataset_test.rb +729 -141
- data/spec/integration/eager_loader_test.rb +38 -54
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +871 -160
- data/spec/integration/prepared_statement_test.rb +272 -128
- data/spec/integration/schema_test.rb +377 -57
- data/spec/integration/spec_helper.rb +49 -32
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +40 -8
- data/spec/model/association_reflection_spec.rb +207 -8
- data/spec/model/associations_spec.rb +1206 -693
- data/spec/model/base_spec.rb +334 -100
- data/spec/model/dataset_methods_spec.rb +45 -27
- data/spec/model/eager_loading_spec.rb +828 -692
- data/spec/model/hooks_spec.rb +355 -67
- data/spec/model/model_spec.rb +251 -179
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +506 -232
- data/spec/model/spec_helper.rb +16 -77
- data/spec/model/validations_spec.rb +38 -2
- data/spec/rcov.opts +2 -0
- metadata +223 -81
- data/spec/core/core_sql_spec.rb +0 -451
|
@@ -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,12 +79,43 @@ 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
|
+
|
|
103
|
+
# The key to use for the key hash when eager loading
|
|
104
|
+
def eager_loader_key
|
|
105
|
+
self[:eager_loader_key]
|
|
106
|
+
end
|
|
107
|
+
|
|
82
108
|
# By default associations do not need to select a key in an associated table
|
|
83
109
|
# to eagerly load.
|
|
84
110
|
def eager_loading_use_associated_key?
|
|
85
111
|
false
|
|
86
112
|
end
|
|
87
113
|
|
|
114
|
+
# Alias of predicate_key, only for backwards compatibility.
|
|
115
|
+
def eager_loading_predicate_key
|
|
116
|
+
predicate_key
|
|
117
|
+
end
|
|
118
|
+
|
|
88
119
|
# Whether to eagerly graph a lazy dataset, true by default. If this
|
|
89
120
|
# is false, the association won't respect the :eager_graph option
|
|
90
121
|
# when loading the association for a single record.
|
|
@@ -92,11 +123,49 @@ module Sequel
|
|
|
92
123
|
true
|
|
93
124
|
end
|
|
94
125
|
|
|
126
|
+
# The limit and offset for this association (returned as a two element array).
|
|
127
|
+
def limit_and_offset
|
|
128
|
+
if (v = self[:limit]).is_a?(Array)
|
|
129
|
+
v
|
|
130
|
+
else
|
|
131
|
+
[v, nil]
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
95
135
|
# Whether the associated object needs a primary key to be added/removed,
|
|
96
136
|
# false by default.
|
|
97
137
|
def need_associated_primary_key?
|
|
98
138
|
false
|
|
99
139
|
end
|
|
140
|
+
|
|
141
|
+
# The keys to use for loading of the regular dataset, as an array.
|
|
142
|
+
def predicate_keys
|
|
143
|
+
cached_fetch(:predicate_keys){Array(predicate_key)}
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Qualify +col+ with the given table name. If +col+ is an array of columns,
|
|
147
|
+
# return an array of qualified columns. Only qualifies Symbols and SQL::Identifier
|
|
148
|
+
# values, other values are not modified.
|
|
149
|
+
def qualify(table, col)
|
|
150
|
+
transform(col) do |k|
|
|
151
|
+
case k
|
|
152
|
+
when Symbol, SQL::Identifier
|
|
153
|
+
SQL::QualifiedIdentifier.new(table, k)
|
|
154
|
+
else
|
|
155
|
+
Sequel::Qualifier.new(self[:model].dataset, table).transform(k)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Qualify col with the associated model's table name.
|
|
161
|
+
def qualify_assoc(col)
|
|
162
|
+
qualify(associated_class.table_name, col)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Qualify col with the current model's table name.
|
|
166
|
+
def qualify_cur(col)
|
|
167
|
+
qualify(self[:model].table_name, col)
|
|
168
|
+
end
|
|
100
169
|
|
|
101
170
|
# Returns the reciprocal association variable, if one exists. The reciprocal
|
|
102
171
|
# association is the association in the associated class that is the opposite
|
|
@@ -105,16 +174,19 @@ module Sequel
|
|
|
105
174
|
# to populate reciprocal associations. For example, when you do this_artist.add_album(album)
|
|
106
175
|
# it sets album.artist to this_artist.
|
|
107
176
|
def reciprocal
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
177
|
+
cached_fetch(:reciprocal) do
|
|
178
|
+
r_types = Array(reciprocal_type)
|
|
179
|
+
keys = self[:keys]
|
|
180
|
+
recip = nil
|
|
181
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
|
182
|
+
if r_types.include?(assoc_reflect[:type]) && assoc_reflect[:keys] == keys && assoc_reflect.associated_class == self[:model]
|
|
183
|
+
cached_set(:reciprocal_type, assoc_reflect[:type])
|
|
184
|
+
recip = assoc_reflect[:name]
|
|
185
|
+
break
|
|
186
|
+
end
|
|
115
187
|
end
|
|
188
|
+
recip
|
|
116
189
|
end
|
|
117
|
-
self[:reciprocal] = nil
|
|
118
190
|
end
|
|
119
191
|
|
|
120
192
|
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
|
@@ -165,6 +237,47 @@ module Sequel
|
|
|
165
237
|
def setter_method
|
|
166
238
|
:"#{self[:name]}="
|
|
167
239
|
end
|
|
240
|
+
|
|
241
|
+
private
|
|
242
|
+
|
|
243
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE != 'ruby'
|
|
244
|
+
# On non-GVL rubies, assume the need to synchronize access. Store the key
|
|
245
|
+
# in a special sub-hash that always uses this method to synchronize access.
|
|
246
|
+
def cached_fetch(key)
|
|
247
|
+
fetch(key) do
|
|
248
|
+
h = self[:cache]
|
|
249
|
+
Sequel.synchronize{return h[key] if h.has_key?(key)}
|
|
250
|
+
value = yield
|
|
251
|
+
Sequel.synchronize{h[key] = value}
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Cache the value at the given key, synchronizing access.
|
|
256
|
+
def cached_set(key, value)
|
|
257
|
+
h = self[:cache]
|
|
258
|
+
Sequel.synchronize{h[key] = value}
|
|
259
|
+
end
|
|
260
|
+
else
|
|
261
|
+
# On MRI, use a plain fetch, since the GVL will synchronize access.
|
|
262
|
+
def cached_fetch(key)
|
|
263
|
+
fetch(key) do
|
|
264
|
+
h = self[:cache]
|
|
265
|
+
h.fetch(key){h[key] = yield}
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# On MRI, just set the value at the key in the cache, since the GVL
|
|
270
|
+
# will synchronize access.
|
|
271
|
+
def cached_set(key, value)
|
|
272
|
+
self[:cache][key] = value
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# If +s+ is an array, map +s+ over the block. Otherwise, just call the
|
|
277
|
+
# block with +s+.
|
|
278
|
+
def transform(s)
|
|
279
|
+
s.is_a?(Array) ? s.map(&Proc.new) : (yield s)
|
|
280
|
+
end
|
|
168
281
|
end
|
|
169
282
|
|
|
170
283
|
class ManyToOneAssociationReflection < AssociationReflection
|
|
@@ -193,21 +306,43 @@ module Sequel
|
|
|
193
306
|
self[:key].nil?
|
|
194
307
|
end
|
|
195
308
|
|
|
196
|
-
#
|
|
197
|
-
def
|
|
198
|
-
|
|
309
|
+
# many_to_one associations don't need an eager limit strategy
|
|
310
|
+
def eager_limit_strategy
|
|
311
|
+
nil
|
|
199
312
|
end
|
|
200
|
-
|
|
313
|
+
|
|
314
|
+
# The expression to use on the left hand side of the IN lookup when eager loading
|
|
315
|
+
def predicate_key
|
|
316
|
+
cached_fetch(:predicate_key){qualified_primary_key}
|
|
317
|
+
end
|
|
318
|
+
|
|
201
319
|
# The column(s) in the associated table that the key in the current table references (either a symbol or an array).
|
|
202
320
|
def primary_key
|
|
203
|
-
|
|
321
|
+
cached_fetch(:primary_key){associated_class.primary_key}
|
|
204
322
|
end
|
|
205
323
|
|
|
206
324
|
# The columns in the associated table that the key in the current table references (always an array).
|
|
207
325
|
def primary_keys
|
|
208
|
-
|
|
326
|
+
cached_fetch(:primary_keys){Array(primary_key)}
|
|
209
327
|
end
|
|
210
328
|
alias associated_object_keys primary_keys
|
|
329
|
+
|
|
330
|
+
# The method symbol or array of method symbols to call on the associated object
|
|
331
|
+
# to get the value to use for the foreign keys.
|
|
332
|
+
def primary_key_method
|
|
333
|
+
cached_fetch(:primary_key_method){primary_key}
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# The array of method symbols to call on the associated object
|
|
337
|
+
# to get the value to use for the foreign keys.
|
|
338
|
+
def primary_key_methods
|
|
339
|
+
cached_fetch(:primary_key_methods){Array(primary_key_method)}
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
# #primary_key qualified by the associated table
|
|
343
|
+
def qualified_primary_key
|
|
344
|
+
cached_fetch(:qualified_primary_key){self[:qualify] == false ? primary_key : qualify_assoc(primary_key)}
|
|
345
|
+
end
|
|
211
346
|
|
|
212
347
|
# True only if the reciprocal is a one_to_many association.
|
|
213
348
|
def reciprocal_array?
|
|
@@ -223,7 +358,7 @@ module Sequel
|
|
|
223
358
|
# True only if the reciprocal is a one_to_one association.
|
|
224
359
|
def set_reciprocal_to_self?
|
|
225
360
|
reciprocal
|
|
226
|
-
|
|
361
|
+
reciprocal_type == :one_to_one
|
|
227
362
|
end
|
|
228
363
|
|
|
229
364
|
private
|
|
@@ -231,7 +366,7 @@ module Sequel
|
|
|
231
366
|
# The reciprocal type of a many_to_one association is either
|
|
232
367
|
# a one_to_many or a one_to_one association.
|
|
233
368
|
def reciprocal_type
|
|
234
|
-
|
|
369
|
+
cached_fetch(:reciprocal_type){[:one_to_many, :one_to_one]}
|
|
235
370
|
end
|
|
236
371
|
end
|
|
237
372
|
|
|
@@ -248,21 +383,27 @@ module Sequel
|
|
|
248
383
|
def can_have_associated_objects?(obj)
|
|
249
384
|
!self[:primary_keys].any?{|k| obj.send(k).nil?}
|
|
250
385
|
end
|
|
251
|
-
|
|
386
|
+
|
|
252
387
|
# Default foreign key name symbol for key in associated table that points to
|
|
253
388
|
# current table's primary key.
|
|
254
389
|
def default_key
|
|
255
390
|
:"#{underscore(demodulize(self[:model].name))}_id"
|
|
256
391
|
end
|
|
257
392
|
|
|
258
|
-
# The key to use for the
|
|
259
|
-
def
|
|
260
|
-
self[:
|
|
393
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))
|
|
394
|
+
def predicate_key
|
|
395
|
+
cached_fetch(:predicate_key){qualify_assoc(self[:key])}
|
|
261
396
|
end
|
|
397
|
+
alias qualified_key predicate_key
|
|
262
398
|
|
|
263
399
|
# The column in the current table that the key in the associated table references.
|
|
264
400
|
def primary_key
|
|
265
|
-
self[:primary_key]
|
|
401
|
+
self[:primary_key]
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
# #primary_key qualified by the current table
|
|
405
|
+
def qualified_primary_key
|
|
406
|
+
cached_fetch(:qualified_primary_key){qualify_cur(primary_key)}
|
|
266
407
|
end
|
|
267
408
|
|
|
268
409
|
# Whether the reciprocal of this association returns an array of objects instead of a single object,
|
|
@@ -297,6 +438,31 @@ module Sequel
|
|
|
297
438
|
class OneToOneAssociationReflection < OneToManyAssociationReflection
|
|
298
439
|
ASSOCIATION_TYPES[:one_to_one] = self
|
|
299
440
|
|
|
441
|
+
# one_to_one associations don't use an eager limit strategy by default, but
|
|
442
|
+
# support both DISTINCT ON and window functions as strategies.
|
|
443
|
+
def eager_limit_strategy
|
|
444
|
+
cached_fetch(:_eager_limit_strategy) do
|
|
445
|
+
case s = self[:eager_limit_strategy]
|
|
446
|
+
when Symbol
|
|
447
|
+
s
|
|
448
|
+
when true
|
|
449
|
+
ds = associated_class.dataset
|
|
450
|
+
if ds.supports_ordered_distinct_on?
|
|
451
|
+
:distinct_on
|
|
452
|
+
elsif ds.supports_window_functions?
|
|
453
|
+
:window_function
|
|
454
|
+
end
|
|
455
|
+
else
|
|
456
|
+
nil
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
# The limit and offset for this association (returned as a two element array).
|
|
462
|
+
def limit_and_offset
|
|
463
|
+
[1, nil]
|
|
464
|
+
end
|
|
465
|
+
|
|
300
466
|
# one_to_one associations return a single object, not an array
|
|
301
467
|
def returns_array?
|
|
302
468
|
false
|
|
@@ -316,13 +482,6 @@ module Sequel
|
|
|
316
482
|
self[:left_key]
|
|
317
483
|
end
|
|
318
484
|
|
|
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
485
|
# Alias of right_primary_keys
|
|
327
486
|
def associated_object_keys
|
|
328
487
|
right_primary_keys
|
|
@@ -357,9 +516,16 @@ module Sequel
|
|
|
357
516
|
:"#{singularize(self[:name])}_id"
|
|
358
517
|
end
|
|
359
518
|
|
|
360
|
-
# The key to use for the
|
|
361
|
-
|
|
362
|
-
|
|
519
|
+
# The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)).
|
|
520
|
+
# The left key qualified by the join table.
|
|
521
|
+
def predicate_key
|
|
522
|
+
cached_fetch(:predicate_key){qualify(join_table_alias, self[:left_key])}
|
|
523
|
+
end
|
|
524
|
+
alias qualified_left_key predicate_key
|
|
525
|
+
|
|
526
|
+
# The right key qualified by the join table.
|
|
527
|
+
def qualified_right_key
|
|
528
|
+
cached_fetch(:qualified_right_key){qualify(join_table_alias, self[:right_key])}
|
|
363
529
|
end
|
|
364
530
|
|
|
365
531
|
# many_to_many associations need to select a key in an associated table to eagerly load
|
|
@@ -367,6 +533,22 @@ module Sequel
|
|
|
367
533
|
true
|
|
368
534
|
end
|
|
369
535
|
|
|
536
|
+
# The source of the join table. This is the join table itself, unless it
|
|
537
|
+
# is aliased, in which case it is the unaliased part.
|
|
538
|
+
def join_table_source
|
|
539
|
+
cached_fetch(:join_table_source){split_join_table_alias[0]}
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
# The join table itself, unless it is aliased, in which case this
|
|
543
|
+
# is the alias.
|
|
544
|
+
def join_table_alias
|
|
545
|
+
cached_fetch(:join_table_alias) do
|
|
546
|
+
s, a = split_join_table_alias
|
|
547
|
+
a || s
|
|
548
|
+
end
|
|
549
|
+
end
|
|
550
|
+
alias associated_key_table join_table_alias
|
|
551
|
+
|
|
370
552
|
# Whether the associated object needs a primary key to be added/removed,
|
|
371
553
|
# true for many_to_many associations.
|
|
372
554
|
def need_associated_primary_key?
|
|
@@ -375,34 +557,60 @@ module Sequel
|
|
|
375
557
|
|
|
376
558
|
# Returns the reciprocal association symbol, if one exists.
|
|
377
559
|
def reciprocal
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
560
|
+
cached_fetch(:reciprocal) do
|
|
561
|
+
left_keys = self[:left_keys]
|
|
562
|
+
right_keys = self[:right_keys]
|
|
563
|
+
join_table = self[:join_table]
|
|
564
|
+
recip = nil
|
|
565
|
+
associated_class.all_association_reflections.each do |assoc_reflect|
|
|
566
|
+
if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_keys] == right_keys &&
|
|
567
|
+
assoc_reflect[:right_keys] == left_keys && assoc_reflect[:join_table] == join_table &&
|
|
568
|
+
assoc_reflect.associated_class == self[:model]
|
|
569
|
+
recip = assoc_reflect[:name]
|
|
570
|
+
break
|
|
571
|
+
end
|
|
387
572
|
end
|
|
573
|
+
recip
|
|
388
574
|
end
|
|
389
|
-
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
# #right_primary_key qualified by the associated table
|
|
578
|
+
def qualified_right_primary_key
|
|
579
|
+
cached_fetch(:qualified_right_primary_key){qualify_assoc(right_primary_key)}
|
|
390
580
|
end
|
|
391
581
|
|
|
392
582
|
# The primary key column(s) to use in the associated table (can be symbol or array).
|
|
393
583
|
def right_primary_key
|
|
394
|
-
|
|
584
|
+
cached_fetch(:right_primary_key){associated_class.primary_key}
|
|
395
585
|
end
|
|
396
586
|
|
|
397
587
|
# The primary key columns to use in the associated table (always array).
|
|
398
588
|
def right_primary_keys
|
|
399
|
-
|
|
589
|
+
cached_fetch(:right_primary_keys){Array(right_primary_key)}
|
|
400
590
|
end
|
|
401
591
|
|
|
592
|
+
# The method symbol or array of method symbols to call on the associated objects
|
|
593
|
+
# to get the foreign key values for the join table.
|
|
594
|
+
def right_primary_key_method
|
|
595
|
+
cached_fetch(:right_primary_key_method){right_primary_key}
|
|
596
|
+
end
|
|
597
|
+
|
|
598
|
+
# The array of method symbols to call on the associated objects
|
|
599
|
+
# to get the foreign key values for the join table.
|
|
600
|
+
def right_primary_key_methods
|
|
601
|
+
cached_fetch(:right_primary_key_methods){Array(right_primary_key_method)}
|
|
602
|
+
end
|
|
603
|
+
|
|
402
604
|
# The columns to select when loading the association, associated_class.table_name.* by default.
|
|
403
605
|
def select
|
|
404
|
-
|
|
405
|
-
|
|
606
|
+
cached_fetch(:select){Sequel::SQL::ColumnAll.new(associated_class.table_name)}
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
private
|
|
610
|
+
|
|
611
|
+
# Split the join table into source and alias parts.
|
|
612
|
+
def split_join_table_alias
|
|
613
|
+
associated_class.dataset.split_alias(self[:join_table])
|
|
406
614
|
end
|
|
407
615
|
end
|
|
408
616
|
|
|
@@ -457,6 +665,12 @@ module Sequel
|
|
|
457
665
|
# Project.association_reflection(:portfolio)
|
|
458
666
|
# => {:type => :many_to_one, :name => :portfolio, ...}
|
|
459
667
|
#
|
|
668
|
+
# Associations should not have the same names as any of the columns in the
|
|
669
|
+
# model's current table they reference. If you are dealing with an existing schema that
|
|
670
|
+
# has a column named status, you can't name the association status, you'd
|
|
671
|
+
# have to name it foo_status or something else. If you give an association the same name
|
|
672
|
+
# as a column, you will probably end up with an association that doesn't work, or a SystemStackError.
|
|
673
|
+
#
|
|
460
674
|
# For a more in depth general overview, as well as a reference guide,
|
|
461
675
|
# see the {Association Basics guide}[link:files/doc/association_basics_rdoc.html].
|
|
462
676
|
# For examples of advanced usage, see the {Advanced Associations guide}[link:files/doc/advanced_associations_rdoc.html].
|
|
@@ -464,181 +678,252 @@ module Sequel
|
|
|
464
678
|
# All association reflections defined for this model (default: {}).
|
|
465
679
|
attr_reader :association_reflections
|
|
466
680
|
|
|
681
|
+
# The default :eager_limit_strategy option to use for *_many associations (default: nil)
|
|
682
|
+
attr_accessor :default_eager_limit_strategy
|
|
683
|
+
|
|
467
684
|
# Array of all association reflections for this model class
|
|
468
685
|
def all_association_reflections
|
|
469
686
|
association_reflections.values
|
|
470
687
|
end
|
|
471
688
|
|
|
689
|
+
# Given an association reflection and a dataset, apply the
|
|
690
|
+
# :select, :conditions, :order, :eager, :distinct, and :eager_block
|
|
691
|
+
# association options to the given dataset and return the dataset
|
|
692
|
+
# or a modified copy of it.
|
|
693
|
+
def apply_association_dataset_opts(opts, ds)
|
|
694
|
+
ds = ds.select(*opts.select) if opts.select
|
|
695
|
+
if c = opts[:conditions]
|
|
696
|
+
ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
|
|
697
|
+
end
|
|
698
|
+
ds = ds.order(*opts[:order]) if opts[:order]
|
|
699
|
+
ds = ds.eager(opts[:eager]) if opts[:eager]
|
|
700
|
+
ds = ds.distinct if opts[:distinct]
|
|
701
|
+
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
702
|
+
ds
|
|
703
|
+
end
|
|
704
|
+
|
|
472
705
|
# Associates a related model with the current model. The following types are
|
|
473
706
|
# supported:
|
|
474
707
|
#
|
|
475
|
-
#
|
|
476
|
-
#
|
|
477
|
-
#
|
|
478
|
-
#
|
|
479
|
-
#
|
|
480
|
-
#
|
|
481
|
-
#
|
|
482
|
-
#
|
|
483
|
-
#
|
|
484
|
-
#
|
|
485
|
-
#
|
|
486
|
-
#
|
|
487
|
-
#
|
|
488
|
-
#
|
|
489
|
-
#
|
|
490
|
-
#
|
|
491
|
-
#
|
|
708
|
+
# :many_to_one :: Foreign key in current model's table points to
|
|
709
|
+
# associated model's primary key. Each associated model object can
|
|
710
|
+
# be associated with more than one current model objects. Each current
|
|
711
|
+
# model object can be associated with only one associated model object.
|
|
712
|
+
# :one_to_many :: Foreign key in associated model's table points to this
|
|
713
|
+
# model's primary key. Each current model object can be associated with
|
|
714
|
+
# more than one associated model objects. Each associated model object
|
|
715
|
+
# can be associated with only one current model object.
|
|
716
|
+
# :one_to_one :: Similar to one_to_many in terms of foreign keys, but
|
|
717
|
+
# only one object is associated to the current object through the
|
|
718
|
+
# association. The methods created are similar to many_to_one, except
|
|
719
|
+
# that the one_to_one setter method saves the passed object.
|
|
720
|
+
# :many_to_many :: A join table is used that has a foreign key that points
|
|
721
|
+
# to this model's primary key and a foreign key that points to the
|
|
722
|
+
# associated model's primary key. Each current model object can be
|
|
723
|
+
# associated with many associated model objects, and each associated
|
|
724
|
+
# model object can be associated with many current model objects.
|
|
492
725
|
#
|
|
493
726
|
# 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
|
-
#
|
|
727
|
+
# === All Types
|
|
728
|
+
# :after_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
729
|
+
# after a new item is added to the association.
|
|
730
|
+
# :after_load :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
731
|
+
# after the associated record(s) have been retrieved from the database.
|
|
732
|
+
# :after_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
733
|
+
# after an item is removed from the association.
|
|
734
|
+
# :after_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
735
|
+
# after an item is set using the association setter method.
|
|
736
|
+
# :allow_eager :: If set to false, you cannot load the association eagerly
|
|
737
|
+
# via eager or eager_graph
|
|
738
|
+
# :before_add :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
739
|
+
# before a new item is added to the association.
|
|
740
|
+
# :before_remove :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
741
|
+
# before an item is removed from the association.
|
|
742
|
+
# :before_set :: Symbol, Proc, or array of both/either specifying a callback to call
|
|
743
|
+
# before an item is set using the association setter method.
|
|
744
|
+
# :cartesian_product_number :: the number of joins completed by this association that could cause more
|
|
745
|
+
# than one row for each row in the current table (default: 0 for
|
|
746
|
+
# many_to_one and one_to_one associations, 1 for one_to_many and
|
|
747
|
+
# many_to_many associations).
|
|
748
|
+
# :class :: The associated class or its name as a string or symbol. If not
|
|
749
|
+
# given, uses the association's name, which is camelized (and
|
|
750
|
+
# singularized unless the type is :many_to_one or :one_to_one). If this is specified
|
|
751
|
+
# as a string or symbol, you must specify the full class name (e.g. "SomeModule::MyModel").
|
|
752
|
+
# :clone :: Merge the current options and block into the options and block used in defining
|
|
753
|
+
# the given association. Can be used to DRY up a bunch of similar associations that
|
|
754
|
+
# all share the same options such as :class and :key, while changing the order and block used.
|
|
755
|
+
# :conditions :: The conditions to use to filter the association, can be any argument passed to filter.
|
|
756
|
+
# :dataset :: A proc that is instance_evaled to get the base dataset
|
|
757
|
+
# to use for the _dataset method (before the other options are applied).
|
|
758
|
+
# :distinct :: Use the DISTINCT clause when selecting associating object, both when
|
|
759
|
+
# lazy loading and eager loading via .eager (but not when using .eager_graph).
|
|
760
|
+
# :eager :: The associations to eagerly load via +eager+ when loading the associated object(s).
|
|
761
|
+
# :eager_block :: If given, use the block instead of the default block when
|
|
762
|
+
# eagerly loading. To not use a block when eager loading (when one is used normally),
|
|
763
|
+
# set to nil.
|
|
764
|
+
# :eager_graph :: The associations to eagerly load via +eager_graph+ when loading the associated object(s).
|
|
765
|
+
# many_to_many associations with this option cannot be eagerly loaded via +eager+.
|
|
766
|
+
# :eager_grapher :: A proc to use to implement eager loading via +eager_graph+, overriding the default.
|
|
767
|
+
# Takes one or three arguments. If three arguments, they are a dataset, an alias to use for
|
|
768
|
+
# the table to graph for this association, and the alias that was used for the current table
|
|
769
|
+
# (since you can cascade associations). If one argument, is passed a hash with keys :self,
|
|
770
|
+
# :table_alias, and :implicit_qualifier, corresponding to the three arguments, and an optional
|
|
771
|
+
# additional key :eager_block, a callback accepting one argument, the associated dataset. This
|
|
772
|
+
# is used to customize the association at query time.
|
|
773
|
+
# Should return a copy of the dataset with the association graphed into it.
|
|
774
|
+
# :eager_limit_strategy :: Determines the strategy used for enforcing limits when eager loading associations via
|
|
775
|
+
# the +eager+ method. For one_to_one associations, no strategy is used by default, and
|
|
776
|
+
# for *_many associations, the :ruby strategy is used by default, which still retrieves
|
|
777
|
+
# all records but slices the resulting array after the association is retrieved. You
|
|
778
|
+
# can pass a +true+ value for this option to have Sequel pick what it thinks is the best
|
|
779
|
+
# choice for the database, or specify a specific symbol to manually select a strategy.
|
|
780
|
+
# one_to_one associations support :distinct_on, :window_function, and :correlated_subquery.
|
|
781
|
+
# *_many associations support :ruby, :window_function, and :correlated_subquery.
|
|
782
|
+
# :eager_loader :: A proc to use to implement eager loading, overriding the default. Takes a single hash argument,
|
|
783
|
+
# with at least the keys: :rows, which is an array of current model instances, :associations,
|
|
784
|
+
# which is a hash of dependent associations, :self, which is the dataset doing the eager loading,
|
|
785
|
+
# :eager_block, which is a dynamic callback that should be called with the dataset, and :id_map,
|
|
786
|
+
# which is a mapping of key values to arrays of current model instances. In the proc, the
|
|
787
|
+
# associated records should be queried from the database and the associations cache for each
|
|
788
|
+
# record should be populated.
|
|
789
|
+
# :eager_loader_key :: A symbol for the key column to use to populate the key_hash
|
|
790
|
+
# for the eager loader. Can be set to nil to not populate the key_hash.
|
|
791
|
+
# :extend :: A module or array of modules to extend the dataset with.
|
|
792
|
+
# :graph_alias_base :: The base name to use for the table alias when eager graphing. Defaults to the name
|
|
793
|
+
# of the association. If the alias name has already been used in the query, Sequel will create
|
|
794
|
+
# a unique alias by appending a numeric suffix (e.g. alias_0, alias_1, ...) until the alias is
|
|
795
|
+
# unique.
|
|
796
|
+
# :graph_block :: The block to pass to join_table when eagerly loading
|
|
797
|
+
# the association via +eager_graph+.
|
|
798
|
+
# :graph_conditions :: The additional conditions to use on the SQL join when eagerly loading
|
|
799
|
+
# the association via +eager_graph+. Should be a hash or an array of two element arrays. If not
|
|
800
|
+
# specified, the :conditions option is used if it is a hash or array of two element arrays.
|
|
801
|
+
# :graph_join_type :: The type of SQL join to use when eagerly loading the association via
|
|
802
|
+
# eager_graph. Defaults to :left_outer.
|
|
803
|
+
# :graph_only_conditions :: The conditions to use on the SQL join when eagerly loading
|
|
804
|
+
# the association via +eager_graph+, instead of the default conditions specified by the
|
|
805
|
+
# foreign/primary keys. This option causes the :graph_conditions option to be ignored.
|
|
806
|
+
# :graph_select :: A column or array of columns to select from the associated table
|
|
807
|
+
# when eagerly loading the association via +eager_graph+. Defaults to all
|
|
808
|
+
# columns in the associated table.
|
|
809
|
+
# :limit :: Limit the number of records to the provided value. Use
|
|
810
|
+
# an array with two elements for the value to specify a
|
|
811
|
+
# limit (first element) and an offset (second element).
|
|
812
|
+
# :methods_module :: The module that methods the association creates will be placed into. Defaults
|
|
813
|
+
# to the module containing the model's columns.
|
|
814
|
+
# :order :: the column(s) by which to order the association dataset. Can be a
|
|
815
|
+
# singular column symbol or an array of column symbols.
|
|
816
|
+
# :order_eager_graph :: Whether to add the association's order to the graphed dataset's order when graphing
|
|
817
|
+
# via +eager_graph+. Defaults to true, so set to false to disable.
|
|
818
|
+
# :read_only :: Do not add a setter method (for many_to_one or one_to_one associations),
|
|
819
|
+
# or add_/remove_/remove_all_ methods (for one_to_many and many_to_many associations).
|
|
820
|
+
# :reciprocal :: the symbol name of the reciprocal association,
|
|
821
|
+
# if it exists. By default, Sequel will try to determine it by looking at the
|
|
822
|
+
# associated model's assocations for a association that matches
|
|
823
|
+
# the current association's key(s). Set to nil to not use a reciprocal.
|
|
824
|
+
# :select :: the columns to select. Defaults to the associated class's
|
|
825
|
+
# table_name.* in a many_to_many association, which means it doesn't include the attributes from the
|
|
826
|
+
# join table. If you want to include the join table attributes, you can
|
|
827
|
+
# use this option, but beware that the join table attributes can clash with
|
|
828
|
+
# attributes from the model table, so you should alias any attributes that have
|
|
829
|
+
# the same name in both the join table and the associated table.
|
|
830
|
+
# :validate :: Set to false to not validate when implicitly saving any associated object.
|
|
831
|
+
# === :many_to_one
|
|
832
|
+
# :key :: foreign key in current model's table that references
|
|
833
|
+
# associated model's primary key, as a symbol. Defaults to :"#{name}_id". Can use an
|
|
834
|
+
# array of symbols for a composite key association.
|
|
835
|
+
# :key_column :: Similar to, and usually identical to, :key, but :key refers to the model method
|
|
836
|
+
# to call, where :key_column refers to the underlying column. Should only be
|
|
837
|
+
# used if the the model method differs from the foreign key column, in conjunction
|
|
838
|
+
# with defining a model alias method for the key column.
|
|
839
|
+
# :primary_key :: column in the associated table that :key option references, as a symbol.
|
|
840
|
+
# Defaults to the primary key of the associated table. Can use an
|
|
841
|
+
# array of symbols for a composite key association.
|
|
842
|
+
# :primary_key_method :: the method symbol or array of method symbols to call on the associated
|
|
843
|
+
# object to get the foreign key values. Defaults to :primary_key option.
|
|
844
|
+
# :qualify :: Whether to use qualifier primary keys when loading the association. The default
|
|
845
|
+
# is true, so you must set to false to not qualify. Qualification rarely causes
|
|
846
|
+
# problems, but it's necessary to disable in some cases, such as when you are doing
|
|
847
|
+
# a JOIN USING operation on the column on Oracle.
|
|
848
|
+
# === :one_to_many and :one_to_one
|
|
849
|
+
# :key :: foreign key in associated model's table that references
|
|
850
|
+
# current model's primary key, as a symbol. Defaults to
|
|
851
|
+
# :"#{self.name.underscore}_id". Can use an
|
|
852
|
+
# array of symbols for a composite key association.
|
|
853
|
+
# :key_method :: the method symbol or array of method symbols to call on the associated
|
|
854
|
+
# object to get the foreign key values. Defaults to :key option.
|
|
855
|
+
# :primary_key :: column in the current table that :key option references, as a symbol.
|
|
856
|
+
# Defaults to primary key of the current table. Can use an
|
|
857
|
+
# array of symbols for a composite key association.
|
|
858
|
+
# :primary_key_column :: Similar to, and usually identical to, :primary_key, but :primary_key refers
|
|
859
|
+
# to the model method call, where :primary_key_column refers to the underlying column.
|
|
860
|
+
# Should only be used if the the model method differs from the primary key column, in
|
|
861
|
+
# conjunction with defining a model alias method for the primary key column.
|
|
862
|
+
# === :many_to_many
|
|
863
|
+
# :graph_join_table_block :: The block to pass to +join_table+ for
|
|
864
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
865
|
+
# :graph_join_table_conditions :: The additional conditions to use on the SQL join for
|
|
866
|
+
# the join table when eagerly loading the association via +eager_graph+.
|
|
867
|
+
# Should be a hash or an array of two element arrays.
|
|
868
|
+
# :graph_join_table_join_type :: The type of SQL join to use for the join table when eagerly
|
|
869
|
+
# loading the association via +eager_graph+. Defaults to the
|
|
870
|
+
# :graph_join_type option or :left_outer.
|
|
871
|
+
# :graph_join_table_only_conditions :: The conditions to use on the SQL join for the join
|
|
872
|
+
# table when eagerly loading the association via +eager_graph+,
|
|
873
|
+
# instead of the default conditions specified by the
|
|
874
|
+
# foreign/primary keys. This option causes the
|
|
875
|
+
# :graph_join_table_conditions option to be ignored.
|
|
876
|
+
# :join_table :: name of table that includes the foreign keys to both
|
|
877
|
+
# the current model and the associated model, as a symbol. Defaults to the name
|
|
878
|
+
# of current model and name of associated model, pluralized,
|
|
879
|
+
# underscored, sorted, and joined with '_'.
|
|
880
|
+
# :join_table_block :: proc that can be used to modify the dataset used in the add/remove/remove_all
|
|
881
|
+
# methods. Should accept a dataset argument and return a modified dataset if present.
|
|
882
|
+
# :left_key :: foreign key in join table that points to current model's
|
|
883
|
+
# primary key, as a symbol. Defaults to :"#{self.name.underscore}_id".
|
|
884
|
+
# Can use an array of symbols for a composite key association.
|
|
885
|
+
# :left_primary_key :: column in current table that :left_key points to, as a symbol.
|
|
886
|
+
# Defaults to primary key of current table. Can use an
|
|
887
|
+
# array of symbols for a composite key association.
|
|
888
|
+
# :left_primary_key_column :: Similar to, and usually identical to, :left_primary_key, but :left_primary_key refers to
|
|
889
|
+
# the model method to call, where :left_primary_key_column refers to the underlying column. Should only
|
|
890
|
+
# be used if the model method differs from the left primary key column, in conjunction
|
|
891
|
+
# with defining a model alias method for the left primary key column.
|
|
892
|
+
# :right_key :: foreign key in join table that points to associated
|
|
893
|
+
# model's primary key, as a symbol. Defaults to :"#{name.to_s.singularize}_id".
|
|
894
|
+
# Can use an array of symbols for a composite key association.
|
|
895
|
+
# :right_primary_key :: column in associated table that :right_key points to, as a symbol.
|
|
896
|
+
# Defaults to primary key of the associated table. Can use an
|
|
897
|
+
# array of symbols for a composite key association.
|
|
898
|
+
# :right_primary_key_method :: the method symbol or array of method symbols to call on the associated
|
|
899
|
+
# object to get the foreign key values for the join table.
|
|
900
|
+
# Defaults to :right_primary_key option.
|
|
901
|
+
# :uniq :: Adds a after_load callback that makes the array of objects unique.
|
|
626
902
|
def associate(type, name, opts = {}, &block)
|
|
627
903
|
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
904
|
raise(Error, 'invalid association type') unless assoc_class = ASSOCIATION_TYPES[type]
|
|
629
905
|
raise(Error, 'Model.associate name argument must be a symbol') unless Symbol === name
|
|
630
906
|
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
|
-
|
|
907
|
+
raise(Error, ':eager_grapher option must have an arity of 1 or 3') if opts[:eager_grapher] && ![1, 3].include?(opts[:eager_grapher].arity)
|
|
908
|
+
|
|
632
909
|
# dup early so we don't modify opts
|
|
633
910
|
orig_opts = opts.dup
|
|
634
|
-
|
|
635
|
-
|
|
911
|
+
if opts[:clone]
|
|
912
|
+
cloned_assoc = association_reflection(opts[:clone])
|
|
913
|
+
raise(Error, "cannot clone an association to an association of different type (association #{name} with type #{type} cloning #{opts[:clone]} with type #{cloned_assoc[:type]})") unless cloned_assoc[:type] == type || [cloned_assoc[:type], type].all?{|t| [:one_to_many, :one_to_one].include?(t)}
|
|
914
|
+
orig_opts = cloned_assoc[:orig_opts].merge(orig_opts)
|
|
915
|
+
end
|
|
916
|
+
opts = orig_opts.merge(:type => type, :name => name, :cache=>{}, :model => self)
|
|
636
917
|
opts[:block] = block if block
|
|
637
918
|
opts = assoc_class.new.merge!(opts)
|
|
638
919
|
opts[:eager_block] = block unless opts.include?(:eager_block)
|
|
920
|
+
if !opts.has_key?(:predicate_key) && opts.has_key?(:eager_loading_predicate_key)
|
|
921
|
+
opts[:predicate_key] = opts[:eager_loading_predicate_key]
|
|
922
|
+
end
|
|
639
923
|
opts[:graph_join_type] ||= :left_outer
|
|
640
924
|
opts[:order_eager_graph] = true unless opts.include?(:order_eager_graph)
|
|
641
925
|
conds = opts[:conditions]
|
|
926
|
+
opts[:graph_alias_base] ||= name
|
|
642
927
|
opts[:graph_conditions] = conds if !opts.include?(:graph_conditions) and Sequel.condition_specifier?(conds)
|
|
643
928
|
opts[:graph_conditions] = opts.fetch(:graph_conditions, []).to_a
|
|
644
929
|
opts[:graph_select] = Array(opts[:graph_select]) if opts[:graph_select]
|
|
@@ -647,6 +932,9 @@ module Sequel
|
|
|
647
932
|
end
|
|
648
933
|
late_binding_class_option(opts, opts.returns_array? ? singularize(name) : name)
|
|
649
934
|
|
|
935
|
+
# Remove :class entry if it exists and is nil, to work with cached_fetch
|
|
936
|
+
opts.delete(:class) unless opts[:class]
|
|
937
|
+
|
|
650
938
|
send(:"def_#{type}", opts)
|
|
651
939
|
|
|
652
940
|
orig_opts.delete(:clone)
|
|
@@ -668,57 +956,120 @@ module Sequel
|
|
|
668
956
|
|
|
669
957
|
# Modify and return eager loading dataset based on association options.
|
|
670
958
|
def eager_loading_dataset(opts, ds, select, associations, eager_options={})
|
|
959
|
+
ds = apply_association_dataset_opts(opts, ds)
|
|
671
960
|
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
961
|
if opts[:eager_graph]
|
|
962
|
+
raise(Error, "cannot eagerly load a #{opts[:type]} association that uses :eager_graph") if opts.eager_loading_use_associated_key?
|
|
679
963
|
ds = ds.eager_graph(opts[:eager_graph])
|
|
680
|
-
|
|
681
|
-
|
|
964
|
+
end
|
|
965
|
+
ds = ds.eager(associations) unless Array(associations).empty?
|
|
966
|
+
ds = eager_options[:eager_block].call(ds) if eager_options[:eager_block]
|
|
967
|
+
if opts.eager_loading_use_associated_key?
|
|
682
968
|
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)})
|
|
969
|
+
ds.select_append(*opts.associated_key_alias.zip(opts.predicate_keys).map{|a, k| SQL::AliasedExpression.new(k, a)})
|
|
685
970
|
else
|
|
686
|
-
ds.
|
|
971
|
+
ds.select_append(SQL::AliasedExpression.new(opts.predicate_key, opts.associated_key_alias))
|
|
687
972
|
end
|
|
688
973
|
end
|
|
689
|
-
ds = ds.eager(associations) unless Array(associations).empty?
|
|
690
|
-
ds = opts[:eager_block].call(ds) if opts[:eager_block]
|
|
691
974
|
ds
|
|
692
975
|
end
|
|
693
976
|
|
|
694
977
|
# Copy the association reflections to the subclass
|
|
695
978
|
def inherited(subclass)
|
|
696
979
|
super
|
|
697
|
-
subclass.instance_variable_set(:@association_reflections,
|
|
980
|
+
subclass.instance_variable_set(:@association_reflections, association_reflections.dup)
|
|
981
|
+
subclass.default_eager_limit_strategy = default_eager_limit_strategy
|
|
698
982
|
end
|
|
699
983
|
|
|
700
|
-
# Shortcut for adding a many_to_many association, see associate
|
|
984
|
+
# Shortcut for adding a many_to_many association, see #associate
|
|
701
985
|
def many_to_many(name, opts={}, &block)
|
|
702
986
|
associate(:many_to_many, name, opts, &block)
|
|
703
987
|
end
|
|
704
988
|
|
|
705
|
-
# Shortcut for adding a many_to_one association, see associate
|
|
989
|
+
# Shortcut for adding a many_to_one association, see #associate
|
|
706
990
|
def many_to_one(name, opts={}, &block)
|
|
707
991
|
associate(:many_to_one, name, opts, &block)
|
|
708
992
|
end
|
|
709
993
|
|
|
710
|
-
# Shortcut for adding a one_to_many association, see associate
|
|
994
|
+
# Shortcut for adding a one_to_many association, see #associate
|
|
711
995
|
def one_to_many(name, opts={}, &block)
|
|
712
996
|
associate(:one_to_many, name, opts, &block)
|
|
713
997
|
end
|
|
714
998
|
|
|
715
|
-
# Shortcut for adding a one_to_one association, see associate.
|
|
999
|
+
# Shortcut for adding a one_to_one association, see #associate.
|
|
716
1000
|
def one_to_one(name, opts={}, &block)
|
|
717
1001
|
associate(:one_to_one, name, opts, &block)
|
|
718
1002
|
end
|
|
719
1003
|
|
|
720
1004
|
private
|
|
721
1005
|
|
|
1006
|
+
# Use a correlated subquery to limit the results of the eager loading dataset.
|
|
1007
|
+
def apply_correlated_subquery_eager_limit_strategy(ds, opts)
|
|
1008
|
+
klass = opts.associated_class
|
|
1009
|
+
kds = klass.dataset
|
|
1010
|
+
dsa = ds.send(:dataset_alias, 1)
|
|
1011
|
+
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
|
|
1012
|
+
pka = Array(pk)
|
|
1013
|
+
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?
|
|
1014
|
+
table = kds.opts[:from]
|
|
1015
|
+
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
|
|
1016
|
+
table = table.first
|
|
1017
|
+
if order = ds.opts[:order]
|
|
1018
|
+
oproc = lambda do |x|
|
|
1019
|
+
case x
|
|
1020
|
+
when Symbol
|
|
1021
|
+
t, c, a = ds.send(:split_symbol, x)
|
|
1022
|
+
if t && t.to_sym == table
|
|
1023
|
+
SQL::QualifiedIdentifier.new(dsa, c)
|
|
1024
|
+
else
|
|
1025
|
+
x
|
|
1026
|
+
end
|
|
1027
|
+
when SQL::QualifiedIdentifier
|
|
1028
|
+
if x.table == table
|
|
1029
|
+
SQL::QualifiedIdentifier.new(dsa, x.column)
|
|
1030
|
+
else
|
|
1031
|
+
x
|
|
1032
|
+
end
|
|
1033
|
+
when SQL::OrderedExpression
|
|
1034
|
+
SQL::OrderedExpression.new(oproc.call(x.expression), x.descending, :nulls=>x.nulls)
|
|
1035
|
+
else
|
|
1036
|
+
x
|
|
1037
|
+
end
|
|
1038
|
+
end
|
|
1039
|
+
order = order.map(&oproc)
|
|
1040
|
+
end
|
|
1041
|
+
limit, offset = opts.limit_and_offset
|
|
1042
|
+
|
|
1043
|
+
subquery = yield kds.
|
|
1044
|
+
unlimited.
|
|
1045
|
+
from(SQL::AliasedExpression.new(table, dsa)).
|
|
1046
|
+
select(*pka.map{|k| SQL::QualifiedIdentifier.new(dsa, k)}).
|
|
1047
|
+
order(*order).
|
|
1048
|
+
limit(limit, offset)
|
|
1049
|
+
|
|
1050
|
+
pk = if pk.is_a?(Array)
|
|
1051
|
+
pk.map{|k| SQL::QualifiedIdentifier.new(table, k)}
|
|
1052
|
+
else
|
|
1053
|
+
SQL::QualifiedIdentifier.new(table, pk)
|
|
1054
|
+
end
|
|
1055
|
+
ds.filter(pk=>subquery)
|
|
1056
|
+
end
|
|
1057
|
+
|
|
1058
|
+
# Use a window function to limit the results of the eager loading dataset.
|
|
1059
|
+
def apply_window_function_eager_limit_strategy(ds, opts)
|
|
1060
|
+
rn = ds.row_number_column
|
|
1061
|
+
limit, offset = opts.limit_and_offset
|
|
1062
|
+
ds = ds.unordered.select_append{row_number(:over, :partition=>opts.predicate_key, :order=>ds.opts[:order]){}.as(rn)}.from_self
|
|
1063
|
+
ds = if opts[:type] == :one_to_one
|
|
1064
|
+
ds.where(rn => 1)
|
|
1065
|
+
elsif offset
|
|
1066
|
+
offset += 1
|
|
1067
|
+
ds.where(rn => (offset...(offset+limit)))
|
|
1068
|
+
else
|
|
1069
|
+
ds.where{SQL::Identifier.new(rn) <= limit}
|
|
1070
|
+
end
|
|
1071
|
+
end
|
|
1072
|
+
|
|
722
1073
|
# The module to use for the association's methods. Defaults to
|
|
723
1074
|
# the overridable_methods_module.
|
|
724
1075
|
def association_module(opts={})
|
|
@@ -753,9 +1104,9 @@ module Sequel
|
|
|
753
1104
|
def_association_method(opts)
|
|
754
1105
|
end
|
|
755
1106
|
|
|
756
|
-
# Adds the association method to the association methods module.
|
|
1107
|
+
# Adds the association method to the association methods module.
|
|
757
1108
|
def def_association_method(opts)
|
|
758
|
-
association_module_def(opts.association_method, opts){|*
|
|
1109
|
+
association_module_def(opts.association_method, opts){|*dynamic_opts, &block| load_associated_objects(opts, dynamic_opts[0], &block)}
|
|
759
1110
|
end
|
|
760
1111
|
|
|
761
1112
|
# Configures many_to_many association reflection and adds the related association methods
|
|
@@ -767,7 +1118,10 @@ module Sequel
|
|
|
767
1118
|
right = (opts[:right_key] ||= opts.default_right_key)
|
|
768
1119
|
rcks = opts[:right_keys] = Array(right)
|
|
769
1120
|
left_pk = (opts[:left_primary_key] ||= self.primary_key)
|
|
1121
|
+
opts[:eager_loader_key] = left_pk unless opts.has_key?(:eager_loader_key)
|
|
770
1122
|
lcpks = opts[:left_primary_keys] = Array(left_pk)
|
|
1123
|
+
lpkc = opts[:left_primary_key_column] ||= left_pk
|
|
1124
|
+
lpkcs = opts[:left_primary_key_columns] ||= Array(lpkc)
|
|
771
1125
|
raise(Error, "mismatched number of left composite keys: #{lcks.inspect} vs #{lcpks.inspect}") unless lcks.length == lcpks.length
|
|
772
1126
|
if opts[:right_primary_key]
|
|
773
1127
|
rcpks = Array(opts[:right_primary_key])
|
|
@@ -781,15 +1135,28 @@ module Sequel
|
|
|
781
1135
|
graph_jt_conds = opts[:graph_join_table_conditions] = opts.fetch(:graph_join_table_conditions, []).to_a
|
|
782
1136
|
opts[:graph_join_table_join_type] ||= opts[:graph_join_type]
|
|
783
1137
|
opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
|
|
784
|
-
opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) +
|
|
785
|
-
|
|
786
|
-
|
|
1138
|
+
opts[:dataset] ||= proc{opts.associated_class.inner_join(join_table, rcks.zip(opts.right_primary_keys) + opts.predicate_keys.zip(lcpks.map{|k| send(k)}), :qualify=>:deep)}
|
|
1139
|
+
|
|
787
1140
|
opts[:eager_loader] ||= proc do |eo|
|
|
788
|
-
h = eo[:
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
1141
|
+
h = eo[:id_map]
|
|
1142
|
+
rows = eo[:rows]
|
|
1143
|
+
rows.each{|object| object.associations[name] = []}
|
|
1144
|
+
r = rcks.zip(opts.right_primary_keys)
|
|
1145
|
+
l = [[opts.predicate_key, h.keys]]
|
|
1146
|
+
ds = model.eager_loading_dataset(opts, opts.associated_class.inner_join(join_table, r + l, :qualify=>:deep), nil, eo[:associations], eo)
|
|
1147
|
+
case opts.eager_limit_strategy
|
|
1148
|
+
when :window_function
|
|
1149
|
+
delete_rn = true
|
|
1150
|
+
rn = ds.row_number_column
|
|
1151
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1152
|
+
when :correlated_subquery
|
|
1153
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1154
|
+
dsa = ds.send(:dataset_alias, 2)
|
|
1155
|
+
xds.inner_join(join_table, r + lcks.map{|k| [k, SQL::QualifiedIdentifier.new(opts.join_table_alias, k)]}, :table_alias=>dsa, :qualify=>:deep)
|
|
1156
|
+
end
|
|
1157
|
+
end
|
|
1158
|
+
ds.all do |assoc_record|
|
|
1159
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
793
1160
|
hash_key = if uses_lcks
|
|
794
1161
|
left_key_alias.map{|k| assoc_record.values.delete(k)}
|
|
795
1162
|
else
|
|
@@ -798,6 +1165,10 @@ module Sequel
|
|
|
798
1165
|
next unless objects = h[hash_key]
|
|
799
1166
|
objects.each{|object| object.associations[name].push(assoc_record)}
|
|
800
1167
|
end
|
|
1168
|
+
if opts.eager_limit_strategy == :ruby
|
|
1169
|
+
limit, offset = opts.limit_and_offset
|
|
1170
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1171
|
+
end
|
|
801
1172
|
end
|
|
802
1173
|
|
|
803
1174
|
join_type = opts[:graph_join_type]
|
|
@@ -810,9 +1181,10 @@ module Sequel
|
|
|
810
1181
|
jt_only_conditions = opts[:graph_join_table_only_conditions]
|
|
811
1182
|
jt_join_type = opts[:graph_join_table_join_type]
|
|
812
1183
|
jt_graph_block = opts[:graph_join_table_block]
|
|
813
|
-
opts[:eager_grapher] ||= proc do |
|
|
814
|
-
ds =
|
|
815
|
-
ds.graph(
|
|
1184
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1185
|
+
ds = eo[:self]
|
|
1186
|
+
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], :qualify=>:deep, :from_self_alias=>ds.opts[:eager_graph][:master], &jt_graph_block)
|
|
1187
|
+
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], :qualify=>:deep, :join_type=>join_type, &graph_block)
|
|
816
1188
|
end
|
|
817
1189
|
|
|
818
1190
|
def_association_dataset_methods(opts)
|
|
@@ -822,11 +1194,11 @@ module Sequel
|
|
|
822
1194
|
association_module_private_def(opts._add_method, opts) do |o|
|
|
823
1195
|
h = {}
|
|
824
1196
|
lcks.zip(lcpks).each{|k, pk| h[k] = send(pk)}
|
|
825
|
-
rcks.zip(opts.
|
|
1197
|
+
rcks.zip(opts.right_primary_key_methods).each{|k, pk| h[k] = o.send(pk)}
|
|
826
1198
|
_join_table_dataset(opts).insert(h)
|
|
827
1199
|
end
|
|
828
1200
|
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.
|
|
1201
|
+
_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
1202
|
end
|
|
831
1203
|
association_module_private_def(opts._remove_all_method, opts) do
|
|
832
1204
|
_join_table_dataset(opts).filter(lcks.zip(lcpks.map{|k| send(k)})).delete
|
|
@@ -840,21 +1212,26 @@ module Sequel
|
|
|
840
1212
|
def def_many_to_one(opts)
|
|
841
1213
|
name = opts[:name]
|
|
842
1214
|
model = self
|
|
843
|
-
opts[:key] = opts.default_key unless opts.
|
|
1215
|
+
opts[:key] = opts.default_key unless opts.has_key?(:key)
|
|
844
1216
|
key = opts[:key]
|
|
845
|
-
|
|
1217
|
+
opts[:eager_loader_key] = key unless opts.has_key?(:eager_loader_key)
|
|
1218
|
+
cks = opts[:graph_keys] = opts[:keys] = Array(key)
|
|
1219
|
+
opts[:key_column] ||= key
|
|
1220
|
+
opts[:graph_keys] = opts[:key_columns] = Array(opts[:key_column])
|
|
1221
|
+
opts[:qualified_key] = opts.qualify_cur(key)
|
|
846
1222
|
if opts[:primary_key]
|
|
847
1223
|
cpks = Array(opts[:primary_key])
|
|
848
1224
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
849
1225
|
end
|
|
850
1226
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
1227
|
+
qualify = opts[:qualify] != false
|
|
851
1228
|
opts[:cartesian_product_number] ||= 0
|
|
852
1229
|
opts[:dataset] ||= proc do
|
|
853
1230
|
klass = opts.associated_class
|
|
854
|
-
klass.filter(opts.
|
|
1231
|
+
klass.filter(opts.predicate_keys.zip(cks.map{|k| send(k)}))
|
|
855
1232
|
end
|
|
856
1233
|
opts[:eager_loader] ||= proc do |eo|
|
|
857
|
-
h = eo[:
|
|
1234
|
+
h = eo[:id_map]
|
|
858
1235
|
keys = h.keys
|
|
859
1236
|
# Default the cached association to nil, so any object that doesn't have it
|
|
860
1237
|
# populated will have cached the negative lookup.
|
|
@@ -862,8 +1239,8 @@ module Sequel
|
|
|
862
1239
|
# Skip eager loading if no objects have a foreign key for this association
|
|
863
1240
|
unless keys.empty?
|
|
864
1241
|
klass = opts.associated_class
|
|
865
|
-
model.eager_loading_dataset(opts, klass.filter(
|
|
866
|
-
hash_key = uses_cks ? opts.
|
|
1242
|
+
model.eager_loading_dataset(opts, klass.filter(opts.predicate_key=>keys), nil, eo[:associations], eo).all do |assoc_record|
|
|
1243
|
+
hash_key = uses_cks ? opts.primary_key_methods.map{|k| assoc_record.send(k)} : assoc_record.send(opts.primary_key_method)
|
|
867
1244
|
next unless objects = h[hash_key]
|
|
868
1245
|
objects.each{|object| object.associations[name] = assoc_record}
|
|
869
1246
|
end
|
|
@@ -876,15 +1253,17 @@ module Sequel
|
|
|
876
1253
|
only_conditions = opts[:graph_only_conditions]
|
|
877
1254
|
conditions = opts[:graph_conditions]
|
|
878
1255
|
graph_block = opts[:graph_block]
|
|
879
|
-
opts[:
|
|
880
|
-
|
|
1256
|
+
graph_cks = opts[:graph_keys]
|
|
1257
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1258
|
+
ds = eo[:self]
|
|
1259
|
+
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, :qualify=>:deep, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
|
|
881
1260
|
end
|
|
882
1261
|
|
|
883
1262
|
def_association_dataset_methods(opts)
|
|
884
1263
|
|
|
885
1264
|
return if opts[:read_only]
|
|
886
1265
|
|
|
887
|
-
association_module_private_def(opts._setter_method, opts){|o| cks.zip(opts.
|
|
1266
|
+
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
1267
|
association_module_def(opts.setter_method, opts){|o| set_associated_object(opts, o)}
|
|
889
1268
|
end
|
|
890
1269
|
|
|
@@ -894,31 +1273,53 @@ module Sequel
|
|
|
894
1273
|
name = opts[:name]
|
|
895
1274
|
model = self
|
|
896
1275
|
key = (opts[:key] ||= opts.default_key)
|
|
1276
|
+
km = opts[:key_method] ||= opts[:key]
|
|
897
1277
|
cks = opts[:keys] = Array(key)
|
|
1278
|
+
opts[:key_methods] = Array(opts[:key_method])
|
|
898
1279
|
primary_key = (opts[:primary_key] ||= self.primary_key)
|
|
1280
|
+
opts[:eager_loader_key] = primary_key unless opts.has_key?(:eager_loader_key)
|
|
899
1281
|
cpks = opts[:primary_keys] = Array(primary_key)
|
|
1282
|
+
pkc = opts[:primary_key_column] ||= primary_key
|
|
1283
|
+
pkcs = opts[:primary_key_columns] ||= Array(pkc)
|
|
900
1284
|
raise(Error, "mismatched number of composite keys: #{cks.inspect} vs #{cpks.inspect}") unless cks.length == cpks.length
|
|
901
1285
|
uses_cks = opts[:uses_composite_keys] = cks.length > 1
|
|
902
1286
|
opts[:dataset] ||= proc do
|
|
903
|
-
|
|
904
|
-
klass.filter(cks.map{|k| SQL::QualifiedIdentifier.new(klass.table_name, k)}.zip(cpks.map{|k| send(k)}))
|
|
1287
|
+
opts.associated_class.filter(opts.predicate_keys.zip(cpks.map{|k| send(k)}))
|
|
905
1288
|
end
|
|
906
1289
|
opts[:eager_loader] ||= proc do |eo|
|
|
907
|
-
h = eo[:
|
|
1290
|
+
h = eo[:id_map]
|
|
1291
|
+
rows = eo[:rows]
|
|
908
1292
|
if one_to_one
|
|
909
|
-
|
|
1293
|
+
rows.each{|object| object.associations[name] = nil}
|
|
910
1294
|
else
|
|
911
|
-
|
|
1295
|
+
rows.each{|object| object.associations[name] = []}
|
|
912
1296
|
end
|
|
913
1297
|
reciprocal = opts.reciprocal
|
|
914
1298
|
klass = opts.associated_class
|
|
915
|
-
|
|
916
|
-
|
|
1299
|
+
filter_keys = opts.predicate_key
|
|
1300
|
+
ds = model.eager_loading_dataset(opts, klass.filter(filter_keys=>h.keys), nil, eo[:associations], eo)
|
|
1301
|
+
case opts.eager_limit_strategy
|
|
1302
|
+
when :distinct_on
|
|
1303
|
+
ds = ds.distinct(*filter_keys).order_prepend(*filter_keys)
|
|
1304
|
+
when :window_function
|
|
1305
|
+
delete_rn = true
|
|
1306
|
+
rn = ds.row_number_column
|
|
1307
|
+
ds = apply_window_function_eager_limit_strategy(ds, opts)
|
|
1308
|
+
when :correlated_subquery
|
|
1309
|
+
ds = apply_correlated_subquery_eager_limit_strategy(ds, opts) do |xds|
|
|
1310
|
+
xds.where(opts.associated_object_keys.map{|k| [SQL::QualifiedIdentifier.new(xds.first_source_alias, k), SQL::QualifiedIdentifier.new(xds.first_source_table, k)]})
|
|
1311
|
+
end
|
|
1312
|
+
end
|
|
1313
|
+
ds.all do |assoc_record|
|
|
1314
|
+
assoc_record.values.delete(rn) if delete_rn
|
|
1315
|
+
hash_key = uses_cks ? km.map{|k| assoc_record.send(k)} : assoc_record.send(km)
|
|
917
1316
|
next unless objects = h[hash_key]
|
|
918
1317
|
if one_to_one
|
|
919
1318
|
objects.each do |object|
|
|
920
|
-
object.associations[name]
|
|
921
|
-
|
|
1319
|
+
unless object.associations[name]
|
|
1320
|
+
object.associations[name] = assoc_record
|
|
1321
|
+
assoc_record.associations[reciprocal] = object if reciprocal
|
|
1322
|
+
end
|
|
922
1323
|
end
|
|
923
1324
|
else
|
|
924
1325
|
objects.each do |object|
|
|
@@ -927,6 +1328,10 @@ module Sequel
|
|
|
927
1328
|
end
|
|
928
1329
|
end
|
|
929
1330
|
end
|
|
1331
|
+
if opts.eager_limit_strategy == :ruby
|
|
1332
|
+
limit, offset = opts.limit_and_offset
|
|
1333
|
+
rows.each{|o| o.associations[name] = o.associations[name].slice(offset||0, limit) || []}
|
|
1334
|
+
end
|
|
930
1335
|
end
|
|
931
1336
|
|
|
932
1337
|
join_type = opts[:graph_join_type]
|
|
@@ -936,10 +1341,11 @@ module Sequel
|
|
|
936
1341
|
conditions = opts[:graph_conditions]
|
|
937
1342
|
opts[:cartesian_product_number] ||= one_to_one ? 0 : 1
|
|
938
1343
|
graph_block = opts[:graph_block]
|
|
939
|
-
opts[:eager_grapher] ||= proc do |
|
|
940
|
-
ds =
|
|
1344
|
+
opts[:eager_grapher] ||= proc do |eo|
|
|
1345
|
+
ds = eo[:self]
|
|
1346
|
+
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, :qualify=>:deep, :from_self_alias=>ds.opts[:eager_graph][:master]), &graph_block)
|
|
941
1347
|
# We only load reciprocals for one_to_many associations, as other reciprocals don't make sense
|
|
942
|
-
ds.opts[:eager_graph][:reciprocals][
|
|
1348
|
+
ds.opts[:eager_graph][:reciprocals][eo[:table_alias]] = opts.reciprocal
|
|
943
1349
|
ds
|
|
944
1350
|
end
|
|
945
1351
|
|
|
@@ -955,7 +1361,7 @@ module Sequel
|
|
|
955
1361
|
association_module_private_def(opts._setter_method, opts) do |o|
|
|
956
1362
|
up_ds = _apply_association_options(opts, opts.associated_class.filter(cks.zip(cpks.map{|k| send(k)})))
|
|
957
1363
|
if o
|
|
958
|
-
up_ds = up_ds.exclude(o.pk_hash)
|
|
1364
|
+
up_ds = up_ds.exclude(o.pk_hash) unless o.new?
|
|
959
1365
|
cks.zip(cpks).each{|k, pk| o.send(:"#{k}=", send(pk))}
|
|
960
1366
|
end
|
|
961
1367
|
checked_transaction do
|
|
@@ -993,6 +1399,15 @@ module Sequel
|
|
|
993
1399
|
association_module_def(opts.remove_method, opts){|o,*args| remove_associated_object(opts, o, *args)}
|
|
994
1400
|
association_module_def(opts.remove_all_method, opts){|*args| remove_all_associated_objects(opts, *args)}
|
|
995
1401
|
end
|
|
1402
|
+
|
|
1403
|
+
# Return dataset to graph into given the association reflection, applying the :callback option if set.
|
|
1404
|
+
def eager_graph_dataset(opts, eager_options)
|
|
1405
|
+
ds = opts.associated_class.dataset
|
|
1406
|
+
if cb = eager_options[:callback]
|
|
1407
|
+
ds = cb.call(ds)
|
|
1408
|
+
end
|
|
1409
|
+
ds
|
|
1410
|
+
end
|
|
996
1411
|
end
|
|
997
1412
|
|
|
998
1413
|
# Instance methods used to implement the associations support.
|
|
@@ -1003,9 +1418,17 @@ module Sequel
|
|
|
1003
1418
|
def associations
|
|
1004
1419
|
@associations ||= {}
|
|
1005
1420
|
end
|
|
1421
|
+
|
|
1422
|
+
# Freeze the associations cache when freezing the object. Note that
|
|
1423
|
+
# retrieving associations after freezing will still work in most cases,
|
|
1424
|
+
# but the associations will not be cached in the association cache.
|
|
1425
|
+
def freeze
|
|
1426
|
+
associations.freeze
|
|
1427
|
+
super
|
|
1428
|
+
end
|
|
1006
1429
|
|
|
1007
|
-
#
|
|
1008
|
-
# an Error if the model has no primary key.
|
|
1430
|
+
# Formally used internally by the associations code, like pk but doesn't raise
|
|
1431
|
+
# an Error if the model has no primary key. Not used any longer, deprecated.
|
|
1009
1432
|
def pk_or_nil
|
|
1010
1433
|
key = primary_key
|
|
1011
1434
|
key.is_a?(Array) ? key.map{|k| @values[k]} : @values[key]
|
|
@@ -1033,6 +1456,15 @@ module Sequel
|
|
|
1033
1456
|
ds
|
|
1034
1457
|
end
|
|
1035
1458
|
|
|
1459
|
+
# Return a dataset for the association after applying any dynamic callback.
|
|
1460
|
+
def _associated_dataset(opts, dynamic_opts)
|
|
1461
|
+
ds = send(opts.dataset_method)
|
|
1462
|
+
if callback = dynamic_opts[:callback]
|
|
1463
|
+
ds = callback.call(ds)
|
|
1464
|
+
end
|
|
1465
|
+
ds
|
|
1466
|
+
end
|
|
1467
|
+
|
|
1036
1468
|
# Return an association dataset for the given association reflection
|
|
1037
1469
|
def _dataset(opts)
|
|
1038
1470
|
raise(Sequel::Error, "model object #{inspect} does not have a primary key") if opts.dataset_need_primary_key? && !pk
|
|
@@ -1041,18 +1473,33 @@ module Sequel
|
|
|
1041
1473
|
|
|
1042
1474
|
# Dataset for the join table of the given many to many association reflection
|
|
1043
1475
|
def _join_table_dataset(opts)
|
|
1044
|
-
ds = model.db.from(opts
|
|
1476
|
+
ds = model.db.from(opts.join_table_source)
|
|
1045
1477
|
opts[:join_table_block] ? opts[:join_table_block].call(ds) : ds
|
|
1046
1478
|
end
|
|
1047
1479
|
|
|
1048
|
-
# Return the associated
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1480
|
+
# Return the associated single object for the given association reflection and dynamic options
|
|
1481
|
+
# (or nil if no associated object).
|
|
1482
|
+
def _load_associated_object(opts, dynamic_opts)
|
|
1483
|
+
_load_associated_object_array(opts, dynamic_opts).first
|
|
1484
|
+
end
|
|
1485
|
+
|
|
1486
|
+
# Load the associated objects for the given association reflection and dynamic options
|
|
1487
|
+
# as an array.
|
|
1488
|
+
def _load_associated_object_array(opts, dynamic_opts)
|
|
1489
|
+
_associated_dataset(opts, dynamic_opts).all
|
|
1490
|
+
end
|
|
1491
|
+
|
|
1492
|
+
# Return the associated objects from the dataset, without association callbacks, reciprocals, and caching.
|
|
1493
|
+
# Still apply the dynamic callback if present.
|
|
1494
|
+
def _load_associated_objects(opts, dynamic_opts={})
|
|
1495
|
+
if opts.can_have_associated_objects?(self)
|
|
1496
|
+
if opts.returns_array?
|
|
1497
|
+
_load_associated_object_array(opts, dynamic_opts)
|
|
1498
|
+
else
|
|
1499
|
+
_load_associated_object(opts, dynamic_opts)
|
|
1055
1500
|
end
|
|
1501
|
+
else
|
|
1502
|
+
[] if opts.returns_array?
|
|
1056
1503
|
end
|
|
1057
1504
|
end
|
|
1058
1505
|
|
|
@@ -1086,6 +1533,7 @@ module Sequel
|
|
|
1086
1533
|
|
|
1087
1534
|
# Add/Set the current object to/as the given object's reciprocal association.
|
|
1088
1535
|
def add_reciprocal_object(opts, o)
|
|
1536
|
+
return if o.frozen?
|
|
1089
1537
|
return unless reciprocal = opts.reciprocal
|
|
1090
1538
|
if opts.reciprocal_array?
|
|
1091
1539
|
if array = o.associations[reciprocal] and !array.include?(self)
|
|
@@ -1113,13 +1561,20 @@ module Sequel
|
|
|
1113
1561
|
end
|
|
1114
1562
|
|
|
1115
1563
|
# Load the associated objects using the dataset, handling callbacks, reciprocals, and caching.
|
|
1116
|
-
def load_associated_objects(opts,
|
|
1564
|
+
def load_associated_objects(opts, dynamic_opts=nil)
|
|
1565
|
+
if dynamic_opts == true or dynamic_opts == false or dynamic_opts == nil
|
|
1566
|
+
dynamic_opts = {:reload=>dynamic_opts}
|
|
1567
|
+
elsif dynamic_opts.respond_to?(:call)
|
|
1568
|
+
dynamic_opts = {:callback=>dynamic_opts}
|
|
1569
|
+
end
|
|
1570
|
+
if block_given?
|
|
1571
|
+
dynamic_opts = dynamic_opts.merge(:callback=>Proc.new)
|
|
1572
|
+
end
|
|
1117
1573
|
name = opts[:name]
|
|
1118
|
-
if associations.include?(name) and !reload
|
|
1574
|
+
if associations.include?(name) and !dynamic_opts[:callback] and !dynamic_opts[:reload]
|
|
1119
1575
|
associations[name]
|
|
1120
1576
|
else
|
|
1121
|
-
objs = _load_associated_objects(opts)
|
|
1122
|
-
run_association_callbacks(opts, :after_load, objs)
|
|
1577
|
+
objs = _load_associated_objects(opts, dynamic_opts)
|
|
1123
1578
|
if opts.set_reciprocal_to_self?
|
|
1124
1579
|
if opts.returns_array?
|
|
1125
1580
|
objs.each{|o| add_reciprocal_object(opts, o)}
|
|
@@ -1127,7 +1582,14 @@ module Sequel
|
|
|
1127
1582
|
add_reciprocal_object(opts, objs)
|
|
1128
1583
|
end
|
|
1129
1584
|
end
|
|
1130
|
-
|
|
1585
|
+
|
|
1586
|
+
# If the current object is frozen, you can't update the associations
|
|
1587
|
+
# cache. This can cause issues for after_load procs that expect
|
|
1588
|
+
# the objects to be already cached in the associations, but
|
|
1589
|
+
# unfortunately that case cannot be handled.
|
|
1590
|
+
associations[name] = objs unless frozen?
|
|
1591
|
+
run_association_callbacks(opts, :after_load, objs)
|
|
1592
|
+
frozen? ? objs : associations[name]
|
|
1131
1593
|
end
|
|
1132
1594
|
end
|
|
1133
1595
|
|
|
@@ -1202,32 +1664,36 @@ module Sequel
|
|
|
1202
1664
|
end
|
|
1203
1665
|
end
|
|
1204
1666
|
|
|
1205
|
-
# Set the given object as the associated object for the given
|
|
1206
|
-
def
|
|
1207
|
-
|
|
1667
|
+
# Set the given object as the associated object for the given *_to_one association reflection
|
|
1668
|
+
def _set_associated_object(opts, o)
|
|
1669
|
+
a = associations[opts[:name]]
|
|
1670
|
+
return if a && a == o && !set_associated_object_if_same?
|
|
1208
1671
|
run_association_callbacks(opts, :before_set, o)
|
|
1209
|
-
|
|
1210
|
-
remove_reciprocal_object(opts, a)
|
|
1211
|
-
end
|
|
1672
|
+
remove_reciprocal_object(opts, a) if a
|
|
1212
1673
|
send(opts._setter_method, o)
|
|
1213
1674
|
associations[opts[:name]] = o
|
|
1214
1675
|
add_reciprocal_object(opts, o) if o
|
|
1215
1676
|
run_association_callbacks(opts, :after_set, o)
|
|
1216
1677
|
o
|
|
1217
1678
|
end
|
|
1679
|
+
|
|
1680
|
+
# Whether run the associated object setter code if passed the same object as the one already
|
|
1681
|
+
# cached in the association. Usually not set (so nil), can be set on a per-object basis
|
|
1682
|
+
# if necessary.
|
|
1683
|
+
def set_associated_object_if_same?
|
|
1684
|
+
@set_associated_object_if_same
|
|
1685
|
+
end
|
|
1218
1686
|
|
|
1687
|
+
# Set the given object as the associated object for the given many_to_one association reflection
|
|
1688
|
+
def set_associated_object(opts, o)
|
|
1689
|
+
raise(Error, "associated object #{o.inspect} does not have a primary key") if o && !o.pk
|
|
1690
|
+
_set_associated_object(opts, o)
|
|
1691
|
+
end
|
|
1692
|
+
|
|
1219
1693
|
# Set the given object as the associated object for the given one_to_one association reflection
|
|
1220
1694
|
def set_one_to_one_associated_object(opts, o)
|
|
1221
1695
|
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
|
|
1696
|
+
_set_associated_object(opts, o)
|
|
1231
1697
|
end
|
|
1232
1698
|
end
|
|
1233
1699
|
|
|
@@ -1238,7 +1704,7 @@ module Sequel
|
|
|
1238
1704
|
# time, as it loads associated records using one query per association. However,
|
|
1239
1705
|
# it does not allow you the ability to filter or order based on columns in associated tables. +eager_graph+ loads
|
|
1240
1706
|
# all records in a single query using JOINs, allowing you to filter or order based on columns in associated
|
|
1241
|
-
# tables. However, +eager_graph+
|
|
1707
|
+
# tables. However, +eager_graph+ is usually slower than +eager+, especially if multiple
|
|
1242
1708
|
# one_to_many or many_to_many associations are joined.
|
|
1243
1709
|
#
|
|
1244
1710
|
# You can cascade the eager loading (loading associations on associated objects)
|
|
@@ -1259,12 +1725,80 @@ module Sequel
|
|
|
1259
1725
|
# Artist.eager_graph(:albums=>:tracks).all
|
|
1260
1726
|
# Artist.eager(:albums=>{:tracks=>:genre}).all
|
|
1261
1727
|
# Artist.eager_graph(:albums=>{:tracks=>:genre}).all
|
|
1728
|
+
#
|
|
1729
|
+
# You can also pass a callback as a hash value in order to customize the dataset being
|
|
1730
|
+
# eager loaded at query time, analogous to the way the :eager_block association option
|
|
1731
|
+
# allows you to customize it at association definition time. For example,
|
|
1732
|
+
# if you wanted artists with their albums since 1990:
|
|
1733
|
+
#
|
|
1734
|
+
# Artist.eager(:albums => proc{|ds| ds.filter{year > 1990}})
|
|
1735
|
+
#
|
|
1736
|
+
# Or if you needed albums and their artist's name only, using a single query:
|
|
1737
|
+
#
|
|
1738
|
+
# Albums.eager_graph(:artist => proc{|ds| ds.select(:name)})
|
|
1739
|
+
#
|
|
1740
|
+
# To cascade eager loading while using a callback, you substitute the cascaded
|
|
1741
|
+
# associations with a single entry hash that has the proc callback as the key and
|
|
1742
|
+
# the cascaded associations as the value. This will load artists with their albums
|
|
1743
|
+
# since 1990, and also the tracks on those albums and the genre for those tracks:
|
|
1744
|
+
#
|
|
1745
|
+
# Artist.eager(:albums => {proc{|ds| ds.filter{year > 1990}}=>{:tracks => :genre}})
|
|
1262
1746
|
module DatasetMethods
|
|
1263
1747
|
# Add the <tt>eager!</tt> and <tt>eager_graph!</tt> mutation methods to the dataset.
|
|
1264
1748
|
def self.extended(obj)
|
|
1265
1749
|
obj.def_mutation_method(:eager, :eager_graph)
|
|
1266
1750
|
end
|
|
1267
1751
|
|
|
1752
|
+
# If the expression is in the form <tt>x = y</tt> where +y+ is a <tt>Sequel::Model</tt>
|
|
1753
|
+
# instance, array of <tt>Sequel::Model</tt> instances, or a <tt>Sequel::Model</tt> dataset,
|
|
1754
|
+
# assume +x+ is an association symbol and look up the association reflection
|
|
1755
|
+
# via the dataset's model. From there, return the appropriate SQL based on the type of
|
|
1756
|
+
# association and the values of the foreign/primary keys of +y+. For most association
|
|
1757
|
+
# types, this is a simple transformation, but for +many_to_many+ associations this
|
|
1758
|
+
# creates a subquery to the join table.
|
|
1759
|
+
def complex_expression_sql_append(sql, op, args)
|
|
1760
|
+
r = args.at(1)
|
|
1761
|
+
if (((op == :'=' || op == :'!=') and r.is_a?(Sequel::Model)) ||
|
|
1762
|
+
(multiple = ((op == :IN || op == :'NOT IN') and ((is_ds = r.is_a?(Sequel::Dataset)) or r.all?{|x| x.is_a?(Sequel::Model)}))))
|
|
1763
|
+
l = args.at(0)
|
|
1764
|
+
if ar = model.association_reflections[l]
|
|
1765
|
+
if multiple
|
|
1766
|
+
klass = ar.associated_class
|
|
1767
|
+
if is_ds
|
|
1768
|
+
if r.respond_to?(:model)
|
|
1769
|
+
unless r.model <= klass
|
|
1770
|
+
# A dataset for a different model class, could be a valid regular query
|
|
1771
|
+
return super
|
|
1772
|
+
end
|
|
1773
|
+
else
|
|
1774
|
+
# Not a model dataset, could be a valid regular query
|
|
1775
|
+
return super
|
|
1776
|
+
end
|
|
1777
|
+
else
|
|
1778
|
+
unless r.all?{|x| x.is_a?(klass)}
|
|
1779
|
+
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}"
|
|
1780
|
+
end
|
|
1781
|
+
end
|
|
1782
|
+
elsif !r.is_a?(ar.associated_class)
|
|
1783
|
+
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}"
|
|
1784
|
+
end
|
|
1785
|
+
|
|
1786
|
+
if exp = association_filter_expression(op, ar, r)
|
|
1787
|
+
literal_append(sql, exp)
|
|
1788
|
+
else
|
|
1789
|
+
raise Sequel::Error, "invalid association type #{ar[:type].inspect} for association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1790
|
+
end
|
|
1791
|
+
elsif multiple && (is_ds || r.empty?)
|
|
1792
|
+
# Not a query designed for this support, could be a valid regular query
|
|
1793
|
+
super
|
|
1794
|
+
else
|
|
1795
|
+
raise Sequel::Error, "invalid association #{l.inspect} used in dataset filter for model #{model.inspect}"
|
|
1796
|
+
end
|
|
1797
|
+
else
|
|
1798
|
+
super
|
|
1799
|
+
end
|
|
1800
|
+
end
|
|
1801
|
+
|
|
1268
1802
|
# The preferred eager loading method. Loads all associated records using one
|
|
1269
1803
|
# query for each association.
|
|
1270
1804
|
#
|
|
@@ -1288,8 +1822,7 @@ module Sequel
|
|
|
1288
1822
|
# need to filter based on columns in associated tables, look at +eager_graph+
|
|
1289
1823
|
# or join the tables you need to filter on manually.
|
|
1290
1824
|
#
|
|
1291
|
-
# Each association's order, if defined, is respected.
|
|
1292
|
-
# on a limited dataset, but does not use any :limit options for associations.
|
|
1825
|
+
# Each association's order, if defined, is respected.
|
|
1293
1826
|
# If the association uses a block or has an :eager_block argument, it is used.
|
|
1294
1827
|
def eager(*associations)
|
|
1295
1828
|
opt = @opts[:eager]
|
|
@@ -1311,9 +1844,9 @@ module Sequel
|
|
|
1311
1844
|
# The secondary eager loading method. Loads all associations in a single query. This
|
|
1312
1845
|
# method should only be used if you need to filter or order based on columns in associated tables.
|
|
1313
1846
|
#
|
|
1314
|
-
# This method
|
|
1315
|
-
#
|
|
1316
|
-
# of model objects.
|
|
1847
|
+
# This method uses <tt>Dataset#graph</tt> to create appropriate aliases for columns in all the
|
|
1848
|
+
# tables. Then it uses the graph's metadata to build the associations from the single hash, and
|
|
1849
|
+
# finally replaces the array of hashes with an array model objects inside all.
|
|
1317
1850
|
#
|
|
1318
1851
|
# Be very careful when using this with multiple one_to_many or many_to_many associations, as you can
|
|
1319
1852
|
# create large cartesian products. If you must graph multiple one_to_many and many_to_many associations,
|
|
@@ -1327,18 +1860,18 @@ module Sequel
|
|
|
1327
1860
|
# all objects. You can use the :graph_* association options to modify the SQL query.
|
|
1328
1861
|
#
|
|
1329
1862
|
# 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).
|
|
1863
|
+
# call +each+, it will yield plain hashes, each containing all columns from all the tables.
|
|
1332
1864
|
def eager_graph(*associations)
|
|
1333
|
-
ds = if @opts[:eager_graph]
|
|
1334
|
-
|
|
1865
|
+
ds = if eg = @opts[:eager_graph]
|
|
1866
|
+
eg = eg.dup
|
|
1867
|
+
[:requirements, :reflections, :reciprocals].each{|k| eg[k] = eg[k].dup}
|
|
1868
|
+
clone(:eager_graph=>eg)
|
|
1335
1869
|
else
|
|
1336
1870
|
# Each of the following have a symbol key for the table alias, with the following values:
|
|
1337
1871
|
# :reciprocals - the reciprocal instance variable to use for this association
|
|
1872
|
+
# :reflections - AssociationReflection instance related to this association
|
|
1338
1873
|
# :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})
|
|
1874
|
+
clone(:eager_graph=>{:requirements=>{}, :master=>alias_symbol(first_source), :reflections=>{}, :reciprocals=>{}, :cartesian_product_number=>0})
|
|
1342
1875
|
end
|
|
1343
1876
|
ds.eager_graph_associations(ds, model, ds.opts[:eager_graph][:master], [], *associations)
|
|
1344
1877
|
end
|
|
@@ -1363,23 +1896,42 @@ module Sequel
|
|
|
1363
1896
|
# model :: Current Model
|
|
1364
1897
|
# ta :: table_alias used for the parent association
|
|
1365
1898
|
# requirements :: an array, used as a stack for requirements
|
|
1366
|
-
# r :: association reflection for the current association
|
|
1899
|
+
# r :: association reflection for the current association, or an SQL::AliasedExpression
|
|
1900
|
+
# with the reflection as the expression and the alias base as the aliaz.
|
|
1367
1901
|
# *associations :: any associations dependent on this one
|
|
1368
1902
|
def eager_graph_association(ds, model, ta, requirements, r, *associations)
|
|
1369
|
-
|
|
1903
|
+
if r.is_a?(SQL::AliasedExpression)
|
|
1904
|
+
alias_base = r.aliaz
|
|
1905
|
+
r = r.expression
|
|
1906
|
+
else
|
|
1907
|
+
alias_base = r[:graph_alias_base]
|
|
1908
|
+
end
|
|
1370
1909
|
assoc_name = r[:name]
|
|
1371
|
-
assoc_table_alias = ds.unused_table_alias(
|
|
1372
|
-
|
|
1910
|
+
assoc_table_alias = ds.unused_table_alias(alias_base)
|
|
1911
|
+
loader = r[:eager_grapher]
|
|
1912
|
+
if !associations.empty?
|
|
1913
|
+
if associations.first.respond_to?(:call)
|
|
1914
|
+
callback = associations.first
|
|
1915
|
+
associations = {}
|
|
1916
|
+
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)
|
|
1917
|
+
callback, assoc = pr_assoc
|
|
1918
|
+
associations = assoc.is_a?(Array) ? assoc : [assoc]
|
|
1919
|
+
end
|
|
1920
|
+
end
|
|
1921
|
+
ds = if loader.arity == 1
|
|
1922
|
+
loader.call(:self=>ds, :table_alias=>assoc_table_alias, :implicit_qualifier=>ta, :callback=>callback)
|
|
1923
|
+
else
|
|
1924
|
+
loader.call(ds, assoc_table_alias, ta)
|
|
1925
|
+
end
|
|
1373
1926
|
ds = ds.order_more(*qualified_expression(r[:order], assoc_table_alias)) if r[:order] and r[:order_eager_graph]
|
|
1374
1927
|
eager_graph = ds.opts[:eager_graph]
|
|
1375
1928
|
eager_graph[:requirements][assoc_table_alias] = requirements.dup
|
|
1376
|
-
eager_graph[:
|
|
1377
|
-
eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array?
|
|
1929
|
+
eager_graph[:reflections][assoc_table_alias] = r
|
|
1378
1930
|
eager_graph[:cartesian_product_number] += r[:cartesian_product_number] || 2
|
|
1379
1931
|
ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
|
|
1380
1932
|
ds
|
|
1381
1933
|
end
|
|
1382
|
-
|
|
1934
|
+
|
|
1383
1935
|
# Check the associations are valid for the given model.
|
|
1384
1936
|
# Call eager_graph_association on each association.
|
|
1385
1937
|
#
|
|
@@ -1393,11 +1945,11 @@ module Sequel
|
|
|
1393
1945
|
return ds if associations.empty?
|
|
1394
1946
|
associations.flatten.each do |association|
|
|
1395
1947
|
ds = case association
|
|
1396
|
-
when Symbol
|
|
1397
|
-
ds.eager_graph_association(ds, model, ta, requirements,
|
|
1948
|
+
when Symbol, SQL::AliasedExpression
|
|
1949
|
+
ds.eager_graph_association(ds, model, ta, requirements, eager_graph_check_association(model, association))
|
|
1398
1950
|
when Hash
|
|
1399
1951
|
association.each do |assoc, assoc_assocs|
|
|
1400
|
-
ds = ds.eager_graph_association(ds, model, ta, requirements,
|
|
1952
|
+
ds = ds.eager_graph_association(ds, model, ta, requirements, eager_graph_check_association(model, assoc), assoc_assocs)
|
|
1401
1953
|
end
|
|
1402
1954
|
ds
|
|
1403
1955
|
else raise(Sequel::Error, 'Associations must be in the form of a symbol or hash')
|
|
@@ -1405,21 +1957,263 @@ module Sequel
|
|
|
1405
1957
|
end
|
|
1406
1958
|
ds
|
|
1407
1959
|
end
|
|
1960
|
+
|
|
1961
|
+
# Replace the array of plain hashes with an array of model objects will all eager_graphed
|
|
1962
|
+
# associations set in the associations cache for each object.
|
|
1963
|
+
def eager_graph_build_associations(hashes)
|
|
1964
|
+
hashes.replace(EagerGraphLoader.new(self).load(hashes))
|
|
1965
|
+
end
|
|
1966
|
+
|
|
1967
|
+
private
|
|
1968
|
+
|
|
1969
|
+
# Return an expression for filtering by the given association reflection and associated object.
|
|
1970
|
+
def association_filter_expression(op, ref, obj)
|
|
1971
|
+
meth = :"#{ref[:type]}_association_filter_expression"
|
|
1972
|
+
send(meth, op, ref, obj) if respond_to?(meth, true)
|
|
1973
|
+
end
|
|
1974
|
+
|
|
1975
|
+
# Handle inversion for association filters by returning an inverted expression,
|
|
1976
|
+
# plus also handling cases where the referenced columns are NULL.
|
|
1977
|
+
def association_filter_handle_inversion(op, exp, cols)
|
|
1978
|
+
if op == :'!=' || op == :'NOT IN'
|
|
1979
|
+
if exp == SQL::Constants::FALSE
|
|
1980
|
+
~exp
|
|
1981
|
+
else
|
|
1982
|
+
~exp | Sequel::SQL::BooleanExpression.from_value_pairs(cols.zip([]), :OR)
|
|
1983
|
+
end
|
|
1984
|
+
else
|
|
1985
|
+
exp
|
|
1986
|
+
end
|
|
1987
|
+
end
|
|
1988
|
+
|
|
1989
|
+
# Return an expression for making sure that the given keys match the value of
|
|
1990
|
+
# the given methods for either the single object given or for any of the objects
|
|
1991
|
+
# given if +obj+ is an array.
|
|
1992
|
+
def association_filter_key_expression(keys, meths, obj)
|
|
1993
|
+
vals = if obj.is_a?(Sequel::Dataset)
|
|
1994
|
+
{(keys.length == 1 ? keys.first : keys)=>obj.select(*meths).exclude(Sequel::SQL::BooleanExpression.from_value_pairs(meths.zip([]), :OR))}
|
|
1995
|
+
else
|
|
1996
|
+
vals = Array(obj).reject{|o| !meths.all?{|m| o.send(m)}}
|
|
1997
|
+
return SQL::Constants::FALSE if vals.empty?
|
|
1998
|
+
if obj.is_a?(Array)
|
|
1999
|
+
if keys.length == 1
|
|
2000
|
+
meth = meths.first
|
|
2001
|
+
{keys.first=>vals.map{|o| o.send(meth)}}
|
|
2002
|
+
else
|
|
2003
|
+
{keys=>vals.map{|o| meths.map{|m| o.send(m)}}}
|
|
2004
|
+
end
|
|
2005
|
+
else
|
|
2006
|
+
keys.zip(meths.map{|k| obj.send(k)})
|
|
2007
|
+
end
|
|
2008
|
+
end
|
|
2009
|
+
SQL::BooleanExpression.from_value_pairs(vals)
|
|
2010
|
+
end
|
|
2011
|
+
|
|
2012
|
+
# Make sure the association is valid for this model, and return the related AssociationReflection.
|
|
2013
|
+
def check_association(model, association)
|
|
2014
|
+
raise(Sequel::UndefinedAssociation, "Invalid association #{association} for #{model.name}") unless reflection = model.association_reflection(association)
|
|
2015
|
+
raise(Sequel::Error, "Eager loading is not allowed for #{model.name} association #{association}") if reflection[:allow_eager] == false
|
|
2016
|
+
reflection
|
|
2017
|
+
end
|
|
2018
|
+
|
|
2019
|
+
# Allow associations that are eagerly graphed to be specified as an SQL::AliasedExpression, for
|
|
2020
|
+
# per-call determining of the alias base.
|
|
2021
|
+
def eager_graph_check_association(model, association)
|
|
2022
|
+
if association.is_a?(SQL::AliasedExpression)
|
|
2023
|
+
SQL::AliasedExpression.new(check_association(model, association.expression), association.aliaz)
|
|
2024
|
+
else
|
|
2025
|
+
check_association(model, association)
|
|
2026
|
+
end
|
|
2027
|
+
end
|
|
1408
2028
|
|
|
1409
|
-
#
|
|
1410
|
-
def
|
|
1411
|
-
|
|
1412
|
-
|
|
2029
|
+
# Eagerly load all specified associations
|
|
2030
|
+
def eager_load(a, eager_assoc=@opts[:eager])
|
|
2031
|
+
return if a.empty?
|
|
2032
|
+
# Key is foreign/primary key name symbol
|
|
2033
|
+
# Value is hash with keys being foreign/primary key values (generally integers)
|
|
2034
|
+
# and values being an array of current model objects with that
|
|
2035
|
+
# specific foreign/primary key
|
|
2036
|
+
key_hash = {}
|
|
2037
|
+
# Reflections for all associations to eager load
|
|
2038
|
+
reflections = eager_assoc.keys.collect{|assoc| model.association_reflection(assoc) || (raise Sequel::UndefinedAssociation, "Model: #{self}, Association: #{assoc}")}
|
|
2039
|
+
|
|
2040
|
+
# Populate the key_hash entry for each association being eagerly loaded
|
|
2041
|
+
reflections.each do |r|
|
|
2042
|
+
if key = r.eager_loader_key
|
|
2043
|
+
# key_hash for this key has already been populated,
|
|
2044
|
+
# skip populating again so that duplicate values
|
|
2045
|
+
# aren't added.
|
|
2046
|
+
unless id_map = key_hash[key]
|
|
2047
|
+
id_map = key_hash[key] = Hash.new{|h,k| h[k] = []}
|
|
2048
|
+
|
|
2049
|
+
# Supporting both single (Symbol) and composite (Array) keys.
|
|
2050
|
+
a.each do |rec|
|
|
2051
|
+
case key
|
|
2052
|
+
when Array
|
|
2053
|
+
if (k = key.map{|k2| rec.send(k2)}) && k.all?
|
|
2054
|
+
id_map[k] << rec
|
|
2055
|
+
end
|
|
2056
|
+
when Symbol
|
|
2057
|
+
if k = rec.send(key)
|
|
2058
|
+
id_map[k] << rec
|
|
2059
|
+
end
|
|
2060
|
+
else
|
|
2061
|
+
raise Error, "unhandled eager_loader_key #{key.inspect} for association #{r[:name]}"
|
|
2062
|
+
end
|
|
2063
|
+
end
|
|
2064
|
+
end
|
|
2065
|
+
else
|
|
2066
|
+
id_map = nil
|
|
2067
|
+
end
|
|
2068
|
+
|
|
2069
|
+
loader = r[:eager_loader]
|
|
2070
|
+
associations = eager_assoc[r[:name]]
|
|
2071
|
+
if associations.respond_to?(:call)
|
|
2072
|
+
eager_block = associations
|
|
2073
|
+
associations = {}
|
|
2074
|
+
elsif associations.is_a?(Hash) && associations.length == 1 && (pr_assoc = associations.to_a.first) && pr_assoc.first.respond_to?(:call)
|
|
2075
|
+
eager_block, associations = pr_assoc
|
|
2076
|
+
end
|
|
2077
|
+
if loader.arity == 1
|
|
2078
|
+
loader.call(:key_hash=>key_hash, :rows=>a, :associations=>associations, :self=>self, :eager_block=>eager_block, :id_map=>id_map)
|
|
2079
|
+
else
|
|
2080
|
+
loader.call(key_hash, a, associations)
|
|
2081
|
+
end
|
|
2082
|
+
a.each{|object| object.send(:run_association_callbacks, r, :after_load, object.associations[r[:name]])} unless r[:after_load].empty?
|
|
2083
|
+
end
|
|
2084
|
+
end
|
|
2085
|
+
|
|
2086
|
+
# Return plain hashes instead of calling the row_proc if eager_graph is being used.
|
|
2087
|
+
def graph_each(&block)
|
|
2088
|
+
@opts[:eager_graph] ? fetch_rows(select_sql, &block) : super
|
|
2089
|
+
end
|
|
2090
|
+
|
|
2091
|
+
# Return a subquery expression for filering by a many_to_many association
|
|
2092
|
+
def many_to_many_association_filter_expression(op, ref, obj)
|
|
2093
|
+
lpks, lks, rks = ref.values_at(:left_primary_key_columns, :left_keys, :right_keys)
|
|
2094
|
+
jt = ref.join_table_alias
|
|
2095
|
+
lpks = lpks.first if lpks.length == 1
|
|
2096
|
+
lpks = ref.qualify(model.table_name, lpks)
|
|
2097
|
+
|
|
2098
|
+
meths = if obj.is_a?(Sequel::Dataset)
|
|
2099
|
+
ref.qualify(obj.model.table_name, ref.right_primary_keys)
|
|
2100
|
+
else
|
|
2101
|
+
ref.right_primary_key_methods
|
|
2102
|
+
end
|
|
2103
|
+
|
|
2104
|
+
exp = association_filter_key_expression(ref.qualify(jt, rks), meths, obj)
|
|
2105
|
+
if exp == SQL::Constants::FALSE
|
|
2106
|
+
association_filter_handle_inversion(op, exp, Array(lpks))
|
|
2107
|
+
else
|
|
2108
|
+
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))
|
|
2109
|
+
end
|
|
2110
|
+
end
|
|
2111
|
+
|
|
2112
|
+
# Return a simple equality expression for filering by a many_to_one association
|
|
2113
|
+
def many_to_one_association_filter_expression(op, ref, obj)
|
|
2114
|
+
keys = ref.qualify(model.table_name, ref[:key_columns])
|
|
2115
|
+
meths = if obj.is_a?(Sequel::Dataset)
|
|
2116
|
+
ref.qualify(obj.model.table_name, ref.primary_keys)
|
|
2117
|
+
else
|
|
2118
|
+
ref.primary_key_methods
|
|
2119
|
+
end
|
|
2120
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
|
|
2121
|
+
end
|
|
2122
|
+
|
|
2123
|
+
# Return a simple equality expression for filering by a one_to_* association
|
|
2124
|
+
def one_to_many_association_filter_expression(op, ref, obj)
|
|
2125
|
+
keys = ref.qualify(model.table_name, ref[:primary_key_columns])
|
|
2126
|
+
meths = if obj.is_a?(Sequel::Dataset)
|
|
2127
|
+
ref.qualify(obj.model.table_name, ref[:keys])
|
|
2128
|
+
else
|
|
2129
|
+
ref[:key_methods]
|
|
2130
|
+
end
|
|
2131
|
+
association_filter_handle_inversion(op, association_filter_key_expression(keys, meths, obj), keys)
|
|
2132
|
+
end
|
|
2133
|
+
alias one_to_one_association_filter_expression one_to_many_association_filter_expression
|
|
2134
|
+
|
|
2135
|
+
# Build associations from the graph if #eager_graph was used,
|
|
2136
|
+
# and/or load other associations if #eager was used.
|
|
2137
|
+
def post_load(all_records)
|
|
2138
|
+
eager_graph_build_associations(all_records) if @opts[:eager_graph]
|
|
2139
|
+
eager_load(all_records) if @opts[:eager]
|
|
2140
|
+
super
|
|
2141
|
+
end
|
|
2142
|
+
end
|
|
2143
|
+
|
|
2144
|
+
# This class is the internal implementation of eager_graph. It is responsible for taking an array of plain
|
|
2145
|
+
# hashes and returning an array of model objects with all eager_graphed associations already set in the
|
|
2146
|
+
# association cache.
|
|
2147
|
+
class EagerGraphLoader
|
|
2148
|
+
# Hash with table alias symbol keys and after_load hook values
|
|
2149
|
+
attr_reader :after_load_map
|
|
2150
|
+
|
|
2151
|
+
# Hash with table alias symbol keys and association name values
|
|
2152
|
+
attr_reader :alias_map
|
|
2153
|
+
|
|
2154
|
+
# Hash with table alias symbol keys and subhash values mapping column_alias symbols to the
|
|
2155
|
+
# symbol of the real name of the column
|
|
2156
|
+
attr_reader :column_maps
|
|
2157
|
+
|
|
2158
|
+
# Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
|
|
2159
|
+
attr_reader :dependency_map
|
|
2160
|
+
|
|
2161
|
+
# Hash with table alias symbol keys and [limit, offset] values
|
|
2162
|
+
attr_reader :limit_map
|
|
2163
|
+
|
|
2164
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
2165
|
+
# The table alias symbol for the primary model
|
|
2166
|
+
attr_reader :master
|
|
2167
|
+
|
|
2168
|
+
# Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for
|
|
2169
|
+
# composite key tables)
|
|
2170
|
+
attr_reader :primary_keys
|
|
2171
|
+
|
|
2172
|
+
# Hash with table alias symbol keys and reciprocal association symbol values,
|
|
2173
|
+
# used for setting reciprocals for one_to_many associations.
|
|
2174
|
+
attr_reader :reciprocal_map
|
|
2175
|
+
|
|
2176
|
+
# Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols)
|
|
2177
|
+
# to model instances. Used so that only a single model instance is created for each object.
|
|
2178
|
+
attr_reader :records_map
|
|
2179
|
+
|
|
2180
|
+
# Hash with table alias symbol keys and AssociationReflection values
|
|
2181
|
+
attr_reader :reflection_map
|
|
2182
|
+
|
|
2183
|
+
# Hash with table alias symbol keys and callable values used to create model instances
|
|
2184
|
+
attr_reader :row_procs
|
|
2185
|
+
|
|
2186
|
+
# Hash with table alias symbol keys and true/false values, where true means the
|
|
2187
|
+
# association represented by the table alias uses an array of values instead of
|
|
2188
|
+
# a single value (i.e. true => *_many, false => *_to_one).
|
|
2189
|
+
attr_reader :type_map
|
|
2190
|
+
|
|
2191
|
+
# Initialize all of the data structures used during loading.
|
|
2192
|
+
def initialize(dataset)
|
|
2193
|
+
opts = dataset.opts
|
|
2194
|
+
eager_graph = opts[:eager_graph]
|
|
2195
|
+
@master = eager_graph[:master]
|
|
1413
2196
|
requirements = eager_graph[:requirements]
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
2197
|
+
reflection_map = @reflection_map = eager_graph[:reflections]
|
|
2198
|
+
reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
|
|
2199
|
+
@unique = eager_graph[:cartesian_product_number] > 1
|
|
1417
2200
|
|
|
2201
|
+
alias_map = @alias_map = {}
|
|
2202
|
+
type_map = @type_map = {}
|
|
2203
|
+
after_load_map = @after_load_map = {}
|
|
2204
|
+
limit_map = @limit_map = {}
|
|
2205
|
+
reflection_map.each do |k, v|
|
|
2206
|
+
alias_map[k] = v[:name]
|
|
2207
|
+
type_map[k] = v.returns_array?
|
|
2208
|
+
after_load_map[k] = v[:after_load] unless v[:after_load].empty?
|
|
2209
|
+
limit_map[k] = v.limit_and_offset if v[:limit]
|
|
2210
|
+
end
|
|
2211
|
+
|
|
1418
2212
|
# Make dependency map hash out of requirements array for each association.
|
|
1419
2213
|
# This builds a tree of dependencies that will be used for recursion
|
|
1420
2214
|
# to ensure that all parts of the object graph are loaded into the
|
|
1421
2215
|
# appropriate subordinate association.
|
|
1422
|
-
dependency_map = {}
|
|
2216
|
+
@dependency_map = {}
|
|
1423
2217
|
# Sort the associations by requirements length, so that
|
|
1424
2218
|
# requirements are added to the dependency hash before their
|
|
1425
2219
|
# dependencies.
|
|
@@ -1439,140 +2233,200 @@ module Sequel
|
|
|
1439
2233
|
# This mapping is used to make sure that duplicate entries in the
|
|
1440
2234
|
# result set are mapped to a single record. For example, using a
|
|
1441
2235
|
# single one_to_many association with 10 associated records,
|
|
1442
|
-
# the main object
|
|
2236
|
+
# the main object column values appear in the object graph 10 times.
|
|
1443
2237
|
# We map by primary key, if available, or by the object's entire values,
|
|
1444
2238
|
# if not. The mapping must be per table, so create sub maps for each table
|
|
1445
2239
|
# alias.
|
|
1446
|
-
records_map = {master=>{}}
|
|
2240
|
+
records_map = {@master=>{}}
|
|
1447
2241
|
alias_map.keys.each{|ta| records_map[ta] = {}}
|
|
2242
|
+
@records_map = records_map
|
|
2243
|
+
|
|
2244
|
+
datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
|
|
2245
|
+
column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
|
|
2246
|
+
primary_keys = {}
|
|
2247
|
+
column_maps = {}
|
|
2248
|
+
models = {}
|
|
2249
|
+
row_procs = {}
|
|
2250
|
+
datasets.each do |ta, ds|
|
|
2251
|
+
models[ta] = ds.model
|
|
2252
|
+
primary_keys[ta] = []
|
|
2253
|
+
column_maps[ta] = {}
|
|
2254
|
+
row_procs[ta] = ds.row_proc
|
|
2255
|
+
end
|
|
2256
|
+
column_aliases.each do |col_alias, tc|
|
|
2257
|
+
ta, column = tc
|
|
2258
|
+
column_maps[ta][col_alias] = column
|
|
2259
|
+
end
|
|
2260
|
+
column_maps.each do |ta, h|
|
|
2261
|
+
pk = models[ta].primary_key
|
|
2262
|
+
if pk.is_a?(Array)
|
|
2263
|
+
primary_keys[ta] = []
|
|
2264
|
+
h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
|
|
2265
|
+
else
|
|
2266
|
+
h.select{|ca, c| primary_keys[ta] = ca if pk == c}
|
|
2267
|
+
end
|
|
2268
|
+
end
|
|
2269
|
+
@column_maps = column_maps
|
|
2270
|
+
@primary_keys = primary_keys
|
|
2271
|
+
@row_procs = row_procs
|
|
2272
|
+
|
|
2273
|
+
# For performance, create two special maps for the master table,
|
|
2274
|
+
# so you can skip a hash lookup.
|
|
2275
|
+
@master_column_map = column_maps[master]
|
|
2276
|
+
@master_primary_keys = primary_keys[master]
|
|
2277
|
+
|
|
2278
|
+
# Add a special hash mapping table alias symbols to 5 element arrays that just
|
|
2279
|
+
# contain the data in other data structures for that table alias. This is
|
|
2280
|
+
# used for performance, to get all values in one hash lookup instead of
|
|
2281
|
+
# separate hash lookups for each data structure.
|
|
2282
|
+
ta_map = {}
|
|
2283
|
+
alias_map.keys.each do |ta|
|
|
2284
|
+
ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
|
|
2285
|
+
end
|
|
2286
|
+
@ta_map = ta_map
|
|
2287
|
+
end
|
|
2288
|
+
|
|
2289
|
+
# Return an array of primary model instances with the associations cache prepopulated
|
|
2290
|
+
# for all model objects (both primary and associated).
|
|
2291
|
+
def load(hashes)
|
|
2292
|
+
master = master()
|
|
1448
2293
|
|
|
2294
|
+
# Assign to local variables for speed increase
|
|
2295
|
+
rp = row_procs[master]
|
|
2296
|
+
rm = records_map[master]
|
|
2297
|
+
dm = dependency_map
|
|
2298
|
+
|
|
1449
2299
|
# This will hold the final record set that we will be replacing the object graph with.
|
|
1450
2300
|
records = []
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
key =
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
2301
|
+
|
|
2302
|
+
hashes.each do |h|
|
|
2303
|
+
unless key = master_pk(h)
|
|
2304
|
+
key = hkey(master_hfor(h))
|
|
2305
|
+
end
|
|
2306
|
+
unless primary_record = rm[key]
|
|
2307
|
+
primary_record = rm[key] = rp.call(master_hfor(h))
|
|
1458
2308
|
# Only add it to the list of records to return if it is a new record
|
|
1459
2309
|
records.push(primary_record)
|
|
1460
2310
|
end
|
|
1461
2311
|
# Build all associations for the current object and it's dependencies
|
|
1462
|
-
|
|
2312
|
+
_load(dm, primary_record, h)
|
|
1463
2313
|
end
|
|
1464
2314
|
|
|
1465
2315
|
# Remove duplicate records from all associations if this graph could possibly be a cartesian product
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
2316
|
+
# Run after_load procs if there are any
|
|
2317
|
+
post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
|
|
2318
|
+
|
|
2319
|
+
records
|
|
1470
2320
|
end
|
|
1471
2321
|
|
|
1472
2322
|
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
|
|
2323
|
+
|
|
2324
|
+
# Recursive method that creates associated model objects and associates them to the current model object.
|
|
2325
|
+
def _load(dependency_map, current, h)
|
|
1490
2326
|
dependency_map.each do |ta, deps|
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
2327
|
+
unless key = pk(ta, h)
|
|
2328
|
+
ta_h = hfor(ta, h)
|
|
2329
|
+
unless ta_h.values.any?
|
|
2330
|
+
assoc_name = alias_map[ta]
|
|
2331
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2332
|
+
assoc[assoc_name] = type_map[ta] ? [] : nil
|
|
2333
|
+
end
|
|
2334
|
+
next
|
|
2335
|
+
end
|
|
2336
|
+
key = hkey(ta_h)
|
|
2337
|
+
end
|
|
2338
|
+
rm, rp, assoc_name, tm, rcm = @ta_map[ta]
|
|
2339
|
+
unless rec = rm[key]
|
|
2340
|
+
rec = rm[key] = rp.call(hfor(ta, h))
|
|
1497
2341
|
end
|
|
1498
|
-
|
|
1499
|
-
if
|
|
1500
|
-
current.associations
|
|
1501
|
-
|
|
1502
|
-
rec.associations[reciprocal] = current
|
|
2342
|
+
|
|
2343
|
+
if tm
|
|
2344
|
+
unless (assoc = current.associations).has_key?(assoc_name)
|
|
2345
|
+
assoc[assoc_name] = []
|
|
1503
2346
|
end
|
|
2347
|
+
assoc[assoc_name].push(rec)
|
|
2348
|
+
rec.associations[rcm] = current if rcm
|
|
1504
2349
|
else
|
|
1505
|
-
current.associations[assoc_name]
|
|
2350
|
+
current.associations[assoc_name] ||= rec
|
|
1506
2351
|
end
|
|
1507
2352
|
# Recurse into dependencies of the current object
|
|
1508
|
-
|
|
2353
|
+
_load(deps, rec, h) unless deps.empty?
|
|
1509
2354
|
end
|
|
1510
2355
|
end
|
|
1511
2356
|
|
|
2357
|
+
# Return the subhash for the specific table alias +ta+ by parsing the values out of the main hash +h+
|
|
2358
|
+
def hfor(ta, h)
|
|
2359
|
+
out = {}
|
|
2360
|
+
@column_maps[ta].each{|ca, c| out[c] = h[ca]}
|
|
2361
|
+
out
|
|
2362
|
+
end
|
|
2363
|
+
|
|
2364
|
+
# Return a suitable hash key for any subhash +h+, which is an array of values by column order.
|
|
2365
|
+
# This is only used if the primary key cannot be used.
|
|
2366
|
+
def hkey(h)
|
|
2367
|
+
h.sort_by{|x| x[0].to_s}
|
|
2368
|
+
end
|
|
2369
|
+
|
|
2370
|
+
# Return the subhash for the master table by parsing the values out of the main hash +h+
|
|
2371
|
+
def master_hfor(h)
|
|
2372
|
+
out = {}
|
|
2373
|
+
@master_column_map.each{|ca, c| out[c] = h[ca]}
|
|
2374
|
+
out
|
|
2375
|
+
end
|
|
2376
|
+
|
|
2377
|
+
# Return a primary key value for the master table by parsing it out of the main hash +h+.
|
|
2378
|
+
def master_pk(h)
|
|
2379
|
+
x = @master_primary_keys
|
|
2380
|
+
if x.is_a?(Array)
|
|
2381
|
+
unless x == []
|
|
2382
|
+
x = x.map{|ca| h[ca]}
|
|
2383
|
+
x if x.all?
|
|
2384
|
+
end
|
|
2385
|
+
else
|
|
2386
|
+
h[x]
|
|
2387
|
+
end
|
|
2388
|
+
end
|
|
2389
|
+
|
|
2390
|
+
# Return a primary key value for the given table alias by parsing it out of the main hash +h+.
|
|
2391
|
+
def pk(ta, h)
|
|
2392
|
+
x = primary_keys[ta]
|
|
2393
|
+
if x.is_a?(Array)
|
|
2394
|
+
unless x == []
|
|
2395
|
+
x = x.map{|ca| h[ca]}
|
|
2396
|
+
x if x.all?
|
|
2397
|
+
end
|
|
2398
|
+
else
|
|
2399
|
+
h[x]
|
|
2400
|
+
end
|
|
2401
|
+
end
|
|
2402
|
+
|
|
1512
2403
|
# If the result set is the result of a cartesian product, then it is possible that
|
|
1513
2404
|
# there are multiple records for each association when there should only be one.
|
|
1514
2405
|
# In that case, for each object in all associations loaded via +eager_graph+, run
|
|
1515
2406
|
# uniq! on the association to make sure no duplicate records show up.
|
|
1516
2407
|
# Note that this can cause legitimate duplicate records to be removed.
|
|
1517
|
-
def
|
|
2408
|
+
def post_process(records, dependency_map)
|
|
1518
2409
|
records.each do |record|
|
|
1519
2410
|
dependency_map.each do |ta, deps|
|
|
1520
|
-
|
|
1521
|
-
list =
|
|
2411
|
+
assoc_name = alias_map[ta]
|
|
2412
|
+
list = record.send(assoc_name)
|
|
2413
|
+
rec_list = if type_map[ta]
|
|
1522
2414
|
list.uniq!
|
|
2415
|
+
if lo = limit_map[ta]
|
|
2416
|
+
limit, offset = lo
|
|
2417
|
+
list.replace(list[offset||0, limit])
|
|
2418
|
+
end
|
|
2419
|
+
list
|
|
2420
|
+
elsif list
|
|
2421
|
+
[list]
|
|
1523
2422
|
else
|
|
1524
|
-
[
|
|
2423
|
+
[]
|
|
1525
2424
|
end
|
|
1526
|
-
|
|
1527
|
-
|
|
2425
|
+
record.send(:run_association_callbacks, reflection_map[ta], :after_load, list) if after_load_map[ta]
|
|
2426
|
+
post_process(rec_list, deps) if !rec_list.empty? && !deps.empty?
|
|
1528
2427
|
end
|
|
1529
2428
|
end
|
|
1530
2429
|
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
2430
|
end
|
|
1577
2431
|
end
|
|
1578
2432
|
end
|