Class: Mkxms::Mssql::ScalarType

Inherits:
Object
  • Object
show all
Includes:
ExtendedProperties, Property::Hosting, Property::SchemaScoped, Utils::SchemaQualifiedName
Defined in:
lib/mkxms/mssql/scalar_type_handler.rb

Constant Summary collapse

SQL_OBJECT_TYPE =
'TYPE'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SchemaQualifiedName

#qualified_name

Methods included from ExtendedProperties

#extended_properties

Methods included from Property::Hosting

#extended_properties_sql

Methods included from Property::SchemaScoped

#property_subject_identifiers, #subitem_extended_properties_sql

Constructor Details

#initialize(attrs) ⇒ ScalarType

Returns a new instance of ScalarType.



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#base_typeObject

Returns the value of attribute base_type.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def base_type
  @base_type
end

#capacityObject

Returns the value of attribute capacity.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def capacity
  @capacity
end

#defaultObject

Returns the value of attribute default.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def default
  @default
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def name
  @name
end

#precisionObject

Returns the value of attribute precision.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def precision
  @precision
end

#scaleObject

Returns the value of attribute scale.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def scale
  @scale
end

#schemaObject

Returns the value of attribute schema.



25
26
27
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25

def schema
  @schema
end

Instance Method Details

#element_sizeObject



59
60
61
62
63
64
65
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 59

def element_size
  if %w[nchar nvarchar]
    2
  else
    1
  end
end

#nullable=(val) ⇒ Object



31
32
33
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 31

def nullable=(val)
  @nullable = !!val
end

#nullable?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 27

def nullable?
  @nullable
end

#to_sqlObject



47
48
49
50
51
52
53
54
55
56
57
# 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_specObject



35
36
37
38
39
40
41
42
43
44
45
# 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