activerecord 4.2.0 → 6.0.5.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +852 -801
- data/MIT-LICENSE +4 -2
- data/README.rdoc +14 -13
- data/examples/performance.rb +33 -32
- data/examples/simple.rb +5 -4
- data/lib/active_record/advisory_lock_base.rb +18 -0
- data/lib/active_record/aggregations.rb +267 -249
- data/lib/active_record/association_relation.rb +26 -6
- data/lib/active_record/associations/alias_tracker.rb +29 -36
- data/lib/active_record/associations/association.rb +137 -55
- data/lib/active_record/associations/association_scope.rb +110 -132
- data/lib/active_record/associations/belongs_to_association.rb +67 -54
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -12
- data/lib/active_record/associations/builder/association.rb +27 -40
- data/lib/active_record/associations/builder/belongs_to.rb +69 -55
- data/lib/active_record/associations/builder/collection_association.rb +10 -29
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +58 -70
- data/lib/active_record/associations/builder/has_many.rb +8 -4
- data/lib/active_record/associations/builder/has_one.rb +46 -5
- data/lib/active_record/associations/builder/singular_association.rb +16 -10
- data/lib/active_record/associations/collection_association.rb +150 -275
- data/lib/active_record/associations/collection_proxy.rb +253 -152
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +35 -84
- data/lib/active_record/associations/has_many_through_association.rb +62 -80
- data/lib/active_record/associations/has_one_association.rb +62 -49
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +43 -78
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +14 -14
- data/lib/active_record/associations/join_dependency.rb +159 -162
- data/lib/active_record/associations/preloader/association.rb +102 -113
- data/lib/active_record/associations/preloader/through_association.rb +85 -65
- data/lib/active_record/associations/preloader.rb +96 -95
- data/lib/active_record/associations/singular_association.rb +18 -45
- data/lib/active_record/associations/through_association.rb +49 -24
- data/lib/active_record/associations.rb +1737 -1596
- data/lib/active_record/attribute_assignment.rb +57 -185
- data/lib/active_record/attribute_decorators.rb +39 -17
- data/lib/active_record/attribute_methods/before_type_cast.rb +14 -5
- data/lib/active_record/attribute_methods/dirty.rb +174 -134
- data/lib/active_record/attribute_methods/primary_key.rb +90 -84
- data/lib/active_record/attribute_methods/query.rb +6 -5
- data/lib/active_record/attribute_methods/read.rb +20 -77
- data/lib/active_record/attribute_methods/serialization.rb +40 -21
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +61 -37
- data/lib/active_record/attribute_methods/write.rb +33 -56
- data/lib/active_record/attribute_methods.rb +124 -143
- data/lib/active_record/attributes.rb +213 -74
- data/lib/active_record/autosave_association.rb +125 -54
- data/lib/active_record/base.rb +60 -49
- data/lib/active_record/callbacks.rb +101 -76
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +36 -13
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +810 -291
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +26 -8
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +253 -108
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +83 -24
- data/lib/active_record/connection_adapters/abstract/quoting.rb +171 -53
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +6 -4
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +74 -47
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +383 -239
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +79 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +736 -235
- data/lib/active_record/connection_adapters/abstract/transaction.rb +190 -87
- data/lib/active_record/connection_adapters/abstract_adapter.rb +487 -192
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +536 -600
- data/lib/active_record/connection_adapters/column.rb +56 -43
- data/lib/active_record/connection_adapters/connection_specification.rb +174 -153
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +29 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +196 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +71 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +71 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +95 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +88 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +268 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +59 -196
- data/lib/active_record/connection_adapters/postgresql/column.rb +21 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +71 -115
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +49 -57
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +9 -8
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +5 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +17 -13
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +6 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -20
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
- data/lib/active_record/connection_adapters/postgresql/oid/{infinity.rb → oid.rb} +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +32 -11
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +70 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +67 -51
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +9 -5
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid.rb +23 -25
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +144 -47
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +178 -108
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +49 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +465 -291
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +11 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +565 -363
- data/lib/active_record/connection_adapters/schema_cache.rb +72 -25
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +119 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +102 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +137 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +299 -364
- data/lib/active_record/connection_adapters/statement_pool.rb +33 -13
- data/lib/active_record/connection_handling.rb +167 -41
- data/lib/active_record/core.rb +277 -233
- data/lib/active_record/counter_cache.rb +71 -50
- data/lib/active_record/database_configurations/database_config.rb +37 -0
- data/lib/active_record/database_configurations/hash_config.rb +50 -0
- data/lib/active_record/database_configurations/url_config.rb +78 -0
- data/lib/active_record/database_configurations.rb +233 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -106
- data/lib/active_record/enum.rb +172 -89
- data/lib/active_record/errors.rb +189 -53
- data/lib/active_record/explain.rb +22 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +11 -6
- data/lib/active_record/fixture_set/file.rb +35 -9
- data/lib/active_record/fixture_set/model_metadata.rb +33 -0
- data/lib/active_record/fixture_set/render_context.rb +17 -0
- data/lib/active_record/fixture_set/table_row.rb +152 -0
- data/lib/active_record/fixture_set/table_rows.rb +46 -0
- data/lib/active_record/fixtures.rb +225 -497
- data/lib/active_record/gem_version.rb +6 -4
- data/lib/active_record/inheritance.rb +158 -115
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +123 -29
- data/lib/active_record/internal_metadata.rb +53 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +99 -98
- data/lib/active_record/locking/pessimistic.rb +18 -6
- data/lib/active_record/log_subscriber.rb +76 -33
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +87 -0
- data/lib/active_record/middleware/database_selector.rb +74 -0
- data/lib/active_record/migration/command_recorder.rb +166 -91
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +8 -7
- data/lib/active_record/migration.rb +636 -290
- data/lib/active_record/model_schema.rb +344 -112
- data/lib/active_record/nested_attributes.rb +265 -215
- data/lib/active_record/no_touching.rb +15 -2
- data/lib/active_record/null_relation.rb +24 -38
- data/lib/active_record/persistence.rb +559 -125
- data/lib/active_record/query_cache.rb +19 -23
- data/lib/active_record/querying.rb +44 -30
- data/lib/active_record/railtie.rb +166 -47
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +34 -33
- data/lib/active_record/railties/databases.rake +341 -202
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +461 -302
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +206 -55
- data/lib/active_record/relation/calculations.rb +270 -249
- data/lib/active_record/relation/delegation.rb +76 -84
- data/lib/active_record/relation/finder_methods.rb +287 -255
- data/lib/active_record/relation/from_clause.rb +30 -0
- data/lib/active_record/relation/merger.rb +86 -68
- data/lib/active_record/relation/predicate_builder/array_handler.rb +27 -25
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +43 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +18 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +53 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/predicate_builder.rb +112 -92
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +612 -392
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +18 -17
- data/lib/active_record/relation/where_clause.rb +189 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/relation.rb +533 -340
- data/lib/active_record/result.rb +79 -43
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +144 -121
- data/lib/active_record/schema.rb +21 -24
- data/lib/active_record/schema_dumper.rb +112 -93
- data/lib/active_record/schema_migration.rb +24 -20
- data/lib/active_record/scoping/default.rb +98 -82
- data/lib/active_record/scoping/named.rb +91 -33
- data/lib/active_record/scoping.rb +45 -27
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +5 -5
- data/lib/active_record/statement_cache.rb +73 -36
- data/lib/active_record/store.rb +127 -42
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +90 -0
- data/lib/active_record/tasks/database_tasks.rb +309 -99
- data/lib/active_record/tasks/mysql_database_tasks.rb +58 -89
- data/lib/active_record/tasks/postgresql_database_tasks.rb +81 -31
- data/lib/active_record/tasks/sqlite_database_tasks.rb +37 -16
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +243 -0
- data/lib/active_record/timestamp.rb +86 -41
- data/lib/active_record/touch_later.rb +65 -0
- data/lib/active_record/transactions.rb +222 -146
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +126 -0
- data/lib/active_record/type/date.rb +4 -41
- data/lib/active_record/type/date_time.rb +4 -38
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +12 -5
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +29 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +21 -16
- data/lib/active_record/type/type_map.rb +16 -19
- data/lib/active_record/type/unsigned_integer.rb +9 -8
- data/lib/active_record/type.rb +77 -23
- data/lib/active_record/type_caster/connection.rb +34 -0
- data/lib/active_record/type_caster/map.rb +20 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +12 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +43 -46
- data/lib/active_record/validations.rb +38 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +44 -21
- data/lib/arel/alias_predication.rb +9 -0
- data/lib/arel/attributes/attribute.rb +37 -0
- data/lib/arel/attributes.rb +22 -0
- data/lib/arel/collectors/bind.rb +24 -0
- data/lib/arel/collectors/composite.rb +31 -0
- data/lib/arel/collectors/plain_string.rb +20 -0
- data/lib/arel/collectors/sql_string.rb +20 -0
- data/lib/arel/collectors/substitute_binds.rb +28 -0
- data/lib/arel/crud.rb +42 -0
- data/lib/arel/delete_manager.rb +18 -0
- data/lib/arel/errors.rb +9 -0
- data/lib/arel/expressions.rb +29 -0
- data/lib/arel/factory_methods.rb +49 -0
- data/lib/arel/insert_manager.rb +49 -0
- data/lib/arel/math.rb +45 -0
- data/lib/arel/nodes/and.rb +32 -0
- data/lib/arel/nodes/ascending.rb +23 -0
- data/lib/arel/nodes/binary.rb +52 -0
- data/lib/arel/nodes/bind_param.rb +36 -0
- data/lib/arel/nodes/case.rb +55 -0
- data/lib/arel/nodes/casted.rb +50 -0
- data/lib/arel/nodes/comment.rb +29 -0
- data/lib/arel/nodes/count.rb +12 -0
- data/lib/arel/nodes/delete_statement.rb +45 -0
- data/lib/arel/nodes/descending.rb +23 -0
- data/lib/arel/nodes/equality.rb +18 -0
- data/lib/arel/nodes/extract.rb +24 -0
- data/lib/arel/nodes/false.rb +16 -0
- data/lib/arel/nodes/full_outer_join.rb +8 -0
- data/lib/arel/nodes/function.rb +44 -0
- data/lib/arel/nodes/grouping.rb +8 -0
- data/lib/arel/nodes/in.rb +8 -0
- data/lib/arel/nodes/infix_operation.rb +80 -0
- data/lib/arel/nodes/inner_join.rb +8 -0
- data/lib/arel/nodes/insert_statement.rb +37 -0
- data/lib/arel/nodes/join_source.rb +20 -0
- data/lib/arel/nodes/matches.rb +18 -0
- data/lib/arel/nodes/named_function.rb +23 -0
- data/lib/arel/nodes/node.rb +50 -0
- data/lib/arel/nodes/node_expression.rb +13 -0
- data/lib/arel/nodes/outer_join.rb +8 -0
- data/lib/arel/nodes/over.rb +15 -0
- data/lib/arel/nodes/regexp.rb +16 -0
- data/lib/arel/nodes/right_outer_join.rb +8 -0
- data/lib/arel/nodes/select_core.rb +67 -0
- data/lib/arel/nodes/select_statement.rb +41 -0
- data/lib/arel/nodes/sql_literal.rb +16 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/table_alias.rb +27 -0
- data/lib/arel/nodes/terminal.rb +16 -0
- data/lib/arel/nodes/true.rb +16 -0
- data/lib/arel/nodes/unary.rb +45 -0
- data/lib/arel/nodes/unary_operation.rb +20 -0
- data/lib/arel/nodes/unqualified_column.rb +22 -0
- data/lib/arel/nodes/update_statement.rb +41 -0
- data/lib/arel/nodes/values_list.rb +9 -0
- data/lib/arel/nodes/window.rb +126 -0
- data/lib/arel/nodes/with.rb +11 -0
- data/lib/arel/nodes.rb +68 -0
- data/lib/arel/order_predications.rb +13 -0
- data/lib/arel/predications.rb +256 -0
- data/lib/arel/select_manager.rb +271 -0
- data/lib/arel/table.rb +110 -0
- data/lib/arel/tree_manager.rb +72 -0
- data/lib/arel/update_manager.rb +34 -0
- data/lib/arel/visitors/depth_first.rb +203 -0
- data/lib/arel/visitors/dot.rb +296 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +156 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +158 -0
- data/lib/arel/visitors/oracle12.rb +65 -0
- data/lib/arel/visitors/postgresql.rb +109 -0
- data/lib/arel/visitors/sqlite.rb +38 -0
- data/lib/arel/visitors/to_sql.rb +888 -0
- data/lib/arel/visitors/visitor.rb +45 -0
- data/lib/arel/visitors/where_sql.rb +22 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +62 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +26 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +42 -37
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +24 -0
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +11 -8
- data/lib/rails/generators/active_record/migration.rb +30 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +18 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +174 -63
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute.rb +0 -149
- data/lib/active_record/attribute_set/builder.rb +0 -86
- data/lib/active_record/attribute_set.rb +0 -77
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/integer.rb +0 -11
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -30
- data/lib/active_record/type/decimal.rb +0 -40
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -55
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -36
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -101
- data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -22
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -10
- /data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
@@ -1,113 +1,63 @@
|
|
1
|
-
#
|
2
|
-
require 'arel/collectors/bind'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
module ActiveRecord
|
5
|
-
# = Active Record Relation
|
4
|
+
# = Active Record \Relation
|
6
5
|
class Relation
|
7
6
|
MULTI_VALUE_METHODS = [:includes, :eager_load, :preload, :select, :group,
|
8
|
-
:order, :joins, :
|
9
|
-
:extending, :unscope]
|
7
|
+
:order, :joins, :left_outer_joins, :references,
|
8
|
+
:extending, :unscope, :optimizer_hints, :annotate]
|
10
9
|
|
11
|
-
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :
|
12
|
-
:reverse_order, :distinct, :create_with, :
|
13
|
-
INVALID_METHODS_FOR_DELETE_ALL = [:limit, :distinct, :offset, :group, :having]
|
10
|
+
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :reordering,
|
11
|
+
:reverse_order, :distinct, :create_with, :skip_query_cache]
|
14
12
|
|
15
|
-
|
13
|
+
CLAUSE_METHODS = [:where, :having, :from]
|
14
|
+
INVALID_METHODS_FOR_DELETE_ALL = [:distinct, :group, :having]
|
16
15
|
|
16
|
+
VALUE_METHODS = MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS
|
17
|
+
|
18
|
+
include Enumerable
|
17
19
|
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
|
18
20
|
|
19
|
-
attr_reader :table, :klass, :loaded
|
21
|
+
attr_reader :table, :klass, :loaded, :predicate_builder
|
22
|
+
attr_accessor :skip_preloading_value
|
20
23
|
alias :model :klass
|
21
24
|
alias :loaded? :loaded
|
25
|
+
alias :locked? :lock_value
|
22
26
|
|
23
|
-
def initialize(klass, table, values
|
27
|
+
def initialize(klass, table: klass.arel_table, predicate_builder: klass.predicate_builder, values: {})
|
24
28
|
@klass = klass
|
25
29
|
@table = table
|
26
30
|
@values = values
|
27
31
|
@offsets = {}
|
28
32
|
@loaded = false
|
33
|
+
@predicate_builder = predicate_builder
|
34
|
+
@delegate_to_klass = false
|
29
35
|
end
|
30
36
|
|
31
37
|
def initialize_copy(other)
|
32
|
-
|
33
|
-
# https://bugs.ruby-lang.org/issues/7166
|
34
|
-
@values = Hash[@values]
|
35
|
-
@values[:bind] = @values[:bind].dup if @values.key? :bind
|
38
|
+
@values = @values.dup
|
36
39
|
reset
|
37
40
|
end
|
38
41
|
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
if primary_key && Hash === values
|
43
|
-
primary_key_value = values[values.keys.find { |k|
|
44
|
-
k.name == primary_key
|
45
|
-
}]
|
46
|
-
|
47
|
-
if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
|
48
|
-
primary_key_value = connection.next_sequence_value(klass.sequence_name)
|
49
|
-
values[klass.arel_table[klass.primary_key]] = primary_key_value
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
im = arel.create_insert
|
54
|
-
im.into @table
|
55
|
-
|
56
|
-
substitutes, binds = substitute_values values
|
57
|
-
|
58
|
-
if values.empty? # empty insert
|
59
|
-
im.values = Arel.sql(connection.empty_insert_statement_value)
|
60
|
-
else
|
61
|
-
im.insert substitutes
|
62
|
-
end
|
63
|
-
|
64
|
-
@klass.connection.insert(
|
65
|
-
im,
|
66
|
-
'SQL',
|
67
|
-
primary_key,
|
68
|
-
primary_key_value,
|
69
|
-
nil,
|
70
|
-
binds)
|
42
|
+
def arel_attribute(name) # :nodoc:
|
43
|
+
klass.arel_attribute(name, table)
|
71
44
|
end
|
72
45
|
|
73
|
-
def
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
if @klass.finder_needs_type_condition?
|
79
|
-
scope.unscope!(where: @klass.inheritance_column)
|
46
|
+
def bind_attribute(name, value) # :nodoc:
|
47
|
+
if reflection = klass._reflect_on_association(name)
|
48
|
+
name = reflection.foreign_key
|
49
|
+
value = value.read_attribute(reflection.klass.primary_key) unless value.nil?
|
80
50
|
end
|
81
51
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
.arel
|
86
|
-
.compile_update(substitutes, @klass.primary_key)
|
87
|
-
|
88
|
-
@klass.connection.update(
|
89
|
-
um,
|
90
|
-
'SQL',
|
91
|
-
bvs,
|
92
|
-
)
|
93
|
-
end
|
94
|
-
|
95
|
-
def substitute_values(values) # :nodoc:
|
96
|
-
binds = values.map do |arel_attr, value|
|
97
|
-
[@klass.columns_hash[arel_attr.name], value]
|
98
|
-
end
|
99
|
-
|
100
|
-
substitutes = values.each_with_index.map do |(arel_attr, _), i|
|
101
|
-
[arel_attr, @klass.connection.substitute_at(binds[i][0])]
|
102
|
-
end
|
103
|
-
|
104
|
-
[substitutes, binds]
|
52
|
+
attr = arel_attribute(name)
|
53
|
+
bind = predicate_builder.build_bind_attribute(attr.name, value)
|
54
|
+
yield attr, bind
|
105
55
|
end
|
106
56
|
|
107
57
|
# Initializes new record from relation while maintaining the current
|
108
58
|
# scope.
|
109
59
|
#
|
110
|
-
# Expects arguments in the same format as
|
60
|
+
# Expects arguments in the same format as {ActiveRecord::Base.new}[rdoc-ref:Core.new].
|
111
61
|
#
|
112
62
|
# users = User.where(name: 'DHH')
|
113
63
|
# user = users.new # => #<User id: nil, name: "DHH", created_at: nil, updated_at: nil>
|
@@ -116,8 +66,9 @@ module ActiveRecord
|
|
116
66
|
#
|
117
67
|
# user = users.new { |user| user.name = 'Oscar' }
|
118
68
|
# user.name # => Oscar
|
119
|
-
def new(
|
120
|
-
|
69
|
+
def new(attributes = nil, &block)
|
70
|
+
block = _deprecated_scope_block("new", &block)
|
71
|
+
scoping { klass.new(attributes, &block) }
|
121
72
|
end
|
122
73
|
|
123
74
|
alias build new
|
@@ -125,30 +76,44 @@ module ActiveRecord
|
|
125
76
|
# Tries to create a new record with the same scoped attributes
|
126
77
|
# defined in the relation. Returns the initialized object if validation fails.
|
127
78
|
#
|
128
|
-
# Expects arguments in the same format as
|
79
|
+
# Expects arguments in the same format as
|
80
|
+
# {ActiveRecord::Base.create}[rdoc-ref:Persistence::ClassMethods#create].
|
129
81
|
#
|
130
82
|
# ==== Examples
|
83
|
+
#
|
131
84
|
# users = User.where(name: 'Oscar')
|
132
|
-
# users.create # #<User id: 3, name: "
|
85
|
+
# users.create # => #<User id: 3, name: "Oscar", ...>
|
133
86
|
#
|
134
87
|
# users.create(name: 'fxn')
|
135
|
-
# users.create # #<User id: 4, name: "fxn", ...>
|
88
|
+
# users.create # => #<User id: 4, name: "fxn", ...>
|
136
89
|
#
|
137
90
|
# users.create { |user| user.name = 'tenderlove' }
|
138
|
-
# # #<User id: 5, name: "tenderlove", ...>
|
91
|
+
# # => #<User id: 5, name: "tenderlove", ...>
|
139
92
|
#
|
140
93
|
# users.create(name: nil) # validation on name
|
141
|
-
# # #<User id: nil, name: nil, ...>
|
142
|
-
def create(
|
143
|
-
|
94
|
+
# # => #<User id: nil, name: nil, ...>
|
95
|
+
def create(attributes = nil, &block)
|
96
|
+
if attributes.is_a?(Array)
|
97
|
+
attributes.collect { |attr| create(attr, &block) }
|
98
|
+
else
|
99
|
+
block = _deprecated_scope_block("create", &block)
|
100
|
+
scoping { klass.create(attributes, &block) }
|
101
|
+
end
|
144
102
|
end
|
145
103
|
|
146
|
-
# Similar to #create, but calls
|
147
|
-
#
|
104
|
+
# Similar to #create, but calls
|
105
|
+
# {create!}[rdoc-ref:Persistence::ClassMethods#create!]
|
106
|
+
# on the base class. Raises an exception if a validation error occurs.
|
148
107
|
#
|
149
|
-
# Expects arguments in the same format as
|
150
|
-
|
151
|
-
|
108
|
+
# Expects arguments in the same format as
|
109
|
+
# {ActiveRecord::Base.create!}[rdoc-ref:Persistence::ClassMethods#create!].
|
110
|
+
def create!(attributes = nil, &block)
|
111
|
+
if attributes.is_a?(Array)
|
112
|
+
attributes.collect { |attr| create!(attr, &block) }
|
113
|
+
else
|
114
|
+
block = _deprecated_scope_block("create!", &block)
|
115
|
+
scoping { klass.create!(attributes, &block) }
|
116
|
+
end
|
152
117
|
end
|
153
118
|
|
154
119
|
def first_or_create(attributes = nil, &block) # :nodoc:
|
@@ -180,7 +145,7 @@ module ActiveRecord
|
|
180
145
|
# User.create_with(last_name: 'Johansson').find_or_create_by(first_name: 'Scarlett')
|
181
146
|
# # => #<User id: 2, first_name: "Scarlett", last_name: "Johansson">
|
182
147
|
#
|
183
|
-
# This method accepts a block, which is passed down to
|
148
|
+
# This method accepts a block, which is passed down to #create. The last example
|
184
149
|
# above can be alternatively written this way:
|
185
150
|
#
|
186
151
|
# # Find the first user named "Scarlett" or create a new one with a
|
@@ -192,34 +157,72 @@ module ActiveRecord
|
|
192
157
|
#
|
193
158
|
# This method always returns a record, but if creation was attempted and
|
194
159
|
# failed due to validation errors it won't be persisted, you get what
|
195
|
-
#
|
160
|
+
# #create returns in such situation.
|
196
161
|
#
|
197
|
-
# Please note
|
162
|
+
# Please note <b>this method is not atomic</b>, it runs first a SELECT, and if
|
198
163
|
# there are no results an INSERT is attempted. If there are other threads
|
199
164
|
# or processes there is a race condition between both calls and it could
|
200
165
|
# be the case that you end up with two similar records.
|
201
166
|
#
|
202
|
-
#
|
203
|
-
# application, but in the particular case in which rows have a UNIQUE
|
204
|
-
# constraint an exception may be raised, just retry:
|
205
|
-
#
|
206
|
-
# begin
|
207
|
-
# CreditAccount.find_or_create_by(user_id: user.id)
|
208
|
-
# rescue ActiveRecord::RecordNotUnique
|
209
|
-
# retry
|
210
|
-
# end
|
211
|
-
#
|
167
|
+
# If this might be a problem for your application, please see #create_or_find_by.
|
212
168
|
def find_or_create_by(attributes, &block)
|
213
169
|
find_by(attributes) || create(attributes, &block)
|
214
170
|
end
|
215
171
|
|
216
|
-
# Like
|
172
|
+
# Like #find_or_create_by, but calls
|
173
|
+
# {create!}[rdoc-ref:Persistence::ClassMethods#create!] so an exception
|
217
174
|
# is raised if the created record is invalid.
|
218
175
|
def find_or_create_by!(attributes, &block)
|
219
176
|
find_by(attributes) || create!(attributes, &block)
|
220
177
|
end
|
221
178
|
|
222
|
-
#
|
179
|
+
# Attempts to create a record with the given attributes in a table that has a unique constraint
|
180
|
+
# on one or several of its columns. If a row already exists with one or several of these
|
181
|
+
# unique constraints, the exception such an insertion would normally raise is caught,
|
182
|
+
# and the existing record with those attributes is found using #find_by!.
|
183
|
+
#
|
184
|
+
# This is similar to #find_or_create_by, but avoids the problem of stale reads between the SELECT
|
185
|
+
# and the INSERT, as that method needs to first query the table, then attempt to insert a row
|
186
|
+
# if none is found.
|
187
|
+
#
|
188
|
+
# There are several drawbacks to #create_or_find_by, though:
|
189
|
+
#
|
190
|
+
# * The underlying table must have the relevant columns defined with unique constraints.
|
191
|
+
# * A unique constraint violation may be triggered by only one, or at least less than all,
|
192
|
+
# of the given attributes. This means that the subsequent #find_by! may fail to find a
|
193
|
+
# matching record, which will then raise an <tt>ActiveRecord::RecordNotFound</tt> exception,
|
194
|
+
# rather than a record with the given attributes.
|
195
|
+
# * While we avoid the race condition between SELECT -> INSERT from #find_or_create_by,
|
196
|
+
# we actually have another race condition between INSERT -> SELECT, which can be triggered
|
197
|
+
# if a DELETE between those two statements is run by another client. But for most applications,
|
198
|
+
# that's a significantly less likely condition to hit.
|
199
|
+
# * It relies on exception handling to handle control flow, which may be marginally slower.
|
200
|
+
# * The primary key may auto-increment on each create, even if it fails. This can accelerate
|
201
|
+
# the problem of running out of integers, if the underlying table is still stuck on a primary
|
202
|
+
# key of type int (note: All Rails apps since 5.1+ have defaulted to bigint, which is not liable
|
203
|
+
# to this problem).
|
204
|
+
#
|
205
|
+
# This method will return a record if all given attributes are covered by unique constraints
|
206
|
+
# (unless the INSERT -> DELETE -> SELECT race condition is triggered), but if creation was attempted
|
207
|
+
# and failed due to validation errors it won't be persisted, you get what #create returns in
|
208
|
+
# such situation.
|
209
|
+
def create_or_find_by(attributes, &block)
|
210
|
+
transaction(requires_new: true) { create(attributes, &block) }
|
211
|
+
rescue ActiveRecord::RecordNotUnique
|
212
|
+
find_by!(attributes)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Like #create_or_find_by, but calls
|
216
|
+
# {create!}[rdoc-ref:Persistence::ClassMethods#create!] so an exception
|
217
|
+
# is raised if the created record is invalid.
|
218
|
+
def create_or_find_by!(attributes, &block)
|
219
|
+
transaction(requires_new: true) { create!(attributes, &block) }
|
220
|
+
rescue ActiveRecord::RecordNotUnique
|
221
|
+
find_by!(attributes)
|
222
|
+
end
|
223
|
+
|
224
|
+
# Like #find_or_create_by, but calls {new}[rdoc-ref:Core#new]
|
225
|
+
# instead of {create}[rdoc-ref:Persistence::ClassMethods#create].
|
223
226
|
def find_or_initialize_by(attributes, &block)
|
224
227
|
find_by(attributes) || new(attributes, &block)
|
225
228
|
end
|
@@ -232,25 +235,25 @@ module ActiveRecord
|
|
232
235
|
# are needed by the next ones when eager loading is going on.
|
233
236
|
#
|
234
237
|
# Please see further details in the
|
235
|
-
# {Active Record Query Interface guide}[
|
238
|
+
# {Active Record Query Interface guide}[https://guides.rubyonrails.org/active_record_querying.html#running-explain].
|
236
239
|
def explain
|
237
|
-
#TODO: Fix for binds.
|
238
240
|
exec_explain(collecting_queries_for_explain { exec_queries })
|
239
241
|
end
|
240
242
|
|
241
243
|
# Converts relation objects to Array.
|
242
|
-
def
|
244
|
+
def to_ary
|
245
|
+
records.dup
|
246
|
+
end
|
247
|
+
alias to_a to_ary
|
248
|
+
|
249
|
+
def records # :nodoc:
|
243
250
|
load
|
244
251
|
@records
|
245
252
|
end
|
246
253
|
|
247
254
|
# Serializes the relation objects Array.
|
248
255
|
def encode_with(coder)
|
249
|
-
coder.represent_seq(nil,
|
250
|
-
end
|
251
|
-
|
252
|
-
def as_json(options = nil) #:nodoc:
|
253
|
-
to_a.as_json(options)
|
256
|
+
coder.represent_seq(nil, records)
|
254
257
|
end
|
255
258
|
|
256
259
|
# Returns size of the records.
|
@@ -261,30 +264,133 @@ module ActiveRecord
|
|
261
264
|
# Returns true if there are no records.
|
262
265
|
def empty?
|
263
266
|
return @records.empty? if loaded?
|
267
|
+
!exists?
|
268
|
+
end
|
264
269
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
270
|
-
end
|
270
|
+
# Returns true if there are no records.
|
271
|
+
def none?
|
272
|
+
return super if block_given?
|
273
|
+
empty?
|
271
274
|
end
|
272
275
|
|
273
276
|
# Returns true if there are any records.
|
274
277
|
def any?
|
275
|
-
if block_given?
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
278
|
+
return super if block_given?
|
279
|
+
!empty?
|
280
|
+
end
|
281
|
+
|
282
|
+
# Returns true if there is exactly one record.
|
283
|
+
def one?
|
284
|
+
return super if block_given?
|
285
|
+
limit_value ? records.one? : size == 1
|
280
286
|
end
|
281
287
|
|
282
288
|
# Returns true if there is more than one record.
|
283
289
|
def many?
|
284
|
-
if block_given?
|
285
|
-
|
290
|
+
return super if block_given?
|
291
|
+
limit_value ? records.many? : size > 1
|
292
|
+
end
|
293
|
+
|
294
|
+
# Returns a stable cache key that can be used to identify this query.
|
295
|
+
# The cache key is built with a fingerprint of the SQL query.
|
296
|
+
#
|
297
|
+
# Product.where("name like ?", "%Cosmic Encounter%").cache_key
|
298
|
+
# # => "products/query-1850ab3d302391b85b8693e941286659"
|
299
|
+
#
|
300
|
+
# If ActiveRecord::Base.collection_cache_versioning is turned off, as it was
|
301
|
+
# in Rails 6.0 and earlier, the cache key will also include a version.
|
302
|
+
#
|
303
|
+
# ActiveRecord::Base.collection_cache_versioning = false
|
304
|
+
# Product.where("name like ?", "%Cosmic Encounter%").cache_key
|
305
|
+
# # => "products/query-1850ab3d302391b85b8693e941286659-1-20150714212553907087000"
|
306
|
+
#
|
307
|
+
# You can also pass a custom timestamp column to fetch the timestamp of the
|
308
|
+
# last updated record.
|
309
|
+
#
|
310
|
+
# Product.where("name like ?", "%Game%").cache_key(:last_reviewed_at)
|
311
|
+
def cache_key(timestamp_column = :updated_at)
|
312
|
+
@cache_keys ||= {}
|
313
|
+
@cache_keys[timestamp_column] ||= klass.collection_cache_key(self, timestamp_column)
|
314
|
+
end
|
315
|
+
|
316
|
+
def compute_cache_key(timestamp_column = :updated_at) # :nodoc:
|
317
|
+
query_signature = ActiveSupport::Digest.hexdigest(to_sql)
|
318
|
+
key = "#{klass.model_name.cache_key}/query-#{query_signature}"
|
319
|
+
|
320
|
+
if cache_version(timestamp_column)
|
321
|
+
key
|
286
322
|
else
|
287
|
-
|
323
|
+
"#{key}-#{compute_cache_version(timestamp_column)}"
|
324
|
+
end
|
325
|
+
end
|
326
|
+
private :compute_cache_key
|
327
|
+
|
328
|
+
# Returns a cache version that can be used together with the cache key to form
|
329
|
+
# a recyclable caching scheme. The cache version is built with the number of records
|
330
|
+
# matching the query, and the timestamp of the last updated record. When a new record
|
331
|
+
# comes to match the query, or any of the existing records is updated or deleted,
|
332
|
+
# the cache version changes.
|
333
|
+
#
|
334
|
+
# If the collection is loaded, the method will iterate through the records
|
335
|
+
# to generate the timestamp, otherwise it will trigger one SQL query like:
|
336
|
+
#
|
337
|
+
# SELECT COUNT(*), MAX("products"."updated_at") FROM "products" WHERE (name like '%Cosmic Encounter%')
|
338
|
+
def cache_version(timestamp_column = :updated_at)
|
339
|
+
if collection_cache_versioning
|
340
|
+
@cache_versions ||= {}
|
341
|
+
@cache_versions[timestamp_column] ||= compute_cache_version(timestamp_column)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
def compute_cache_version(timestamp_column) # :nodoc:
|
346
|
+
if loaded? || distinct_value
|
347
|
+
size = records.size
|
348
|
+
if size > 0
|
349
|
+
timestamp = max_by(×tamp_column)._read_attribute(timestamp_column)
|
350
|
+
end
|
351
|
+
else
|
352
|
+
collection = eager_loading? ? apply_join_dependency : self
|
353
|
+
|
354
|
+
column = connection.visitor.compile(arel_attribute(timestamp_column))
|
355
|
+
select_values = "COUNT(*) AS #{connection.quote_column_name("size")}, MAX(%s) AS timestamp"
|
356
|
+
|
357
|
+
if collection.has_limit_or_offset?
|
358
|
+
query = collection.select("#{column} AS collection_cache_key_timestamp")
|
359
|
+
subquery_alias = "subquery_for_cache_key"
|
360
|
+
subquery_column = "#{subquery_alias}.collection_cache_key_timestamp"
|
361
|
+
arel = query.build_subquery(subquery_alias, select_values % subquery_column)
|
362
|
+
else
|
363
|
+
query = collection.unscope(:order)
|
364
|
+
query.select_values = [select_values % column]
|
365
|
+
arel = query.arel
|
366
|
+
end
|
367
|
+
|
368
|
+
result = connection.select_one(arel, nil)
|
369
|
+
|
370
|
+
if result
|
371
|
+
column_type = klass.type_for_attribute(timestamp_column)
|
372
|
+
timestamp = column_type.deserialize(result["timestamp"])
|
373
|
+
size = result["size"]
|
374
|
+
else
|
375
|
+
timestamp = nil
|
376
|
+
size = 0
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
if timestamp
|
381
|
+
"#{size}-#{timestamp.utc.to_s(cache_timestamp_format)}"
|
382
|
+
else
|
383
|
+
"#{size}"
|
384
|
+
end
|
385
|
+
end
|
386
|
+
private :compute_cache_version
|
387
|
+
|
388
|
+
# Returns a cache key along with the version.
|
389
|
+
def cache_key_with_version
|
390
|
+
if version = cache_version
|
391
|
+
"#{cache_key}-#{version}"
|
392
|
+
else
|
393
|
+
cache_key
|
288
394
|
end
|
289
395
|
end
|
290
396
|
|
@@ -298,17 +404,22 @@ module ActiveRecord
|
|
298
404
|
# Please check unscoped if you want to remove all previous scopes (including
|
299
405
|
# the default_scope) during the execution of a block.
|
300
406
|
def scoping
|
301
|
-
|
302
|
-
|
407
|
+
already_in_scope? ? yield : _scoping(self) { yield }
|
408
|
+
end
|
409
|
+
|
410
|
+
def _exec_scope(name, *args, &block) # :nodoc:
|
411
|
+
@delegate_to_klass = true
|
412
|
+
_scoping(_deprecated_spawn(name)) { instance_exec(*args, &block) || self }
|
303
413
|
ensure
|
304
|
-
|
414
|
+
@delegate_to_klass = false
|
305
415
|
end
|
306
416
|
|
307
417
|
# Updates all records in the current relation with details given. This method constructs a single SQL UPDATE
|
308
418
|
# statement and sends it straight to the database. It does not instantiate the involved models and it does not
|
309
|
-
# trigger Active Record callbacks or validations.
|
310
|
-
#
|
311
|
-
#
|
419
|
+
# trigger Active Record callbacks or validations. However, values passed to #update_all will still go through
|
420
|
+
# Active Record's normal type casting and serialization.
|
421
|
+
#
|
422
|
+
# Note: As Active Record callbacks are not triggered, this method will not automatically update +updated_at+/+updated_on+ columns.
|
312
423
|
#
|
313
424
|
# ==== Parameters
|
314
425
|
#
|
@@ -324,57 +435,99 @@ module ActiveRecord
|
|
324
435
|
#
|
325
436
|
# # Update all books that match conditions, but limit it to 5 ordered by date
|
326
437
|
# Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(author: 'David')
|
438
|
+
#
|
439
|
+
# # Update all invoices and set the number column to its id value.
|
440
|
+
# Invoice.update_all('number = id')
|
327
441
|
def update_all(updates)
|
328
442
|
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
|
329
443
|
|
330
|
-
|
444
|
+
if eager_loading?
|
445
|
+
relation = apply_join_dependency
|
446
|
+
return relation.update_all(updates)
|
447
|
+
end
|
331
448
|
|
332
|
-
stmt
|
333
|
-
stmt.table(table)
|
334
|
-
stmt.key =
|
449
|
+
stmt = Arel::UpdateManager.new
|
450
|
+
stmt.table(arel.join_sources.empty? ? table : arel.source)
|
451
|
+
stmt.key = arel_attribute(primary_key)
|
452
|
+
stmt.take(arel.limit)
|
453
|
+
stmt.offset(arel.offset)
|
454
|
+
stmt.order(*arel.orders)
|
455
|
+
stmt.wheres = arel.constraints
|
456
|
+
|
457
|
+
if updates.is_a?(Hash)
|
458
|
+
if klass.locking_enabled? &&
|
459
|
+
!updates.key?(klass.locking_column) &&
|
460
|
+
!updates.key?(klass.locking_column.to_sym)
|
461
|
+
attr = arel_attribute(klass.locking_column)
|
462
|
+
updates[attr.name] = _increment_attribute(attr)
|
463
|
+
end
|
464
|
+
stmt.set _substitute_values(updates)
|
465
|
+
else
|
466
|
+
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
467
|
+
end
|
335
468
|
|
336
|
-
|
337
|
-
|
469
|
+
@klass.connection.update stmt, "#{@klass} Update All"
|
470
|
+
end
|
471
|
+
|
472
|
+
def update(id = :all, attributes) # :nodoc:
|
473
|
+
if id == :all
|
474
|
+
each { |record| record.update(attributes) }
|
338
475
|
else
|
339
|
-
|
340
|
-
|
341
|
-
|
476
|
+
klass.update(id, attributes)
|
477
|
+
end
|
478
|
+
end
|
479
|
+
|
480
|
+
def update_counters(counters) # :nodoc:
|
481
|
+
touch = counters.delete(:touch)
|
482
|
+
|
483
|
+
updates = {}
|
484
|
+
counters.each do |counter_name, value|
|
485
|
+
attr = arel_attribute(counter_name)
|
486
|
+
updates[attr.name] = _increment_attribute(attr, value)
|
342
487
|
end
|
343
488
|
|
344
|
-
|
345
|
-
|
489
|
+
if touch
|
490
|
+
names = touch if touch != true
|
491
|
+
names = Array.wrap(names)
|
492
|
+
options = names.extract_options!
|
493
|
+
touch_updates = klass.touch_attributes_with_time(*names, **options)
|
494
|
+
updates.merge!(touch_updates) unless touch_updates.empty?
|
495
|
+
end
|
496
|
+
|
497
|
+
update_all updates
|
346
498
|
end
|
347
499
|
|
348
|
-
#
|
349
|
-
#
|
500
|
+
# Touches all records in the current relation, setting the +updated_at+/+updated_on+ attributes to the current time or the time specified.
|
501
|
+
# It does not instantiate the involved models, and it does not trigger Active Record callbacks or validations.
|
502
|
+
# This method can be passed attribute names and an optional time argument.
|
503
|
+
# If attribute names are passed, they are updated along with +updated_at+/+updated_on+ attributes.
|
504
|
+
# If no time argument is passed, the current time is used as default.
|
350
505
|
#
|
351
|
-
#
|
506
|
+
# === Examples
|
352
507
|
#
|
353
|
-
#
|
354
|
-
#
|
508
|
+
# # Touch all records
|
509
|
+
# Person.all.touch_all
|
510
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670'"
|
355
511
|
#
|
356
|
-
#
|
512
|
+
# # Touch multiple records with a custom attribute
|
513
|
+
# Person.all.touch_all(:created_at)
|
514
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670', \"created_at\" = '2018-01-04 22:55:23.132670'"
|
357
515
|
#
|
358
|
-
# #
|
359
|
-
# Person.
|
516
|
+
# # Touch multiple records with a specified time
|
517
|
+
# Person.all.touch_all(time: Time.new(2020, 5, 16, 0, 0, 0))
|
518
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2020-05-16 00:00:00'"
|
360
519
|
#
|
361
|
-
# #
|
362
|
-
#
|
363
|
-
#
|
364
|
-
def
|
365
|
-
|
366
|
-
id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
|
367
|
-
else
|
368
|
-
object = find(id)
|
369
|
-
object.update(attributes)
|
370
|
-
object
|
371
|
-
end
|
520
|
+
# # Touch records with scope
|
521
|
+
# Person.where(name: 'David').touch_all
|
522
|
+
# # => "UPDATE \"people\" SET \"updated_at\" = '2018-01-04 22:55:23.132670' WHERE \"people\".\"name\" = 'David'"
|
523
|
+
def touch_all(*names, time: nil)
|
524
|
+
update_all klass.touch_attributes_with_time(*names, time: time)
|
372
525
|
end
|
373
526
|
|
374
|
-
# Destroys the records
|
375
|
-
# record and calling its
|
376
|
-
# executed (including <tt>:dependent</tt> association options).
|
377
|
-
# collection of objects that were destroyed; each will be frozen, to
|
527
|
+
# Destroys the records by instantiating each
|
528
|
+
# record and calling its {#destroy}[rdoc-ref:Persistence#destroy] method.
|
529
|
+
# Each object's callbacks are executed (including <tt>:dependent</tt> association options).
|
530
|
+
# Returns the collection of objects that were destroyed; each will be frozen, to
|
378
531
|
# reflect that no changes should be made (since they can't be persisted).
|
379
532
|
#
|
380
533
|
# Note: Instantiation, callback execution, and deletion of each
|
@@ -382,126 +535,85 @@ module ActiveRecord
|
|
382
535
|
# once. It generates at least one SQL +DELETE+ query per record (or
|
383
536
|
# possibly more, to enforce your callbacks). If you want to delete many
|
384
537
|
# rows quickly, without concern for their associations or callbacks, use
|
385
|
-
#
|
386
|
-
#
|
387
|
-
# ==== Parameters
|
388
|
-
#
|
389
|
-
# * +conditions+ - A string, array, or hash that specifies which records
|
390
|
-
# to destroy. If omitted, all records are destroyed. See the
|
391
|
-
# Conditions section in the introduction to ActiveRecord::Base for
|
392
|
-
# more information.
|
538
|
+
# #delete_all instead.
|
393
539
|
#
|
394
540
|
# ==== Examples
|
395
541
|
#
|
396
|
-
# Person.destroy_all("last_login < '2004-04-04'")
|
397
|
-
# Person.destroy_all(status: "inactive")
|
398
542
|
# Person.where(age: 0..18).destroy_all
|
399
|
-
def destroy_all
|
400
|
-
|
401
|
-
where(conditions).destroy_all
|
402
|
-
else
|
403
|
-
to_a.each {|object| object.destroy }.tap { reset }
|
404
|
-
end
|
405
|
-
end
|
406
|
-
|
407
|
-
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
|
408
|
-
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
409
|
-
# less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
|
410
|
-
#
|
411
|
-
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
412
|
-
# from the attributes, and then calls destroy on it.
|
413
|
-
#
|
414
|
-
# ==== Parameters
|
415
|
-
#
|
416
|
-
# * +id+ - Can be either an Integer or an Array of Integers.
|
417
|
-
#
|
418
|
-
# ==== Examples
|
419
|
-
#
|
420
|
-
# # Destroy a single object
|
421
|
-
# Todo.destroy(1)
|
422
|
-
#
|
423
|
-
# # Destroy multiple objects
|
424
|
-
# todos = [1,2,3]
|
425
|
-
# Todo.destroy(todos)
|
426
|
-
def destroy(id)
|
427
|
-
if id.is_a?(Array)
|
428
|
-
id.map { |one_id| destroy(one_id) }
|
429
|
-
else
|
430
|
-
find(id).destroy
|
431
|
-
end
|
543
|
+
def destroy_all
|
544
|
+
records.each(&:destroy).tap { reset }
|
432
545
|
end
|
433
546
|
|
434
|
-
# Deletes the records
|
435
|
-
# first, and hence not calling the
|
436
|
-
#
|
437
|
-
#
|
547
|
+
# Deletes the records without instantiating the records
|
548
|
+
# first, and hence not calling the {#destroy}[rdoc-ref:Persistence#destroy]
|
549
|
+
# method nor invoking callbacks.
|
550
|
+
# This is a single SQL DELETE statement that goes straight to the database, much more
|
551
|
+
# efficient than #destroy_all. Be careful with relations though, in particular
|
438
552
|
# <tt>:dependent</tt> rules defined on associations are not honored. Returns the
|
439
553
|
# number of rows affected.
|
440
554
|
#
|
441
|
-
# Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")
|
442
|
-
# Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])
|
443
555
|
# Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all
|
444
556
|
#
|
445
557
|
# Both calls delete the affected posts all at once with a single DELETE statement.
|
446
558
|
# If you need to destroy dependent associations or call your <tt>before_*</tt> or
|
447
|
-
# +after_destroy+ callbacks, use the
|
559
|
+
# +after_destroy+ callbacks, use the #destroy_all method instead.
|
448
560
|
#
|
449
|
-
# If an invalid method is supplied,
|
561
|
+
# If an invalid method is supplied, #delete_all raises an ActiveRecordError:
|
450
562
|
#
|
451
|
-
# Post.
|
452
|
-
# # => ActiveRecord::ActiveRecordError: delete_all doesn't support
|
453
|
-
def delete_all
|
454
|
-
invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
send("#{method}_value")
|
459
|
-
end
|
460
|
-
}
|
563
|
+
# Post.distinct.delete_all
|
564
|
+
# # => ActiveRecord::ActiveRecordError: delete_all doesn't support distinct
|
565
|
+
def delete_all
|
566
|
+
invalid_methods = INVALID_METHODS_FOR_DELETE_ALL.select do |method|
|
567
|
+
value = @values[method]
|
568
|
+
method == :distinct ? value : value&.any?
|
569
|
+
end
|
461
570
|
if invalid_methods.any?
|
462
571
|
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
|
463
572
|
end
|
464
573
|
|
465
|
-
if
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
stmt.from(table)
|
574
|
+
if eager_loading?
|
575
|
+
relation = apply_join_dependency
|
576
|
+
return relation.delete_all
|
577
|
+
end
|
470
578
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
579
|
+
stmt = Arel::DeleteManager.new
|
580
|
+
stmt.from(arel.join_sources.empty? ? table : arel.source)
|
581
|
+
stmt.key = arel_attribute(primary_key)
|
582
|
+
stmt.take(arel.limit)
|
583
|
+
stmt.offset(arel.offset)
|
584
|
+
stmt.order(*arel.orders)
|
585
|
+
stmt.wheres = arel.constraints
|
476
586
|
|
477
|
-
|
587
|
+
affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
|
478
588
|
|
479
|
-
|
480
|
-
|
481
|
-
end
|
589
|
+
reset
|
590
|
+
affected
|
482
591
|
end
|
483
592
|
|
484
|
-
#
|
485
|
-
#
|
486
|
-
#
|
487
|
-
# executed, including any <tt>:dependent</tt> association options.
|
593
|
+
# Finds and destroys all records matching the specified conditions.
|
594
|
+
# This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
|
595
|
+
# Returns the collection of objects that were destroyed.
|
488
596
|
#
|
489
|
-
#
|
597
|
+
# If no record is found, returns empty array.
|
490
598
|
#
|
491
|
-
#
|
492
|
-
#
|
493
|
-
#
|
494
|
-
|
495
|
-
|
496
|
-
|
599
|
+
# Person.destroy_by(id: 13)
|
600
|
+
# Person.destroy_by(name: 'Spartacus', rating: 4)
|
601
|
+
# Person.destroy_by("published_at < ?", 2.weeks.ago)
|
602
|
+
def destroy_by(*args)
|
603
|
+
where(*args).destroy_all
|
604
|
+
end
|
605
|
+
|
606
|
+
# Finds and deletes all records matching the specified conditions.
|
607
|
+
# This is short-hand for <tt>relation.where(condition).delete_all</tt>.
|
608
|
+
# Returns the number of rows affected.
|
497
609
|
#
|
498
|
-
#
|
499
|
-
# Todo.delete(1)
|
610
|
+
# If no record is found, returns <tt>0</tt> as zero rows were affected.
|
500
611
|
#
|
501
|
-
#
|
502
|
-
#
|
503
|
-
|
504
|
-
|
612
|
+
# Person.delete_by(id: 13)
|
613
|
+
# Person.delete_by(name: 'Spartacus', rating: 4)
|
614
|
+
# Person.delete_by("published_at < ?", 2.weeks.ago)
|
615
|
+
def delete_by(*args)
|
616
|
+
where(*args).delete_all
|
505
617
|
end
|
506
618
|
|
507
619
|
# Causes the records to be loaded from the database if they have not
|
@@ -510,8 +622,8 @@ module ActiveRecord
|
|
510
622
|
# return value is the relation itself, not the records.
|
511
623
|
#
|
512
624
|
# Post.where(published: true).load # => #<ActiveRecord::Relation>
|
513
|
-
def load
|
514
|
-
exec_queries unless loaded?
|
625
|
+
def load(&block)
|
626
|
+
exec_queries(&block) unless loaded?
|
515
627
|
|
516
628
|
self
|
517
629
|
end
|
@@ -523,10 +635,12 @@ module ActiveRecord
|
|
523
635
|
end
|
524
636
|
|
525
637
|
def reset
|
526
|
-
@
|
527
|
-
@
|
528
|
-
@
|
638
|
+
@delegate_to_klass = false
|
639
|
+
@_deprecated_scope_source = nil
|
640
|
+
@to_sql = @arel = @loaded = @should_eager_load = nil
|
641
|
+
@records = [].freeze
|
529
642
|
@offsets = {}
|
643
|
+
@take = nil
|
530
644
|
self
|
531
645
|
end
|
532
646
|
|
@@ -536,47 +650,31 @@ module ActiveRecord
|
|
536
650
|
# # => SELECT "users".* FROM "users" WHERE "users"."name" = 'Oscar'
|
537
651
|
def to_sql
|
538
652
|
@to_sql ||= begin
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
binds.map! { |bv| connection.quote(*bv.reverse) }
|
550
|
-
collect = visitor.accept(arel.ast, Arel::Collectors::Bind.new)
|
551
|
-
collect.substitute_binds(binds).join
|
552
|
-
end
|
653
|
+
if eager_loading?
|
654
|
+
apply_join_dependency do |relation, join_dependency|
|
655
|
+
relation = join_dependency.apply_column_aliases(relation)
|
656
|
+
relation.to_sql
|
657
|
+
end
|
658
|
+
else
|
659
|
+
conn = klass.connection
|
660
|
+
conn.unprepared_statement { conn.to_sql(arel) }
|
661
|
+
end
|
662
|
+
end
|
553
663
|
end
|
554
664
|
|
555
665
|
# Returns a hash of where conditions.
|
556
666
|
#
|
557
667
|
# User.where(name: 'Oscar').where_values_hash
|
558
668
|
# # => {name: "Oscar"}
|
559
|
-
def where_values_hash(relation_table_name = table_name)
|
560
|
-
|
561
|
-
node.left.relation.name == relation_table_name
|
562
|
-
}
|
563
|
-
|
564
|
-
binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]
|
565
|
-
|
566
|
-
Hash[equalities.map { |where|
|
567
|
-
name = where.left.name
|
568
|
-
[name, binds.fetch(name.to_s) {
|
569
|
-
case where.right
|
570
|
-
when Array then where.right.map(&:val)
|
571
|
-
else
|
572
|
-
where.right.val
|
573
|
-
end
|
574
|
-
}]
|
575
|
-
}]
|
669
|
+
def where_values_hash(relation_table_name = klass.table_name)
|
670
|
+
where_clause.to_h(relation_table_name)
|
576
671
|
end
|
577
672
|
|
578
673
|
def scope_for_create
|
579
|
-
|
674
|
+
hash = where_values_hash
|
675
|
+
hash.delete(klass.inheritance_column) if klass.finder_needs_type_condition?
|
676
|
+
create_with_value.each { |k, v| hash[k.to_s] = v } unless create_with_value.empty?
|
677
|
+
hash
|
580
678
|
end
|
581
679
|
|
582
680
|
# Returns true if relation needs eager loading.
|
@@ -594,88 +692,183 @@ module ActiveRecord
|
|
594
692
|
includes_values & joins_values
|
595
693
|
end
|
596
694
|
|
597
|
-
# +uniq+ and +uniq!+ are silently deprecated. +uniq_value+ delegates to +distinct_value+
|
598
|
-
# to maintain backwards compatibility. Use +distinct_value+ instead.
|
599
|
-
def uniq_value
|
600
|
-
distinct_value
|
601
|
-
end
|
602
|
-
|
603
695
|
# Compares two relations for equality.
|
604
696
|
def ==(other)
|
605
697
|
case other
|
606
698
|
when Associations::CollectionProxy, AssociationRelation
|
607
|
-
self == other.
|
699
|
+
self == other.records
|
608
700
|
when Relation
|
609
701
|
other.to_sql == to_sql
|
610
702
|
when Array
|
611
|
-
|
703
|
+
records == other
|
612
704
|
end
|
613
705
|
end
|
614
706
|
|
615
707
|
def pretty_print(q)
|
616
|
-
q.pp(
|
708
|
+
q.pp(records)
|
617
709
|
end
|
618
710
|
|
619
711
|
# Returns true if relation is blank.
|
620
712
|
def blank?
|
621
|
-
|
713
|
+
records.blank?
|
622
714
|
end
|
623
715
|
|
624
716
|
def values
|
625
|
-
|
717
|
+
@values.dup
|
626
718
|
end
|
627
719
|
|
628
720
|
def inspect
|
629
|
-
|
630
|
-
entries
|
721
|
+
subject = loaded? ? records : self
|
722
|
+
entries = subject.take([limit_value, 11].compact.min).map!(&:inspect)
|
723
|
+
|
724
|
+
entries[10] = "..." if entries.size == 11
|
631
725
|
|
632
726
|
"#<#{self.class.name} [#{entries.join(', ')}]>"
|
633
727
|
end
|
634
728
|
|
635
|
-
|
729
|
+
def empty_scope? # :nodoc:
|
730
|
+
@values == klass.unscoped.values
|
731
|
+
end
|
732
|
+
|
733
|
+
def has_limit_or_offset? # :nodoc:
|
734
|
+
limit_value || offset_value
|
735
|
+
end
|
636
736
|
|
637
|
-
def
|
638
|
-
|
737
|
+
def alias_tracker(joins = [], aliases = nil) # :nodoc:
|
738
|
+
joins += [aliases] if aliases
|
739
|
+
ActiveRecord::Associations::AliasTracker.create(connection, table.name, joins)
|
740
|
+
end
|
639
741
|
|
742
|
+
def preload_associations(records) # :nodoc:
|
640
743
|
preload = preload_values
|
641
|
-
preload +=
|
642
|
-
preloader =
|
744
|
+
preload += includes_values unless eager_loading?
|
745
|
+
preloader = nil
|
643
746
|
preload.each do |associations|
|
644
|
-
preloader
|
747
|
+
preloader ||= build_preloader
|
748
|
+
preloader.preload records, associations
|
645
749
|
end
|
750
|
+
end
|
646
751
|
|
647
|
-
|
752
|
+
attr_reader :_deprecated_scope_source # :nodoc:
|
648
753
|
|
649
|
-
|
650
|
-
|
651
|
-
end
|
754
|
+
protected
|
755
|
+
attr_writer :_deprecated_scope_source # :nodoc:
|
652
756
|
|
653
|
-
|
654
|
-
|
655
|
-
|
757
|
+
def load_records(records)
|
758
|
+
@records = records.freeze
|
759
|
+
@loaded = true
|
760
|
+
end
|
761
|
+
|
762
|
+
def null_relation? # :nodoc:
|
763
|
+
is_a?(NullRelation)
|
764
|
+
end
|
765
|
+
|
766
|
+
private
|
767
|
+
def already_in_scope?
|
768
|
+
@delegate_to_klass && begin
|
769
|
+
scope = klass.current_scope(true)
|
770
|
+
scope && !scope._deprecated_scope_source
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|
774
|
+
def _deprecated_spawn(name)
|
775
|
+
spawn.tap { |scope| scope._deprecated_scope_source = name }
|
776
|
+
end
|
777
|
+
|
778
|
+
def _deprecated_scope_block(name, &block)
|
779
|
+
-> record do
|
780
|
+
klass.current_scope = _deprecated_spawn(name)
|
781
|
+
yield record if block_given?
|
782
|
+
end
|
783
|
+
end
|
656
784
|
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
785
|
+
def _scoping(scope)
|
786
|
+
previous, klass.current_scope = klass.current_scope(true), scope
|
787
|
+
yield
|
788
|
+
ensure
|
789
|
+
klass.current_scope = previous
|
790
|
+
end
|
791
|
+
|
792
|
+
def _substitute_values(values)
|
793
|
+
values.map do |name, value|
|
794
|
+
attr = arel_attribute(name)
|
795
|
+
unless Arel.arel_node?(value)
|
796
|
+
type = klass.type_for_attribute(attr.name)
|
797
|
+
value = predicate_builder.build_bind_attribute(attr.name, type.cast(value))
|
798
|
+
end
|
799
|
+
[attr, value]
|
800
|
+
end
|
801
|
+
end
|
802
|
+
|
803
|
+
def _increment_attribute(attribute, value = 1)
|
804
|
+
bind = predicate_builder.build_bind_attribute(attribute.name, value.abs)
|
805
|
+
expr = table.coalesce(Arel::Nodes::UnqualifiedColumn.new(attribute), 0)
|
806
|
+
expr = value < 0 ? expr - bind : expr + bind
|
807
|
+
expr.expr
|
808
|
+
end
|
809
|
+
|
810
|
+
def exec_queries(&block)
|
811
|
+
skip_query_cache_if_necessary do
|
812
|
+
@records =
|
813
|
+
if eager_loading?
|
814
|
+
apply_join_dependency do |relation, join_dependency|
|
815
|
+
if relation.null_relation?
|
816
|
+
[]
|
817
|
+
else
|
818
|
+
relation = join_dependency.apply_column_aliases(relation)
|
819
|
+
rows = connection.select_all(relation.arel, "SQL")
|
820
|
+
join_dependency.instantiate(rows, &block)
|
821
|
+
end.freeze
|
822
|
+
end
|
823
|
+
else
|
824
|
+
klass.find_by_sql(arel, &block).freeze
|
825
|
+
end
|
826
|
+
|
827
|
+
preload_associations(@records) unless skip_preloading_value
|
828
|
+
|
829
|
+
@records.each(&:readonly!) if readonly_value
|
830
|
+
|
831
|
+
@loaded = true
|
832
|
+
@records
|
833
|
+
end
|
834
|
+
end
|
835
|
+
|
836
|
+
def skip_query_cache_if_necessary
|
837
|
+
if skip_query_cache_value
|
838
|
+
uncached do
|
839
|
+
yield
|
840
|
+
end
|
661
841
|
else
|
662
|
-
|
842
|
+
yield
|
663
843
|
end
|
664
844
|
end
|
665
845
|
|
666
|
-
|
846
|
+
def build_preloader
|
847
|
+
ActiveRecord::Associations::Preloader.new
|
848
|
+
end
|
667
849
|
|
668
|
-
|
669
|
-
|
850
|
+
def references_eager_loaded_tables?
|
851
|
+
joined_tables = arel.join_sources.map do |join|
|
852
|
+
if join.is_a?(Arel::Nodes::StringJoin)
|
853
|
+
tables_in_string(join.left)
|
854
|
+
else
|
855
|
+
[join.left.table_name, join.left.table_alias]
|
856
|
+
end
|
857
|
+
end
|
670
858
|
|
671
|
-
|
672
|
-
end
|
859
|
+
joined_tables += [table.name, table.table_alias]
|
673
860
|
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
861
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
862
|
+
joined_tables = joined_tables.flatten.compact.map(&:downcase).uniq
|
863
|
+
|
864
|
+
(references_values - joined_tables).any?
|
865
|
+
end
|
866
|
+
|
867
|
+
def tables_in_string(string)
|
868
|
+
return [] if string.blank?
|
869
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
870
|
+
# ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
|
871
|
+
string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map(&:downcase).uniq - ["raw_sql_"]
|
872
|
+
end
|
680
873
|
end
|
681
874
|
end
|