Class: Dbsketch::Model::Type
- Inherits:
-
Object
- Object
- Dbsketch::Model::Type
- Defined in:
- lib/dbsketch/model/type.rb
Instance Attribute Summary collapse
-
#sizes ⇒ Object
readonly
Returns the value of attribute sizes.
-
#sql_type ⇒ Object
readonly
Returns the value of attribute sql_type.
Instance Method Summary collapse
- #category ⇒ Object
-
#compatible_with?(other_type) ⇒ Boolean
Return true if current type can be stored in other_type without error nor information loss.
-
#initialize(sql_type, sizes = []) ⇒ Type
constructor
A new instance of Type.
Constructor Details
#initialize(sql_type, sizes = []) ⇒ Type
Returns a new instance of Type.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dbsketch/model/type.rb', line 9 def initialize(sql_type, sizes = []) @sizes = sizes.is_a?(Array) ? sizes : [sizes] ### Preconditions raise ArgumentError, "sql_type is not a String" unless sql_type.is_a? String @sizes.each do |size| if 'max' == size if not ['varchar', 'nvarchar'].include? sql_type raise ArgumentError, "size 'max' allowed only for [n]varchar" end elsif not size.is_a? Fixnum and not size.is_a? Integer raise ArgumentError, "size #{size} of sql type #{sql_type} is not a number" end end raise ArgumentError, "#{sql_type} do not allow defining sizes" if ['datetime', 'datetime2', 'int'].include? sql_type and @sizes.count > 0 raise ArgumentError, "#{sql_type} do not allow more than one size" if sql_type.match(/(n){0,1}(var){0,1}char/) and @sizes.count > 1 raise ArgumentError, "#{sql_type} need at least a precision, but no sizes were given" if ['decimal', 'numeric'].include? sql_type and @sizes.count == 0 raise ArgumentError, "defining more than 2 sizes for #{sql_type}" if @sizes.count > 2 ### @sql_type = sql_type end |
Instance Attribute Details
#sizes ⇒ Object (readonly)
Returns the value of attribute sizes.
30 31 32 |
# File 'lib/dbsketch/model/type.rb', line 30 def sizes @sizes end |
#sql_type ⇒ Object (readonly)
Returns the value of attribute sql_type.
30 31 32 |
# File 'lib/dbsketch/model/type.rb', line 30 def sql_type @sql_type end |
Instance Method Details
#category ⇒ Object
60 61 62 |
# File 'lib/dbsketch/model/type.rb', line 60 def category get_category(@sql_type) end |
#compatible_with?(other_type) ⇒ Boolean
Return true if current type can be stored in other_type without error nor information loss
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dbsketch/model/type.rb', line 33 def compatible_with? other_type ### Preconditions raise ArgumentError, "other_type is not a Dbsketch::Model::Type" unless other_type.is_a? Dbsketch::Model::Type ### # TODO enhance this function (numeric and int may be compatible with particular sizes) compatible = false if :numeric == get_category(@sql_type) if :numeric == get_category(other_type.sql_type) compatible = size_compatibile_with? other_type.sizes end elsif :datetime == get_category(@sql_type) compatible = (:datetime == get_category(other_type.sql_type)) elsif :string == get_category(@sql_type) compatible = false if :string == get_category(other_type.sql_type) if (@sql_type.match(/^(var){0,1}char$/) and other_type.sql_type.match(/^(var){0,1}char$/)) or (@sql_type.match(/^n(var){0,1}char$/) and other_type.sql_type.match(/^n(var){0,1}char$/)) compatible = size_compatibile_with? other_type.sizes end end elsif :boolean == get_category(@sql_type) compatible = (:boolean == get_category(other_type.sql_type)) end compatible end |