Skip to content

Commit 252630a

Browse files
committed
minor postres compatibility tunings - on AR 4.2 load the type map eagerly (like in Rails)
1 parent e1dbb6c commit 252630a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/arjdbc/postgresql/adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ def native_database_types
265265
end
266266

267267
# Adds `:array` option to the default set provided by the `AbstractAdapter`.
268+
# @override
268269
def prepare_column_options(column, types)
269270
spec = super
270271
spec[:array] = 'true' if column.respond_to?(:array) && column.array
@@ -273,6 +274,7 @@ def prepare_column_options(column, types)
273274
end if AR4_COMPAT
274275

275276
# Adds `:array` as a valid migration key.
277+
# @override
276278
def migration_keys
277279
super + [:array]
278280
end if AR4_COMPAT
@@ -1378,6 +1380,8 @@ def initialize(*args)
13781380

13791381
@table_alias_length = nil
13801382

1383+
initialize_type_map(@type_map = Type::HashLookupTypeMap.new) if AR42_COMPAT
1384+
13811385
@use_insert_returning = @config.key?(:insert_returning) ?
13821386
self.class.type_cast_config_to_boolean(@config[:insert_returning]) : nil
13831387
end

lib/arjdbc/postgresql/oid_types.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_oid_type(oid, fmod, column_name)
4444
}
4545
end unless AR42_COMPAT
4646

47-
def get_oid_type(oid, fmod, column_name, sql_type = '') # :nodoc:
47+
def get_oid_type(oid, fmod, column_name, sql_type = '')
4848
if !type_map.key?(oid)
4949
load_additional_types(type_map, [oid])
5050
end
@@ -79,7 +79,8 @@ def dup
7979
end
8080

8181
def type_map
82-
# NOTE: our type_map is lazy since it's only used for `adapter.accessor`
82+
# NOTE: our type_map is lazy (on AR < 4.2)
83+
# ... since it's only used for `adapter.accessor`
8384
@type_map ||= begin
8485
if type_map = @@type_map_cache[ type_cache_key ]
8586
type_map.dup
@@ -150,10 +151,10 @@ def initialize_type_map(type_map)
150151
end
151152
end unless AR42_COMPAT
152153

153-
def initialize_type_map(m) # :nodoc:
154+
def initialize_type_map(m)
154155
register_class_with_limit m, 'int2', OID::Integer
155-
m.alias_type 'int4', 'int2'
156-
m.alias_type 'int8', 'int2'
156+
register_class_with_limit m, 'int4', OID::Integer
157+
register_class_with_limit m, 'int8', OID::Integer
157158
m.alias_type 'oid', 'int2'
158159
m.register_type 'float4', OID::Float.new
159160
m.alias_type 'float8', 'float4'
@@ -221,7 +222,7 @@ def initialize_type_map(m) # :nodoc:
221222
load_additional_types(m)
222223
end if AR42_COMPAT
223224

224-
def load_additional_types(type_map, oids = nil) # :nodoc:
225+
def load_additional_types(type_map, oids = nil)
225226
if supports_ranges?
226227
query = <<-SQL
227228
SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
@@ -235,11 +236,16 @@ def load_additional_types(type_map, oids = nil) # :nodoc:
235236
SQL
236237
end
237238

239+
initializer = OID::TypeMapInitializer.new(type_map)
240+
238241
if oids
239-
query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
242+
query << "WHERE t.oid::integer IN (%s)" % oids.join(", ")
243+
else
244+
if initializer.respond_to?(:query_conditions_for_initial_load)
245+
query << initializer.query_conditions_for_initial_load(type_map)
246+
end
240247
end
241248

242-
initializer = OID::TypeMapInitializer.new(type_map)
243249
records = execute(query, 'SCHEMA')
244250
initializer.run(records)
245251
end if AR42_COMPAT

0 commit comments

Comments
 (0)