public
Rubygem
Description: DataMapper - Core
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-core.git
Fixed bug reversing scale and precision in BigDecimal properties

* Updated Float to also handle scale and precision by default
* Updated Postgres to not use scale and precision for Float columns
dkubb (author)
Sat May 10 01:03:53 -0700 2008
commit  ac4e4b469bd5947c58b41baf40774dc565fcda7e
tree    977e20cc53a85fb43940bfae3e6d5b30172036e6
parent  ad0583cff4d8c4fadcd4b71ba5d32c67959984ad
...
94
95
96
97
98
 
 
99
100
101
...
103
104
105
106
 
107
108
109
 
110
111
112
...
406
407
408
409
410
 
411
 
412
413
414
...
422
423
424
425
426
 
 
427
428
429
...
94
95
96
 
 
97
98
99
100
101
...
103
104
105
 
106
107
108
 
109
110
111
112
...
406
407
408
 
 
409
410
411
412
413
414
...
422
423
424
 
 
425
426
427
428
429
0
@@ -94,8 +94,8 @@ module DataMapper
0
           tm.map(Fixnum).to('INT')
0
           tm.map(String).to('VARCHAR').with(:size => Property::DEFAULT_LENGTH)
0
           tm.map(Class).to('VARCHAR').with(:size => Property::DEFAULT_LENGTH)
0
- tm.map(BigDecimal).to('DECIMAL').with(:precision => Property::DEFAULT_PRECISION, :scale => Property::DEFAULT_SCALE)
0
- tm.map(Float).to('FLOAT')
0
+ tm.map(BigDecimal).to('DECIMAL').with(:scale => Property::DEFAULT_SCALE, :precision => Property::DEFAULT_PRECISION)
0
+ tm.map(Float).to('FLOAT').with(:scale => Property::DEFAULT_SCALE, :precision => Property::DEFAULT_PRECISION)
0
           tm.map(DateTime).to('DATETIME')
0
           tm.map(Date).to('DATE')
0
           tm.map(TrueClass).to('BOOLEAN')
0
@@ -103,10 +103,10 @@ module DataMapper
0
           tm.map(DM::Text).to('TEXT')
0
         end
0
       end
0
-
0
+
0
       def initialize(name, uri_or_options)
0
         super
0
-
0
+
0
         # Default the driver-specifc logger to DataMapper's logger
0
         driver_module = DataObjects.const_get(@uri.scheme.capitalize) rescue nil
0
         driver_module.logger = DataMapper.logger if driver_module && driver_module.respond_to?(:logger)
0
@@ -406,9 +406,9 @@ module DataMapper
0
           # - use this to make it so all TEXT primitive fields do not have size
0
           if property.primitive == String && schema[:primitive] != 'TEXT'
0
             schema[:size] = property.length
0
- elsif property.primitive == BigDecimal
0
- schema[:precision] = property.precision
0
+ elsif property.primitive == BigDecimal || property.primitive == Float
0
             schema[:scale] = property.scale
0
+ schema[:precision] = property.precision
0
           end
0
 
0
           schema[:nullable?] = property.nullable?
0
@@ -422,8 +422,8 @@ module DataMapper
0
           statement = quote_column_name(schema[:name])
0
           statement << " #{schema[:primitive]}"
0
 
0
- if schema[:precision] && schema[:scale]
0
- statement << "(#{schema[:precision]},#{schema[:scale]})"
0
+ if schema[:scale] && schema[:precision]
0
+ statement << "(#{schema[:scale]},#{schema[:precision]})"
0
           elsif schema[:size]
0
             statement << "(#{schema[:size]})"
0
           end
...
14
15
16
 
17
18
19
...
73
74
75
76
 
77
78
79
...
95
96
97
98
 
99
100
101
...
103
104
105
106
107
108
109
110
111
112
...
159
160
161
162
 
 
 
 
 
 
 
 
 
 
 
 
163
164
165
...
14
15
16
17
18
19
20
...
74
75
76
 
