class RedTrack::DataTypes

Public Class Methods

new(options) click to toggle source

Constructor - non-static… Want runtime bound interface

# File lib/redtrack_datatypes.rb, line 12
def initialize(options)
  if options && options[:logger] != nil
    @logger = options[:logger]
  else
    @logger = Logger.new(STDOUT)
  end
end

Public Instance Methods

check_bigint(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 60
def check_bigint(value,type_definition=nil,column_name=nil)
  if value.is_a?(Integer) == false
    raise_exception(column_name,value,type_definition)
  end
  # TODO: range /overflow check
  return value
end
check_char(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid - truncated if it is too long

# File lib/redtrack_datatypes.rb, line 103
def check_char(value,type_definition=nil,column_name=nil)
  if value.is_a?(String) == false
    raise_exception(column_name,value,type_definition)
  end
  # Truncate values that are too long
  value = truncate_string(column_name,value,type_definition)
  return value
end
check_date(column_name,value,type_definition) click to toggle source
# File lib/redtrack_datatypes.rb, line 127
def check_date(column_name,value,type_definition)

end
check_decimal(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 74
def check_decimal(value,type_definition=nil,column_name=nil)
  if value.is_a?(String) == false || is_numeric(value) == false
    raise_exception(column_name,value,type_definition)
    #raise ""
  end

  return value
end
check_integer(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 46
def check_integer(value,type_definition=nil,column_name=nil)
  if value.is_a?(Integer) == false
    raise_exception(column_name,value,type_definition)
  end
  # TODO: range / overflow check
  return value
end
check_real(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 89
def check_real(value,type_definition=nil,column_name=nil)
  if is_numeric(value) == false
    raise_exception(column_name,value,type_definition)
  end

  return value
end
check_smallint(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 32
def check_smallint(value,type_definition=nil,column_name=nil)
  if value.is_a?(Integer) == false
    raise_exception(column_name,value,type_definition)
  end
  # TODO: Range / overflow check
  return value
end
check_timestamp(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid

# File lib/redtrack_datatypes.rb, line 137
def check_timestamp(value,type_definition=nil,column_name=nil)
  if value.is_a?(String) == false || value[/\A\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\z/] == nil
    raise_exception(column_name,value,type_definition)
  end
  return value
end
check_varchar(value,type_definition=nil,column_name=nil) click to toggle source

Check and clean value to ensure it conforms to the redshfit data type

@param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema @param [String] column_name The name of the redshift column @return [Object] The value if it is valid - truncated if too long

# File lib/redtrack_datatypes.rb, line 118
def check_varchar(value,type_definition=nil,column_name=nil)
  if value.is_a?(String) == false
    raise_exception(column_name,value,type_definition)
  end
  # Truncate values that are too long
  value = truncate_string(column_name,value,type_definition)
  return value
end
valid_data_types() click to toggle source

@return [Array] Return an array of valid data types

# File lib/redtrack_datatypes.rb, line 21
def valid_data_types
  result = %w(smallint integer bigint decimal real boolean char varchar date timestamp)
  return result
end

Private Instance Methods

is_numeric(value) click to toggle source

Determine whether the typed value is a legit number, (eg, string)

@param [Numeric] value The value to check as valid numeric @return [Boolean] Whether or not the value is a numeric

# File lib/redtrack_datatypes.rb, line 159
def is_numeric(value)
  Float(value) != nil rescue false
end
raise_exception(column_name,value,type_definition) click to toggle source

Helper function, raise a general exception message

@param [String] column_name The name of the redshift column @param [Object] value the value to set for the column @param [String] type_definition the the type defined by the schema

# File lib/redtrack_datatypes.rb, line 151
def raise_exception(column_name,value,type_definition)
  raise "Value for column #{column_name}, #{value.to_s}, does not conform to type '#{type_definition}'"
end
truncate_string(column_name,value,type_definition) click to toggle source
# File lib/redtrack_datatypes.rb, line 163
def truncate_string(column_name,value,type_definition)
  num_chars = type_definition[/\((\d*)\)/,1].to_i
  puts "Num chars: #{num_chars}"
  if(value.length > num_chars)
    @logger.warn("#{TAG} Data for column #{column_name} is too long (#{value.length} characters) for column type and will be truncated to #{num_chars} characters: '#{value}'")
    return value[0..num_chars-1]
  else
    return value
  end
end