Skip to content

Commit c9bf152

Browse files
committed
Add support/test around handling of float/real column types [Lucas Maxwell]
1 parent 43348ec commit c9bf152

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
* 3.1.0 *
33

4+
* Add support/test around handling of float/real column types [Lucas Maxwell]
5+
46
* Make auto reconnect duration configurable. Fixes #109 [David Chelimsky]
57

68
* Quote most time objects to use ISO8601 format to be multi-language dateformat compatible. The [datetime] data type is

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def column_definitions(table_name)
214214
ci[:type]
215215
when /^numeric|decimal$/i
216216
"#{ci[:type]}(#{ci[:numeric_precision]},#{ci[:numeric_scale]})"
217+
when /^float|real$/i
218+
"#{ci[:type]}(#{ci[:numeric_precision]})"
217219
when /^char|nchar|varchar|nvarchar|varbinary|bigint|int|smallint$/
218220
ci[:length].to_i == -1 ? "#{ci[:type]}(max)" : "#{ci[:type]}(#{ci[:length]})"
219221
else

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,16 @@ def is_integer?
7979
!!(@sql_type =~ /int/i)
8080
end
8181

82+
def is_real?
83+
!!(@sql_type =~ /real/i)
84+
end
85+
8286
def sql_type_for_statement
83-
is_integer? ? sql_type.sub(/\(\d+\)/,'') : sql_type
87+
if is_integer? || is_real?
88+
sql_type.sub(/\(\d+\)/,'')
89+
else
90+
sql_type
91+
end
8492
end
8593

8694
def default_function

test/cases/adapter_test_sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ def setup
481481
assert_equal 'bigint', @connection.type_to_sql(:integer, 8)
482482
end
483483

484+
should 'create floats when no limit supplied' do
485+
assert_equal 'float(8)', @connection.type_to_sql(:float)
486+
end
487+
488+
should 'create floats when limit is supplied' do
489+
assert_equal 'float(27)', @connection.type_to_sql(:float, 27)
490+
end
491+
484492
end
485493

486494
end

test/cases/column_test_sqlserver.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,53 @@ def setup
273273

274274
end
275275

276+
context 'For float columns' do
277+
# NOTE: float limits are adjusted to 24 or 53 by the database as per
278+
# http://msdn.microsoft.com/en-us/library/ms173773.aspx
279+
# NOTE: floats with a limit of <= 24 are reduced to reals by sqlserver on creation
280+
281+
setup do
282+
@temperature = FloatData.columns_hash['temperature']
283+
@freezing = FloatData.columns_hash['temperature_8']
284+
@mild = FloatData.columns_hash['temperature_24']
285+
@beach = FloatData.columns_hash['temperature_32']
286+
@desert = FloatData.columns_hash['temperature_53']
287+
end
288+
289+
should 'have correct simplified types' do
290+
assert_equal :float, @temperature.type
291+
assert_equal :float, @freezing.type
292+
assert_equal :float, @mild.type
293+
assert_equal :float, @beach.type
294+
assert_equal :float, @desert.type
295+
end
296+
297+
should 'have correct #sql_type' do
298+
assert_equal 'real(24)', @temperature.sql_type
299+
assert_equal 'real(24)', @freezing.sql_type
300+
assert_equal 'real(24)', @mild.sql_type
301+
assert_equal 'float(53)', @beach.sql_type
302+
assert_equal 'float(53)', @desert.sql_type
303+
end
304+
305+
should 'have correct #limit' do
306+
assert_equal 24, @temperature.limit
307+
assert_equal 24, @freezing.limit
308+
assert_equal 24, @mild.limit
309+
assert_equal 53, @beach.limit
310+
assert_equal 53, @desert.limit
311+
end
312+
313+
should 'return nil precisions and scales' do
314+
assert_equal [nil,nil], [@temperature.precision, @temperature.scale]
315+
assert_equal [nil,nil], [@freezing.precision, @freezing.scale]
316+
assert_equal [nil,nil], [@mild.precision, @mild.scale]
317+
assert_equal [nil,nil], [@beach.precision, @beach.scale]
318+
assert_equal [nil,nil], [@desert.precision, @desert.scale]
319+
end
320+
321+
end
322+
276323
context 'For tinyint columns' do
277324

278325
setup do

test/cases/sqlserver_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TableWithRealColumn < ActiveRecord::Base; end
3232
class FkTestHasFk < ActiveRecord::Base ; end
3333
class FkTestHasPk < ActiveRecord::Base ; end
3434
class NumericData < ActiveRecord::Base ; self.table_name = 'numeric_data' ; end
35+
class FloatData < ActiveRecord::Base ; self.table_name = 'float_data' ; end
3536
class CustomersView < ActiveRecord::Base ; self.table_name = 'customers_view' ; end
3637
class StringDefaultsView < ActiveRecord::Base ; self.table_name = 'string_defaults_view' ; end
3738
class StringDefaultsBigView < ActiveRecord::Base ; self.table_name = 'string_defaults_big_view' ; end

test/schema/sqlserver_specific_schema.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
t.column :COLUMN2, :integer
66
end
77

8+
create_table :float_data, :force => true do |t|
9+
t.float :temperature
10+
t.float :temperature_8, :limit => 8
11+
t.float :temperature_24, :limit => 24
12+
t.float :temperature_32, :limit => 32
13+
t.float :temperature_53, :limit => 53
14+
end
15+
816
create_table :table_with_real_columns, :force => true do |t|
917
t.column :real_number, :real
1018
end

0 commit comments

Comments
 (0)