mssql

package
v1.0.59 Latest Latest
Warning

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

Go to latest
Published: May 20, 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",
	"boolean": "BIT",

	"int8":   "TINYINT",
	"int16":  "SMALLINT",
	"int":    "INT",
	"int32":  "INT",
	"int64":  "BIGINT",
	"uint":   "BIGINT",
	"uint8":  "TINYINT",
	"uint16": "SMALLINT",
	"uint32": "BIGINT",
	"uint64": "BIGINT",

	"integer":     "INT",
	"smallint":    "SMALLINT",
	"bigint":      "BIGINT",
	"tinyint":     "TINYINT",
	"serial":      "INT",
	"smallserial": "SMALLINT",
	"bigserial":   "BIGINT",

	"float32": "REAL",
	"float64": "FLOAT",

	"real":             "REAL",
	"double precision": "FLOAT",
	"double":           "FLOAT",

	"decimal": "NUMERIC",
	"numeric": "NUMERIC",
	"money":   "MONEY",

	"string": "NVARCHAR(255)",
	"text":   "NVARCHAR(MAX)",

	"varchar":  "NVARCHAR(255)",
	"char":     "NCHAR",
	"nvarchar": "NVARCHAR(255)",
	"nchar":    "NCHAR",
	"citext":   "NVARCHAR(MAX)",

	"date":        "DATE",
	"time":        "TIME",
	"timetz":      "DATETIMEOFFSET",
	"timestamp":   "DATETIME2",
	"timestamptz": "DATETIMEOFFSET",
	"datetime":    "DATETIME2",
	"interval":    "NVARCHAR(50)",

	"uuid": "UNIQUEIDENTIFIER",

	"json":  "NVARCHAR(MAX)",
	"jsonb": "NVARCHAR(MAX)",

	"bytea": "VARBINARY(MAX)",
	"blob":  "VARBINARY(MAX)",

	"xml":     "XML",
	"inet":    "NVARCHAR(45)",
	"cidr":    "NVARCHAR(43)",
	"macaddr": "NVARCHAR(17)",
}

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.

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

View Source
var MSSQLTypeSynonyms = map[string]string{
	"integer":  "int",
	"dec":      "decimal",
	"float(n)": "float",
}

MSSQLTypeSynonyms maps MSSQL type aliases to their canonical MSSQL name.

Functions

func ConvertCanonicalToMSSQL

func ConvertCanonicalToMSSQL(canonicalType string) string

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 ConvertMSSQLToCanonical

func ConvertMSSQLToCanonical(mssqlType string) string

ConvertMSSQLToCanonical converts an MSSQL type to the canonical type. Strips dimension parameters before lookup. Defaults to "string" for unknown types.

func NormalizeMSSQLType added in v1.0.58

func NormalizeMSSQLType(baseType string) string

NormalizeMSSQLType maps an MSSQL base type (no dimension parameters) to its canonical MSSQL form. Unknown types are returned as-is (lowercased).

Types

This section is empty.

Jump to

Keyboard shortcuts

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