Skip to content

Commit bcd8bb8

Browse files
EliSauderCopilot
andcommitted
feat(golang): generate domain columns as their base type
A column typed with a PostgreSQL domain or a SQL Server alias type was generated as interface{} or any, since no mapper knew the domain's name. The Go generator now looks the domain up in the plugin catalog and maps the column as the domain's base, wherever the domain appears in the type, so an array of a domain is a slice of its base. A db_type override that names the domain still applies first, so one override covers every column of it, as #434 describes. Refs #3324 Refs #434 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a0a9ba2 commit bcd8bb8

20 files changed

Lines changed: 438 additions & 7 deletions

File tree

‎docs/howto/overrides.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ sql:
3838
go_type: "time.Time"
3939
```
4040
41+
A column typed with a domain (PostgreSQL's `CREATE DOMAIN`, SQL Server's
42+
`CREATE TYPE ... FROM`) is generated as the domain's base type. A `db_type`
43+
override naming the domain itself, qualified by its schema when it is not in
44+
the default one (`billing.ksuid`), takes precedence and applies to every
45+
column of the domain.
46+
4147
> [!TIP]
4248
> A single `db_type` override configuration applies to either nullable or non-nullable
4349
> columns, but not both. If you want the same Go type to override regardless of
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package golang
2+
3+
import (
4+
"strings"
5+
6+
"google.golang.org/protobuf/proto"
7+
8+
"github.com/sqlc-dev/sqlc/internal/plugin"
9+
)
10+
11+
// domainColumn is col with every domain its type names replaced by the
12+
// domain's base, or nil when its type names none.
13+
func domainColumn(req *plugin.GenerateRequest, col *plugin.Column) *plugin.Column {
14+
if col.TypeExpr != nil {
15+
t, ok := resolveDomains(req, col.TypeExpr)
16+
if !ok {
17+
return nil
18+
}
19+
out := proto.Clone(col).(*plugin.Column)
20+
out.TypeExpr = t
21+
out.Type = &plugin.Identifier{Name: t.GetName()}
22+
return out
23+
}
24+
d := findDomain(req, col.Type.GetSchema(), col.Type.GetName())
25+
if d == nil || d.Base == nil {
26+
return nil
27+
}
28+
out := proto.Clone(col).(*plugin.Column)
29+
out.Type = &plugin.Identifier{Name: d.Base.Name}
30+
return out
31+
}
32+
33+
// resolveDomains replaces each domain in t with its base, keeping the
34+
// nullability t says each position has.
35+
func resolveDomains(req *plugin.GenerateRequest, t *plugin.TypeExpr) (*plugin.TypeExpr, bool) {
36+
if d := findDomain(req, "", t.GetName()); d != nil && d.Base != nil {
37+
base := proto.Clone(d.Base).(*plugin.TypeExpr)
38+
base.Nullable = t.Nullable
39+
if resolved, ok := resolveDomains(req, base); ok {
40+
return resolved, true
41+
}
42+
return base, true
43+
}
44+
var out *plugin.TypeExpr
45+
for i, arg := range t.GetArgs() {
46+
inner, ok := resolveDomains(req, arg.GetType())
47+
if arg.GetType() == nil || !ok {
48+
continue
49+
}
50+
if out == nil {
51+
out = proto.Clone(t).(*plugin.TypeExpr)
52+
}
53+
out.Args[i].Value = &plugin.TypeArg_Type{Type: inner}
54+
}
55+
return out, out != nil
56+
}
57+
58+
// findDomain looks a domain up by the name a column's type reports: a
59+
// qualified name in its schema, and a bare one in the default schema
60+
// first, as the type would resolve.
61+
func findDomain(req *plugin.GenerateRequest, schema, name string) *plugin.Domain {
62+
if name == "" {
63+
return nil
64+
}
65+
if schema == "" {
66+
if i := strings.LastIndex(name, "."); i >= 0 {
67+
schema, name = name[:i], name[i+1:]
68+
}
69+
}
70+
var fallback *plugin.Domain
71+
for _, s := range req.GetCatalog().GetSchemas() {
72+
for _, d := range s.GetDomains() {
73+
if d.Name != name {
74+
continue
75+
}
76+
switch {
77+
case schema != "" && s.Name == schema:
78+
return d
79+
case schema == "" && s.Name == req.Catalog.DefaultSchema:
80+
return d
81+
case schema == "" && fallback == nil:
82+
fallback = d
83+
}
84+
}
85+
}
86+
return fallback
87+
}

‎internal/codegen/golang/go_type.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
9595
}
9696
}
9797

98+
// A domain is generated as the type it stands on, unless an override
99+
// above named the domain itself.
100+
if base := domainColumn(req, col); base != nil {
101+
return goInnerType(req, options, base, param)
102+
}
103+
98104
// TODO: Extend the engine interface to handle types
99105
switch req.Settings.Engine {
100106
case engineMySQL:

‎internal/codegen/golang/mssql_type.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import (
1010
// come back as their decimal text, so they map to string the way
1111
// PostgreSQL's numeric does under database/sql. A uniqueidentifier maps to
1212
// the driver's own type, which keeps SQL Server's byte order straight. An
13-
// alias type created with CREATE TYPE ... FROM is reported by its own name,
14-
// which the catalog does not yet resolve to its base, so it maps to any
15-
// until it does.
13+
// alias type created with CREATE TYPE ... FROM never reaches here: it is
14+
// mapped as its base.
1615
func mssqlType(req *plugin.GenerateRequest, options *opts.Options, col *plugin.Column) string {
1716
t, nullable := columnType(col)
1817
null := func(typ, nullTyp string) string {

‎internal/endtoend/testdata/core_types/mssql/go/models.go‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/core_types/mssql/go/query.sql.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/domain_types/mssql/go/db.go‎

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/mssql/go/models.go‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/mssql/go/query.sql.go‎

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)