Skip to content

Commit effda1e

Browse files
committed
fix: Override types of aliased columns and named parameters
Signed-off-by: Andrew Haines <andrew@haines.org.nz>
1 parent fd3ed9d commit effda1e

File tree

13 files changed

+7517
-95
lines changed

13 files changed

+7517
-95
lines changed

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
242242
}
243243
out := &plugin.Column{
244244
Name: c.Name,
245+
OriginalName: c.OriginalName,
245246
Comment: c.Comment,
246247
NotNull: c.NotNull,
247248
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
@@ -223,15 +223,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
223223
cname = *res.Name
224224
}
225225
cols = append(cols, &Column{
226-
Name: cname,
227-
Type: c.Type,
228-
Scope: scope,
229-
Table: c.Table,
230-
TableAlias: t.Rel.Name,
231-
DataType: c.DataType,
232-
NotNull: c.NotNull,
233-
IsArray: c.IsArray,
234-
Length: c.Length,
226+
Name: cname,
227+
OriginalName: c.Name,
228+
Type: c.Type,
229+
Scope: scope,
230+
Table: c.Table,
231+
TableAlias: t.Rel.Name,
232+
DataType: c.DataType,
233+
NotNull: c.NotNull,
234+
IsArray: c.IsArray,
235+
Length: c.Length,
235236
})
236237
}
237238
}
@@ -519,16 +520,18 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
519520
if res.Name != nil {
520521
cname = *res.Name
521522
}
523+
522524
cols = append(cols, &Column{
523-
Name: cname,
524-
Type: c.Type,
525-
Table: c.Table,
526-
TableAlias: alias,
527-
DataType: c.DataType,
528-
NotNull: c.NotNull,
529-
IsArray: c.IsArray,
530-
Length: c.Length,
531-
EmbedTable: c.EmbedTable,
525+
Name: cname,
526+
OriginalName: c.Name,
527+
Type: c.Type,
528+
Table: c.Table,
529+
TableAlias: alias,
530+
DataType: c.DataType,
531+
NotNull: c.NotNull,
532+
IsArray: c.IsArray,
533+
Length: c.Length,
534+
EmbedTable: c.EmbedTable,
532535
})
533536
}
534537
}

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)