class DDLParser::Translator::Column

Public Class Methods

new(column_hash) click to toggle source
# File lib/ddl_parser/translator/column.rb, line 3
def initialize(column_hash)
  @column_hash = column_hash.is_a?(Hash) ? column_hash : {}
end

Public Instance Methods

cache_value() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 168
def cache_value
  unless option(:identity).nil?
    begin
      return option(:identity)[:identity][:cache_value][:integer].to_i
    rescue
    end
  else
    return 0
  end
end
data_type() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 15
def data_type
  raise "DataType not specified #{@column_hash.inspect}" if @column_hash.nil? || @column_hash[:data_type].nil?
  if @column_hash[:data_type].is_a?(Hash)
    @column_hash[:data_type].keys.first
  else
    @column_hash[:data_type].to_sym
  end

end
default_clause() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 85
def default_clause
  unless options.nil?
    default_value = options.select{|h|h.has_key?(:default_clause)}.first
    if default_value
      if default_value[:default_clause].is_a?(Array)
        default_value[:default_clause].each do |c|
          # TODO must handle other then integer
          case c.keys.first
            when :integer
              return c[:integer].to_i
            else
              return c[c.keys.first].to_s
          end
        end
      else
        raise "Unknown default_clause #{default_value.inspect}"
      end
    end
  end
end
default_value() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 135
def default_value
  unless option(:default_clause).nil?
    begin
      return option(:default_clause)[:default_clause].first[:string]
    rescue
    end
  else
    return ''
  end
end
identity?() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 121
def identity?
  if options.nil?
    false
  else
    options.select{|h|h.has_key?(:identity)}.length > 0
  end
end
identity_options() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 106
def identity_options
  unless options.nil?
    start_value = options.select{|h|h.has_key?(:identity_options)}.first
    if start_value
      if start_value[:identity_options].is_a?(Array)
        start_value[:identity_options].each do |c|
          return c[c.keys.first]
        end
      else
        raise "Unknown identity_options #{start_value.inspect}"
      end
    end
  end
end
increment_value() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 157
def increment_value
  unless option(:identity).nil?
    begin
      return option(:identity)[:identity][:increment_value][:integer].to_i
    rescue
    end
  else
    return 0
  end
end
length() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 25
def length
  case data_type
    when :decimal
      "#{precision}.#{scale}".to_f
    when :char
      begin
        @column_hash[:data_type][:char][:length][:integer].to_i
      rescue
        raise "DataType CHAR length wrong format #{@column_hash[:data_type].inspect}"
      end
    when :varchar
      begin
        @column_hash[:data_type][:varchar][:length][:integer].to_i
      rescue
        raise "DataType VARCHAR length wrong format #{@column_hash[:data_type].inspect}"
      end
    else
      nil
  end
end
name() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 11
def name
  @column_hash[:field]
end
not_null() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 77
def not_null
  if options.nil?
    false
  else
    options.select{|h|h.has_key?(:column_option) && h.has_value?('not null')}.length > 0
  end
end
option(key) click to toggle source
# File lib/ddl_parser/translator/column.rb, line 129
def option(key)
  return if options.nil?
  options.select{|h|h.has_key?(:column_option)}.length > 0    # return the right option
  options.select{|o| o.is_a?(Hash) && o.has_key?(key)}.first
end
options() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 72
def options
  @column_hash[:options] if @column_hash[:options].length > 0
end
precision() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 46
def precision
  case data_type
    when :decimal
      begin
        @column_hash[:data_type][:decimal][:precision][:total][:integer].to_i
      rescue
        5 # decimal default if no precision present
      end
    else
      nil
  end
end
scale() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 59
def scale
  case data_type
    when :decimal
      begin
        @column_hash[:data_type][:decimal][:precision][:scale][:integer].to_i
      rescue
        0 # decimal default if no scale present
      end
    else
      nil
  end
end
start_value() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 146
def start_value
  unless option(:identity).nil?
    begin
      return option(:identity)[:identity][:start_value][:integer].to_i
    rescue
    end
  else
    return 0
  end
end
to_hash() click to toggle source
# File lib/ddl_parser/translator/column.rb, line 7
def to_hash
  @column_hash
end