cb735f0754
* Implement SQLiteToCanonicalTypes for type mapping * Add ConvertSQLiteToCanonical and ConvertCanonicalToSQLite functions * Update mapDataType to utilize new conversion logic
157 lines
4.1 KiB
Go
157 lines
4.1 KiB
Go
package mariadb
|
|
|
|
import "strings"
|
|
|
|
// MariaDBToCanonicalTypes maps MariaDB/MySQL type names to canonical types.
|
|
var MariaDBToCanonicalTypes = map[string]string{
|
|
// Integer types
|
|
"tinyint": "int8",
|
|
"smallint": "int16",
|
|
"mediumint": "int",
|
|
"int": "int",
|
|
"integer": "int",
|
|
"int2": "int16",
|
|
"int4": "int",
|
|
"int8": "int64",
|
|
"bigint": "int64",
|
|
// Boolean (TINYINT(1) alias)
|
|
"boolean": "bool",
|
|
"bool": "bool",
|
|
"bit": "bool",
|
|
// Float types
|
|
"float": "float32",
|
|
"double": "float64",
|
|
"real": "float64",
|
|
"double precision": "float64",
|
|
// Decimal types
|
|
"decimal": "decimal",
|
|
"numeric": "decimal",
|
|
"dec": "decimal",
|
|
"fixed": "decimal",
|
|
// String types
|
|
"char": "string",
|
|
"character": "string",
|
|
"varchar": "string",
|
|
"nchar": "string",
|
|
"nvarchar": "string",
|
|
"tinytext": "text",
|
|
"text": "text",
|
|
"mediumtext": "text",
|
|
"longtext": "text",
|
|
// Binary/blob types
|
|
"binary": "bytea",
|
|
"varbinary": "bytea",
|
|
"tinyblob": "bytea",
|
|
"blob": "bytea",
|
|
"mediumblob": "bytea",
|
|
"longblob": "bytea",
|
|
// Date/time types
|
|
"date": "date",
|
|
"time": "time",
|
|
"datetime": "timestamp",
|
|
"timestamp": "timestamp",
|
|
"year": "int",
|
|
// Other types
|
|
"json": "json",
|
|
"enum": "string",
|
|
"set": "string",
|
|
"uuid": "uuid",
|
|
}
|
|
|
|
// CanonicalToMariaDBTypes maps canonical types to MariaDB/MySQL types.
|
|
var CanonicalToMariaDBTypes = map[string]string{
|
|
"bool": "TINYINT(1)",
|
|
"int8": "TINYINT",
|
|
"int16": "SMALLINT",
|
|
"int": "INT",
|
|
"int32": "INT",
|
|
"int64": "BIGINT",
|
|
"uint": "INT UNSIGNED",
|
|
"uint8": "TINYINT UNSIGNED",
|
|
"uint16": "SMALLINT UNSIGNED",
|
|
"uint32": "INT UNSIGNED",
|
|
"uint64": "BIGINT UNSIGNED",
|
|
"float32": "FLOAT",
|
|
"float64": "DOUBLE",
|
|
"decimal": "DECIMAL",
|
|
"string": "VARCHAR(255)",
|
|
"text": "TEXT",
|
|
"date": "DATE",
|
|
"time": "TIME",
|
|
"timestamp": "DATETIME",
|
|
"timestamptz": "DATETIME",
|
|
"uuid": "CHAR(36)",
|
|
"json": "JSON",
|
|
"jsonb": "JSON",
|
|
"bytea": "BLOB",
|
|
}
|
|
|
|
// MariaDBTypeSynonyms maps MariaDB/MySQL type aliases to their canonical MariaDB name.
|
|
var MariaDBTypeSynonyms = map[string]string{
|
|
"integer": "int",
|
|
"int2": "smallint",
|
|
"int4": "int",
|
|
"int8": "bigint",
|
|
"double precision": "double",
|
|
"character": "char",
|
|
"dec": "decimal",
|
|
"fixed": "decimal",
|
|
"numeric": "decimal",
|
|
"boolean": "tinyint",
|
|
"bool": "tinyint",
|
|
}
|
|
|
|
// NormalizeMariaDBType maps a MariaDB/MySQL base type (no dimension parameters)
|
|
// to its canonical MariaDB form. Unknown types are returned as-is (lowercased).
|
|
func NormalizeMariaDBType(baseType string) string {
|
|
lower := strings.ToLower(strings.TrimSpace(baseType))
|
|
if canonical, ok := MariaDBTypeSynonyms[lower]; ok {
|
|
return canonical
|
|
}
|
|
return lower
|
|
}
|
|
|
|
// ConvertMariaDBToCanonical converts a MariaDB/MySQL type name to the canonical type.
|
|
// Strips dimension parameters and normalizes aliases. Defaults to "string".
|
|
func ConvertMariaDBToCanonical(mariadbType string) string {
|
|
base := strings.ToLower(strings.TrimSpace(mariadbType))
|
|
if idx := strings.Index(base, "("); idx >= 0 {
|
|
base = strings.TrimSpace(base[:idx])
|
|
}
|
|
|
|
if canonical, ok := MariaDBToCanonicalTypes[base]; ok {
|
|
return canonical
|
|
}
|
|
|
|
// Prefix match for composite types (e.g., "unsigned bigint")
|
|
for key, canonical := range MariaDBToCanonicalTypes {
|
|
if strings.HasPrefix(base, key) {
|
|
return canonical
|
|
}
|
|
}
|
|
|
|
return "string"
|
|
}
|
|
|
|
// ConvertCanonicalToMariaDB converts a canonical type to a MariaDB/MySQL type.
|
|
// Defaults to VARCHAR(255) for unrecognised types.
|
|
func ConvertCanonicalToMariaDB(canonicalType string) string {
|
|
lower := strings.ToLower(strings.TrimSpace(canonicalType))
|
|
if idx := strings.Index(lower, "("); idx >= 0 {
|
|
lower = strings.TrimSpace(lower[:idx])
|
|
}
|
|
|
|
if mariadbType, ok := CanonicalToMariaDBTypes[lower]; ok {
|
|
return mariadbType
|
|
}
|
|
|
|
// Prefix fallback
|
|
for canonical, mariadb := range CanonicalToMariaDBTypes {
|
|
if strings.HasPrefix(lower, canonical) {
|
|
return mariadb
|
|
}
|
|
}
|
|
|
|
return "VARCHAR(255)"
|
|
}
|