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
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Money < Type::Decimal # :nodoc:
|
8
|
+
def type
|
9
|
+
:money
|
10
|
+
end
|
11
|
+
|
12
|
+
def scale
|
13
|
+
2
|
14
|
+
end
|
15
|
+
|
16
|
+
def cast_value(value)
|
17
|
+
return value unless ::String === value
|
18
|
+
|
19
|
+
# Because money output is formatted according to the locale, there are two
|
20
|
+
# cases to consider (note the decimal separators):
|
21
|
+
# (1) $12,345,678.12
|
22
|
+
# (2) $12.345.678,12
|
23
|
+
# Negative values are represented as follows:
|
24
|
+
# (3) -$2.55
|
25
|
+
# (4) ($2.55)
|
26
|
+
|
27
|
+
value = value.sub(/^\((.+)\)$/, '-\1') # (4)
|
28
|
+
case value
|
29
|
+
when /^-?\D*[\d,]+\.\d{2}$/ # (1)
|
30
|
+
value.gsub!(/[^-\d.]/, "")
|
31
|
+
when /^-?\D*[\d.]+,\d{2}$/ # (2)
|
32
|
+
value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
|
33
|
+
end
|
34
|
+
|
35
|
+
super(value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
Point = Struct.new(:x, :y)
|
5
|
+
|
6
|
+
module ConnectionAdapters
|
7
|
+
module PostgreSQL
|
8
|
+
module OID # :nodoc:
|
9
|
+
class Point < Type::Value # :nodoc:
|
10
|
+
include ActiveModel::Type::Helpers::Mutable
|
11
|
+
|
12
|
+
def type
|
13
|
+
:point
|
14
|
+
end
|
15
|
+
|
16
|
+
def cast(value)
|
17
|
+
case value
|
18
|
+
when ::String
|
19
|
+
return if value.blank?
|
20
|
+
|
21
|
+
if value[0] == "(" && value[-1] == ")"
|
22
|
+
value = value[1...-1]
|
23
|
+
end
|
24
|
+
x, y = value.split(",")
|
25
|
+
build_point(x, y)
|
26
|
+
when ::Array
|
27
|
+
build_point(*value)
|
28
|
+
else
|
29
|
+
value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def serialize(value)
|
34
|
+
case value
|
35
|
+
when ActiveRecord::Point
|
36
|
+
"(#{number_for_point(value.x)},#{number_for_point(value.y)})"
|
37
|
+
when ::Array
|
38
|
+
serialize(build_point(*value))
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def type_cast_for_schema(value)
|
45
|
+
if ActiveRecord::Point === value
|
46
|
+
[value.x, value.y]
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def number_for_point(number)
|
55
|
+
number.to_s.gsub(/\.0$/, "")
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_point(x, y)
|
59
|
+
ActiveRecord::Point.new(Float(x), Float(y))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Range < Type::Value # :nodoc:
|
8
|
+
attr_reader :subtype, :type
|
9
|
+
delegate :user_input_in_time_zone, to: :subtype
|
10
|
+
|
11
|
+
def initialize(subtype, type = :range)
|
12
|
+
@subtype = subtype
|
13
|
+
@type = type
|
14
|
+
end
|
15
|
+
|
16
|
+
def type_cast_for_schema(value)
|
17
|
+
value.inspect.gsub("Infinity", "::Float::INFINITY")
|
18
|
+
end
|
19
|
+
|
20
|
+
def cast_value(value)
|
21
|
+
return if value == "empty"
|
22
|
+
return value unless value.is_a?(::String)
|
23
|
+
|
24
|
+
extracted = extract_bounds(value)
|
25
|
+
from = type_cast_single extracted[:from]
|
26
|
+
to = type_cast_single extracted[:to]
|
27
|
+
|
28
|
+
if !infinity?(from) && extracted[:exclude_start]
|
29
|
+
raise ArgumentError, "The Ruby Range object does not support excluding the beginning of a Range. (unsupported value: '#{value}')"
|
30
|
+
end
|
31
|
+
::Range.new(from, to, extracted[:exclude_end])
|
32
|
+
end
|
33
|
+
|
34
|
+
def serialize(value)
|
35
|
+
if value.is_a?(::Range)
|
36
|
+
from = type_cast_single_for_database(value.begin)
|
37
|
+
to = type_cast_single_for_database(value.end)
|
38
|
+
::Range.new(from, to, value.exclude_end?)
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def ==(other)
|
45
|
+
other.is_a?(Range) &&
|
46
|
+
other.subtype == subtype &&
|
47
|
+
other.type == type
|
48
|
+
end
|
49
|
+
|
50
|
+
def map(value) # :nodoc:
|
51
|
+
new_begin = yield(value.begin)
|
52
|
+
new_end = yield(value.end)
|
53
|
+
::Range.new(new_begin, new_end, value.exclude_end?)
|
54
|
+
end
|
55
|
+
|
56
|
+
def force_equality?(value)
|
57
|
+
value.is_a?(::Range)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def type_cast_single(value)
|
63
|
+
infinity?(value) ? value : @subtype.deserialize(value)
|
64
|
+
end
|
65
|
+
|
66
|
+
def type_cast_single_for_database(value)
|
67
|
+
infinity?(value) ? value : @subtype.serialize(@subtype.cast(value))
|
68
|
+
end
|
69
|
+
|
70
|
+
def extract_bounds(value)
|
71
|
+
from, to = value[1..-2].split(",")
|
72
|
+
{
|
73
|
+
from: (value[1] == "," || from == "-infinity") ? infinity(negative: true) : from,
|
74
|
+
to: (value[-2] == "," || to == "infinity") ? infinity : to,
|
75
|
+
exclude_start: (value[0] == "("),
|
76
|
+
exclude_end: (value[-1] == ")")
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def infinity(negative: false)
|
81
|
+
if subtype.respond_to?(:infinity)
|
82
|
+
subtype.infinity(negative: negative)
|
83
|
+
elsif negative
|
84
|
+
-::Float::INFINITY
|
85
|
+
else
|
86
|
+
::Float::INFINITY
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def infinity?(value)
|
91
|
+
value.respond_to?(:infinite?) && value.infinite?
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class SpecializedString < Type::String # :nodoc:
|
8
|
+
attr_reader :type
|
9
|
+
|
10
|
+
def initialize(type, **options)
|
11
|
+
@type = type
|
12
|
+
super(options)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/extract"
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
module ConnectionAdapters
|
7
|
+
module PostgreSQL
|
8
|
+
module OID # :nodoc:
|
9
|
+
# This class uses the data from PostgreSQL pg_type table to build
|
10
|
+
# the OID -> Type mapping.
|
11
|
+
# - OID is an integer representing the type.
|
12
|
+
# - Type is an OID::Type object.
|
13
|
+
# This class has side effects on the +store+ passed during initialization.
|
14
|
+
class TypeMapInitializer # :nodoc:
|
15
|
+
def initialize(store)
|
16
|
+
@store = store
|
17
|
+
end
|
18
|
+
|
19
|
+
def run(records)
|
20
|
+
nodes = records.reject { |row| @store.key? row["oid"].to_i }
|
21
|
+
mapped = nodes.extract! { |row| @store.key? row["typname"] }
|
22
|
+
ranges = nodes.extract! { |row| row["typtype"] == "r" }
|
23
|
+
enums = nodes.extract! { |row| row["typtype"] == "e" }
|
24
|
+
domains = nodes.extract! { |row| row["typtype"] == "d" }
|
25
|
+
arrays = nodes.extract! { |row| row["typinput"] == "array_in" }
|
26
|
+
composites = nodes.extract! { |row| row["typelem"].to_i != 0 }
|
27
|
+
|
28
|
+
mapped.each { |row| register_mapped_type(row) }
|
29
|
+
enums.each { |row| register_enum_type(row) }
|
30
|
+
domains.each { |row| register_domain_type(row) }
|
31
|
+
arrays.each { |row| register_array_type(row) }
|
32
|
+
ranges.each { |row| register_range_type(row) }
|
33
|
+
composites.each { |row| register_composite_type(row) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def query_conditions_for_initial_load
|
37
|
+
known_type_names = @store.keys.map { |n| "'#{n}'" }
|
38
|
+
known_type_types = %w('r' 'e' 'd')
|
39
|
+
<<~SQL % [known_type_names.join(", "), known_type_types.join(", ")]
|
40
|
+
WHERE
|
41
|
+
t.typname IN (%s)
|
42
|
+
OR t.typtype IN (%s)
|
43
|
+
OR t.typinput = 'array_in(cstring,oid,integer)'::regprocedure
|
44
|
+
OR t.typelem != 0
|
45
|
+
SQL
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def register_mapped_type(row)
|
50
|
+
alias_type row["oid"], row["typname"]
|
51
|
+
end
|
52
|
+
|
53
|
+
def register_enum_type(row)
|
54
|
+
register row["oid"], OID::Enum.new
|
55
|
+
end
|
56
|
+
|
57
|
+
def register_array_type(row)
|
58
|
+
register_with_subtype(row["oid"], row["typelem"].to_i) do |subtype|
|
59
|
+
OID::Array.new(subtype, row["typdelim"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def register_range_type(row)
|
64
|
+
register_with_subtype(row["oid"], row["rngsubtype"].to_i) do |subtype|
|
65
|
+
OID::Range.new(subtype, row["typname"].to_sym)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def register_domain_type(row)
|
70
|
+
if base_type = @store.lookup(row["typbasetype"].to_i)
|
71
|
+
register row["oid"], base_type
|
72
|
+
else
|
73
|
+
warn "unknown base type (OID: #{row["typbasetype"]}) for domain #{row["typname"]}."
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def register_composite_type(row)
|
78
|
+
if subtype = @store.lookup(row["typelem"].to_i)
|
79
|
+
register row["oid"], OID::Vector.new(row["typdelim"], subtype)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def register(oid, oid_type = nil, &block)
|
84
|
+
oid = assert_valid_registration(oid, oid_type || block)
|
85
|
+
if block_given?
|
86
|
+
@store.register_type(oid, &block)
|
87
|
+
else
|
88
|
+
@store.register_type(oid, oid_type)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def alias_type(oid, target)
|
93
|
+
oid = assert_valid_registration(oid, target)
|
94
|
+
@store.alias_type(oid, target)
|
95
|
+
end
|
96
|
+
|
97
|
+
def register_with_subtype(oid, target_oid)
|
98
|
+
if @store.key?(target_oid)
|
99
|
+
register(oid) do |_, *args|
|
100
|
+
yield @store.lookup(target_oid, *args)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def assert_valid_registration(oid, oid_type)
|
106
|
+
raise ArgumentError, "can't register nil type for OID #{oid}" if oid_type.nil?
|
107
|
+
oid.to_i
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Uuid < Type::Value # :nodoc:
|
8
|
+
ACCEPTABLE_UUID = %r{\A(\{)?([a-fA-F0-9]{4}-?){8}(?(1)\}|)\z}
|
9
|
+
|
10
|
+
alias_method :serialize, :deserialize
|
11
|
+
|
12
|
+
def type
|
13
|
+
:uuid
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def cast_value(value)
|
19
|
+
casted = value.to_s
|
20
|
+
casted if casted.match?(ACCEPTABLE_UUID)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Vector < Type::Value # :nodoc:
|
8
|
+
attr_reader :delim, :subtype
|
9
|
+
|
10
|
+
# +delim+ corresponds to the `typdelim` column in the pg_types
|
11
|
+
# table. +subtype+ is derived from the `typelem` column in the
|
12
|
+
# pg_types table.
|
13
|
+
def initialize(delim, subtype)
|
14
|
+
@delim = delim
|
15
|
+
@subtype = subtype
|
16
|
+
end
|
17
|
+
|
18
|
+
# FIXME: this should probably split on +delim+ and use +subtype+
|
19
|
+
# to cast the values. Unfortunately, the current Rails behavior
|
20
|
+
# is to just return the string.
|
21
|
+
def cast(value)
|
22
|
+
value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
class Xml < Type::String # :nodoc:
|
8
|
+
def type
|
9
|
+
:xml
|
10
|
+
end
|
11
|
+
|
12
|
+
def serialize(value)
|
13
|
+
return unless value
|
14
|
+
Data.new(super)
|
15
|
+
end
|
16
|
+
|
17
|
+
class Data # :nodoc:
|
18
|
+
def initialize(value)
|
19
|
+
@value = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record/connection_adapters/postgresql/oid/array"
|
4
|
+
require "active_record/connection_adapters/postgresql/oid/bit"
|
5
|
+
require "active_record/connection_adapters/postgresql/oid/bit_varying"
|
6
|
+
require "active_record/connection_adapters/postgresql/oid/bytea"
|
7
|
+
require "active_record/connection_adapters/postgresql/oid/cidr"
|
8
|
+
require "active_record/connection_adapters/postgresql/oid/date"
|
9
|
+
require "active_record/connection_adapters/postgresql/oid/date_time"
|
10
|
+
require "active_record/connection_adapters/postgresql/oid/decimal"
|
11
|
+
require "active_record/connection_adapters/postgresql/oid/enum"
|
12
|
+
require "active_record/connection_adapters/postgresql/oid/hstore"
|
13
|
+
require "active_record/connection_adapters/postgresql/oid/inet"
|
14
|
+
require "active_record/connection_adapters/postgresql/oid/jsonb"
|
15
|
+
require "active_record/connection_adapters/postgresql/oid/money"
|
16
|
+
require "active_record/connection_adapters/postgresql/oid/oid"
|
17
|
+
require "active_record/connection_adapters/postgresql/oid/point"
|
18
|
+
require "active_record/connection_adapters/postgresql/oid/legacy_point"
|
19
|
+
require "active_record/connection_adapters/postgresql/oid/range"
|
20
|
+
require "active_record/connection_adapters/postgresql/oid/specialized_string"
|
21
|
+
require "active_record/connection_adapters/postgresql/oid/uuid"
|
22
|
+
require "active_record/connection_adapters/postgresql/oid/vector"
|
23
|
+
require "active_record/connection_adapters/postgresql/oid/xml"
|
24
|
+
|
25
|
+
require "active_record/connection_adapters/postgresql/oid/type_map_initializer"
|
26
|
+
|
27
|
+
module ActiveRecord
|
28
|
+
module ConnectionAdapters
|
29
|
+
module PostgreSQL
|
30
|
+
module OID # :nodoc:
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,205 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module Quoting
|
7
|
+
# Escapes binary strings for bytea input to the database.
|
8
|
+
def escape_bytea(value)
|
9
|
+
@connection.escape_bytea(value) if value
|
10
|
+
end
|
11
|
+
|
12
|
+
# Unescapes bytea output from a database to the binary string it represents.
|
13
|
+
# NOTE: This is NOT an inverse of escape_bytea! This is only to be used
|
14
|
+
# on escaped binary output from database drive.
|
15
|
+
def unescape_bytea(value)
|
16
|
+
@connection.unescape_bytea(value) if value
|
17
|
+
end
|
18
|
+
|
19
|
+
# Quotes strings for use in SQL input.
|
20
|
+
def quote_string(s) #:nodoc:
|
21
|
+
@connection.escape(s)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Checks the following cases:
|
25
|
+
#
|
26
|
+
# - table_name
|
27
|
+
# - "table.name"
|
28
|
+
# - schema_name.table_name
|
29
|
+
# - schema_name."table.name"
|
30
|
+
# - "schema.name".table_name
|
31
|
+
# - "schema.name"."table.name"
|
32
|
+
def quote_table_name(name) # :nodoc:
|
33
|
+
self.class.quoted_table_names[name] ||= Utils.extract_schema_qualified_name(name.to_s).quoted.freeze
|
34
|
+
end
|
35
|
+
|
36
|
+
# Quotes schema names for use in SQL queries.
|
37
|
+
def quote_schema_name(name)
|
38
|
+
PG::Connection.quote_ident(name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def quote_table_name_for_assignment(table, attr)
|
42
|
+
quote_column_name(attr)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Quotes column names for use in SQL queries.
|
46
|
+
def quote_column_name(name) # :nodoc:
|
47
|
+
self.class.quoted_column_names[name] ||= PG::Connection.quote_ident(super).freeze
|
48
|
+
end
|
49
|
+
|
50
|
+
# Quote date/time values for use in SQL input.
|
51
|
+
def quoted_date(value) #:nodoc:
|
52
|
+
if value.year <= 0
|
53
|
+
bce_year = format("%04d", -value.year + 1)
|
54
|
+
super.sub(/^-?\d+/, bce_year) + " BC"
|
55
|
+
else
|
56
|
+
super
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def quoted_binary(value) # :nodoc:
|
61
|
+
"'#{escape_bytea(value.to_s)}'"
|
62
|
+
end
|
63
|
+
|
64
|
+
def quote_default_expression(value, column) # :nodoc:
|
65
|
+
if value.is_a?(Proc)
|
66
|
+
value.call
|
67
|
+
elsif column.type == :uuid && value.is_a?(String) && /\(\)/.match?(value)
|
68
|
+
value # Does not quote function default values for UUID columns
|
69
|
+
elsif column.respond_to?(:array?)
|
70
|
+
value = type_cast_from_column(column, value)
|
71
|
+
quote(value)
|
72
|
+
else
|
73
|
+
super
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def lookup_cast_type_from_column(column) # :nodoc:
|
78
|
+
type_map.lookup(column.oid, column.fmod, column.sql_type)
|
79
|
+
end
|
80
|
+
|
81
|
+
def column_name_matcher
|
82
|
+
COLUMN_NAME
|
83
|
+
end
|
84
|
+
|
85
|
+
def column_name_with_order_matcher
|
86
|
+
COLUMN_NAME_WITH_ORDER
|
87
|
+
end
|
88
|
+
|
89
|
+
COLUMN_NAME = /
|
90
|
+
\A
|
91
|
+
(
|
92
|
+
(?:
|
93
|
+
# "table_name"."column_name"::type_name | function(one or no argument)::type_name
|
94
|
+
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)?
|
95
|
+
)
|
96
|
+
(?:\s+AS\s+(?:\w+|"\w+"))?
|
97
|
+
)
|
98
|
+
(?:\s*,\s*\g<1>)*
|
99
|
+
\z
|
100
|
+
/ix
|
101
|
+
|
102
|
+
COLUMN_NAME_WITH_ORDER = /
|
103
|
+
\A
|
104
|
+
(
|
105
|
+
(?:
|
106
|
+
# "table_name"."column_name"::type_name | function(one or no argument)::type_name
|
107
|
+
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)?
|
108
|
+
)
|
109
|
+
(?:\s+ASC|\s+DESC)?
|
110
|
+
(?:\s+NULLS\s+(?:FIRST|LAST))?
|
111
|
+
)
|
112
|
+
(?:\s*,\s*\g<1>)*
|
113
|
+
\z
|
114
|
+
/ix
|
115
|
+
|
116
|
+
private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER
|
117
|
+
|
118
|
+
private
|
119
|
+
def lookup_cast_type(sql_type)
|
120
|
+
super(query_value("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").to_i)
|
121
|
+
end
|
122
|
+
|
123
|
+
def _quote(value)
|
124
|
+
case value
|
125
|
+
when OID::Xml::Data
|
126
|
+
"xml '#{quote_string(value.to_s)}'"
|
127
|
+
when OID::Bit::Data
|
128
|
+
if value.binary?
|
129
|
+
"B'#{value}'"
|
130
|
+
elsif value.hex?
|
131
|
+
"X'#{value}'"
|
132
|
+
end
|
133
|
+
when Numeric
|
134
|
+
if value.finite?
|
135
|
+
super
|
136
|
+
else
|
137
|
+
"'#{value}'"
|
138
|
+
end
|
139
|
+
when OID::Array::Data
|
140
|
+
_quote(encode_array(value))
|
141
|
+
when Range
|
142
|
+
_quote(encode_range(value))
|
143
|
+
else
|
144
|
+
super
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def _type_cast(value)
|
149
|
+
case value
|
150
|
+
when Type::Binary::Data
|
151
|
+
# Return a bind param hash with format as binary.
|
152
|
+
# See https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared-doc
|
153
|
+
# for more information
|
154
|
+
{ value: value.to_s, format: 1 }
|
155
|
+
when OID::Xml::Data, OID::Bit::Data
|
156
|
+
value.to_s
|
157
|
+
when OID::Array::Data
|
158
|
+
encode_array(value)
|
159
|
+
when Range
|
160
|
+
encode_range(value)
|
161
|
+
else
|
162
|
+
super
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def encode_array(array_data)
|
167
|
+
encoder = array_data.encoder
|
168
|
+
values = type_cast_array(array_data.values)
|
169
|
+
|
170
|
+
result = encoder.encode(values)
|
171
|
+
if encoding = determine_encoding_of_strings_in_array(values)
|
172
|
+
result.force_encoding(encoding)
|
173
|
+
end
|
174
|
+
result
|
175
|
+
end
|
176
|
+
|
177
|
+
def encode_range(range)
|
178
|
+
"[#{type_cast_range_value(range.begin)},#{type_cast_range_value(range.end)}#{range.exclude_end? ? ')' : ']'}"
|
179
|
+
end
|
180
|
+
|
181
|
+
def determine_encoding_of_strings_in_array(value)
|
182
|
+
case value
|
183
|
+
when ::Array then determine_encoding_of_strings_in_array(value.first)
|
184
|
+
when ::String then value.encoding
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def type_cast_array(values)
|
189
|
+
case values
|
190
|
+
when ::Array then values.map { |item| type_cast_array(item) }
|
191
|
+
else _type_cast(values)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def type_cast_range_value(value)
|
196
|
+
infinity?(value) ? "" : type_cast(value)
|
197
|
+
end
|
198
|
+
|
199
|
+
def infinity?(value)
|
200
|
+
value.respond_to?(:infinite?) && value.infinite?
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|