Skip to content

Commit 4f4cf37

Browse files
EliSauderCopilot
andcommitted
plugin: pass domains to codegen plugins
A column typed with a domain reports the domain's name, as the analysis core and the databases themselves do, but nothing told a plugin what the domain stands on, so generators fell back to an untyped value. The plugin catalog's Schema gains domains: each domain's name, its base as a type expression, and whether it forbids NULL. The field is additive; plugins that do not read it are unaffected. The core path fills it from its catalog, which already records every domain and SQL Server alias type with its base, so SQL Server's CREATE TYPE ... FROM, and PostgreSQL's CREATE DOMAIN under the core analyzer, reach plugins. A domain outside the default schema is named qualified, as columns of it are. Refs sqlc-dev#4483 Co-authored-by: Copilot <[email protected]>
1 parent 3ad8b20 commit 4f4cf37

16 files changed

Lines changed: 928 additions & 239 deletions

File tree

‎internal/cmd/shim.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
6565
for _, s := range c.Schemas {
6666
var enums []*plugin.Enum
6767
var cts []*plugin.CompositeType
68+
var domains []*plugin.Domain
6869
for _, typ := range s.Types {
6970
switch typ := typ.(type) {
7071
case *catalog.Enum:
@@ -78,6 +79,13 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
7879
Name: typ.Name,
7980
Comment: typ.Comment,
8081
})
82+
case *catalog.Domain:
83+
domains = append(domains, &plugin.Domain{
84+
Name: typ.Name,
85+
Base: pluginTypeExpr(typ.Base),
86+
NotNull: typ.NotNull,
87+
Comment: typ.Comment,
88+
})
8189
}
8290
}
8391
var tables []*plugin.Table
@@ -125,6 +133,7 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
125133
Tables: tables,
126134
Enums: enums,
127135
CompositeTypes: cts,
136+
Domains: domains,
128137
})
129138
}
130139
return &plugin.Catalog{

‎internal/compiler/catalog_core.go‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
// coreResultCatalog dumps the core catalog into the legacy catalog shape a
1212
// Result carries, so codegen sees the same table models either way a query
13-
// set was analyzed. Only relations make the trip: codegen reads tables and
14-
// their columns to build models, and none of the types, functions or
15-
// operators the core catalog also holds.
13+
// set was analyzed. Codegen reads tables and their columns to build models,
14+
// and the domains those columns may be typed with; none of the other types,
15+
// functions or operators the core catalog also holds.
1616
func coreResultCatalog(c *core.Catalog) (*catalog.Catalog, error) {
1717
cat := catalog.New("public")
1818
namespaces, err := c.Namespaces()
@@ -60,6 +60,17 @@ func coreResultCatalog(c *core.Catalog) (*catalog.Catalog, error) {
6060
}
6161
schema.Tables = append(schema.Tables, t)
6262
}
63+
domains, err := c.DomainsInNamespace(ns.OID)
64+
if err != nil {
65+
return nil, err
66+
}
67+
for _, d := range domains {
68+
base, err := c.TypeExprOf(d.BaseOID)
69+
if err != nil {
70+
return nil, err
71+
}
72+
schema.Types = append(schema.Types, &catalog.Domain{Name: d.Name, Base: base, NotNull: d.NotNull})
73+
}
6374
cat.Schemas = append(cat.Schemas, schema)
6475
}
6576
return cat, nil

‎internal/core/catalogdb/query.sql.go‎

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

‎internal/core/catalogdef/query.sql‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ SELECT oid, name FROM sql_class
143143
WHERE namespace_oid = ? AND kind = 'r'
144144
ORDER BY oid;
145145

146+
-- name: ListDomainsInNamespace :many
147+
SELECT oid FROM sql_type
148+
WHERE namespace_oid = ? AND typtype = 'd'
149+
ORDER BY oid;
150+
146151
-- name: DeleteClass :exec
147152
DELETE FROM sql_class WHERE oid = ?;
148153

‎internal/core/class.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,23 @@ func (c *Catalog) RenameClass(classOID int64, name string) error {
6060
return nil
6161
}
6262

63+
// DomainsInNamespace lists the domains and alias types a namespace holds.
64+
func (c *Catalog) DomainsInNamespace(namespaceOID int64) ([]TypeInfo, error) {
65+
oids, err := c.q.ListDomainsInNamespace(context.Background(), namespaceOID)
66+
if err != nil {
67+
return nil, fmt.Errorf("list domains in namespace %d: %w", namespaceOID, err)
68+
}
69+
out := make([]TypeInfo, 0, len(oids))
70+
for _, oid := range oids {
71+
info, err := c.LookupType(oid)
72+
if err != nil {
73+
return nil, err
74+
}
75+
out = append(out, info)
76+
}
77+
return out, nil
78+
}
79+
6380
type ClassInfo struct {
6481
OID int64
6582
Name string

‎internal/endtoend/testdata/codegen_json/gen/codegen.json‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@
119119
}
120120
],
121121
"enums": [],
122-
"composite_types": []
122+
"composite_types": [],
123+
"domains": []
123124
},
124125
{
125126
"comment": "",
126127
"name": "pg_temp",
127128
"tables": [],
128129
"enums": [],
129-
"composite_types": []
130+
"composite_types": [],
131+
"domains": []
130132
},
131133
{
132134
"comment": "",
@@ -48568,7 +48570,8 @@
4856848570
}
4856948571
],
4857048572
"enums": [],
48571-
"composite_types": []
48573+
"composite_types": [],
48574+
"domains": []
4857248575
},
4857348576
{
4857448577
"comment": "",
@@ -68733,7 +68736,8 @@
6873368736
}
6873468737
],
6873568738
"enums": [],
68736-
"composite_types": []
68739+
"composite_types": [],
68740+
"domains": []
6873768741
}
6873868742
]
6873968743
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}

0 commit comments

Comments
 (0)