77
78
79
80
...
96
97
98
 
99
100
101
102
...
104
105
106
 
 
 
 
107
108
109
...
156
157
158
 
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
0
@@ -14,6 +14,7 @@ module DataMapper
0
         @type_map ||= TypeMap.new(super) do |tm|
0
           tm.map(DateTime).to('TIMESTAMP')
0
           tm.map(Fixnum).to('INT4')
0
+ tm.map(Float).to('FLOAT8')
0
         end
0
       end
0
 
0
@@ -73,7 +74,7 @@ module DataMapper
0
         connection = create_connection
0
 
0
         model.properties.each do |property|
0
- create_sequence_column(connection, model, property) if sequenced?(property)
0
+ create_sequence_column(connection, model, property) if property.serial?
0
         end
0
 
0
         command = connection.create_command(sql)
0
@@ -95,7 +96,7 @@ module DataMapper
0
         result = command.execute_non_query
0
 
0
         model.properties.each do |property|
0
- drop_sequence_column(connection, model, property) if sequenced?(property)
0
+ drop_sequence_column(connection, model, property) if property.serial?
0
         end
0
 
0
         close_connection(connection)
0
@@ -103,10 +104,6 @@ module DataMapper
0
         result.to_i == 1
0
       end
0
 
0
- def sequenced?(property)
0
- property.serial?
0
- end
0
-
0
       def create_sequence_column(connection, model, property)
0
         sql = create_sequence_statement(model, property)
0
 
0
@@ -159,7 +156,18 @@ module DataMapper
0
 
0
       def property_schema_hash(property, model)
0
         schema = super
0
- schema[:sequence_name] = sequence_name(model, property) if sequenced?(property)
0
+ schema[:sequence_name] = sequence_name(model, property) if property.serial?
0
+
0
+ # TODO: see if TypeMap can be updated to set specific attributes
0
+ # to nil for different adapters. scale/precision are perfect
0
+ # examples for Postgres floats
0
+
0
+ # postgres does not support scale and precision for Float
0
+ if property.primitive == Float
0
+ schema.delete(:scale)
0
+ schema.delete(:precision)
0
+ end
0
+
0
         schema
0
       end
0
     end # class PostgresAdapter
...
250
251
252
253
254
 
 
255
256
257
...
430
431
432
433
 
434
435
436
 
437
 
438
439
440
...
250
251
252
 
 
253
254
255
256
257
...
430
431
432
 
433
434
 
 
435
436
437
438
439
440
0
@@ -250,8 +250,8 @@ module DataMapper
0
     VISIBILITY_OPTIONS = [ :public, :protected, :private ]
0
 
0
     DEFAULT_LENGTH = 50
0
- DEFAULT_PRECISION = 10
0
- DEFAULT_SCALE = 0
0
+ DEFAULT_SCALE = 10
0
+ DEFAULT_PRECISION = 0
0
 
0
     attr_reader :primitive, :model, :name, :instance_variable_name,
0
       :type, :reader_visibility, :writer_visibility, :getter, :options,
0
@@ -430,11 +430,11 @@ module DataMapper
0
       @nullable = @options.fetch(:nullable, @key == false && @default.nil?)
0
 
0
       # assign attributes per-type
0
- if @primitive == String || @primitive == Class
0
+ if String == @primitive || Class == @primitive
0
         @length = @options.fetch(:length, @options.fetch(:size, DEFAULT_LENGTH))
0
- elsif @primitive == BigDecimal
0
- @precision = @options.fetch(:precision, DEFAULT_PRECISION)
0
+ elsif BigDecimal == @primitive || Float == @primitive
0
         @scale = @options.fetch(:scale, DEFAULT_SCALE)
0
+ @precision = @options.fetch(:precision, DEFAULT_PRECISION)
0
       end
0
 
0
       determine_visibility
...
17
18
19
20
21
 
 
22
23
24
...
78
79
80
81
 
