Skip to content

Commit 2ff3b63

Browse files
committed
fix: Override types of aliased columns and named parameters
Signed-off-by: Andrew Haines <[email protected]>
1 parent 9e13aa7 commit 2ff3b63

File tree

12 files changed

+5079
-88
lines changed

12 files changed

+5079
-88
lines changed

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
235235
}
236236
out := &plugin.Column{
237237
Name: c.Name,
238+
OriginalName: c.OriginalName,
238239
Comment: c.Comment,
239240
NotNull: c.NotNull,
240241
IsArray: c.IsArray,

internal/codegen/golang/go_type.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
1414
// Different table.
1515
continue
1616
}
17-
if !sdk.MatchString(oride.ColumnName, col.Name) {
17+
cname := col.Name
18+
if col.OriginalName != "" {
19+
cname = col.OriginalName
20+
}
21+
if !sdk.MatchString(oride.ColumnName, cname) {
1822
// Different column.
1923
continue
2024
}
@@ -31,8 +35,12 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
3135
if oride.GoType.TypeName == "" {
3236
continue
3337
}
38+
cname := col.Name
39+
if col.OriginalName != "" {
40+
cname = col.OriginalName
41+
}
3442
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
35-
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
43+
if oride.Column != "" && sdk.MatchString(oride.ColumnName, cname) && sameTable {
3644
return oride.GoType.TypeName
3745
}
3846
}

internal/compiler/output_columns.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
190190
cname = *res.Name
191191
}
192192
cols = append(cols, &Column{
193-
Name: cname,
194-
Type: c.Type,
195-
Scope: scope,
196-
Table: c.Table,
197-
TableAlias: t.Rel.Name,
198-
DataType: c.DataType,
199-
NotNull: c.NotNull,
200-
IsArray: c.IsArray,
201-
Length: c.Length,
193+
Name: cname,
194+
OriginalName: c.Name,
195+
Type: c.Type,
196+
Scope: scope,
197+
Table: c.Table,
198+
TableAlias: t.Rel.Name,
199+
DataType: c.DataType,
200+
NotNull: c.NotNull,
201+
IsArray: c.IsArray,
202+
Length: c.Length,
202203
})
203204
}
204205
}
@@ -486,15 +487,17 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
486487
if res.Name != nil {
487488
cname = *res.Name
488489
}
490+
489491
cols = append(cols, &Column{
490-
Name: cname,
491-
Type: c.Type,
492-
Table: c.Table,
493-
TableAlias: alias,
494-
DataType: c.DataType,
495-
NotNull: c.NotNull,
496-
IsArray: c.IsArray,
497-
Length: c.Length,
492+
Name: cname,
493+
OriginalName: c.Name,
494+
Type: c.Type,
495+
Table: c.Table,
496+
TableAlias: alias,
497+
DataType: c.DataType,
498+
NotNull: c.NotNull,
499+
IsArray: c.IsArray,
500+
Length: c.Length,
498501
})
499502
}
500503
}

internal/compiler/query.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Table struct {
1616

1717
type Column struct {
1818
Name string
19+
OriginalName string
1920
DataType string
2021
NotNull bool
2122
IsArray bool

internal/compiler/resolve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
189189
Number: ref.ref.Number,
190190
Column: &Column{
191191
Name: p.Name(),
192+
OriginalName: c.Name,
192193
DataType: dataType(&c.Type),
193194
NotNull: p.NotNull(),
194195
IsArray: c.IsArray,
@@ -413,6 +414,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
413414
Number: ref.ref.Number,
414415
Column: &Column{
415416
Name: p.Name(),
417+
OriginalName: c.Name,
416418
DataType: dataType(&c.Type),
417419
NotNull: p.NotNull(),
418420
IsArray: c.IsArray,

0 commit comments

Comments
 (0)