提交 57cc778f 编写于 作者: S Sean Griffin

Push scale to type objects

Ideally types will be usable without having to specify a sql type
string, so we should keep the information related to parsing them on the
adapter or another object.
上级 3b814ed2
......@@ -390,10 +390,12 @@ def initialize_type_map(m) # :nodoc:
m.alias_type %r(double)i, 'float'
m.register_type %r(int)i, Type::Integer.new
m.register_type(%r(decimal)i) do |sql_type|
if Type.extract_scale(sql_type) == 0
scale = extract_scale(sql_type)
if scale == 0
Type::Integer.new
else
Type::Decimal.new
Type::Decimal.new(scale: scale)
end
end
end
......@@ -403,6 +405,13 @@ def reload_type_map # :nodoc:
initialize_type_map(type_map)
end
def extract_scale(sql_type) # :nodoc:
case sql_type
when /\((\d+)\)/ then 0
when /\((\d+)(,(\d+))\)/ then $3.to_i
end
end
def translate_exception_class(e, sql)
message = "#{e.class.name}: #{e.message}: #{sql}"
@logger.error message if @logger
......
......@@ -13,12 +13,12 @@ module Format
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
end
attr_reader :name, :default, :cast_type, :limit, :null, :sql_type, :precision, :scale, :default_function
attr_reader :name, :default, :cast_type, :limit, :null, :sql_type, :precision, :default_function
attr_accessor :primary, :coder
alias :encoded? :coder
delegate :type, :klass, :text?, :number?, :binary?, :type_cast_for_write, to: :cast_type
delegate :type, :scale, :klass, :text?, :number?, :binary?, :type_cast_for_write, to: :cast_type
# Instantiates a new column in the table.
#
......@@ -36,7 +36,6 @@ def initialize(name, default, cast_type, sql_type = nil, null = true)
@null = null
@limit = extract_limit(sql_type)
@precision = extract_precision(sql_type)
@scale = extract_scale(sql_type)
@default = extract_default(default)
@default_function = nil
@primary = nil
......@@ -69,7 +68,7 @@ def extract_default(default)
end
private
delegate :extract_scale, :extract_precision, to: :cast_type
delegate :extract_precision, to: :cast_type
def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/
......
......@@ -7,7 +7,7 @@ class Money < Type::Decimal
class_attribute :precision
def extract_scale(sql_type)
def scale
2
end
......
......@@ -571,7 +571,6 @@ def initialize_type_map(m)
m.alias_type 'int4', 'int2'
m.alias_type 'int8', 'int2'
m.alias_type 'oid', 'int2'
m.register_type 'numeric', OID::Decimal.new
m.register_type 'float4', OID::Float.new
m.alias_type 'float8', 'float4'
m.register_type 'text', Type::Text.new
......@@ -610,6 +609,11 @@ def initialize_type_map(m)
m.alias_type 'lseg', 'varchar'
m.alias_type 'box', 'varchar'
m.register_type 'numeric' do |_, sql_type|
scale = extract_scale(sql_type)
OID::Decimal.new(scale: scale)
end
load_additional_types(m)
end
......
......@@ -19,14 +19,6 @@
module ActiveRecord
module ConnectionAdapters
module Type # :nodoc:
class << self
def extract_scale(sql_type)
case sql_type
when /\((\d+)\)/ then 0
when /\((\d+)(,(\d+))\)/ then $3.to_i
end
end
end
end
end
end
......@@ -4,8 +4,6 @@ module Type
class Decimal < Value # :nodoc:
include Numeric
delegate :extract_scale, to: Type
def type
:decimal
end
......
......@@ -2,9 +2,14 @@ module ActiveRecord
module ConnectionAdapters
module Type
class Value # :nodoc:
def type; end
attr_reader :scale
def initialize(options = {})
options.assert_valid_keys(:scale)
@scale = options[:scale]
end
def extract_scale(sql_type); end
def type; end
def extract_precision(sql_type); end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册