From 9a4a095ed7ea6f0f65cc9e3bf3258cbdd0ddc210 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 16 Dec 2012 04:10:58 +0200 Subject: AR supporting new intrange data type on PostgreSQL >= 9.2 --- .../active_record/connection_adapters/column.rb | 1 + .../connection_adapters/postgresql/cast.rb | 23 ++++++++++++++++++++++ .../connection_adapters/postgresql/oid.rb | 11 +++++++++++ .../connection_adapters/postgresql/quoting.rb | 10 ++++++++++ .../postgresql/schema_statements.rb | 8 ++++++++ .../connection_adapters/postgresql_adapter.rb | 19 ++++++++++++++---- 6 files changed, 68 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters') diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index df23dbfb60..fd36a5b075 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -126,6 +126,7 @@ module ActiveRecord when :hstore then "#{klass}.string_to_hstore(#{var_name})" when :inet, :cidr then "#{klass}.string_to_cidr(#{var_name})" when :json then "#{klass}.string_to_json(#{var_name})" + when :intrange then "#{klass}.string_to_intrange(#{var_name})" else var_name end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index c04a799b8d..3772f7ddaa 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -92,6 +92,29 @@ module ActiveRecord parse_pg_array(string).map{|val| oid.type_cast val} end + def string_to_intrange(string) + if string.nil? + nil + elsif "empty" == string + (nil..nil) + elsif String === string + matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string) + lower_bound = ("(" == matches[1] ? (matches[2].to_i + 1) : matches[2].to_i) + upper_bound = (")" == matches[5] ? (matches[4].to_i - 1) : matches[4].to_i) + (lower_bound..upper_bound) + else + string + end + end + + def intrange_to_string(object) + if Range === object + "[#{object.first},#{object.exclude_end? ? object.last : object.last.to_i + 1})" + else + object + end + end + private HstorePair = begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index 52344f61c0..18ea83ce42 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -168,6 +168,14 @@ module ActiveRecord end end + class IntRange < Type + def type_cast(value) + return if value.nil? + + ConnectionAdapters::PostgreSQLColumn.string_to_intrange value + end + end + class TypeMap def initialize @mapping = {} @@ -269,6 +277,9 @@ module ActiveRecord register_type 'hstore', OID::Hstore.new register_type 'json', OID::Json.new + register_type 'int4range', OID::IntRange.new + alias_type 'int8range', 'int4range' + register_type 'cidr', OID::Cidr.new alias_type 'inet', 'cidr' end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 62a4d76928..c2fcef94da 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -31,6 +31,11 @@ module ActiveRecord when 'json' then super(PostgreSQLColumn.json_to_string(value), column) else super end + when Range + case column.sql_type + when 'int4range', 'int8range' then super(PostgreSQLColumn.intrange_to_string(value), column) + else super + end when IPAddr case column.sql_type when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column) @@ -89,6 +94,11 @@ module ActiveRecord when 'json' then PostgreSQLColumn.json_to_string(value) else super(value, column) end + when Range + case column.sql_type + when 'int4range', 'int8range' then PostgreSQLColumn.intrange_to_string(value) + else super(value, column) + end when IPAddr return super(value, column) unless ['inet','cidr'].include? column.sql_type PostgreSQLColumn.cidr_to_string(value) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 18bf14d1fb..e10b562fa4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -417,6 +417,14 @@ module ActiveRecord when 0..6; "timestamp(#{precision})" else raise(ActiveRecordError, "No timestamp type has precision of #{precision}. The allowed range of precision is from 0 to 6") end + when 'intrange' + return 'int4range' unless limit + + case limit + when 1..4; 'int4range' + when 5..8; 'int8range' + else raise(ActiveRecordError, "No range type has byte size #{limit}. Use a numeric with precision 0 instead.") + end else super end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e24ee1efdd..d62a375470 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -114,6 +114,9 @@ module ActiveRecord # JSON when /\A'(.*)'::json\z/ $1 + # int4range, int8range + when /\A'(.*)'::int(4|8)range\z/ + $1 # Object identifier types when /\A-?\d+\z/ $1 @@ -209,9 +212,12 @@ module ActiveRecord # UUID type when 'uuid' :uuid - # JSON type - when 'json' - :json + # JSON type + when 'json' + :json + # int4range, int8range types + when 'int4range', 'int8range' + :intrange # Small and big integer types when /^(?:small|big)int$/ :integer @@ -289,6 +295,10 @@ module ActiveRecord column(name, 'json', options) end + def intrange(name, options = {}) + column(name, 'intrange', options) + end + def column(name, type = nil, options = {}) super column = self[name] @@ -329,7 +339,8 @@ module ActiveRecord cidr: { name: "cidr" }, macaddr: { name: "macaddr" }, uuid: { name: "uuid" }, - json: { name: "json" } + json: { name: "json" }, + intrange: { name: "int4range" } } include Quoting -- cgit v1.2.3 From 49182b87b258786ae5942eeb475618794291e976 Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 17 Dec 2012 21:16:40 +0200 Subject: AR supporting new int4range and int8range data type on PostgreSQL >= 9.2. Fix realization --- .../active_record/connection_adapters/postgresql/cast.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index 3772f7ddaa..f7d734a2f1 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -97,8 +97,7 @@ module ActiveRecord nil elsif "empty" == string (nil..nil) - elsif String === string - matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string) + elsif String === string && (matches = /^(\(|\[)([0-9]+),(\s?)([0-9]+)(\)|\])$/i.match(string)) lower_bound = ("(" == matches[1] ? (matches[2].to_i + 1) : matches[2].to_i) upper_bound = (")" == matches[5] ? (matches[4].to_i - 1) : matches[4].to_i) (lower_bound..upper_bound) @@ -108,8 +107,16 @@ module ActiveRecord end def intrange_to_string(object) - if Range === object - "[#{object.first},#{object.exclude_end? ? object.last : object.last.to_i + 1})" + if object.nil? + nil + elsif Range === object + if [object.first, object.last].all? { |el| Integer === el } + "[#{object.first.to_i},#{object.exclude_end? ? object.last.to_i : object.last.to_i + 1})" + elsif [object.first, object.last].all? { |el| NilClass === el } + "empty" + else + nil + end else object end -- cgit v1.2.3