Skip to content

Commit 2ec2075

Browse files
committed
Make type_map to private because it is only used in the connection adapter
`type_map` is an internal API and it is only used in the connection adapter. And also, some type map initializer methods requires passed `type_map`, but those instances already has `type_map` in itself. So we don't need explicit passing `type_map` to the initializers.
1 parent 526d4b8 commit 2ec2075

File tree

9 files changed

+28
-31
lines changed

9 files changed

+28
-31
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,6 @@ def close
451451
pool.checkin self
452452
end
453453

454-
def type_map # :nodoc:
455-
@type_map ||= Type::TypeMap.new.tap do |mapping|
456-
initialize_type_map(mapping)
457-
end
458-
end
459-
460454
def column_name_for_operation(operation, node) # :nodoc:
461455
visitor.accept(node, collector).value
462456
end
@@ -484,8 +478,13 @@ def default_index_type?(index) # :nodoc:
484478
end
485479

486480
private
481+
def type_map
482+
@type_map ||= Type::TypeMap.new.tap do |mapping|
483+
initialize_type_map(mapping)
484+
end
485+
end
487486

488-
def initialize_type_map(m)
487+
def initialize_type_map(m = type_map)
489488
register_class_with_limit m, %r(boolean)i, Type::Boolean
490489
register_class_with_limit m, %r(char)i, Type::String
491490
register_class_with_limit m, %r(binary)i, Type::Binary
@@ -518,7 +517,7 @@ def initialize_type_map(m)
518517

519518
def reload_type_map
520519
type_map.clear
521-
initialize_type_map(type_map)
520+
initialize_type_map
522521
end
523522

524523
def register_class_with_limit(mapping, key, klass)

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def without_sql_mode(mode)
545545
execute("SET @@SESSION.sql_mode = #{sql_mode}")
546546
end
547547

548-
def initialize_type_map(m)
548+
def initialize_type_map(m = type_map)
549549
super
550550

551551
register_class_with_limit m, %r(char)i, MysqlString

activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def run(records)
2929
composites.each { |row| register_composite_type(row) }
3030
end
3131

32-
def query_conditions_for_initial_load(type_map)
33-
known_type_names = type_map.keys.map { |n| "'#{n}'" }
32+
def query_conditions_for_initial_load
33+
known_type_names = @store.keys.map { |n| "'#{n}'" }
3434
known_type_types = %w('r' 'e' 'd')
3535
<<-SQL % [known_type_names.join(", "), known_type_types.join(", ")]
3636
WHERE

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def initialize(connection, logger, connection_parameters, config)
216216
add_pg_decoders
217217

218218
@type_map = Type::HashLookupTypeMap.new
219-
initialize_type_map(type_map)
219+
initialize_type_map
220220
@local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
221221
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
222222
end
@@ -425,7 +425,7 @@ def translate_exception(exception, message)
425425

426426
def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
427427
if !type_map.key?(oid)
428-
load_additional_types(type_map, [oid])
428+
load_additional_types([oid])
429429
end
430430

431431
type_map.fetch(oid, fmod, sql_type) {
@@ -436,7 +436,7 @@ def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
436436
}
437437
end
438438

439-
def initialize_type_map(m)
439+
def initialize_type_map(m = type_map)
440440
register_class_with_limit m, "int2", Type::Integer
441441
register_class_with_limit m, "int4", Type::Integer
442442
register_class_with_limit m, "int8", Type::Integer
@@ -503,7 +503,7 @@ def initialize_type_map(m)
503503
end
504504
end
505505

506-
load_additional_types(m)
506+
load_additional_types
507507
end
508508

509509
def extract_limit(sql_type)
@@ -552,7 +552,7 @@ def has_default_function?(default_value, default)
552552
!default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
553553
end
554554

555-
def load_additional_types(type_map, oids = nil)
555+
def load_additional_types(oids = nil)
556556
initializer = OID::TypeMapInitializer.new(type_map)
557557

558558
if supports_ranges?
@@ -571,7 +571,7 @@ def load_additional_types(type_map, oids = nil)
571571
if oids
572572
query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
573573
else
574-
query += initializer.query_conditions_for_initial_load(type_map)
574+
query += initializer.query_conditions_for_initial_load
575575
end
576576

