Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Override types of aliased columns and named parameters #1884

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,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 @@ -248,15 +248,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 @@ -544,16 +545,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