Class: RADT::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/radt/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, length) ⇒ Column

Initialize a new RADT::Column

Parameters:

  • name (String)
  • type (String)
  • length (Fixnum)

Raises:



18
19
20
21
22
23
# 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

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



11
12
13
# File 'lib/radt/column.rb', line 11

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/radt/column.rb', line 11

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/radt/column.rb', line 11

def type
  @type
end

Instance Method Details

#data_type(id) ⇒ Object



25
26
27
# File 'lib/radt/column.rb', line 25

def data_type(id)
  TYPES[id]
end

#decode_datetime(value) ⇒ DateTime

Decode a DateTime value

Parameters:

  • value (String)

Returns:

  • (DateTime)


42
43
44
45
46
# 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) ⇒ Object



29
30
31
32
33
34
35
36
# 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_typeString

Column type for schema definition

Returns:

  • (String)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_definitionString

Schema definition

Returns:

  • (String)


51
52
53
# File 'lib/radt/column.rb', line 51

def schema_definition
  "\"#{name.underscore}\", #{schema_data_type}\n"
end

#strip_non_ascii_chars(s) ⇒ String

Strip all non-ascii and non-printable characters

Parameters:

  • s (String)

Returns:

  • (String)


85
86
87
88
89
90
# 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