class Dbsketch::Model::Type
Attributes
sizes[R]
sql_type[R]
Public Class Methods
new(sql_type, sizes = [])
click to toggle source
# 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
Public Instance Methods
category()
click to toggle source
# File lib/dbsketch/model/type.rb, line 60 def category get_category(@sql_type) end
compatible_with?(other_type)
click to toggle source
Return true if current type can be stored in other_type without error nor information loss
# 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
Private Instance Methods
get_category(sql_type)
click to toggle source
# File lib/dbsketch/model/type.rb, line 66 def get_category sql_type if ['decimal', 'int', 'numeric'].include? sql_type :numeric elsif ['datetime', 'datetime2'].include? sql_type :datetime elsif sql_type.match(/(n){0,1}(var){0,1}char/) :string elsif 'bit' == sql_type :boolean else :unknown end end
size_compatibile_with?(other_sizes)
click to toggle source
# File lib/dbsketch/model/type.rb, line 80 def size_compatibile_with? other_sizes compatible = false if @sizes.empty? and other_sizes.empty? compatible = true elsif not @sizes.empty? and not other_sizes.empty? compatible = (@sizes.first <= other_sizes.first) end compatible end