Skip to content

Commit 99ecfff

Browse files
authored
fix: Override types of aliased columns and named parameters (#1884)
Signed-off-by: Andrew Haines <andrew@haines.org.nz>
1 parent ee391e6 commit 99ecfff

File tree

13 files changed

+7526
-96
lines changed

13 files changed

+7526
-96
lines changed

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
243243
}
244244
out := &plugin.Column{
245245
Name: c.Name,
246+
OriginalName: c.OriginalName,
246247
Comment: c.Comment,
247248
NotNull: c.NotNull,
248249
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: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
248248
cname = *res.Name
249249
}
250250
cols = append(cols, &Column{
251-
Name: cname,
252-
Type: c.Type,
253-
Scope: scope,
254-
Table: c.Table,
255-
TableAlias: t.Rel.Name,
256-
DataType: c.DataType,
257-
NotNull: c.NotNull,
258-
IsArray: c.IsArray,
259-
Length: c.Length,
251+
Name: cname,
252+
OriginalName: c.Name,
253+
Type: c.Type,
254+
Scope: scope,
255+
Table: c.Table,
256+
TableAlias: t.Rel.Name,
257+
DataType: c.DataType,
258+
NotNull: c.NotNull,
259+
IsArray: c.IsArray,
260+
Length: c.Length,
260261
})
261262
}
262263
}
@@ -544,16 +545,18 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
544545
if res.Name != nil {
545546
cname = *res.Name
546547
}
548+
547549
cols = append(cols, &Column{
548-
Name: cname,
549-
Type: c.Type,
550-
Table: c.Table,
551-
TableAlias: alias,
552-
DataType: c.DataType,
553-
NotNull: c.NotNull,
554-
IsArray: c.IsArray,
555-
Length: c.Length,
556-
EmbedTable: c.EmbedTable,
550+
Name: cname,
551+
OriginalName: c.Name,
552+
Type: c.Type,
553+
Table: c.Table,
554+
TableAlias: alias,
555+
DataType: c.DataType,
556+
NotNull: c.NotNull,
557+
IsArray: c.IsArray,
558+
Length: c.Length,
559+
EmbedTable: c.EmbedTable,
557560
})
558561
}
559562
}

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
@@ -212,6 +212,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
212212
Number: ref.ref.Number,
213213
Column: &Column{
214214
Name: p.Name(),
215+
OriginalName: c.Name,
215216
DataType: dataType(&c.Type),
216217
NotNull: p.NotNull(),
217218
IsArray: c.IsArray,
@@ -442,6 +443,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
442443
Number: ref.ref.Number,
443444
Column: &Column{
444445
Name: p.Name(),
446+
OriginalName: c.Name,
445447
DataType: dataType(&c.Type),
446448
NotNull: p.NotNull(),
447449
IsArray: c.IsArray,

0 commit comments

Comments
 (0)