Module: Flattener::Column::Types

Defined in:
lib/has_helpers/flattener/column/types.rb

Overview

FIXME: This can probably be moved elsewhere *

Constant Summary collapse

ALL =

Normalized data types

[
  STRING = :string,
  CURRENCY = :currency,
  PERCENT = :percent,
  DATE = :date,
  DATETIME = :datetime,
  BOOLEAN = :boolean,
  TEXT = :text,
  INTEGER = :integer,
  DECIMAL = :decimal,
  FLOAT = :float,
  JSON = :json,
  ARRAY = :array,
  UUID = :uuid,
  INTERVAL = :interval
]

Class Method Summary collapse

Class Method Details

.sql_type_to_type(raw_sql_type) ⇒ Object

Params

raw_sql_type: The description of the SQL type, e.g. "boolean", "date", "character varying(63)", "timestamp without time zone"



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/has_helpers/flattener/column/types.rb', line 28

def self.sql_type_to_type(raw_sql_type)
  sql_type = raw_sql_type.to_s.downcase # Some mild normalization to allow a broader domain range.
  type =
    if sql_type =~ /character varying/ || sql_type == "string" || sql_type == "import_key"
      STRING
    elsif /\A(?:numeric|decimal)(?:\((?<precision>\d+),(?<scale>\d+)\))?\z/ =~ sql_type
      # FIXME: This handles numeric (no scale and precision) to work around the current state of the DW. This should be removed once all numeric columns have scale and precision set. *
      # elsif /\A(numeric|decimal)\((?<precision>\d+),(?<scale>\d+)\)\z/ =~ sql_type
      if (precision == "7" && scale == "2") || # App small $
         (precision == "12" && scale == "2") || # App large $
         (precision == "20" && scale == "5") # DW $
        CURRENCY
      elsif precision == "6" && scale == "3" || precision == "9" && scale == "6"
        PERCENT
      else
        DECIMAL
      end
    elsif sql_type == "date"
      DATE
    elsif sql_type =~ /timestamp/ || sql_type == "datetime" || sql_type == "past_or_present_date"
      DATETIME
    elsif sql_type == "boolean" || sql_type == "bi_state_boolean"
      BOOLEAN
    elsif sql_type == "text"
      TEXT
    elsif sql_type == "integer" || sql_type == "bigint"
      INTEGER
    elsif sql_type == "double precision"
      FLOAT
    elsif sql_type == "json" || sql_type == "jsonb"
      JSON
    elsif sql_type == "array"
      ARRAY
    elsif sql_type == "uuid"
      UUID
    elsif sql_type == "interval"
      INTERVAL
    end

  type || raise(::ArgumentError, "No type matches SQL type '#{ raw_sql_type }'")
end