82
83
84
...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
 
 
 
 
 
 
 
 
 
 
 
 
166
167
168
...
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
 
 
 
 
 
 
 
 
 
 
 
 
 
282
283
284
...
17
18
19
 
 
20
21
22
23
24
...
78
79
80
 
81
82
83
84
...
150
151
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
...
266
267
268
 
 
 
 
 
 
 
 
 
 
 
 
 
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
0
@@ -17,8 +17,8 @@ class Book
0
   property :false_class, TrueClass, :nullable => false, :default => false
0
   property :text, DM::Text, :nullable => false, :default => 'text'
0
 # property :class, Class, :nullable => false, :default => Class # FIXME: Class types cause infinite recursions in Resource
0
- property :big_decimal, BigDecimal, :nullable => false, :default => BigDecimal('1.1'), :precision => 2, :scale => 1
0
- property :float, Float, :nullable => false, :default => 1.1
0
+ property :big_decimal, BigDecimal, :nullable => false, :default => BigDecimal('1.1'), :scale => 2, :precision => 1
0
+ property :float, Float, :nullable => false, :default => 1.1, :scale => 2, :precision => 1
0
   property :date_time, DateTime, :nullable => false, :default => NOW
0
   property :object, Object, :nullable => true # FIXME: cannot supply a default for Object
0
 end
0
@@ -78,7 +78,7 @@ if HAS_SQLITE3
0
         :text => [ DM::Text, 'TEXT', false, 'text', 'text', false ],
0
 # :class => [ Class, 'VARCHAR(50)', false, 'Class', 'Class', false ],
0
         :big_decimal => [ BigDecimal, 'DECIMAL(2,1)', false, '1.1', BigDecimal('1.1'), false ],
0
- :float => [ Float, 'FLOAT', false, '1.1', 1.1, false ],
0
+ :float => [ Float, 'FLOAT(2,1)', false, '1.1', 1.1, false ],
0
         :date_time => [ DateTime, 'DATETIME', false, NOW.strftime('%Y-%m-%d %H:%M:%S'), NOW, false ],
0
         :object => [ Object, 'TEXT', true, nil, nil, false ],
0
       }
0
@@ -150,19 +150,19 @@ if HAS_MYSQL
0
       end
0
 
0
       types = {
0
- :serial => [ Fixnum, 'INT(11)', false, nil, 1, true ],
0
- :fixnum => [ Fixnum, 'INT(11)', false, '1', 1, false ],
0
- :string => [ String, 'VARCHAR(50)', false, 'default', 'default', false ],
0
- :empty => [ String, 'VARCHAR(50)', false, '', '', false ],
0
- :date => [ Date, 'DATE', false, TODAY.strftime('%Y-%m-%d'), TODAY, false ],
0
- :true_class => [ TrueClass, 'TINYINT(1)', false, '1', true, false ],
0
- :false_class => [ TrueClass, 'TINYINT(1)', false, '0', false, false ],
0
- :text => [ DM::Text, 'TEXT', false, nil, 'text', false ],
0
-# :class => [ Class, 'VARCHAR(50)', false, 'Class', 'Class', false ],
0
- :big_decimal => [ BigDecimal, 'DECIMAL(2,1)', false, '1.1', BigDecimal('1.1'), false ],
0
- :float => [ Float, 'FLOAT', false, '1.1', 1.1, false ],
0
- :date_time => [ DateTime, 'DATETIME', false, NOW.strftime('%Y-%m-%d %H:%M:%S'), NOW, false ],
0
- :object => [ Object, 'TEXT', true, nil, nil, false ],
0
+ :serial => [ Fixnum, 'INT(11)', false, nil, 1, true ],
0
+ :fixnum => [ Fixnum, 'INT(11)', false, '1', 1, false ],
0
+ :string => [ String, 'VARCHAR(50)', false, 'default', 'default', false ],
0
+ :empty => [ String, 'VARCHAR(50)', false, '', '', false ],
0
+ :date => [ Date, 'DATE', false, TODAY.strftime('%Y-%m-%d'), TODAY, false ],
0
+ :true_class => [ TrueClass, 'TINYINT(1)', false, '1', true, false ],
0
+ :false_class => [ TrueClass, 'TINYINT(1)', false, '0', false, false ],
0
+ :text => [ DM::Text, 'TEXT', false, nil, 'text', false ],
0
+# :class => [ Class, 'VARCHAR(50)', false, 'Class', 'Class', false ],
0
+ :big_decimal => [ BigDecimal, 'DECIMAL(2,1)', false, '1.1', BigDecimal('1.1'), false ],
0
+ :float => [ Float, 'FLOAT(2,1)', false, '1.1', 1.1, false ],
0
+ :date_time => [ DateTime, 'DATETIME', false, NOW.strftime('%Y-%m-%d %H:%M:%S'), NOW, false ],
0
+ :object => [ Object, 'TEXT', true, nil, nil, false ],
0
       }
