Class: Innodb::FieldType

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/field_type.rb

Defined Under Namespace

Classes: GenericType, IntegerType, VariableStringType

Constant Summary collapse

TYPES =

Maps data type to fixed storage length.

{
  :TINYINT    => { :class => IntegerType, :length => 1 },
  :SMALLINT   => { :class => IntegerType, :length => 2 },
  :MEDIUMINT  => { :class => IntegerType, :length => 3 },
  :INT        => { :class => IntegerType, :length => 4 },
  :INT6       => { :class => IntegerType, :length => 6 },
  :BIGINT     => { :class => IntegerType, :length => 8 },
  :CHAR       => { :class => GenericType },
  :VARCHAR    => { :class => VariableStringType },
  :BLOB       => { :class => GenericType },
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_string, properties) ⇒ FieldType

Returns a new instance of FieldType.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/innodb/field_type.rb', line 73

def initialize(type_string, properties)
  @base_type, modifiers = self.class.parse_base_type_and_modifiers(type_string)
  @length = nil
  @nullable = !properties.include?(:NOT_NULL)
  @unsigned = properties.include?(:UNSIGNED)
  @variable = false
  @blob = false
  case
  when TYPES[base_type][:class] == IntegerType
    @length = TYPES[base_type][:length]
  when base_type == :CHAR
    @length = modifiers[0]
  when base_type == :VARCHAR
    @length = modifiers[0]
    @variable = true
  when base_type == :BLOB
    @blob = true
    @variable = true
  else
    raise "Data type '#{type_string}' is not supported"
  end
  @reader = TYPES[base_type][:class].new(self)
end

Instance Attribute Details

#base_typeObject (readonly)

Returns the value of attribute base_type.



70
71
72
# File 'lib/innodb/field_type.rb', line 70

def base_type
  @base_type
end

#lengthObject (readonly)

Returns the value of attribute length.



71
72
73
# File 'lib/innodb/field_type.rb', line 71

def length
  @length
end

#readerObject (readonly)

Returns the value of attribute reader.



72
73
74
# File 'lib/innodb/field_type.rb', line 72

def reader
  @reader
end

Class Method Details

.parse_base_type_and_modifiers(type_string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/innodb/field_type.rb', line 58

def self.parse_base_type_and_modifiers(type_string)
  if matches = /^([a-zA-Z0-9]+)(\(([0-9, ]+)\))?$/.match(type_string)
    base_type = matches[1].upcase.to_sym
    if matches[3]
      modifiers = matches[3].sub(/[ ]/, "").split(/,/).map { |s| s.to_i }
    else
      modifiers = []
    end
    [base_type, modifiers]
  end
end

Instance Method Details

#blob?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/innodb/field_type.rb', line 109

def blob?
  @blob
end

#nameObject



121
122
123
# File 'lib/innodb/field_type.rb', line 121

def name
  "#{base_type}#{name_suffix}"
end

#name_suffixObject



113
114
115
116
117
118
119
# File 'lib/innodb/field_type.rb', line 113

def name_suffix
  if [:CHAR, :VARCHAR].include?(base_type)
    "(#{length})"
  else
    ""
  end
end

#nullable?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/innodb/field_type.rb', line 101

def nullable?
  @nullable
end

#unsigned?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/innodb/field_type.rb', line 97

def unsigned?
  @unsigned
end

#variable?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/innodb/field_type.rb', line 105

def variable?
  @variable
end