577577
execute_and_clear(query, "SCHEMA", []) do |records|

activerecord/test/cases/adapters/postgresql/composite_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def serialize(value)
104104
def setup
105105
super
106106

107-
@connection.type_map.register_type "full_address", FullAddressType.new
107+
@connection.send(:type_map).register_type "full_address", FullAddressType.new
108108
end
109109

110110
def test_column

activerecord/test/cases/adapters/postgresql/type_lookup_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ class PostgresqlTypeLookupTest < ActiveRecord::PostgreSQLTestCase
66
end
77

88
test "array delimiters are looked up correctly" do
9-
box_array = @connection.type_map.lookup(1020)
10-
int_array = @connection.type_map.lookup(1007)
9+
box_array = @connection.send(:type_map).lookup(1020)
10+
int_array = @connection.send(:type_map).lookup(1007)
1111

1212
assert_equal ";", box_array.delimiter
1313
assert_equal ",", int_array.delimiter
1414
end
1515

1616
test "array types correctly respect registration of subtypes" do
17-
int_array = @connection.type_map.lookup(1007, -1, "integer[]")
18-
bigint_array = @connection.type_map.lookup(1016, -1, "bigint[]")
17+
int_array = @connection.send(:type_map).lookup(1007, -1, "integer[]")
18+
bigint_array = @connection.send(:type_map).lookup(1016, -1, "bigint[]")
1919
big_array = [123456789123456789]
2020

2121
assert_raises(ActiveModel::RangeError) { int_array.serialize(big_array) }
2222
assert_equal "{123456789123456789}", @connection.type_cast(bigint_array.serialize(big_array))
2323
end
2424

2525
test "range types correctly respect registration of subtypes" do
26-
int_range = @connection.type_map.lookup(3904, -1, "int4range")
27-
bigint_range = @connection.type_map.lookup(3926, -1, "int8range")
26+
int_range = @connection.send(:type_map).lookup(3904, -1, "int4range")
27+
bigint_range = @connection.send(:type_map).lookup(3926, -1, "int8range")
2828
big_range = 0..123456789123456789
2929

3030
assert_raises(ActiveModel::RangeError) { int_range.serialize(big_range) }

activerecord/test/cases/connection_adapters/mysql_type_lookup_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_integer_types
4747
private
4848

4949
def assert_lookup_type(type, lookup)
50-
cast_type = @connection.type_map.lookup(lookup)
50+
cast_type = @connection.send(:type_map).lookup(lookup)
5151
assert_equal type, cast_type.type
5252
end
5353

activerecord/test/cases/connection_adapters/type_lookup_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_integer_types
8080
end
8181

8282
def test_bigint_limit
83-
cast_type = @connection.type_map.lookup("bigint")
83+
cast_type = @connection.send(:type_map).lookup("bigint")
8484
if current_adapter?(:OracleAdapter)
8585
assert_equal 19, cast_type.limit
8686
else
@@ -98,7 +98,7 @@ def test_decimal_without_scale
9898
{ decimal: %w{decimal(2) decimal(2,0) numeric(2) numeric(2,0) number(2) number(2,0)} }
9999
end.each do |expected_type, types|
100100
types.each do |type|
101-
cast_type = @connection.type_map.lookup(type)
101+
cast_type = @connection.send(:type_map).lookup(type)
102102

103103
assert_equal expected_type, cast_type.type
104104
assert_equal 2, cast_type.cast(2.1)
@@ -109,7 +109,7 @@ def test_decimal_without_scale
109109
private
110110

111111
def assert_lookup_type(type, lookup)
112-
cast_type = @connection.type_map.lookup(lookup)
112+
cast_type = @connection.send(:type_map).lookup(lookup)
113113
assert_equal type, cast_type.type
114114
end
115115
end

activerecord/test/cases/query_cache_test.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,8 @@ def test_query_cached_even_when_types_are_reset
400400
# Warm the cache
401401
Task.find(1)
402402

403-
Task.connection.type_map.clear
404-
405403
# Preload the type cache again (so we don't have those queries issued during our assertions)
406-
Task.connection.send(:initialize_type_map, Task.connection.type_map)
404+
Task.connection.send(:reload_type_map)
407405

408406
# Clear places where type information is cached
409407
Task.reset_column_information

0 commit comments

Comments
 (0)