Skip to content

Commit 67293b7

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

File tree

13 files changed

+7518
-96
lines changed

13 files changed

+7518
-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
@@ -241,15 +241,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
241241
cname = *res.Name
242242
}
243243
cols = append(cols, &Column{
244-
Name: cname,
245-
Type: c.Type,
246-
Scope: scope,
247-
Table: c.Table,
248-
TableAlias: t.Rel.Name,
249-
DataType: c.DataType,
250-
NotNull: c.NotNull,
251-
IsArray: c.IsArray,
252-
Length: c.Length,
244+
Name: cname,
245+
OriginalName: c.Name,
246+
Type: c.Type,
247+
Scope: scope,
248+
Table: c.Table,
249+
TableAlias: t.Rel.Name,
250+
DataType: c.DataType,
251+
NotNull: c.NotNull,
252+
IsArray: c.IsArray,
253+
Length: c.Length,
253254
})
254255
}
255256
}
@@ -537,16 +538,18 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
537538
if res.Name != nil {
538539
cname = *res.Name
539540
}
541+
540542
cols = append(cols, &Column{
541-
Name: cname,
542-
Type: c.Type,
543-
Table: c.Table,
544-
TableAlias: alias,
545-
DataType: c.DataType,
546-
NotNull: c.NotNull,
547-
IsArray: c.IsArray,
548-
Length: c.Length,
549-
EmbedTable: c.EmbedTable,
543+
Name: cname,
544+
OriginalName: c.Name,
545+
Type: c.Type,
546+
Table: c.Table,
547+
TableAlias: alias,
548+
DataType: c.DataType,
549+
NotNull: c.NotNull,
550+
IsArray: c.IsArray,
551+
Length: c.Length,
552+
EmbedTable: c.EmbedTable,
550553
})
551554
}
552555
}

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)