Skip to content

Commit 376352c

Browse files
EliSauderCopilot
andcommitted
postgresql: record domains in the catalog
The catalog PostgreSQL is analyzed with by default ignored CREATE DOMAIN, so a plugin generating for PostgreSQL was told nothing about a domain even with the protocol able to carry it. The catalog now records each domain with its base type and whether it is declared NOT NULL, in the schema it was created in, and DROP DOMAIN removes it as DROP TYPE does. Refs sqlc-dev#4483 Co-authored-by: Copilot <[email protected]>
1 parent 642f1e2 commit 376352c

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎internal/engine/postgresql/parse.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ func translate(node *nodes.Node) (ast.Node, error) {
672672
}
673673
return drop, nil
674674

675-
case nodes.ObjectType_OBJECT_TYPE:
675+
case nodes.ObjectType_OBJECT_TYPE, nodes.ObjectType_OBJECT_DOMAIN:
676676
drop := &ast.DropTypeStmt{
677677
IfExists: n.MissingOk,
678678
}

‎internal/sql/catalog/catalog.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
8282
case *ast.CompositeTypeStmt:
8383
err = c.createCompositeType(n)
8484

85+
case *ast.CreateDomainStmt:
86+
err = c.createDomain(n)
87+
8588
case *ast.CreateEnumStmt:
8689
err = c.createEnum(n)
8790

‎internal/sql/catalog/schema.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (s *Schema) getType(rel *ast.TypeName) (Type, int, error) {
8181
if typ.Name == rel.Name {
8282
return s.Types[i], i, nil
8383
}
84+
case *Domain:
85+
if typ.Name == rel.Name {
86+
return s.Types[i], i, nil
87+
}
8488
}
8589
}
8690
return nil, -1, sqlerr.TypeNotFound(rel.Name)

‎internal/sql/catalog/types.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
108108
return nil
109109
}
110110

111+
func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
112+
names := stringSlice(stmt.Domainname)
113+
if len(names) == 0 || stmt.TypeName == nil {
114+
return fmt.Errorf("create domain: missing name or type")
115+
}
116+
name := &ast.TypeName{Name: names[len(names)-1]}
117+
if len(names) > 1 {
118+
name.Schema = names[len(names)-2]
119+
}
120+
ns := name.Schema
121+
if ns == "" {
122+
ns = c.DefaultSchema
123+
}
124+
schema, err := c.getSchema(ns)
125+
if err != nil {
126+
return err
127+
}
128+
if _, _, err := schema.getType(name); err == nil {
129+
return sqlerr.TypeExists(name.Name)
130+
}
131+
domain := &Domain{
132+
Name: name.Name,
133+
Base: core.TypeExprOfTypeName(stmt.TypeName),
134+
}
135+
if stmt.Constraints != nil {
136+
for _, item := range stmt.Constraints.Items {
137+
if con, ok := item.(*ast.Constraint); ok && con.Contype == ast.ConstrTypeNotNull {
138+
domain.NotNull = true
139+
}
140+
}
141+
}
142+
schema.Types = append(schema.Types, domain)
143+
return nil
144+
}
145+
111146
func stringSlice(list *ast.List) []string {
112147
items := []string{}
113148
for _, item := range list.Items {

0 commit comments

Comments
 (0)