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"

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.



29
30
31
32
33
34
35
36
37
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 29

def initialize(schema_type, value_type = nil, timestamp_format = nil, timezone = nil)
  @schema_type = schema_type
  @value_type = value_type || schema_type.to_s
  if @schema_type == :timestamp || @value_type == 'timestamp'
    @timestamp_format = timestamp_format
    @timezone = timezone
    @zone_offset = get_zone_offset(@timezone) if @timezone
  end
end

Instance Attribute Details

#schema_typeObject (readonly)

Returns the value of attribute schema_type.



8
9
10
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 8

def schema_type
  @schema_type
end

#timestamp_formatObject (readonly)

Returns the value of attribute timestamp_format.



8
9
10
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 8

def timestamp_format
  @timestamp_format
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



8
9
10
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 8

def timezone
  @timezone
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



8
9
10
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 8

def value_type
  @value_type
end

#zone_offsetObject (readonly)

Returns the value of attribute zone_offset.



8
9
10
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 8

def zone_offset
  @zone_offset
end

Class Method Details

.create_converters(schema, default_timezone, column_options) ⇒ Hash

Returns hash whose key is column_name, and value is its converter (Proc).

Parameters:

  • schema (Schema)

    embulk defined column types

  • default_timezone (String)
  • column_options (Hash)

    user defined column types

Returns:

  • (Hash)

    hash whose key is column_name, and value is its converter (Proc)



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 16

def self.create_converters(schema, default_timezone, column_options)
  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'] || DEFAULT_TIMESTAMP_FORMAT
      timezone         = column_options[column_name]['timezone'] || default_timezone
      [column_name, self.new(schema_type, value_type, timestamp_format, timezone).create_converter]
    else
      [column_name, self.new(schema_type, nil, nil, default_timezone).create_converter]
    end
  end]
end

Instance Method Details

#boolean_converterObject



50
51
52
53
54
55
56
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 50

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 39

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



69
70
71
72
73
74
75
76
77
78
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 69

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| val ? Time.at(val).localtime(zone_offset) : nil }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for double column"
  end
end

#long_converterObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 58

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| val ? Time.at(val).localtime(zone_offset) : nil }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for long column"
  end
end

#string_converterObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 80

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| val ? strptime_with_zone(val, timestamp_format, zone_offset) : nil }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for string column"
  end
end

#timestamp_converterObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/embulk/output/vertica/value_converter_factory.rb', line 91

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 ? val.localtime(zone_offset).strftime(timestamp_format) : nil }
  when 'timestamp' then Proc.new {|val| val ? val.localtime(zone_offset) : nil }
  else raise NotSupportedType, "embulk-output-vertica cannot take column value_type #{value_type} for timesatmp column"
  end
end