0
 
0
       types.each do |name,(klass,type,nullable,default,key)|
0
@@ -266,19 +266,19 @@ if HAS_POSTGRES
0
       end
0
 
0
       types = {
0
- :serial => [ Fixnum, 'INT4', false, nil, 1, true ],
0
- :fixnum => [ Fixnum, 'INT4', false, '1', 1, false ],
0
- :string => [ String, 'VARCHAR', false, "'default'::character varying", 'default', false ],
0
- :empty => [ String, 'VARCHAR', false, "''::character varying", '', false ],
0
- :date => [ Date, 'DATE', false, "'#{TODAY.strftime('%Y-%m-%d')}'::date", TODAY, false ],
0
- :true_class => [ TrueClass, 'BOOL', false, 'true', true, false ],
0
- :false_class => [ TrueClass, 'BOOL', false, 'false', false, false ],
0
- :text => [ DM::Text, 'TEXT', false, "'text'::text", 'text', false ],
0
-# :class => [ Class, 'VARCHAR(50)', false, 'Class', 'Class', false ],
0
- :big_decimal => [ BigDecimal, 'NUMERIC', false, '1.1', BigDecimal('1.1'), false ],
0
- :float => [ Float, 'FLOAT8', false, '1.1', 1.1, false ],
0
- :date_time => [ DateTime, 'TIMESTAMP', false, "'#{NOW.strftime('%Y-%m-%d %H:%M:%S')}'::timestamp without time zone", NOW, false ],
0
- :object => [ Object, 'TEXT', true, nil, nil, false ],
0
+ :serial => [ Fixnum, 'INT4', false, nil, 1, true ],
0
+ :fixnum => [ Fixnum, 'INT4', false, '1', 1, false ],
0
+ :string => [ String, 'VARCHAR', false, "'default'::character varying", 'default', false ],
0
+ :empty => [ String, 'VARCHAR', false, "''::character varying", '', false ],
0
+ :date => [ Date, 'DATE', false, "'#{TODAY.strftime('%Y-%m-%d')}'::date", TODAY, false ],
0
+ :true_class => [ TrueClass, 'BOOL', false, 'true', true, false ],
0
+ :false_class => [ TrueClass, 'BOOL', false, 'false', false, false ],
0
+ :text => [ DM::Text, 'TEXT', false, "'text'::text", 'text', false ],
0
+# :class => [ Class, 'VARCHAR(50)', false, 'Class', 'Class', false ],
0
+ :big_decimal => [ BigDecimal, 'NUMERIC', false, '1.1', BigDecimal('1.1'), false ],
0
+ :float => [ Float, 'FLOAT8', false, '1.1', 1.1, false ],
0
+ :date_time => [ DateTime, 'TIMESTAMP', false, "'#{NOW.strftime('%Y-%m-%d %H:%M:%S')}'::timestamp without time zone", NOW, false ],
0
+ :object => [ Object, 'TEXT', true, nil, nil, false ],
0
       }
0
 
0
       types.each do |name,(klass,type,nullable,default,key)|

Comments

    No one has commented yet.