activerecord 3.2.6 → 6.0.0
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 +7 -0
- data/CHANGELOG.md +611 -6417
- data/MIT-LICENSE +4 -2
- data/README.rdoc +44 -47
- data/examples/performance.rb +79 -71
- data/examples/simple.rb +6 -5
- data/lib/active_record/aggregations.rb +268 -238
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations/alias_tracker.rb +47 -42
- data/lib/active_record/associations/association.rb +173 -81
- data/lib/active_record/associations/association_scope.rb +124 -92
- data/lib/active_record/associations/belongs_to_association.rb +83 -38
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +11 -9
- data/lib/active_record/associations/builder/association.rb +113 -32
- data/lib/active_record/associations/builder/belongs_to.rb +105 -60
- data/lib/active_record/associations/builder/collection_association.rb +53 -56
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +98 -41
- data/lib/active_record/associations/builder/has_many.rb +11 -63
- data/lib/active_record/associations/builder/has_one.rb +47 -45
- data/lib/active_record/associations/builder/singular_association.rb +30 -18
- data/lib/active_record/associations/collection_association.rb +217 -295
- data/lib/active_record/associations/collection_proxy.rb +1074 -77
- data/lib/active_record/associations/foreign_association.rb +20 -0
- data/lib/active_record/associations/has_many_association.rb +78 -50
- data/lib/active_record/associations/has_many_through_association.rb +99 -61
- data/lib/active_record/associations/has_one_association.rb +75 -30
- data/lib/active_record/associations/has_one_through_association.rb +20 -11
- data/lib/active_record/associations/join_dependency/join_association.rb +45 -119
- data/lib/active_record/associations/join_dependency/join_base.rb +11 -12
- data/lib/active_record/associations/join_dependency/join_part.rb +35 -42
- data/lib/active_record/associations/join_dependency.rb +208 -164
- data/lib/active_record/associations/preloader/association.rb +93 -87
- data/lib/active_record/associations/preloader/through_association.rb +87 -38
- data/lib/active_record/associations/preloader.rb +134 -110
- data/lib/active_record/associations/singular_association.rb +19 -24
- data/lib/active_record/associations/through_association.rb +61 -27
- data/lib/active_record/associations.rb +1766 -1505
- data/lib/active_record/attribute_assignment.rb +57 -193
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +58 -8
- data/lib/active_record/attribute_methods/dirty.rb +187 -67
- data/lib/active_record/attribute_methods/primary_key.rb +100 -78
- data/lib/active_record/attribute_methods/query.rb +10 -8
- data/lib/active_record/attribute_methods/read.rb +29 -118
- data/lib/active_record/attribute_methods/serialization.rb +60 -72
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +69 -42
- data/lib/active_record/attribute_methods/write.rb +36 -44
- data/lib/active_record/attribute_methods.rb +306 -161
- data/lib/active_record/attributes.rb +279 -0
- data/lib/active_record/autosave_association.rb +324 -238
- data/lib/active_record/base.rb +114 -507
- data/lib/active_record/callbacks.rb +147 -83
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +32 -23
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +962 -279
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +32 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +331 -209
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -23
- data/lib/active_record/connection_adapters/abstract/quoting.rb +201 -65
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +153 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +510 -289
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +93 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1182 -313
- data/lib/active_record/connection_adapters/abstract/transaction.rb +323 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +585 -120
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +610 -463
- data/lib/active_record/connection_adapters/column.rb +58 -233
- data/lib/active_record/connection_adapters/connection_specification.rb +297 -0
- 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 +200 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +81 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +72 -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 +264 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +31 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +75 -207
- data/lib/active_record/connection_adapters/postgresql/column.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +182 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +53 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +17 -0
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +23 -0
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +21 -0
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +71 -0
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +41 -0
- data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +15 -0
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +97 -0
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +18 -0
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +113 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +26 -0
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +28 -0
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +30 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +205 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +222 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +776 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +36 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +695 -1052
- data/lib/active_record/connection_adapters/schema_cache.rb +115 -24
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +118 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +103 -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 +528 -26
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +267 -0
- data/lib/active_record/core.rb +599 -0
- data/lib/active_record/counter_cache.rb +177 -103
- 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 +79 -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 +107 -64
- data/lib/active_record/enum.rb +274 -0
- data/lib/active_record/errors.rb +254 -61
- data/lib/active_record/explain.rb +35 -70
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +18 -8
- data/lib/active_record/fixture_set/file.rb +82 -0
- 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 +153 -0
- data/lib/active_record/fixture_set/table_rows.rb +47 -0
- data/lib/active_record/fixtures.rb +291 -475
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +219 -100
- data/lib/active_record/insert_all.rb +179 -0
- data/lib/active_record/integration.rb +175 -17
- 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 +9 -1
- data/lib/active_record/locking/optimistic.rb +106 -92
- data/lib/active_record/locking/pessimistic.rb +23 -11
- data/lib/active_record/log_subscriber.rb +80 -30
- data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
- data/lib/active_record/middleware/database_selector/resolver.rb +92 -0
- data/lib/active_record/middleware/database_selector.rb +75 -0
- data/lib/active_record/migration/command_recorder.rb +235 -56
- data/lib/active_record/migration/compatibility.rb +244 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/migration.rb +917 -301
- data/lib/active_record/model_schema.rb +351 -175
- data/lib/active_record/nested_attributes.rb +366 -235
- data/lib/active_record/no_touching.rb +65 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +761 -166
- data/lib/active_record/query_cache.rb +22 -44
- data/lib/active_record/querying.rb +55 -31
- data/lib/active_record/railtie.rb +185 -47
- data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
- data/lib/active_record/railties/console_sandbox.rb +5 -4
- data/lib/active_record/railties/controller_runtime.rb +35 -33
- data/lib/active_record/railties/databases.rake +366 -463
- data/lib/active_record/readonly_attributes.rb +4 -6
- data/lib/active_record/reflection.rb +736 -228
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +252 -52
- data/lib/active_record/relation/calculations.rb +340 -270
- data/lib/active_record/relation/delegation.rb +117 -36
- data/lib/active_record/relation/finder_methods.rb +439 -286
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +184 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +49 -0
- 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 +19 -0
- data/lib/active_record/relation/predicate_builder.rb +131 -39
- data/lib/active_record/relation/query_attribute.rb +50 -0
- data/lib/active_record/relation/query_methods.rb +1163 -221
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +49 -120
- data/lib/active_record/relation/where_clause.rb +190 -0
- data/lib/active_record/relation/where_clause_factory.rb +33 -0
- data/lib/active_record/relation.rb +671 -349
- data/lib/active_record/result.rb +149 -15
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +153 -133
- data/lib/active_record/schema.rb +22 -19
- data/lib/active_record/schema_dumper.rb +178 -112
- data/lib/active_record/schema_migration.rb +60 -0
- data/lib/active_record/scoping/default.rb +107 -98
- data/lib/active_record/scoping/named.rb +130 -115
- data/lib/active_record/scoping.rb +77 -123
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +10 -6
- data/lib/active_record/statement_cache.rb +148 -0
- data/lib/active_record/store.rb +256 -16
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +75 -0
- data/lib/active_record/tasks/database_tasks.rb +506 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +141 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +77 -0
- data/lib/active_record/test_databases.rb +23 -0
- data/lib/active_record/test_fixtures.rb +224 -0
- data/lib/active_record/timestamp.rb +93 -39
- data/lib/active_record/touch_later.rb +66 -0
- data/lib/active_record/transactions.rb +260 -129
- data/lib/active_record/translation.rb +3 -1
- data/lib/active_record/type/adapter_specific_registry.rb +129 -0
- data/lib/active_record/type/date.rb +9 -0
- data/lib/active_record/type/date_time.rb +9 -0
- data/lib/active_record/type/decimal_without_scale.rb +15 -0
- data/lib/active_record/type/hash_lookup_type_map.rb +25 -0
- 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 +71 -0
- data/lib/active_record/type/text.rb +11 -0
- data/lib/active_record/type/time.rb +21 -0
- data/lib/active_record/type/type_map.rb +62 -0
- data/lib/active_record/type/unsigned_integer.rb +17 -0
- data/lib/active_record/type.rb +78 -0
- 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 +35 -18
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +68 -0
- data/lib/active_record/validations/uniqueness.rb +123 -77
- data/lib/active_record/validations.rb +54 -43
- data/lib/active_record/version.rb +7 -7
- data/lib/active_record.rb +97 -49
- 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 +257 -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 +204 -0
- data/lib/arel/visitors/dot.rb +297 -0
- data/lib/arel/visitors/ibm_db.rb +34 -0
- data/lib/arel/visitors/informix.rb +62 -0
- data/lib/arel/visitors/mssql.rb +157 -0
- data/lib/arel/visitors/mysql.rb +83 -0
- data/lib/arel/visitors/oracle.rb +159 -0
- data/lib/arel/visitors/oracle12.rb +66 -0
- data/lib/arel/visitors/postgresql.rb +110 -0
- data/lib/arel/visitors/sqlite.rb +39 -0
- data/lib/arel/visitors/to_sql.rb +889 -0
- data/lib/arel/visitors/visitor.rb +46 -0
- data/lib/arel/visitors/where_sql.rb +23 -0
- data/lib/arel/visitors.rb +20 -0
- data/lib/arel/window_predications.rb +9 -0
- data/lib/arel.rb +51 -0
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -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 +59 -9
- 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.tt +48 -0
- data/lib/rails/generators/active_record/migration.rb +41 -8
- data/lib/rails/generators/active_record/model/model_generator.rb +24 -22
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +22 -0
- data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +1 -1
- data/lib/rails/generators/active_record.rb +10 -16
- metadata +285 -149
- data/examples/associations.png +0 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +0 -63
- data/lib/active_record/associations/join_helper.rb +0 -55
- 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_and_belongs_to_many.rb +0 -60
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -15
- 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_methods/deprecated_underscore_read.rb +0 -32
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -188
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -426
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -579
- data/lib/active_record/dynamic_finder_match.rb +0 -68
- data/lib/active_record/dynamic_scope_match.rb +0 -23
- data/lib/active_record/fixtures/file.rb +0 -65
- data/lib/active_record/identity_map.rb +0 -162
- data/lib/active_record/observer.rb +0 -121
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -203
- data/lib/active_record/session_store.rb +0 -358
- data/lib/active_record/test_case.rb +0 -73
- data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -34
- data/lib/rails/generators/active_record/model/templates/migration.rb +0 -15
- data/lib/rails/generators/active_record/model/templates/model.rb +0 -12
- data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
- data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -4
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -25
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -12
@@ -1,120 +1,285 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/connection_adapters/determine_if_preparable_visitor"
|
4
|
+
require "active_record/connection_adapters/schema_cache"
|
5
|
+
require "active_record/connection_adapters/sql_type_metadata"
|
6
|
+
require "active_record/connection_adapters/abstract/schema_dumper"
|
7
|
+
require "active_record/connection_adapters/abstract/schema_creation"
|
8
|
+
require "active_support/concurrency/load_interlock_aware_monitor"
|
9
|
+
require "active_support/deprecation"
|
10
|
+
require "arel/collectors/bind"
|
11
|
+
require "arel/collectors/composite"
|
12
|
+
require "arel/collectors/sql_string"
|
13
|
+
require "arel/collectors/substitute_binds"
|
14
|
+
require "concurrent/atomic/thread_local_var"
|
8
15
|
|
9
16
|
module ActiveRecord
|
10
17
|
module ConnectionAdapters # :nodoc:
|
11
18
|
extend ActiveSupport::Autoload
|
12
19
|
|
13
20
|
autoload :Column
|
21
|
+
autoload :ConnectionSpecification
|
22
|
+
|
23
|
+
autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
|
24
|
+
autoload :IndexDefinition
|
25
|
+
autoload :ColumnDefinition
|
26
|
+
autoload :ChangeColumnDefinition
|
27
|
+
autoload :ForeignKeyDefinition
|
28
|
+
autoload :TableDefinition
|
29
|
+
autoload :Table
|
30
|
+
autoload :AlterTable
|
31
|
+
autoload :ReferenceDefinition
|
32
|
+
end
|
14
33
|
|
15
|
-
|
16
|
-
autoload :
|
17
|
-
|
18
|
-
autoload :TableDefinition, 'active_record/connection_adapters/abstract/schema_definitions'
|
19
|
-
autoload :Table, 'active_record/connection_adapters/abstract/schema_definitions'
|
34
|
+
autoload_at "active_record/connection_adapters/abstract/connection_pool" do
|
35
|
+
autoload :ConnectionHandler
|
36
|
+
end
|
20
37
|
|
38
|
+
autoload_under "abstract" do
|
21
39
|
autoload :SchemaStatements
|
22
40
|
autoload :DatabaseStatements
|
23
41
|
autoload :DatabaseLimits
|
24
42
|
autoload :Quoting
|
25
|
-
|
26
43
|
autoload :ConnectionPool
|
27
|
-
autoload :ConnectionHandler, 'active_record/connection_adapters/abstract/connection_pool'
|
28
|
-
autoload :ConnectionManagement, 'active_record/connection_adapters/abstract/connection_pool'
|
29
|
-
autoload :ConnectionSpecification
|
30
|
-
|
31
44
|
autoload :QueryCache
|
45
|
+
autoload :Savepoints
|
46
|
+
end
|
47
|
+
|
48
|
+
autoload_at "active_record/connection_adapters/abstract/transaction" do
|
49
|
+
autoload :TransactionManager
|
50
|
+
autoload :NullTransaction
|
51
|
+
autoload :RealTransaction
|
52
|
+
autoload :SavepointTransaction
|
53
|
+
autoload :TransactionState
|
32
54
|
end
|
33
55
|
|
34
56
|
# Active Record supports multiple database systems. AbstractAdapter and
|
35
57
|
# related classes form the abstraction layer which makes this possible.
|
36
58
|
# An AbstractAdapter represents a connection to a database, and provides an
|
37
59
|
# abstract interface for database-specific functionality such as establishing
|
38
|
-
# a connection, escaping values, building the right SQL fragments for
|
39
|
-
# and
|
60
|
+
# a connection, escaping values, building the right SQL fragments for +:offset+
|
61
|
+
# and +:limit+ options, etc.
|
40
62
|
#
|
41
63
|
# All the concrete database adapters follow the interface laid down in this class.
|
42
|
-
# ActiveRecord::Base.connection returns an AbstractAdapter object, which
|
64
|
+
# {ActiveRecord::Base.connection}[rdoc-ref:ConnectionHandling#connection] returns an AbstractAdapter object, which
|
43
65
|
# you can use.
|
44
66
|
#
|
45
67
|
# Most of the methods in the adapter are useful during migrations. Most
|
46
|
-
# notably, the instance methods provided by
|
68
|
+
# notably, the instance methods provided by SchemaStatements are very useful.
|
47
69
|
class AbstractAdapter
|
70
|
+
ADAPTER_NAME = "Abstract"
|
71
|
+
include ActiveSupport::Callbacks
|
72
|
+
define_callbacks :checkout, :checkin
|
73
|
+
|
48
74
|
include Quoting, DatabaseStatements, SchemaStatements
|
49
75
|
include DatabaseLimits
|
50
76
|
include QueryCache
|
51
|
-
include
|
52
|
-
include MonitorMixin
|
77
|
+
include Savepoints
|
53
78
|
|
54
|
-
|
79
|
+
SIMPLE_INT = /\A\d+\z/
|
55
80
|
|
56
|
-
attr_accessor :
|
57
|
-
attr_reader :
|
58
|
-
alias :in_use? :
|
81
|
+
attr_accessor :pool
|
82
|
+
attr_reader :visitor, :owner, :logger, :lock
|
83
|
+
alias :in_use? :owner
|
59
84
|
|
60
|
-
|
85
|
+
set_callback :checkin, :after, :enable_lazy_transactions!
|
86
|
+
|
87
|
+
def self.type_cast_config_to_integer(config)
|
88
|
+
if config.is_a?(Integer)
|
89
|
+
config
|
90
|
+
elsif SIMPLE_INT.match?(config)
|
91
|
+
config.to_i
|
92
|
+
else
|
93
|
+
config
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.type_cast_config_to_boolean(config)
|
98
|
+
if config == "false"
|
99
|
+
false
|
100
|
+
else
|
101
|
+
config
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.build_read_query_regexp(*parts) # :nodoc:
|
106
|
+
parts = parts.map { |part| /\A[\(\s]*#{part}/i }
|
107
|
+
Regexp.union(*parts)
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.quoted_column_names # :nodoc:
|
111
|
+
@quoted_column_names ||= {}
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.quoted_table_names # :nodoc:
|
115
|
+
@quoted_table_names ||= {}
|
116
|
+
end
|
117
|
+
|
118
|
+
def initialize(connection, logger = nil, config = {}) # :nodoc:
|
61
119
|
super()
|
62
120
|
|
63
|
-
@active = nil
|
64
121
|
@connection = connection
|
65
|
-
@
|
122
|
+
@owner = nil
|
66
123
|
@instrumenter = ActiveSupport::Notifications.instrumenter
|
67
|
-
@last_use = false
|
68
124
|
@logger = logger
|
69
|
-
@
|
70
|
-
@pool =
|
71
|
-
@
|
72
|
-
@
|
73
|
-
@
|
74
|
-
@
|
125
|
+
@config = config
|
126
|
+
@pool = ActiveRecord::ConnectionAdapters::NullPool.new
|
127
|
+
@idle_since = Concurrent.monotonic_time
|
128
|
+
@visitor = arel_visitor
|
129
|
+
@statements = build_statement_pool
|
130
|
+
@lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
|
131
|
+
|
132
|
+
if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
|
133
|
+
@prepared_statement_status = Concurrent::ThreadLocalVar.new(true)
|
134
|
+
@visitor.extend(DetermineIfPreparableVisitor)
|
135
|
+
else
|
136
|
+
@prepared_statement_status = Concurrent::ThreadLocalVar.new(false)
|
137
|
+
end
|
138
|
+
|
139
|
+
@advisory_locks_enabled = self.class.type_cast_config_to_boolean(
|
140
|
+
config.fetch(:advisory_locks, true)
|
141
|
+
)
|
142
|
+
end
|
143
|
+
|
144
|
+
def replica?
|
145
|
+
@config[:replica] || false
|
146
|
+
end
|
147
|
+
|
148
|
+
# Determines whether writes are currently being prevents.
|
149
|
+
#
|
150
|
+
# Returns true if the connection is a replica, or if +prevent_writes+
|
151
|
+
# is set to true.
|
152
|
+
def preventing_writes?
|
153
|
+
replica? || ActiveRecord::Base.connection_handler.prevent_writes
|
154
|
+
end
|
155
|
+
|
156
|
+
def migrations_paths # :nodoc:
|
157
|
+
@config[:migrations_paths] || Migrator.migrations_paths
|
158
|
+
end
|
159
|
+
|
160
|
+
def migration_context # :nodoc:
|
161
|
+
MigrationContext.new(migrations_paths, schema_migration)
|
162
|
+
end
|
163
|
+
|
164
|
+
def schema_migration # :nodoc:
|
165
|
+
@schema_migration ||= begin
|
166
|
+
conn = self
|
167
|
+
spec_name = conn.pool.spec.name
|
168
|
+
name = "#{spec_name}::SchemaMigration"
|
169
|
+
|
170
|
+
Class.new(ActiveRecord::SchemaMigration) do
|
171
|
+
define_singleton_method(:name) { name }
|
172
|
+
define_singleton_method(:to_s) { name }
|
173
|
+
|
174
|
+
self.connection_specification_name = spec_name
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def prepared_statements
|
180
|
+
@prepared_statement_status.value
|
75
181
|
end
|
76
182
|
|
183
|
+
class Version
|
184
|
+
include Comparable
|
185
|
+
|
186
|
+
attr_reader :full_version_string
|
187
|
+
|
188
|
+
def initialize(version_string, full_version_string = nil)
|
189
|
+
@version = version_string.split(".").map(&:to_i)
|
190
|
+
@full_version_string = full_version_string
|
191
|
+
end
|
192
|
+
|
193
|
+
def <=>(version_string)
|
194
|
+
@version <=> version_string.split(".").map(&:to_i)
|
195
|
+
end
|
196
|
+
|
197
|
+
def to_s
|
198
|
+
@version.join(".")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def valid_type?(type) # :nodoc:
|
203
|
+
!native_database_types[type].nil?
|
204
|
+
end
|
205
|
+
|
206
|
+
# this method must only be called while holding connection pool's mutex
|
77
207
|
def lease
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
208
|
+
if in_use?
|
209
|
+
msg = +"Cannot lease connection, "
|
210
|
+
if @owner == Thread.current
|
211
|
+
msg << "it is already leased by the current thread."
|
212
|
+
else
|
213
|
+
msg << "it is already in use by a different thread: #{@owner}. " \
|
214
|
+
"Current thread: #{Thread.current}."
|
82
215
|
end
|
216
|
+
raise ActiveRecordError, msg
|
83
217
|
end
|
218
|
+
|
219
|
+
@owner = Thread.current
|
84
220
|
end
|
85
221
|
|
222
|
+
def schema_cache
|
223
|
+
@pool.get_schema_cache(self)
|
224
|
+
end
|
225
|
+
|
226
|
+
def schema_cache=(cache)
|
227
|
+
cache.connection = self
|
228
|
+
@pool.set_schema_cache(cache)
|
229
|
+
end
|
230
|
+
|
231
|
+
# this method must only be called while holding connection pool's mutex
|
86
232
|
def expire
|
87
|
-
|
233
|
+
if in_use?
|
234
|
+
if @owner != Thread.current
|
235
|
+
raise ActiveRecordError, "Cannot expire connection, " \
|
236
|
+
"it is owned by a different thread: #{@owner}. " \
|
237
|
+
"Current thread: #{Thread.current}."
|
238
|
+
end
|
239
|
+
|
240
|
+
@idle_since = Concurrent.monotonic_time
|
241
|
+
@owner = nil
|
242
|
+
else
|
243
|
+
raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
|
244
|
+
end
|
88
245
|
end
|
89
246
|
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
247
|
+
# this method must only be called while holding connection pool's mutex (and a desire for segfaults)
|
248
|
+
def steal! # :nodoc:
|
249
|
+
if in_use?
|
250
|
+
if @owner != Thread.current
|
251
|
+
pool.send :remove_connection_from_thread_cache, self, @owner
|
252
|
+
|
253
|
+
@owner = Thread.current
|
254
|
+
end
|
255
|
+
else
|
256
|
+
raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
|
257
|
+
end
|
94
258
|
end
|
95
259
|
|
96
|
-
#
|
97
|
-
|
98
|
-
|
99
|
-
|
260
|
+
# Seconds since this connection was returned to the pool
|
261
|
+
def seconds_idle # :nodoc:
|
262
|
+
return 0 if in_use?
|
263
|
+
Concurrent.monotonic_time - @idle_since
|
100
264
|
end
|
101
265
|
|
102
|
-
|
103
|
-
|
104
|
-
# the abstract adapter always returns +false+.
|
105
|
-
def supports_primary_key?
|
106
|
-
false
|
266
|
+
def unprepared_statement
|
267
|
+
@prepared_statement_status.bind(false) { yield }
|
107
268
|
end
|
108
269
|
|
109
|
-
#
|
110
|
-
#
|
111
|
-
def
|
112
|
-
|
270
|
+
# Returns the human-readable name of the adapter. Use mixed case - one
|
271
|
+
# can always use downcase if needed.
|
272
|
+
def adapter_name
|
273
|
+
self.class::ADAPTER_NAME
|
274
|
+
end
|
275
|
+
|
276
|
+
# Does the database for this adapter exist?
|
277
|
+
def self.database_exists?(config)
|
278
|
+
raise NotImplementedError
|
113
279
|
end
|
114
280
|
|
115
281
|
# Does this adapter support DDL rollbacks in transactions? That is, would
|
116
|
-
# CREATE TABLE or ALTER TABLE get rolled back by a transaction?
|
117
|
-
# SQL Server, and others support this. MySQL and others do not.
|
282
|
+
# CREATE TABLE or ALTER TABLE get rolled back by a transaction?
|
118
283
|
def supports_ddl_transactions?
|
119
284
|
false
|
120
285
|
end
|
@@ -123,16 +288,19 @@ module ActiveRecord
|
|
123
288
|
false
|
124
289
|
end
|
125
290
|
|
126
|
-
# Does this adapter support savepoints?
|
127
|
-
# SQLite < 3.6.8 does not.
|
291
|
+
# Does this adapter support savepoints?
|
128
292
|
def supports_savepoints?
|
129
293
|
false
|
130
294
|
end
|
131
295
|
|
296
|
+
# Does this adapter support application-enforced advisory locking?
|
297
|
+
def supports_advisory_locks?
|
298
|
+
false
|
299
|
+
end
|
300
|
+
|
132
301
|
# Should primary key values be selected from their corresponding
|
133
302
|
# sequence before the insert statement? If true, next_sequence_value
|
134
303
|
# is called before each insert to set the record's primary key.
|
135
|
-
# This is false for all adapters but Firebird.
|
136
304
|
def prefetch_primary_key?(table_name = nil)
|
137
305
|
false
|
138
306
|
end
|
@@ -142,23 +310,159 @@ module ActiveRecord
|
|
142
310
|
false
|
143
311
|
end
|
144
312
|
|
145
|
-
# Does this adapter support
|
146
|
-
|
313
|
+
# Does this adapter support partial indices?
|
314
|
+
def supports_partial_index?
|
315
|
+
false
|
316
|
+
end
|
317
|
+
|
318
|
+
# Does this adapter support expression indices?
|
319
|
+
def supports_expression_index?
|
320
|
+
false
|
321
|
+
end
|
322
|
+
|
323
|
+
# Does this adapter support explain?
|
147
324
|
def supports_explain?
|
148
325
|
false
|
149
326
|
end
|
150
327
|
|
151
|
-
#
|
328
|
+
# Does this adapter support setting the isolation level for a transaction?
|
329
|
+
def supports_transaction_isolation?
|
330
|
+
false
|
331
|
+
end
|
332
|
+
|
333
|
+
# Does this adapter support database extensions?
|
334
|
+
def supports_extensions?
|
335
|
+
false
|
336
|
+
end
|
337
|
+
|
338
|
+
# Does this adapter support creating indexes in the same statement as
|
339
|
+
# creating the table?
|
340
|
+
def supports_indexes_in_create?
|
341
|
+
false
|
342
|
+
end
|
343
|
+
|
344
|
+
# Does this adapter support creating foreign key constraints?
|
345
|
+
def supports_foreign_keys?
|
346
|
+
false
|
347
|
+
end
|
348
|
+
|
349
|
+
# Does this adapter support creating invalid constraints?
|
350
|
+
def supports_validate_constraints?
|
351
|
+
false
|
352
|
+
end
|
152
353
|
|
153
|
-
#
|
154
|
-
|
155
|
-
|
354
|
+
# Does this adapter support creating foreign key constraints
|
355
|
+
# in the same statement as creating the table?
|
356
|
+
def supports_foreign_keys_in_create?
|
357
|
+
supports_foreign_keys?
|
156
358
|
end
|
359
|
+
deprecate :supports_foreign_keys_in_create?
|
157
360
|
|
158
|
-
#
|
159
|
-
|
160
|
-
|
161
|
-
|
361
|
+
# Does this adapter support views?
|
362
|
+
def supports_views?
|
363
|
+
false
|
364
|
+
end
|
365
|
+
|
366
|
+
# Does this adapter support materialized views?
|
367
|
+
def supports_materialized_views?
|
368
|
+
false
|
369
|
+
end
|
370
|
+
|
371
|
+
# Does this adapter support datetime with precision?
|
372
|
+
def supports_datetime_with_precision?
|
373
|
+
false
|
374
|
+
end
|
375
|
+
|
376
|
+
# Does this adapter support json data type?
|
377
|
+
def supports_json?
|
378
|
+
false
|
379
|
+
end
|
380
|
+
|
381
|
+
# Does this adapter support metadata comments on database objects (tables, columns, indexes)?
|
382
|
+
def supports_comments?
|
383
|
+
false
|
384
|
+
end
|
385
|
+
|
386
|
+
# Can comments for tables, columns, and indexes be specified in create/alter table statements?
|
387
|
+
def supports_comments_in_create?
|
388
|
+
false
|
389
|
+
end
|
390
|
+
|
391
|
+
# Does this adapter support multi-value insert?
|
392
|
+
def supports_multi_insert?
|
393
|
+
true
|
394
|
+
end
|
395
|
+
deprecate :supports_multi_insert?
|
396
|
+
|
397
|
+
# Does this adapter support virtual columns?
|
398
|
+
def supports_virtual_columns?
|
399
|
+
false
|
400
|
+
end
|
401
|
+
|
402
|
+
# Does this adapter support foreign/external tables?
|
403
|
+
def supports_foreign_tables?
|
404
|
+
false
|
405
|
+
end
|
406
|
+
|
407
|
+
# Does this adapter support optimizer hints?
|
408
|
+
def supports_optimizer_hints?
|
409
|
+
false
|
410
|
+
end
|
411
|
+
|
412
|
+
def supports_lazy_transactions?
|
413
|
+
false
|
414
|
+
end
|
415
|
+
|
416
|
+
def supports_insert_returning?
|
417
|
+
false
|
418
|
+
end
|
419
|
+
|
420
|
+
def supports_insert_on_duplicate_skip?
|
421
|
+
false
|
422
|
+
end
|
423
|
+
|
424
|
+
def supports_insert_on_duplicate_update?
|
425
|
+
false
|
426
|
+
end
|
427
|
+
|
428
|
+
def supports_insert_conflict_target?
|
429
|
+
false
|
430
|
+
end
|
431
|
+
|
432
|
+
# This is meant to be implemented by the adapters that support extensions
|
433
|
+
def disable_extension(name)
|
434
|
+
end
|
435
|
+
|
436
|
+
# This is meant to be implemented by the adapters that support extensions
|
437
|
+
def enable_extension(name)
|
438
|
+
end
|
439
|
+
|
440
|
+
def advisory_locks_enabled? # :nodoc:
|
441
|
+
supports_advisory_locks? && @advisory_locks_enabled
|
442
|
+
end
|
443
|
+
|
444
|
+
# This is meant to be implemented by the adapters that support advisory
|
445
|
+
# locks
|
446
|
+
#
|
447
|
+
# Return true if we got the lock, otherwise false
|
448
|
+
def get_advisory_lock(lock_id) # :nodoc:
|
449
|
+
end
|
450
|
+
|
451
|
+
# This is meant to be implemented by the adapters that support advisory
|
452
|
+
# locks.
|
453
|
+
#
|
454
|
+
# Return true if we released the lock, otherwise false
|
455
|
+
def release_advisory_lock(lock_id) # :nodoc:
|
456
|
+
end
|
457
|
+
|
458
|
+
# A list of extensions, to be filled in by adapters that support them.
|
459
|
+
def extensions
|
460
|
+
[]
|
461
|
+
end
|
462
|
+
|
463
|
+
# A list of index algorithms, to be filled by adapters that support them.
|
464
|
+
def index_algorithms
|
465
|
+
{}
|
162
466
|
end
|
163
467
|
|
164
468
|
# REFERENTIAL INTEGRITY ====================================
|
@@ -174,19 +478,37 @@ module ActiveRecord
|
|
174
478
|
# checking whether the database is actually capable of responding, i.e. whether
|
175
479
|
# the connection isn't stale.
|
176
480
|
def active?
|
177
|
-
@active != false
|
178
481
|
end
|
179
482
|
|
180
483
|
# Disconnects from the database if already connected, and establishes a
|
181
|
-
# new connection with the database.
|
484
|
+
# new connection with the database. Implementors should call super if they
|
485
|
+
# override the default implementation.
|
182
486
|
def reconnect!
|
183
|
-
|
487
|
+
clear_cache!
|
488
|
+
reset_transaction
|
184
489
|
end
|
185
490
|
|
186
491
|
# Disconnects from the database if already connected. Otherwise, this
|
187
492
|
# method does nothing.
|
188
493
|
def disconnect!
|
189
|
-
|
494
|
+
clear_cache!
|
495
|
+
reset_transaction
|
496
|
+
end
|
497
|
+
|
498
|
+
# Immediately forget this connection ever existed. Unlike disconnect!,
|
499
|
+
# this will not communicate with the server.
|
500
|
+
#
|
501
|
+
# After calling this method, the behavior of all other methods becomes
|
502
|
+
# undefined. This is called internally just before a forked process gets
|
503
|
+
# rid of a connection that belonged to its parent.
|
504
|
+
def discard!
|
505
|
+
# This should be overridden by concrete adapters.
|
506
|
+
#
|
507
|
+
# Prevent @connection's finalizer from touching the socket, or
|
508
|
+
# otherwise communicating with its server, when it is collected.
|
509
|
+
if schema_cache.connection == self
|
510
|
+
schema_cache.connection = nil
|
511
|
+
end
|
190
512
|
end
|
191
513
|
|
192
514
|
# Reset the state of this connection, directing the DBMS to clear
|
@@ -199,98 +521,241 @@ module ActiveRecord
|
|
199
521
|
# this should be overridden by concrete adapters
|
200
522
|
end
|
201
523
|
|
202
|
-
|
203
|
-
# Clear any caching the database adapter may be doing, for example
|
204
|
-
# clearing the prepared statement cache. This is database specific.
|
524
|
+
# Clear any caching the database adapter may be doing.
|
205
525
|
def clear_cache!
|
206
|
-
|
526
|
+
@lock.synchronize { @statements.clear } if @statements
|
207
527
|
end
|
208
528
|
|
209
529
|
# Returns true if its required to reload the connection between requests for development mode.
|
210
|
-
# This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite.
|
211
530
|
def requires_reloading?
|
212
531
|
false
|
213
532
|
end
|
214
533
|
|
215
534
|
# Checks whether the connection to the database is still active (i.e. not stale).
|
216
|
-
# This is done under the hood by calling
|
535
|
+
# This is done under the hood by calling #active?. If the connection
|
217
536
|
# is no longer active, then this method will reconnect to the database.
|
218
|
-
def verify!
|
537
|
+
def verify!
|
219
538
|
reconnect! unless active?
|
220
539
|
end
|
221
540
|
|
222
541
|
# Provides access to the underlying database driver for this adapter. For
|
223
|
-
# example, this method returns a
|
224
|
-
# and a
|
542
|
+
# example, this method returns a Mysql2::Client object in case of Mysql2Adapter,
|
543
|
+
# and a PG::Connection object in case of PostgreSQLAdapter.
|
225
544
|
#
|
226
545
|
# This is useful for when you need to call a proprietary method such as
|
227
546
|
# PostgreSQL's lo_* methods.
|
228
547
|
def raw_connection
|
548
|
+
disable_lazy_transactions!
|
229
549
|
@connection
|
230
550
|
end
|
231
551
|
|
232
|
-
|
552
|
+
def default_uniqueness_comparison(attribute, value, klass) # :nodoc:
|
553
|
+
attribute.eq(value)
|
554
|
+
end
|
233
555
|
|
234
|
-
def
|
235
|
-
|
556
|
+
def case_sensitive_comparison(attribute, value) # :nodoc:
|
557
|
+
attribute.eq(value)
|
236
558
|
end
|
237
559
|
|
238
|
-
def
|
239
|
-
|
560
|
+
def case_insensitive_comparison(attribute, value) # :nodoc:
|
561
|
+
column = column_for_attribute(attribute)
|
562
|
+
|
563
|
+
if can_perform_case_insensitive_comparison_for?(column)
|
564
|
+
attribute.lower.eq(attribute.relation.lower(value))
|
565
|
+
else
|
566
|
+
attribute.eq(value)
|
567
|
+
end
|
240
568
|
end
|
241
569
|
|
242
|
-
def
|
243
|
-
|
570
|
+
def can_perform_case_insensitive_comparison_for?(column)
|
571
|
+
true
|
244
572
|
end
|
573
|
+
private :can_perform_case_insensitive_comparison_for?
|
245
574
|
|
246
|
-
|
575
|
+
# Check the connection back in to the connection pool
|
576
|
+
def close
|
577
|
+
pool.checkin self
|
247
578
|
end
|
248
579
|
|
249
|
-
def
|
580
|
+
def column_name_for_operation(operation, node) # :nodoc:
|
581
|
+
visitor.compile(node)
|
250
582
|
end
|
251
583
|
|
252
|
-
def
|
584
|
+
def default_index_type?(index) # :nodoc:
|
585
|
+
index.using.nil?
|
253
586
|
end
|
254
587
|
|
255
|
-
|
256
|
-
|
588
|
+
# Called by ActiveRecord::InsertAll,
|
589
|
+
# Passed an instance of ActiveRecord::InsertAll::Builder,
|
590
|
+
# This method implements standard bulk inserts for all databases, but
|
591
|
+
# should be overridden by adapters to implement common features with
|
592
|
+
# non-standard syntax like handling duplicates or returning values.
|
593
|
+
def build_insert_sql(insert) # :nodoc:
|
594
|
+
if insert.skip_duplicates? || insert.update_duplicates?
|
595
|
+
raise NotImplementedError, "#{self.class} should define `build_insert_sql` to implement adapter-specific logic for handling duplicates during INSERT"
|
596
|
+
end
|
597
|
+
|
598
|
+
"INSERT #{insert.into} #{insert.values_list}"
|
257
599
|
end
|
258
600
|
|
259
|
-
def
|
260
|
-
table[attribute].lower.eq(table.lower(value))
|
601
|
+
def get_database_version # :nodoc:
|
261
602
|
end
|
262
603
|
|
263
|
-
def
|
264
|
-
|
604
|
+
def database_version # :nodoc:
|
605
|
+
schema_cache.database_version
|
265
606
|
end
|
266
607
|
|
267
|
-
|
268
|
-
def close
|
269
|
-
pool.checkin self
|
608
|
+
def check_version # :nodoc:
|
270
609
|
end
|
271
610
|
|
272
|
-
|
611
|
+
private
|
612
|
+
|
613
|
+
def type_map
|
614
|
+
@type_map ||= Type::TypeMap.new.tap do |mapping|
|
615
|
+
initialize_type_map(mapping)
|
616
|
+
end
|
617
|
+
end
|
618
|
+
|
619
|
+
def initialize_type_map(m = type_map)
|
620
|
+
register_class_with_limit m, %r(boolean)i, Type::Boolean
|
621
|
+
register_class_with_limit m, %r(char)i, Type::String
|
622
|
+
register_class_with_limit m, %r(binary)i, Type::Binary
|
623
|
+
register_class_with_limit m, %r(text)i, Type::Text
|
624
|
+
register_class_with_precision m, %r(date)i, Type::Date
|
625
|
+
register_class_with_precision m, %r(time)i, Type::Time
|
626
|
+
register_class_with_precision m, %r(datetime)i, Type::DateTime
|
627
|
+
register_class_with_limit m, %r(float)i, Type::Float
|
628
|
+
register_class_with_limit m, %r(int)i, Type::Integer
|
629
|
+
|
630
|
+
m.alias_type %r(blob)i, "binary"
|
631
|
+
m.alias_type %r(clob)i, "text"
|
632
|
+
m.alias_type %r(timestamp)i, "datetime"
|
633
|
+
m.alias_type %r(numeric)i, "decimal"
|
634
|
+
m.alias_type %r(number)i, "decimal"
|
635
|
+
m.alias_type %r(double)i, "float"
|
636
|
+
|
637
|
+
m.register_type %r(^json)i, Type::Json.new
|
638
|
+
|
639
|
+
m.register_type(%r(decimal)i) do |sql_type|
|
640
|
+
scale = extract_scale(sql_type)
|
641
|
+
precision = extract_precision(sql_type)
|
642
|
+
|
643
|
+
if scale == 0
|
644
|
+
# FIXME: Remove this class as well
|
645
|
+
Type::DecimalWithoutScale.new(precision: precision)
|
646
|
+
else
|
647
|
+
Type::Decimal.new(precision: precision, scale: scale)
|
648
|
+
end
|
649
|
+
end
|
650
|
+
end
|
651
|
+
|
652
|
+
def reload_type_map
|
653
|
+
type_map.clear
|
654
|
+
initialize_type_map
|
655
|
+
end
|
656
|
+
|
657
|
+
def register_class_with_limit(mapping, key, klass)
|
658
|
+
mapping.register_type(key) do |*args|
|
659
|
+
limit = extract_limit(args.last)
|
660
|
+
klass.new(limit: limit)
|
661
|
+
end
|
662
|
+
end
|
663
|
+
|
664
|
+
def register_class_with_precision(mapping, key, klass)
|
665
|
+
mapping.register_type(key) do |*args|
|
666
|
+
precision = extract_precision(args.last)
|
667
|
+
klass.new(precision: precision)
|
668
|
+
end
|
669
|
+
end
|
273
670
|
|
274
|
-
def
|
671
|
+
def extract_scale(sql_type)
|
672
|
+
case sql_type
|
673
|
+
when /\((\d+)\)/ then 0
|
674
|
+
when /\((\d+)(,(\d+))\)/ then $3.to_i
|
675
|
+
end
|
676
|
+
end
|
677
|
+
|
678
|
+
def extract_precision(sql_type)
|
679
|
+
$1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
|
680
|
+
end
|
681
|
+
|
682
|
+
def extract_limit(sql_type)
|
683
|
+
$1.to_i if sql_type =~ /\((.*)\)/
|
684
|
+
end
|
685
|
+
|
686
|
+
def translate_exception_class(e, sql, binds)
|
687
|
+
message = "#{e.class.name}: #{e.message}"
|
688
|
+
|
689
|
+
exception = translate_exception(
|
690
|
+
e, message: message, sql: sql, binds: binds
|
691
|
+
)
|
692
|
+
exception.set_backtrace e.backtrace
|
693
|
+
exception
|
694
|
+
end
|
695
|
+
|
696
|
+
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) # :doc:
|
275
697
|
@instrumenter.instrument(
|
276
698
|
"sql.active_record",
|
277
|
-
:sql
|
278
|
-
:name
|
279
|
-
:
|
280
|
-
:
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
699
|
+
sql: sql,
|
700
|
+
name: name,
|
701
|
+
binds: binds,
|
702
|
+
type_casted_binds: type_casted_binds,
|
703
|
+
statement_name: statement_name,
|
704
|
+
connection_id: object_id,
|
705
|
+
connection: self) do
|
706
|
+
@lock.synchronize do
|
707
|
+
yield
|
708
|
+
end
|
709
|
+
rescue => e
|
710
|
+
raise translate_exception_class(e, sql, binds)
|
711
|
+
end
|
287
712
|
end
|
288
713
|
|
289
|
-
def translate_exception(
|
714
|
+
def translate_exception(exception, message:, sql:, binds:)
|
290
715
|
# override in derived class
|
291
|
-
|
716
|
+
case exception
|
717
|
+
when RuntimeError
|
718
|
+
exception
|
719
|
+
else
|
720
|
+
ActiveRecord::StatementInvalid.new(message, sql: sql, binds: binds)
|
721
|
+
end
|
722
|
+
end
|
723
|
+
|
724
|
+
def without_prepared_statement?(binds)
|
725
|
+
!prepared_statements || binds.empty?
|
726
|
+
end
|
727
|
+
|
728
|
+
def column_for(table_name, column_name)
|
729
|
+
column_name = column_name.to_s
|
730
|
+
columns(table_name).detect { |c| c.name == column_name } ||
|
731
|
+
raise(ActiveRecordError, "No such column: #{table_name}.#{column_name}")
|
732
|
+
end
|
733
|
+
|
734
|
+
def column_for_attribute(attribute)
|
735
|
+
table_name = attribute.relation.name
|
736
|
+
schema_cache.columns_hash(table_name)[attribute.name.to_s]
|
292
737
|
end
|
293
738
|
|
739
|
+
def collector
|
740
|
+
if prepared_statements
|
741
|
+
Arel::Collectors::Composite.new(
|
742
|
+
Arel::Collectors::SQLString.new,
|
743
|
+
Arel::Collectors::Bind.new,
|
744
|
+
)
|
745
|
+
else
|
746
|
+
Arel::Collectors::SubstituteBinds.new(
|
747
|
+
self,
|
748
|
+
Arel::Collectors::SQLString.new,
|
749
|
+
)
|
750
|
+
end
|
751
|
+
end
|
752
|
+
|
753
|
+
def arel_visitor
|
754
|
+
Arel::Visitors::ToSql.new(self)
|
755
|
+
end
|
756
|
+
|
757
|
+
def build_statement_pool
|
758
|
+
end
|
294
759
|
end
|
295
760
|
end
|
296
761
|
end
|