Skip to content

Commit f9d1b90

Browse files
Pass full column definition instead of just typename
1 parent 992cfa2 commit f9d1b90

File tree

4 files changed

+73
-67
lines changed

4 files changed

+73
-67
lines changed

Diff for: internal/cmd/shim.go

+34-39
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/sqlc-dev/sqlc/internal/config/convert"
77
"github.com/sqlc-dev/sqlc/internal/info"
88
"github.com/sqlc-dev/sqlc/internal/plugin"
9-
"github.com/sqlc-dev/sqlc/internal/sql/ast"
109
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
1110
)
1211

@@ -60,16 +59,38 @@ func pluginWASM(p config.Plugin) *plugin.Codegen_WASM {
6059
return nil
6160
}
6261

63-
func identifierSlice(types []*ast.TypeName) []*plugin.Identifier {
64-
ids := []*plugin.Identifier{}
65-
for _, typ := range types {
66-
ids = append(ids, &plugin.Identifier{
67-
Catalog: typ.Catalog,
68-
Schema: typ.Schema,
69-
Name: typ.Name,
62+
func columnSlice(cols []*catalog.Column, table *catalog.Table) []*plugin.Column {
63+
var columns []*plugin.Column
64+
for _, c := range cols {
65+
l := -1
66+
if c.Length != nil {
67+
l = *c.Length
68+
}
69+
var tableId *plugin.Identifier = nil
70+
if table != nil {
71+
tableId = &plugin.Identifier{
72+
Catalog: table.Rel.Catalog,
73+
Schema: table.Rel.Schema,
74+
Name: table.Rel.Name,
75+
}
76+
}
77+
columns = append(columns, &plugin.Column{
78+
Name: c.Name,
79+
Type: &plugin.Identifier{
80+
Catalog: c.Type.Catalog,
81+
Schema: c.Type.Schema,
82+
Name: c.Type.Name,
83+
},
84+
Comment: c.Comment,
85+
NotNull: c.IsNotNull,
86+
Unsigned: c.IsUnsigned,
87+
IsArray: c.IsArray,
88+
ArrayDims: int32(c.ArrayDims),
89+
Length: int32(l),
90+
Table: tableId,
7091
})
7192
}
72-
return ids
93+
return columns
7394
}
7495

7596
func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
@@ -87,47 +108,21 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
87108
})
88109
case *catalog.CompositeType:
89110
cts = append(cts, &plugin.CompositeType{
90-
Name: typ.Name,
91-
Comment: typ.Comment,
92-
ColTypeNames: identifierSlice(typ.ColTypeNames),
111+
Name: typ.Name,
112+
Comment: typ.Comment,
113+
Columns: columnSlice(typ.Columns, nil),
93114
})
94115
}
95116
}
96117
var tables []*plugin.Table
97118
for _, t := range s.Tables {
98-
var columns []*plugin.Column
99-
for _, c := range t.Columns {
100-
l := -1
101-
if c.Length != nil {
102-
l = *c.Length
103-
}
104-
columns = append(columns, &plugin.Column{
105-
Name: c.Name,
106-
Type: &plugin.Identifier{
107-
Catalog: c.Type.Catalog,
108-
Schema: c.Type.Schema,
109-
Name: c.Type.Name,
110-
},
111-
Comment: c.Comment,
112-
NotNull: c.IsNotNull,
113-
Unsigned: c.IsUnsigned,
114-
IsArray: c.IsArray,
115-
ArrayDims: int32(c.ArrayDims),
116-
Length: int32(l),
117-
Table: &plugin.Identifier{
118-
Catalog: t.Rel.Catalog,
119-
Schema: t.Rel.Schema,
120-
Name: t.Rel.Name,
121-
},
122-
})
123-
}
124119
tables = append(tables, &plugin.Table{
125120
Rel: &plugin.Identifier{
126121
Catalog: t.Rel.Catalog,
127122
Schema: t.Rel.Schema,
128123
Name: t.Rel.Name,
129124
},
130-
Columns: columns,
125+
Columns: columnSlice(t.Columns, t),
131126
Comment: t.Comment,
132127
})
133128
}

Diff for: internal/plugin/codegen.pb.go

+11-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: internal/sql/catalog/types.go

+27-15
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ func (e *Enum) isType() {
2828
}
2929

3030
type CompositeType struct {
31-
Name string
32-
ColTypeNames []*ast.TypeName
33-
Comment string
31+
Name string
32+
Columns []*Column
33+
Comment string
3434
}
3535

3636
func (ct *CompositeType) isType() {
@@ -102,11 +102,11 @@ func stringSlice(list *ast.List) []string {
102102
return items
103103
}
104104

105-
func columnTypeNamesSlice(list *ast.List) []*ast.TypeName {
106-
items := []*ast.TypeName{}
105+
func columnDefsSlice(list *ast.List) []*ast.ColumnDef {
106+
items := []*ast.ColumnDef{}
107107
for _, item := range list.Items {
108108
if n, ok := item.(*ast.ColumnDef); ok {
109-
items = append(items, n.TypeName)
109+
items = append(items, n)
110110
}
111111
}
112112
return items
@@ -146,10 +146,22 @@ func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt) error {
146146
if _, _, err := schema.getType(stmt.TypeName); err == nil {
147147
return sqlerr.TypeExists(tbl.Name)
148148
}
149-
schema.Types = append(schema.Types, &CompositeType{
150-
Name: stmt.TypeName.Name,
151-
ColTypeNames: columnTypeNamesSlice(stmt.ColDefList),
152-
})
149+
ct := &CompositeType{
150+
Name: stmt.TypeName.Name,
151+
}
152+
for _, col := range columnDefsSlice(stmt.ColDefList) {
153+
ct.Columns = append(ct.Columns, &Column{
154+
Name: col.Colname,
155+
Type: *col.TypeName,
156+
IsNotNull: col.IsNotNull,
157+
IsUnsigned: col.IsUnsigned,
158+
IsArray: col.IsArray,
159+
ArrayDims: col.ArrayDims,
160+
Comment: col.Comment,
161+
Length: col.Length,
162+
})
163+
}
164+
schema.Types = append(schema.Types, ct)
153165
return nil
154166
}
155167

@@ -350,9 +362,9 @@ func (c *Catalog) renameType(stmt *ast.RenameTypeStmt) error {
350362

351363
case *CompositeType:
352364
schema.Types[idx] = &CompositeType{
353-
Name: newName,
354-
ColTypeNames: typ.ColTypeNames,
355-
Comment: typ.Comment,
365+
Name: newName,
366+
Columns: typ.Columns,
367+
Comment: typ.Comment,
356368
}
357369

358370
case *Enum:
@@ -390,8 +402,8 @@ func (c *Catalog) updateTypeNames(typeUpdater func(t *ast.TypeName)) error {
390402
if !ok {
391403
continue
392404
}
393-
for _, fieldType := range composite.ColTypeNames {
394-
typeUpdater(fieldType)
405+
for _, fieldType := range composite.Columns {
406+
typeUpdater(&fieldType.Type)
395407
}
396408
}
397409
}

Diff for: protos/plugin/codegen.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ message Schema {
6161
message CompositeType {
6262
string name = 1;
6363
string comment = 2;
64-
repeated Identifier col_type_names = 3;
64+
repeated Column columns = 3;
6565
}
6666

6767
message Enum {

0 commit comments

Comments
 (0)