class RADT::Column

Attributes

length[R]
name[R]
type[R]

Public Class Methods

new(name, type, length) click to toggle source

Initialize a new RADT::Column

@param [String] name @param [String] type @param [Fixnum] length

# File lib/radt/column.rb, line 18
def initialize(name, type, length)
  @name, @type, @length = strip_non_ascii_chars(name), type, length

  raise ColumnLengthError, "field length must be greater than 0" unless length > 0
  raise ColumnNameError, "column name cannot be empty" if @name.length == 0
end

Public Instance Methods

data_type(id) click to toggle source
# File lib/radt/column.rb, line 25
def data_type(id)
  TYPES[id]
end
decode_datetime(value) click to toggle source

Decode a DateTime value

@param [String] value @return [DateTime]

# File lib/radt/column.rb, line 42
def decode_datetime(value)
  days, milliseconds = value.unpack('l2')
  seconds = milliseconds / 1000
  DateTime.jd(days, seconds/3600, seconds/60 % 60, seconds % 60) rescue nil
end
flag(type, length = 0) click to toggle source
# File lib/radt/column.rb, line 29
def flag(type, length = 0)
  data_type = data_type(type)
  flag = FLAGS[data_type]
  if flag.eql? 'A'
    return flag + length.to_s
  end
  return flag
end
schema_data_type() click to toggle source

Column type for schema definition

@return [String]

# File lib/radt/column.rb, line 58
def schema_data_type
  case data_type(type)
  when "character"
    ":string, :limit => #{length}"
  when "cicharacter"
    ":string, :limit => #{length}"
  when "double"
    ":float"
  when "date"
    ":date"
  when "time"
    ":timestamp"
  when "timestamp"
    ":timestamp"
  when "integer"
    ":integer"
  when "autoinc"
    ":integer"
  else
    ":string, :limit => #{length}"
  end
end
schema_definition() click to toggle source

Schema definition

@return [String]

# File lib/radt/column.rb, line 51
def schema_definition
  "\"#{name.underscore}\", #{schema_data_type}\n"
end
strip_non_ascii_chars(s) click to toggle source

Strip all non-ascii and non-printable characters

@param [String] s @return [String]

# File lib/radt/column.rb, line 85
def strip_non_ascii_chars(s)
  # truncate the string at the first null character
  s = s[0, s.index("\x00")] if s.index("\x00")

  s.gsub(/[^\x20-\x7E]/,"")
end