提交 3b814ed2 编写于 作者: R Rafael Mendonça França

Merge pull request #15239 from croaky/dc-sg-move-extract-precision-to-types

Move `extract_precision` onto type objects
...@@ -69,15 +69,11 @@ def extract_default(default) ...@@ -69,15 +69,11 @@ def extract_default(default)
end end
private private
delegate :extract_scale, to: :cast_type delegate :extract_scale, :extract_precision, to: :cast_type
def extract_limit(sql_type) def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/ $1.to_i if sql_type =~ /\((.*)\)/
end end
def extract_precision(sql_type)
$2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
end
end end
end end
# :startdoc: # :startdoc:
......
...@@ -35,8 +35,6 @@ class << self ...@@ -35,8 +35,6 @@ class << self
require 'active_record/connection_adapters/postgresql/array_parser' require 'active_record/connection_adapters/postgresql/array_parser'
include PostgreSQL::ArrayParser include PostgreSQL::ArrayParser
end end
attr_accessor :money_precision
end end
# :startdoc: # :startdoc:
...@@ -121,17 +119,6 @@ def extract_limit(sql_type) ...@@ -121,17 +119,6 @@ def extract_limit(sql_type)
else super else super
end end
end end
# Extracts the precision from PostgreSQL-specific data types.
def extract_precision(sql_type)
if sql_type == 'money'
self.class.money_precision
elsif sql_type =~ /timestamp/i
$1.to_i if sql_type =~ /\((\d+)\)/
else
super
end
end
end end
end end
end end
...@@ -5,6 +5,8 @@ module OID # :nodoc: ...@@ -5,6 +5,8 @@ module OID # :nodoc:
class Money < Type::Decimal class Money < Type::Decimal
include Infinity include Infinity
class_attribute :precision
def extract_scale(sql_type) def extract_scale(sql_type)
2 2
end end
......
...@@ -713,7 +713,7 @@ def connect ...@@ -713,7 +713,7 @@ def connect
# Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of
# PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision
# should know about this but can't detect it there, so deal with it here. # should know about this but can't detect it there, so deal with it here.
PostgreSQLColumn.money_precision = (postgresql_version >= 80300) ? 19 : 10 OID::Money.precision = (postgresql_version >= 80300) ? 19 : 10
configure_connection configure_connection
rescue ::PG::Error => error rescue ::PG::Error => error
......
...@@ -8,6 +8,10 @@ def type ...@@ -8,6 +8,10 @@ def type
:datetime :datetime
end end
def extract_precision(sql_type)
$1.to_i if sql_type =~ /\((\d+)\)/
end
private private
def cast_value(string) def cast_value(string)
......
...@@ -14,6 +14,10 @@ def type_cast_for_write(value) ...@@ -14,6 +14,10 @@ def type_cast_for_write(value)
else super else super
end end
end end
def extract_precision(sql_type)
$1.to_i if sql_type =~ /\((\d+)(,\d+)?\)/
end
end end
end end
end end
......
...@@ -3,8 +3,11 @@ module ConnectionAdapters ...@@ -3,8 +3,11 @@ module ConnectionAdapters
module Type module Type
class Value # :nodoc: class Value # :nodoc:
def type; end def type; end
def extract_scale(sql_type); end def extract_scale(sql_type); end
def extract_precision(sql_type); end
def type_cast(value) def type_cast(value)
cast_value(value) unless value.nil? cast_value(value) unless value.nil?
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册