Skip to content

Commit

Permalink
Make decimal columns with no precision or scale stay decimals
Browse files Browse the repository at this point in the history
Fixes jruby#55 for MySQL, Derby, HSQLDB/H2, and PG.
  • Loading branch information
nicksieger committed Jun 18, 2011
1 parent fec1fff commit 8d210c3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
10 changes: 7 additions & 3 deletions lib/arjdbc/derby/adapter.rb
Expand Up @@ -37,9 +37,13 @@ def self.included(*args)

module Column
def simplified_type(field_type)
return :boolean if field_type =~ /smallint/i
return :float if field_type =~ /real/i
super
case field_type
when /smallint/i then :boolean
when /real/i then :float
when /decimal/i then :decimal
else
super
end
end

# Post process default value from JDBC into a Rails-friendly format (columns{-internal})
Expand Down
12 changes: 5 additions & 7 deletions lib/arjdbc/hsqldb/adapter.rb
Expand Up @@ -8,14 +8,12 @@ module Column
private
def simplified_type(field_type)
case field_type
when /longvarchar/i
:text
when /tinyint/i
:boolean
when /real/i
:float
when /longvarchar/i then :text
when /tinyint/i then :boolean
when /real/i then :float
when /decimal/i then :decimal
else
super(field_type)
super
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/arjdbc/mysql/adapter.rb
Expand Up @@ -39,9 +39,13 @@ def has_default?
end

def simplified_type(field_type)
return :boolean if field_type =~ /tinyint\(1\)|bit/i
return :string if field_type =~ /enum/i
super
case field_type
when /tinyint\(1\)|bit/i then :boolean
when /enum/i then :string
when /decimal/i then :decimal
else
super
end
end

def extract_limit(sql_type)
Expand Down
54 changes: 46 additions & 8 deletions lib/arjdbc/postgresql/adapter.rb
Expand Up @@ -41,14 +41,52 @@ def extract_limit(sql_type)
end

def simplified_type(field_type)
return :integer if field_type =~ /^serial/i
return :string if field_type =~ /\[\]$/i || field_type =~ /^interval/i
return :string if field_type =~ /^(?:point|lseg|box|"?path"?|polygon|circle)/i
return :datetime if field_type =~ /^timestamp/i
return :float if field_type =~ /^(?:real|double precision)$/i
return :binary if field_type =~ /^bytea/i
return :boolean if field_type =~ /^bool/i
super
case field_type
# Numeric and monetary types
when /^(?:real|double precision)$/
:float
# Monetary types
when 'money', 'numeric(131089)'
:decimal
# Character types
when /^(?:character varying|bpchar)(?:\(\d+\))?$/
:string
# Binary data types
when 'bytea'
:binary
# Date/time types
when /^timestamp with(?:out)? time zone$/
:datetime
when 'interval'
:string
# Geometric types
when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
:string
# Network address types
when /^(?:cidr|inet|macaddr)$/
:string
# Bit strings
when /^bit(?: varying)?(?:\(\d+\))?$/
:string
# XML type
when 'xml'
:xml
# Arrays
when /^\D+\[\]$/
:string
# Object identifier types
when 'oid'
:integer
# UUID type
when 'uuid'
:string
# Small and big integer types
when /^(?:small|big)int$/
:integer
# Pass through all types that are not specific to PostgreSQL.
else
super
end
end

def cast_to_boolean(value)
Expand Down
1 change: 1 addition & 0 deletions test/models/data_types.rb
Expand Up @@ -7,6 +7,7 @@ def self.up
t.column :sample_time, :time
t.column :sample_decimal, :decimal, :precision => 15, :scale => 0
t.column :sample_small_decimal, :decimal, :precision => 3, :scale => 2
t.column :sample_default_decimal, :decimal
t.column :sample_float, :float
t.column :sample_binary, :binary
t.column :sample_boolean, :boolean
Expand Down
6 changes: 6 additions & 0 deletions test/simple.rb
Expand Up @@ -256,6 +256,12 @@ def test_save_binary
assert_equal binary_string, e.sample_binary
end

def test_default_decimal_should_keep_fractional_part
expected = 7.3
actual = DbType.create(:sample_default_decimal => expected).sample_default_decimal
assert_equal expected, actual
end

def test_indexes
# Only test indexes if we have implemented it for the particular adapter
if @connection.respond_to?(:indexes)
Expand Down

0 comments on commit 8d210c3

Please sign in to comment.