feat(mssql): add MSSQL writer for generating DDL from database schema
All checks were successful
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.
This commit is contained in:
99
pkg/mssql/README.md
Normal file
99
pkg/mssql/README.md
Normal file
@@ -0,0 +1,99 @@
|
||||
# MSSQL Package
|
||||
|
||||
Provides utilities for working with Microsoft SQL Server data types and conversions.
|
||||
|
||||
## Components
|
||||
|
||||
### Type Mapping
|
||||
|
||||
Provides bidirectional conversion between canonical types and MSSQL types:
|
||||
|
||||
- **CanonicalToMSSQL**: Convert abstract types to MSSQL-specific types
|
||||
- **MSSQLToCanonical**: Convert MSSQL types to abstract representation
|
||||
|
||||
## Type Conversion Tables
|
||||
|
||||
### Canonical → MSSQL
|
||||
|
||||
| Canonical | MSSQL | Notes |
|
||||
|-----------|-------|-------|
|
||||
| int | INT | 32-bit signed integer |
|
||||
| int64 | BIGINT | 64-bit signed integer |
|
||||
| int32 | INT | 32-bit signed integer |
|
||||
| int16 | SMALLINT | 16-bit signed integer |
|
||||
| int8 | TINYINT | 8-bit unsigned integer |
|
||||
| bool | BIT | 0 (false) or 1 (true) |
|
||||
| float32 | REAL | Single precision floating point |
|
||||
| float64 | FLOAT | Double precision floating point |
|
||||
| decimal | NUMERIC | Fixed-point decimal number |
|
||||
| string | NVARCHAR(255) | Unicode variable-length string |
|
||||
| text | NVARCHAR(MAX) | Unicode large text |
|
||||
| timestamp | DATETIME2 | Date and time without timezone |
|
||||
| timestamptz | DATETIMEOFFSET | Date and time with timezone offset |
|
||||
| uuid | UNIQUEIDENTIFIER | GUID/UUID type |
|
||||
| bytea | VARBINARY(MAX) | Variable-length binary data |
|
||||
| date | DATE | Date only |
|
||||
| time | TIME | Time only |
|
||||
| json | NVARCHAR(MAX) | Stored as text (MSSQL v2016+) |
|
||||
| jsonb | NVARCHAR(MAX) | Stored as text (MSSQL v2016+) |
|
||||
|
||||
### MSSQL → Canonical
|
||||
|
||||
| MSSQL | Canonical | Notes |
|
||||
|-------|-----------|-------|
|
||||
| INT, INTEGER | int | Standard integer |
|
||||
| BIGINT | int64 | Large integer |
|
||||
| SMALLINT | int16 | Small integer |
|
||||
| TINYINT | int8 | Tiny integer |
|
||||
| BIT | bool | Boolean/bit flag |
|
||||
| REAL | float32 | Single precision |
|
||||
| FLOAT | float64 | Double precision |
|
||||
| NUMERIC, DECIMAL | decimal | Exact decimal |
|
||||
| NVARCHAR, VARCHAR | string | Variable-length string |
|
||||
| NCHAR, CHAR | string | Fixed-length string |
|
||||
| DATETIME2 | timestamp | Default timestamp |
|
||||
| DATETIMEOFFSET | timestamptz | Timestamp with timezone |
|
||||
| DATE | date | Date only |
|
||||
| TIME | time | Time only |
|
||||
| UNIQUEIDENTIFIER | uuid | UUID/GUID |
|
||||
| VARBINARY, BINARY | bytea | Binary data |
|
||||
| XML | string | Stored as text |
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.warky.dev/wdevs/relspecgo/pkg/mssql"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Convert canonical to MSSQL
|
||||
mssqlType := mssql.ConvertCanonicalToMSSQL("int")
|
||||
fmt.Println(mssqlType) // Output: INT
|
||||
|
||||
// Convert MSSQL to canonical
|
||||
canonicalType := mssql.ConvertMSSQLToCanonical("BIGINT")
|
||||
fmt.Println(canonicalType) // Output: int64
|
||||
|
||||
// Handle parameterized types
|
||||
canonicalType = mssql.ConvertMSSQLToCanonical("NVARCHAR(255)")
|
||||
fmt.Println(canonicalType) // Output: string
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
Run tests with:
|
||||
```bash
|
||||
go test ./pkg/mssql/...
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- Type conversions are case-insensitive
|
||||
- Parameterized types (e.g., `NVARCHAR(255)`) have their base type extracted
|
||||
- Unmapped types default to `string` for safety
|
||||
- The package supports SQL Server 2016 and later versions
|
||||
114
pkg/mssql/datatypes.go
Normal file
114
pkg/mssql/datatypes.go
Normal file
@@ -0,0 +1,114 @@
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user