diff --git a/datatype.go b/datatype.go index 2c44bebd4..73fb91499 100644 --- a/datatype.go +++ b/datatype.go @@ -32,27 +32,48 @@ const ( unSupportedType ) -var snowflakeTypes = [...]string{"FIXED", "REAL", "TEXT", "DATE", "VARIANT", - "TIMESTAMP_LTZ", "TIMESTAMP_NTZ", "TIMESTAMP_TZ", "OBJECT", "ARRAY", - "BINARY", "TIME", "BOOLEAN", "NULL", "SLICE", "CHANGE_TYPE", "NOT_SUPPORTED"} +var snowflakeToDriverType = map[string]snowflakeType{ + "FIXED": fixedType, + "REAL": realType, + "TEXT": textType, + "DATE": dateType, + "VARIANT": variantType, + "TIMESTAMP_LTZ": timestampLtzType, + "TIMESTAMP_NTZ": timestampNtzType, + "TIMESTAMP_TZ": timestampTzType, + "OBJECT": objectType, + "ARRAY": arrayType, + "BINARY": binaryType, + "TIME": timeType, + "BOOLEAN": booleanType, + "NULL": nullType, + "SLICE": sliceType, + "CHANGE_TYPE": changeType, + "NOT_SUPPORTED": unSupportedType} -func (st snowflakeType) String() string { - return snowflakeTypes[st] +var driverTypeToSnowflake = invertMap(snowflakeToDriverType) + +func invertMap(m map[string]snowflakeType) map[snowflakeType]string { + inv := make(map[snowflakeType]string) + for k, v := range m { + if _, ok := inv[v]; ok { + panic("failed to create driverTypeToSnowflake map due to duplicated values") + } + inv[v] = k + } + return inv } func (st snowflakeType) Byte() byte { return byte(st) } +func (st snowflakeType) String() string { + return driverTypeToSnowflake[st] +} + func getSnowflakeType(typ string) snowflakeType { - for i, sft := range snowflakeTypes { - if sft == typ { - return snowflakeType(i) - } else if snowflakeType(i) == nullType { - break - } - } - return nullType + return snowflakeToDriverType[typ] } var (