mssql

package
v1.0.36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

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

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:

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
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)",
}

CanonicalToMSSQLTypes maps canonical types to MSSQL types

View Source
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",
}

MSSQLToCanonicalTypes maps MSSQL types to canonical types

Functions

func ConvertCanonicalToMSSQL

func ConvertCanonicalToMSSQL(canonicalType string) string

ConvertCanonicalToMSSQL converts a canonical type to MSSQL type

func ConvertMSSQLToCanonical

func ConvertMSSQLToCanonical(mssqlType string) string

ConvertMSSQLToCanonical converts an MSSQL type to canonical type

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL