Skip to content

golang: Go types for SQL Server (decimal, money, uniqueidentifier, result names) - #7

Open
EliSauder wants to merge 5 commits into
mainfrom
mssql/go-types
Open

EliSauder wants to merge 5 commits into
mainfrom
mssql/go-types

Conversation

@EliSauder

@EliSauder EliSauder commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Go code generated for SQL Server.

  • A single result column's variable is a Go identifier, even for names such as 2023 or [total amount].
  • decimal/numeric map to decimal.Decimal (shopspring, which go-mssqldb uses) and money/smallmoney to mssql.Money[...], as ClickHouse's decimals map; verified round-tripping 38 digits and NULL.
  • A nullable uniqueidentifier maps to *mssql.UniqueIdentifier: go-mssqldb sends a NULL mssql.NullUniqueIdentifier by value as a malformed GUID (TDS error 8009, still in v1.11.2).
  • SQL Server's Go type choices and dependency import metadata live in its engine package, supplied to Go generation through an optional generic mapper.

Commits

  • a43976a36 fix(golang): make a single result column's variable a Go identifier
  • 9ef6bba75 fix(golang): read a result variable's first character as a rune
  • deeeed207 fix(golang): map SQL Server decimal to decimal.Decimal and money to mssql.Money
  • e604a5a0a fix(golang): map a nullable uniqueidentifier to *mssql.UniqueIdentifier
  • 5f04dd381 refactor(mssql): own Go type mappings and import metadata in the engine

Type and import dependency inversion

The engine implements a neutral GoTypeMapper contract describing the value type, nullable wrapper, array completeness, and dependency imports. Compiler results carry the optional mapper; the built-in Go assembly injects it at runtime. The Go generator retains override precedence, nullable-pointer rendering, and its existing recursive matching of generic type arguments. It no longer owns SQL Server type mappings, the go-mssqldb import path, or a Money-specific import match.

Other engines retain their existing fallback mappings. External generators and the plugin protocol are unchanged; the mapper is not a serialized configuration option.

The focused end-to-end fixture covers money-only generic dependencies, nullable money/UUID values, pointer mode, database-type overrides, and column overrides with a different package using the same qualifier. Its goldens were generated with the unmodified PR head and match after the refactor.

Test changes for independence

  • mssql_result_variable_name: the column named 2023 comes from an alias rather than a PIVOT, which is in the core PR.

Independence. This is one of five PRs split from #1, each off main with no dependency on the others. Existing goldens remain unchanged. The compiler and Go generator assembly points overlap #6's execution-capability inversion; retain both optional providers when integrating those edits.

Verification. SQL behavior was checked against SQL Server 2025 in #1 (goldeneye or sp_describe_*). For this refactor, the complete base replay corpus, targeted cross-engine type/override cases, touched-package race tests, targeted go vet, and CLI build pass. The existing SQL Server type golden and all three new generated Go packages compile and pass Go's default vet checks.

EliSauder and others added 4 commits November 12, 2026 18:13
A query returning one column scans it into a variable named after the
column, with only $ replaced. A database may name a column anything, as
SQL Server names a pivoted column 2023, a FOR JSON result
JSON_F52E2B61-18A1-11d1-B105-00805F49916B, and a bracketed alias
[total amount], each of which generated code that did not compile. Every
character a Go identifier cannot hold is now replaced, and a leading
digit prefixed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
goIdent checked the first byte of a column name for a digit, so a name
starting with a non-ASCII digit, such as [١total], kept it and did not
compile, and a name with no identifier characters at all, such as [!],
became the blank identifier. The first rune is now checked, and a name
left with nothing but underscores falls back to column_1, as an unnamed
column is called.

Addresses review comment r4557024827 on #1.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ssql.Money

decimal, numeric, money and smallmoney mapped to string, leaving each
caller to parse it. They now map to shopspring's decimal, which
go-mssqldb already depends on and sends as a decimal, as ClickHouse's
decimals map, and money to the driver's Money over it, which it sends
as money. A generic type's argument brings its package's import.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
go-mssqldb sends a NULL mssql.NullUniqueIdentifier passed by value as a
GUID of length 0 followed by 16 bytes, which SQL Server rejects with
error 8009, so a generated query could not pass NULL for one. A nil
*mssql.UniqueIdentifier it sends as NULL, and scans NULL into, so a
nullable uniqueidentifier now maps to that.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread internal/codegen/golang/imports.go Resolved
Inject an optional Go type mapper through compiler results and built-in generator assembly. Keep nullable-pointer rendering and recursive generic import matching shared, preserving legacy engine fallbacks and user override precedence.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant