Class: Mkxms::Mssql::ScalarType
- Inherits:
-
Object
- Object
- Mkxms::Mssql::ScalarType
- Defined in:
- lib/mkxms/mssql/scalar_type_handler.rb
Constant Summary collapse
- SQL_OBJECT_TYPE =
'TYPE'
Instance Attribute Summary collapse
-
#base_type ⇒ Object
Returns the value of attribute base_type.
-
#capacity ⇒ Object
Returns the value of attribute capacity.
-
#default ⇒ Object
Returns the value of attribute default.
-
#name ⇒ Object
Returns the value of attribute name.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#scale ⇒ Object
Returns the value of attribute scale.
-
#schema ⇒ Object
Returns the value of attribute schema.
Instance Method Summary collapse
- #element_size ⇒ Object
-
#initialize(attrs) ⇒ ScalarType
constructor
A new instance of ScalarType.
- #nullable=(val) ⇒ Object
- #nullable? ⇒ Boolean
- #to_sql ⇒ Object
- #type_spec ⇒ Object
Methods included from Utils::SchemaQualifiedName
Methods included from ExtendedProperties
Methods included from Property::Hosting
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_type ⇒ Object
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 |
#capacity ⇒ Object
Returns the value of attribute capacity.
25 26 27 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25 def capacity @capacity end |
#default ⇒ Object
Returns the value of attribute default.
25 26 27 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25 def default @default end |
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25 def name @name end |
#precision ⇒ Object
Returns the value of attribute precision.
25 26 27 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25 def precision @precision end |
#scale ⇒ Object
Returns the value of attribute scale.
25 26 27 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 25 def scale @scale end |
#schema ⇒ Object
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_size ⇒ Object
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
27 28 29 |
# File 'lib/mkxms/mssql/scalar_type_handler.rb', line 27 def nullable? @nullable end |
#to_sql ⇒ Object
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_spec ⇒ Object
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 |