Class: Embulk::Output::Vertica::ValueConverterFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/output/vertica/value_converter_factory.rb

Constant Summary collapse

DEFAULT_TIMESTAMP_FORMAT =
"%Y-%m-%d %H:%M:%S %z"
DEFAULT_TIMEZONE =
"+00:00"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema_type, value_type = nil, timestamp_format = nil, timezone = nil) ⇒ ValueConverterFactory

Returns a new instance of ValueConverterFactory.



26
27
28
29
30
31
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 26

def initialize(schema_type, value_type = nil, timestamp_format = nil, timezone = nil)
  @schema_type = schema_type
  @value_type = value_type || schema_type.to_s
  @timestampt_format = timestamp_format || DEFAULT_TIMESTAMP_FORMAT
  @timezone = timezone || DEFAULT_TIMEZONE
end

Instance Attribute Details

#schema_typeObject (readonly)

Returns the value of attribute schema_type.



5
6
7
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 5

def schema_type
  @schema_type
end

#timestamp_formatObject (readonly)

Returns the value of attribute timestamp_format.



5
6
7
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 5

def timestamp_format
  @timestamp_format
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



5
6
7
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 5

def timezone
  @timezone
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



5
6
7
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 5

def value_type
  @value_type
end

Class Method Details

.create_converters(schema, column_options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 10

def self.create_converters(schema, column_options)
  # @param [Schema] schema embulk defined column types
  # @param [Hash]   column_options user defined column types
  # @return [Array] value converters (array of Proc)
  Hash[*(schema.names.zip(schema.types).map do |column_name, schema_type|
    if column_options[column_name]
      value_type       = column_options[column_name]['value_type']
      timestamp_format = column_options[column_name]['timestamp_format']
      timezone         = column_options[column_name]['timezone']
      [column_name, self.new(schema_type, value_type, timestamp_format, timezone).create_converter]
    else
      [column_name, Proc.new {|val| val }]
    end
  end.flatten!(1))]
end

Instance Method Details

#boolean_converterObject



44
45
46
47
48
49
50
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 44

def boolean_converter
  case value_type
  when 'boolean' then Proc.new {|val| val }
  when 'string'  then Proc.new {|val| val.to_s }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for boolean column"
  end
end

#create_converterObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 33

def create_converter
  case schema_type
  when :boolean   then boolean_converter
  when :long      then long_converter
  when :double    then double_converter
  when :string    then string_converter
  when :timestamp then timestamp_converter
  else raise NotSupportedType, "embulk-output-vertica cannot take column type #{schema_type}"
  end
end

#double_converterObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 63

def double_converter
  case value_type
  when 'boolean'   then Proc.new {|val| !!val }
  when 'long'      then Proc.new {|val| val.to_i }
  when 'double'    then Proc.new {|val| val }
  when 'string'    then Proc.new {|val| val.to_s }
  when 'timestamp' then Proc.new {|val| Time.at(val).localtime(timezone) }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for double column"
  end
end

#long_converterObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 52

def long_converter
  case value_type
  when 'boolean'    then Proc.new {|val| !!val }
  when 'long'       then Proc.new {|val| val }
  when 'double'     then Proc.new {|val| val.to_f }
  when 'string'     then Proc.new {|val| val.to_s }
  when 'timestamp'  then Proc.new {|val| Time.at(val).localtime(timezone) }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for long column"
  end
end

#string_converterObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 74

def string_converter
  case value_type
  when 'boolean'   then Proc.new {|val| !!val }
  when 'long'      then Proc.new {|val| val.to_i }
  when 'double'    then Proc.new {|val| val.to_f }
  when 'string'    then Proc.new {|val| val }
  when 'timestamp' then Proc.new {|val| Time.strptime(val, timestamp_format) } # ToDo: timezone
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for string column"
  end
end

#timestamp_converterObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 85

def timestamp_converter
  case value_type
  when 'boolean'   then Proc.new {|val| !!val }
  when 'long'      then Proc.new {|val| val.to_i }
  when 'double'    then Proc.new {|val| val.to_f }
  when 'string'    then Proc.new {|val| val.localtime(timezone).strftime(timestamp_format) }
  when 'timestamp' then Proc.new {|val| val.localtime(timezone) }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for timesatmp column"
  end
end