159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package mssql
|
|
|
|
import "strings"
|
|
|
|
// CanonicalToMSSQLTypes maps canonical types to MSSQL types.
|
|
// Accepts both Go canonical names ("int", "string") and SQL canonical names
|
|
// ("integer", "varchar") so the writer handles input from any reader.
|
|
var CanonicalToMSSQLTypes = map[string]string{
|
|
// Boolean — Go and SQL canonical
|
|
"bool": "BIT",
|
|
"boolean": "BIT",
|
|
// Integer — Go canonical
|
|
"int8": "TINYINT",
|
|
"int16": "SMALLINT",
|
|
"int": "INT",
|
|
"int32": "INT",
|
|
"int64": "BIGINT",
|
|
"uint": "BIGINT",
|
|
"uint8": "TINYINT",
|
|
"uint16": "SMALLINT",
|
|
"uint32": "BIGINT",
|
|
"uint64": "BIGINT",
|
|
// Integer — SQL canonical (serial types map to base integer; IDENTITY is set via AutoIncrement)
|
|
"integer": "INT",
|
|
"smallint": "SMALLINT",
|
|
"bigint": "BIGINT",
|
|
"tinyint": "TINYINT",
|
|
"serial": "INT",
|
|
"smallserial": "SMALLINT",
|
|
"bigserial": "BIGINT",
|
|
// Float — Go canonical
|
|
"float32": "REAL",
|
|
"float64": "FLOAT",
|
|
// Float — SQL canonical
|
|
"real": "REAL",
|
|
"double precision": "FLOAT",
|
|
"double": "FLOAT",
|
|
// Decimal/numeric
|
|
"decimal": "NUMERIC",
|
|
"numeric": "NUMERIC",
|
|
"money": "MONEY",
|
|
// String — Go canonical
|
|
"string": "NVARCHAR(255)",
|
|
"text": "NVARCHAR(MAX)",
|
|
// String — SQL canonical
|
|
"varchar": "NVARCHAR(255)",
|
|
"char": "NCHAR",
|
|
"nvarchar": "NVARCHAR(255)",
|
|
"nchar": "NCHAR",
|
|
"citext": "NVARCHAR(MAX)",
|
|
// Date/time
|
|
"date": "DATE",
|
|
"time": "TIME",
|
|
"timetz": "DATETIMEOFFSET",
|
|
"timestamp": "DATETIME2",
|
|
"timestamptz": "DATETIMEOFFSET",
|
|
"datetime": "DATETIME2",
|
|
"interval": "NVARCHAR(50)",
|
|
// UUID
|
|
"uuid": "UNIQUEIDENTIFIER",
|
|
// JSON — MSSQL has no native JSON type; stored as NVARCHAR(MAX)
|
|
"json": "NVARCHAR(MAX)",
|
|
"jsonb": "NVARCHAR(MAX)",
|
|
// Binary
|
|
"bytea": "VARBINARY(MAX)",
|
|
"blob": "VARBINARY(MAX)",
|
|
// Network/geo types — no MSSQL native equivalent
|
|
"xml": "XML",
|
|
"inet": "NVARCHAR(45)",
|
|
"cidr": "NVARCHAR(43)",
|
|
"macaddr": "NVARCHAR(17)",
|
|
}
|
|
|
|
// MSSQLToCanonicalTypes maps MSSQL types to canonical types
|
|
var MSSQLToCanonicalTypes = map[string]string{
|
|
"bit": "bool",
|
|
"tinyint": "int8",
|
|
"smallint": "int16",
|
|
"int": "int",
|
|
"integer": "int",
|
|
"bigint": "int64",
|
|
"real": "float32",
|
|
"float": "float64",
|
|
"numeric": "decimal",
|
|
"decimal": "decimal",
|
|
"money": "decimal",
|
|
"smallmoney": "decimal",
|
|
"nvarchar": "string",
|
|
"nchar": "string",
|
|
"varchar": "string",
|
|
"char": "string",
|
|
"text": "string",
|
|
"ntext": "string",
|
|
"date": "date",
|
|
"time": "time",
|
|
"datetime": "timestamp",
|
|
"datetime2": "timestamp",
|
|
"smalldatetime": "timestamp",
|
|
"datetimeoffset": "timestamptz",
|
|
"uniqueidentifier": "uuid",
|
|
"varbinary": "bytea",
|
|
"binary": "bytea",
|
|
"image": "bytea",
|
|
"xml": "string",
|
|
"json": "json",
|
|
"sql_variant": "string",
|
|
"hierarchyid": "string",
|
|
"geography": "string",
|
|
"geometry": "string",
|
|
}
|
|
|
|
// MSSQLTypeSynonyms maps MSSQL type aliases to their canonical MSSQL name.
|
|
var MSSQLTypeSynonyms = map[string]string{
|
|
"integer": "int",
|
|
"dec": "decimal",
|
|
"float(n)": "float",
|
|
}
|
|
|
|
// NormalizeMSSQLType maps an MSSQL base type (no dimension parameters) to its
|
|
// canonical MSSQL form. Unknown types are returned as-is (lowercased).
|
|
func NormalizeMSSQLType(baseType string) string {
|
|
lower := strings.ToLower(strings.TrimSpace(baseType))
|
|
if canonical, ok := MSSQLTypeSynonyms[lower]; ok {
|
|
return canonical
|
|
}
|
|
return lower
|
|
}
|
|
|
|
// ConvertCanonicalToMSSQL converts a canonical type (Go or SQL) to an MSSQL type.
|
|
// Strips dimension parameters before lookup. Defaults to NVARCHAR(255) for unknown types.
|
|
func ConvertCanonicalToMSSQL(canonicalType string) string {
|
|
base := strings.ToLower(strings.TrimSpace(canonicalType))
|
|
if idx := strings.Index(base, "("); idx >= 0 {
|
|
base = strings.TrimSpace(base[:idx])
|
|
}
|
|
base = strings.TrimSuffix(base, "[]")
|
|
|
|
if mssqlType, exists := CanonicalToMSSQLTypes[base]; exists {
|
|
return mssqlType
|
|
}
|
|
|
|
return "NVARCHAR(255)"
|
|
}
|
|
|
|
// ConvertMSSQLToCanonical converts an MSSQL type to the canonical type.
|
|
// Strips dimension parameters before lookup. Defaults to "string" for unknown types.
|
|
func ConvertMSSQLToCanonical(mssqlType string) string {
|
|
base := strings.ToLower(strings.TrimSpace(mssqlType))
|
|
if idx := strings.Index(base, "("); idx >= 0 {
|
|
base = strings.TrimSpace(base[:idx])
|
|
}
|
|
|
|
if canonicalType, exists := MSSQLToCanonicalTypes[base]; exists {
|
|
return canonicalType
|
|
}
|
|
|
|
return "string"
|
|
}
|