All checks were successful
- Implement MSSQL writer to generate SQL scripts for creating schemas, tables, and constraints. - Support for identity columns, indexes, and extended properties. - Add tests for column definitions, table creation, primary keys, foreign keys, and comments. - Include testing guide and sample schema for integration tests.
115 lines
3.1 KiB
Go
115 lines
3.1 KiB
Go
package mssql
|
|
|
|
import "strings"
|
|
|
|
// CanonicalToMSSQLTypes maps canonical types to MSSQL types
|
|
var CanonicalToMSSQLTypes = map[string]string{
|
|
"bool": "BIT",
|
|
"int8": "TINYINT",
|
|
"int16": "SMALLINT",
|
|
"int": "INT",
|
|
"int32": "INT",
|
|
"int64": "BIGINT",
|
|
"uint": "BIGINT",
|
|
"uint8": "SMALLINT",
|
|
"uint16": "INT",
|
|
"uint32": "BIGINT",
|
|
"uint64": "BIGINT",
|
|
"float32": "REAL",
|
|
"float64": "FLOAT",
|
|
"decimal": "NUMERIC",
|
|
"string": "NVARCHAR(255)",
|
|
"text": "NVARCHAR(MAX)",
|
|
"date": "DATE",
|
|
"time": "TIME",
|
|
"timestamp": "DATETIME2",
|
|
"timestamptz": "DATETIMEOFFSET",
|
|
"uuid": "UNIQUEIDENTIFIER",
|
|
"json": "NVARCHAR(MAX)",
|
|
"jsonb": "NVARCHAR(MAX)",
|
|
"bytea": "VARBINARY(MAX)",
|
|
}
|
|
|
|
// 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",
|
|
}
|
|
|
|
// ConvertCanonicalToMSSQL converts a canonical type to MSSQL type
|
|
func ConvertCanonicalToMSSQL(canonicalType string) string {
|
|
// Check direct mapping
|
|
if mssqlType, exists := CanonicalToMSSQLTypes[strings.ToLower(canonicalType)]; exists {
|
|
return mssqlType
|
|
}
|
|
|
|
// Try to find by prefix
|
|
lowerType := strings.ToLower(canonicalType)
|
|
for canonical, mssql := range CanonicalToMSSQLTypes {
|
|
if strings.HasPrefix(lowerType, canonical) {
|
|
return mssql
|
|
}
|
|
}
|
|
|
|
// Default to NVARCHAR
|
|
return "NVARCHAR(255)"
|
|
}
|
|
|
|
// ConvertMSSQLToCanonical converts an MSSQL type to canonical type
|
|
func ConvertMSSQLToCanonical(mssqlType string) string {
|
|
// Extract base type (remove parentheses and parameters)
|
|
baseType := mssqlType
|
|
if idx := strings.Index(baseType, "("); idx != -1 {
|
|
baseType = baseType[:idx]
|
|
}
|
|
baseType = strings.TrimSpace(baseType)
|
|
|
|
// Check direct mapping
|
|
if canonicalType, exists := MSSQLToCanonicalTypes[strings.ToLower(baseType)]; exists {
|
|
return canonicalType
|
|
}
|
|
|
|
// Try to find by prefix
|
|
lowerType := strings.ToLower(baseType)
|
|
for mssql, canonical := range MSSQLToCanonicalTypes {
|
|
if strings.HasPrefix(lowerType, mssql) {
|
|
return canonical
|
|
}
|
|
}
|
|
|
|
// Default to string
|
|
return "string"
|
|
}
|