Skip to content

Commit e70cb01

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

File tree

5 files changed

+59
-40
lines changed

5 files changed

+59
-40
lines changed

Diff for: internal/cmd/shim.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,29 @@ func pluginWASM(p config.Plugin) *plugin.Codegen_WASM {
6060
return nil
6161
}
6262

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,
63+
func columnsSlice(columnDefs []*ast.ColumnDef) []*plugin.Column {
64+
var columns []*plugin.Column
65+
for _, c := range columnDefs {
66+
l := -1
67+
if c.Length != nil {
68+
l = *c.Length
69+
}
70+
columns = append(columns, &plugin.Column{
71+
Name: c.Colname,
72+
Type: &plugin.Identifier{
73+
Catalog: c.TypeName.Catalog,
74+
Schema: c.TypeName.Schema,
75+
Name: c.TypeName.Name,
76+
},
77+
Comment: c.Comment,
78+
NotNull: c.IsNotNull,
79+
Unsigned: c.IsUnsigned,
80+
IsArray: c.IsArray,
81+
ArrayDims: int32(c.ArrayDims),
82+
Length: int32(l),
7083
})
7184
}
72-
return ids
85+
return columns
7386
}
7487

7588
func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
@@ -87,9 +100,9 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
87100
})
88101
case *catalog.CompositeType:
89102
cts = append(cts, &plugin.CompositeType{
90-
Name: typ.Name,
91-
Comment: typ.Comment,
92-
ColTypeNames: identifierSlice(typ.ColTypeNames),
103+
Name: typ.Name,
104+
Comment: typ.Comment,
105+
Columns: columnsSlice(typ.Columns),
93106
})
94107
}
95108
}

Diff for: internal/codegen/golang/postgresql_type.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,20 @@ func postgresType(req *plugin.GenerateRequest, options *opts.Options, col *plugi
587587

588588
for _, ct := range schema.CompositeTypes {
589589
if rel.Name == ct.Name && rel.Schema == schema.Name {
590+
var ctName string
591+
if schema.Name == req.Catalog.DefaultSchema {
592+
ctName = StructName(ct.Name, options)
593+
} else {
594+
ctName = StructName(schema.Name+"_"+ct.Name, options)
595+
}
596+
590597
if notNull {
591-
return "string"
598+
return ctName
592599
}
593600
if emitPointersForNull {
594-
return "*string"
601+
return "*" + ctName
595602
}
596-
return "sql.NullString"
603+
return "Null" + ctName
597604
}
598605
}
599606
}

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

+13-13
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 []*ast.ColumnDef
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
@@ -147,8 +147,8 @@ func (c *Catalog) createCompositeType(stmt *ast.CompositeTypeStmt) error {
147147
return sqlerr.TypeExists(tbl.Name)
148148
}
149149
schema.Types = append(schema.Types, &CompositeType{
150-
Name: stmt.TypeName.Name,
151-
ColTypeNames: columnTypeNamesSlice(stmt.ColDefList),
150+
Name: stmt.TypeName.Name,
151+
Columns: columnDefsSlice(stmt.ColDefList),
152152
})
153153
return nil
154154
}
@@ -350,9 +350,9 @@ func (c *Catalog) renameType(stmt *ast.RenameTypeStmt) error {
350350

351351
case *CompositeType:
352352
schema.Types[idx] = &CompositeType{
353-
Name: newName,
354-
ColTypeNames: typ.ColTypeNames,
355-
Comment: typ.Comment,
353+
Name: newName,
354+
Columns: typ.Columns,
355+
Comment: typ.Comment,
356356
}
357357

358358
case *Enum:
@@ -390,8 +390,8 @@ func (c *Catalog) updateTypeNames(typeUpdater func(t *ast.TypeName)) error {
390390
if !ok {
391391
continue
392392
}
393-
for _, fieldType := range composite.ColTypeNames {
394-
typeUpdater(fieldType)
393+
for _, fieldType := range composite.Columns {
394+
typeUpdater(fieldType.TypeName)
395395
}
396396
}
397397
}

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)