activerecord 5.2.3
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 +937 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +217 -0
- data/examples/performance.rb +185 -0
- data/examples/simple.rb +15 -0
- data/lib/active_record.rb +188 -0
- data/lib/active_record/aggregations.rb +283 -0
- data/lib/active_record/association_relation.rb +40 -0
- data/lib/active_record/associations.rb +1860 -0
- data/lib/active_record/associations/alias_tracker.rb +81 -0
- data/lib/active_record/associations/association.rb +299 -0
- data/lib/active_record/associations/association_scope.rb +168 -0
- data/lib/active_record/associations/belongs_to_association.rb +130 -0
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +40 -0
- data/lib/active_record/associations/builder/association.rb +140 -0
- data/lib/active_record/associations/builder/belongs_to.rb +163 -0
- data/lib/active_record/associations/builder/collection_association.rb +82 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +135 -0
- data/lib/active_record/associations/builder/has_many.rb +17 -0
- data/lib/active_record/associations/builder/has_one.rb +30 -0
- data/lib/active_record/associations/builder/singular_association.rb +42 -0
- data/lib/active_record/associations/collection_association.rb +513 -0
- data/lib/active_record/associations/collection_proxy.rb +1131 -0
- data/lib/active_record/associations/foreign_association.rb +13 -0
- data/lib/active_record/associations/has_many_association.rb +144 -0
- data/lib/active_record/associations/has_many_through_association.rb +227 -0
- data/lib/active_record/associations/has_one_association.rb +120 -0
- data/lib/active_record/associations/has_one_through_association.rb +45 -0
- data/lib/active_record/associations/join_dependency.rb +262 -0
- data/lib/active_record/associations/join_dependency/join_association.rb +60 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +23 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +71 -0
- data/lib/active_record/associations/preloader.rb +193 -0
- data/lib/active_record/associations/preloader/association.rb +131 -0
- data/lib/active_record/associations/preloader/through_association.rb +107 -0
- data/lib/active_record/associations/singular_association.rb +73 -0
- data/lib/active_record/associations/through_association.rb +121 -0
- data/lib/active_record/attribute_assignment.rb +88 -0
- data/lib/active_record/attribute_decorators.rb +90 -0
- data/lib/active_record/attribute_methods.rb +492 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +78 -0
- data/lib/active_record/attribute_methods/dirty.rb +150 -0
- data/lib/active_record/attribute_methods/primary_key.rb +143 -0
- data/lib/active_record/attribute_methods/query.rb +42 -0
- data/lib/active_record/attribute_methods/read.rb +85 -0
- data/lib/active_record/attribute_methods/serialization.rb +90 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +91 -0
- data/lib/active_record/attribute_methods/write.rb +68 -0
- data/lib/active_record/attributes.rb +266 -0
- data/lib/active_record/autosave_association.rb +498 -0
- data/lib/active_record/base.rb +329 -0
- data/lib/active_record/callbacks.rb +353 -0
- data/lib/active_record/coders/json.rb +15 -0
- data/lib/active_record/coders/yaml_column.rb +50 -0
- data/lib/active_record/collection_cache_key.rb +53 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +1068 -0
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +72 -0
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +540 -0
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +145 -0
- data/lib/active_record/connection_adapters/abstract/quoting.rb +200 -0
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +23 -0
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +146 -0
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +685 -0
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +95 -0
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1396 -0
- data/lib/active_record/connection_adapters/abstract/transaction.rb +283 -0
- data/lib/active_record/connection_adapters/abstract_adapter.rb +628 -0
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +887 -0
- data/lib/active_record/connection_adapters/column.rb +91 -0
- data/lib/active_record/connection_adapters/connection_specification.rb +287 -0
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +129 -0
- data/lib/active_record/connection_adapters/postgresql/column.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +163 -0
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid.rb +34 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +92 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +56 -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 +111 -0
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +23 -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/quoting.rb +168 -0
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +43 -0
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +206 -0
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +774 -0
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +81 -0
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +863 -0
- data/lib/active_record/connection_adapters/schema_cache.rb +118 -0
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -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 +106 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +573 -0
- data/lib/active_record/connection_adapters/statement_pool.rb +61 -0
- data/lib/active_record/connection_handling.rb +145 -0
- data/lib/active_record/core.rb +559 -0
- data/lib/active_record/counter_cache.rb +218 -0
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +122 -0
- data/lib/active_record/enum.rb +244 -0
- data/lib/active_record/errors.rb +380 -0
- data/lib/active_record/explain.rb +50 -0
- data/lib/active_record/explain_registry.rb +32 -0
- data/lib/active_record/explain_subscriber.rb +34 -0
- data/lib/active_record/fixture_set/file.rb +82 -0
- data/lib/active_record/fixtures.rb +1065 -0
- data/lib/active_record/gem_version.rb +17 -0
- data/lib/active_record/inheritance.rb +283 -0
- data/lib/active_record/integration.rb +155 -0
- data/lib/active_record/internal_metadata.rb +45 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +48 -0
- data/lib/active_record/locking/optimistic.rb +198 -0
- data/lib/active_record/locking/pessimistic.rb +89 -0
- data/lib/active_record/log_subscriber.rb +137 -0
- data/lib/active_record/migration.rb +1378 -0
- data/lib/active_record/migration/command_recorder.rb +240 -0
- data/lib/active_record/migration/compatibility.rb +217 -0
- data/lib/active_record/migration/join_table.rb +17 -0
- data/lib/active_record/model_schema.rb +521 -0
- data/lib/active_record/nested_attributes.rb +600 -0
- data/lib/active_record/no_touching.rb +58 -0
- data/lib/active_record/null_relation.rb +68 -0
- data/lib/active_record/persistence.rb +763 -0
- data/lib/active_record/query_cache.rb +45 -0
- data/lib/active_record/querying.rb +70 -0
- data/lib/active_record/railtie.rb +226 -0
- data/lib/active_record/railties/console_sandbox.rb +7 -0
- data/lib/active_record/railties/controller_runtime.rb +56 -0
- data/lib/active_record/railties/databases.rake +377 -0
- data/lib/active_record/readonly_attributes.rb +24 -0
- data/lib/active_record/reflection.rb +1044 -0
- data/lib/active_record/relation.rb +629 -0
- data/lib/active_record/relation/batches.rb +287 -0
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/calculations.rb +417 -0
- data/lib/active_record/relation/delegation.rb +147 -0
- data/lib/active_record/relation/finder_methods.rb +565 -0
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +193 -0
- data/lib/active_record/relation/predicate_builder.rb +152 -0
- data/lib/active_record/relation/predicate_builder/array_handler.rb +48 -0
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +19 -0
- data/lib/active_record/relation/query_attribute.rb +45 -0
- data/lib/active_record/relation/query_methods.rb +1231 -0
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +77 -0
- data/lib/active_record/relation/where_clause.rb +186 -0
- data/lib/active_record/relation/where_clause_factory.rb +34 -0
- data/lib/active_record/result.rb +149 -0
- data/lib/active_record/runtime_registry.rb +24 -0
- data/lib/active_record/sanitization.rb +222 -0
- data/lib/active_record/schema.rb +70 -0
- data/lib/active_record/schema_dumper.rb +255 -0
- data/lib/active_record/schema_migration.rb +56 -0
- data/lib/active_record/scoping.rb +106 -0
- data/lib/active_record/scoping/default.rb +152 -0
- data/lib/active_record/scoping/named.rb +213 -0
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +22 -0
- data/lib/active_record/statement_cache.rb +121 -0
- data/lib/active_record/store.rb +211 -0
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +82 -0
- data/lib/active_record/tasks/database_tasks.rb +337 -0
- data/lib/active_record/tasks/mysql_database_tasks.rb +115 -0
- data/lib/active_record/tasks/postgresql_database_tasks.rb +143 -0
- data/lib/active_record/tasks/sqlite_database_tasks.rb +83 -0
- data/lib/active_record/timestamp.rb +153 -0
- data/lib/active_record/touch_later.rb +64 -0
- data/lib/active_record/transactions.rb +502 -0
- data/lib/active_record/translation.rb +24 -0
- data/lib/active_record/type.rb +79 -0
- data/lib/active_record/type/adapter_specific_registry.rb +136 -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_caster.rb +9 -0
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -0
- data/lib/active_record/validations.rb +93 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +60 -0
- 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 +238 -0
- data/lib/active_record/version.rb +10 -0
- data/lib/rails/generators/active_record.rb +19 -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.rb +35 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +78 -0
- 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 +46 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
- data/lib/rails/generators/active_record/model/templates/model.rb.tt +13 -0
- data/lib/rails/generators/active_record/model/templates/module.rb.tt +7 -0
- metadata +333 -0
@@ -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 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(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,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module ConnectionAdapters
|
5
|
+
module PostgreSQL
|
6
|
+
module OID # :nodoc:
|
7
|
+
# This class uses the data from PostgreSQL pg_type table to build
|
8
|
+
# the OID -> Type mapping.
|
9
|
+
# - OID is an integer representing the type.
|
10
|
+
# - Type is an OID::Type object.
|
11
|
+
# This class has side effects on the +store+ passed during initialization.
|
12
|
+
class TypeMapInitializer # :nodoc:
|
13
|
+
def initialize(store)
|
14
|
+
@store = store
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(records)
|
18
|
+
nodes = records.reject { |row| @store.key? row["oid"].to_i }
|
19
|
+
mapped, nodes = nodes.partition { |row| @store.key? row["typname"] }
|
20
|
+
ranges, nodes = nodes.partition { |row| row["typtype"] == "r".freeze }
|
21
|
+
enums, nodes = nodes.partition { |row| row["typtype"] == "e".freeze }
|
22
|
+
domains, nodes = nodes.partition { |row| row["typtype"] == "d".freeze }
|
23
|
+
arrays, nodes = nodes.partition { |row| row["typinput"] == "array_in".freeze }
|
24
|
+
composites, nodes = nodes.partition { |row| row["typelem"].to_i != 0 }
|
25
|
+
|
26
|
+
mapped.each { |row| register_mapped_type(row) }
|
27
|
+
enums.each { |row| register_enum_type(row) }
|
28
|
+
domains.each { |row| register_domain_type(row) }
|
29
|
+
arrays.each { |row| register_array_type(row) }
|
30
|
+
ranges.each { |row| register_range_type(row) }
|
31
|
+
composites.each { |row| register_composite_type(row) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def query_conditions_for_initial_load
|
35
|
+
known_type_names = @store.keys.map { |n| "'#{n}'" }
|
36
|
+
known_type_types = %w('r' 'e' 'd')
|
37
|
+
<<-SQL % [known_type_names.join(", "), known_type_types.join(", ")]
|
38
|
+
WHERE
|
39
|
+
t.typname IN (%s)
|
40
|
+
OR t.typtype IN (%s)
|
41
|
+
OR t.typinput = 'array_in(cstring,oid,integer)'::regprocedure
|
42
|
+
OR t.typelem != 0
|
43
|
+
SQL
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def register_mapped_type(row)
|
48
|
+
alias_type row["oid"], row["typname"]
|
49
|
+
end
|
50
|
+
|
51
|
+
def register_enum_type(row)
|
52
|
+
register row["oid"], OID::Enum.new
|
53
|
+
end
|
54
|
+
|
55
|
+
def register_array_type(row)
|
56
|
+
register_with_subtype(row["oid"], row["typelem"].to_i) do |subtype|
|
57
|
+
OID::Array.new(subtype, row["typdelim"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def register_range_type(row)
|
62
|
+
register_with_subtype(row["oid"], row["rngsubtype"].to_i) do |subtype|
|
63
|
+
OID::Range.new(subtype, row["typname"].to_sym)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def register_domain_type(row)
|
68
|
+
if base_type = @store.lookup(row["typbasetype"].to_i)
|
69
|
+
register row["oid"], base_type
|
70
|
+
else
|
71
|
+
warn "unknown base type (OID: #{row["typbasetype"]}) for domain #{row["typname"]}."
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def register_composite_type(row)
|
76
|
+
if subtype = @store.lookup(row["typelem"].to_i)
|
77
|
+
register row["oid"], OID::Vector.new(row["typdelim"], subtype)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def register(oid, oid_type = nil, &block)
|
82
|
+
oid = assert_valid_registration(oid, oid_type || block)
|
83
|
+
if block_given?
|
84
|
+
@store.register_type(oid, &block)
|
85
|
+
else
|
86
|
+
@store.register_type(oid, oid_type)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def alias_type(oid, target)
|
91
|
+
oid = assert_valid_registration(oid, target)
|
92
|
+
@store.alias_type(oid, target)
|
93
|
+
end
|
94
|
+
|
95
|
+
def register_with_subtype(oid, target_oid)
|
96
|
+
if @store.key?(target_oid)
|
97
|
+
register(oid) do |_, *args|
|
98
|
+
yield @store.lookup(target_oid, *args)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def assert_valid_registration(oid, oid_type)
|
104
|
+
raise ArgumentError, "can't register nil type for OID #{oid}" if oid_type.nil?
|
105
|
+
oid.to_i
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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
|
+
def cast(value)
|
17
|
+
value.to_s[ACCEPTABLE_UUID, 0]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
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,168 @@
|
|
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
|
+
@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
|
+
@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
|
+
private
|
82
|
+
def lookup_cast_type(sql_type)
|
83
|
+
super(query_value("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").to_i)
|
84
|
+
end
|
85
|
+
|
86
|
+
def _quote(value)
|
87
|
+
case value
|
88
|
+
when OID::Xml::Data
|
89
|
+
"xml '#{quote_string(value.to_s)}'"
|
90
|
+
when OID::Bit::Data
|
91
|
+
if value.binary?
|
92
|
+
"B'#{value}'"
|
93
|
+
elsif value.hex?
|
94
|
+
"X'#{value}'"
|
95
|
+
end
|
96
|
+
when Float
|
97
|
+
if value.infinite? || value.nan?
|
98
|
+
"'#{value}'"
|
99
|
+
else
|
100
|
+
super
|
101
|
+
end
|
102
|
+
when OID::Array::Data
|
103
|
+
_quote(encode_array(value))
|
104
|
+
when Range
|
105
|
+
_quote(encode_range(value))
|
106
|
+
else
|
107
|
+
super
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def _type_cast(value)
|
112
|
+
case value
|
113
|
+
when Type::Binary::Data
|
114
|
+
# Return a bind param hash with format as binary.
|
115
|
+
# See https://deveiate.org/code/pg/PG/Connection.html#method-i-exec_prepared-doc
|
116
|
+
# for more information
|
117
|
+
{ value: value.to_s, format: 1 }
|
118
|
+
when OID::Xml::Data, OID::Bit::Data
|
119
|
+
value.to_s
|
120
|
+
when OID::Array::Data
|
121
|
+
encode_array(value)
|
122
|
+
when Range
|
123
|
+
encode_range(value)
|
124
|
+
else
|
125
|
+
super
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def encode_array(array_data)
|
130
|
+
encoder = array_data.encoder
|
131
|
+
values = type_cast_array(array_data.values)
|
132
|
+
|
133
|
+
result = encoder.encode(values)
|
134
|
+
if encoding = determine_encoding_of_strings_in_array(values)
|
135
|
+
result.force_encoding(encoding)
|
136
|
+
end
|
137
|
+
result
|
138
|
+
end
|
139
|
+
|
140
|
+
def encode_range(range)
|
141
|
+
"[#{type_cast_range_value(range.first)},#{type_cast_range_value(range.last)}#{range.exclude_end? ? ')' : ']'}"
|
142
|
+
end
|
143
|
+
|
144
|
+
def determine_encoding_of_strings_in_array(value)
|
145
|
+
case value
|
146
|
+
when ::Array then determine_encoding_of_strings_in_array(value.first)
|
147
|
+
when ::String then value.encoding
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def type_cast_array(values)
|
152
|
+
case values
|
153
|
+
when ::Array then values.map { |item| type_cast_array(item) }
|
154
|
+
else _type_cast(values)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def type_cast_range_value(value)
|
159
|
+
infinity?(value) ? "" : type_cast(value)
|
160
|
+
end
|
161
|
+
|
162
|
+
def infinity?(value)
|
163
|
+
value.respond_to?(:infinite?) && value.infinite?
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|