Skip to content

Commit

Permalink
fix: Override types of aliased columns and named parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Haines <[email protected]>
  • Loading branch information
haines committed Apr 12, 2023
1 parent fd3ed9d commit effda1e
Show file tree
Hide file tree
Showing 13 changed files with 7,517 additions and 95 deletions.
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func pluginQueryColumn(c *compiler.Column) *plugin.Column {
}
out := &plugin.Column{
Name: c.Name,
OriginalName: c.OriginalName,
Comment: c.Comment,
NotNull: c.NotNull,
IsArray: c.IsArray,
Expand Down
12 changes: 10 additions & 2 deletions internal/codegen/golang/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ func addExtraGoStructTags(tags map[string]string, req *plugin.CodeGenRequest, co
// Different table.
continue
}
if !sdk.MatchString(oride.ColumnName, col.Name) {
cname := col.Name
if col.OriginalName != "" {
cname = col.OriginalName
}
if !sdk.MatchString(oride.ColumnName, cname) {
// Different column.
continue
}
Expand All @@ -31,8 +35,12 @@ func goType(req *plugin.CodeGenRequest, col *plugin.Column) string {
if oride.GoType.TypeName == "" {
continue
}
cname := col.Name
if col.OriginalName != "" {
cname = col.OriginalName
}
sameTable := sdk.Matches(oride, col.Table, req.Catalog.DefaultSchema)
if oride.Column != "" && sdk.MatchString(oride.ColumnName, col.Name) && sameTable {
if oride.Column != "" && sdk.MatchString(oride.ColumnName, cname) && sameTable {
return oride.GoType.TypeName
}
}
Expand Down
39 changes: 21 additions & 18 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,16 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
cname = *res.Name
}
cols = append(cols, &Column{
Name: cname,
Type: c.Type,
Scope: scope,
Table: c.Table,
TableAlias: t.Rel.Name,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
Name: cname,
OriginalName: c.Name,
Type: c.Type,
Scope: scope,
Table: c.Table,
TableAlias: t.Rel.Name,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
})
}
}
Expand Down Expand Up @@ -519,16 +520,18 @@ func outputColumnRefs(res *ast.ResTarget, tables []*Table, node *ast.ColumnRef)
if res.Name != nil {
cname = *res.Name
}

cols = append(cols, &Column{
Name: cname,
Type: c.Type,
Table: c.Table,
TableAlias: alias,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
EmbedTable: c.EmbedTable,
Name: cname,
OriginalName: c.Name,
Type: c.Type,
Table: c.Table,
TableAlias: alias,
DataType: c.DataType,
NotNull: c.NotNull,
IsArray: c.IsArray,
Length: c.Length,
EmbedTable: c.EmbedTable,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Table struct {

type Column struct {
Name string
OriginalName string
DataType string
NotNull bool
IsArray bool
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
OriginalName: c.Name,
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
IsArray: c.IsArray,
Expand Down Expand Up @@ -442,6 +443,7 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
Number: ref.ref.Number,
Column: &Column{
Name: p.Name(),
OriginalName: c.Name,
DataType: dataType(&c.Type),
NotNull: p.NotNull(),
IsArray: c.IsArray,
Expand Down
Loading

0 comments on commit effda1e

Please sign in to comment.