class Embulk::Output::Vertica::ValueConverterFactory

Constants

DEFAULT_TIMESTAMP_FORMAT
NAME_PATTERN

Region/Zone, Region/Zone/Zone

NUMERIC_PATTERN

[+-]HH:MM, [+-]HHMM, [+-]HH

Attributes

schema_type[R]
timestamp_format[R]
timezone[R]
value_type[R]
zone_offset[R]

Public Class Methods

create_converters(schema, default_timezone, column_options) click to toggle source

@param [Schema] schema embulk defined column types @param [String] default_timezone @param [Hash] column_options user defined column types @return [Hash] hash whose key is column_name, and value is its converter (Proc)

# 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
new(schema_type, value_type = nil, timestamp_format = nil, timezone = nil) click to toggle source
# 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

Public Instance Methods

boolean_converter() click to toggle source
# 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_converter() click to toggle source
# 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_converter() click to toggle source
# 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_converter() click to toggle source
# 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_converter() click to toggle source
# 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_converter() click to toggle source
# 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

Private Instance Methods

get_zone_offset(timezone) click to toggle source
# File lib/embulk/output/vertica/value_converter_factory.rb, line 116
def get_zone_offset(timezone)
  if NUMERIC_PATTERN === timezone
    Time.zone_offset(timezone)
  elsif NAME_PATTERN === timezone || 'UTC' == timezone
    tz = TZInfo::Timezone.get(timezone)
    tz.period_for_utc(Time.now).utc_total_offset
  else
    raise ArgumentError, "timezone format is invalid: #{timezone}"
  end
end
strptime_with_zone(date, timestamp_format, zone_offset) click to toggle source
# File lib/embulk/output/vertica/value_converter_factory.rb, line 110
def strptime_with_zone(date, timestamp_format, zone_offset)
  time = Time.strptime(date, timestamp_format)
  utc_offset = time.utc_offset
  time.localtime(zone_offset) + utc_offset - zone_offset
end