class Mkxms::Mssql::ScalarType

Constants

SQL_OBJECT_TYPE

Attributes

base_type[RW]
capacity[RW]
default[RW]
name[RW]
precision[RW]
scale[RW]
schema[RW]

Public Class Methods

new(attrs) click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 13
def initialize(attrs)
  a = attrs
  @schema = a['schema']
  @name = a['name']
  @base_type = a['base-type']
  @capacity = a['capacity']
  @capacity = @capacity.to_i unless @capacity.nil? || @capacity == 'max'
  @precision = a['precision']
  @scale = a['scale']
  @nullable = !!a['nullable']
end

Public Instance Methods

element_size() click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 59
def element_size
  if %w[nchar nvarchar]
    2
  else
    1
  end
end
nullable=(val) click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 31
def nullable=(val)
  @nullable = !!val
end
nullable?() click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 27
def nullable?
  @nullable
end
to_sql() click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 47
def to_sql
  [].tap do |lines|
    lines << "CREATE TYPE #{qualified_name}"
    lines << "FROM #{type_spec};"
    
    if default
      lines << default.to_sql
      lines << "EXEC sp_bindefault #{default.qualified_name.sql_quoted}, #{qualified_name.sql_quoted};"
    end
  end.join("\n")
end
type_spec() click to toggle source
# File lib/mkxms/mssql/scalar_type_handler.rb, line 35
def type_spec
  base_type.dup.tap do |ts|
    case 
    when capacity
      ts << "(#{capacity})"
    when precision
      ts << "(#{[precision, scale].compact.join(", ")})"
    end
    ts << " NOT NULL" unless nullable?
  end
end