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