aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-01-21 12:14:11 +0100
committerYves Senn <yves.senn@gmail.com>2014-02-23 12:47:30 +0100
commit4cb47167e747e8f9dc12b0ddaf82bdb68c03e032 (patch)
tree2dd135847166865e70077a2586751fc5997ee18c /activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
parent40a9d8987711f7973c5e95087bcc0c59e167294b (diff)
downloadrails-4cb47167e747e8f9dc12b0ddaf82bdb68c03e032.tar.gz
rails-4cb47167e747e8f9dc12b0ddaf82bdb68c03e032.tar.bz2
rails-4cb47167e747e8f9dc12b0ddaf82bdb68c03e032.zip
dynamically define PostgreSQL OID range types.
This gets AR working with custom defined range types. It also removes the need for subtype specific branches in `OID::Range`. This expands the interface of all `OID` types with the `infinity` method. It's responsible to provide a value for positive and negative infinity.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb28
1 files changed, 23 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 36c7462419..d1fb132b60 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -785,18 +785,29 @@ module ActiveRecord
end
def initialize_type_map(type_map)
- result = execute('SELECT oid, typname, typelem, typdelim, typinput FROM pg_type', 'SCHEMA')
- leaves, nodes = result.partition { |row| row['typelem'] == '0' }
+ if supports_ranges?
+ result = execute(<<-SQL, 'SCHEMA')
+ SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype
+ FROM pg_type as t
+ LEFT JOIN pg_range as r ON oid = rngtypid
+ SQL
+ else
+ result = execute(<<-SQL, 'SCHEMA')
+ SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput
+ FROM pg_type as t
+ SQL
+ end
+ ranges, nodes = result.partition { |row| row['typinput'] == 'range_in' }
+ leaves, nodes = nodes.partition { |row| row['typelem'] == '0' }
+ arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }
- # populate the leaf nodes
+ # populate the base types
leaves.find_all { |row| OID.registered_type? row['typname'] }.each do |row|
type_map[row['oid'].to_i] = OID::NAMES[row['typname']]
end
records_by_oid = result.group_by { |row| row['oid'] }
- arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }
-
# populate composite types
nodes.each do |row|
add_oid row, records_by_oid, type_map
@@ -807,6 +818,13 @@ module ActiveRecord
array = OID::Array.new type_map[row['typelem'].to_i]
type_map[row['oid'].to_i] = array
end
+
+ # populate range types
+ ranges.find_all { |row| type_map.key? row['rngsubtype'].to_i }.each do |row|
+ subtype = type_map[row['rngsubtype'].to_i]
+ range = OID::Range.new type_map[row['rngsubtype'].to_i]
+ type_map[row['oid'].to_i] = range
+ end
end
FEATURE_NOT_SUPPORTED = "0A000" #:nodoc: