Skip to content
Open
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
10 changes: 8 additions & 2 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
name = *res.Name
}
// TODO Validate column names
col := toColumn(tc.TypeName)
col, err := toColumn(tc.TypeName)
if err != nil {
return nil, err
}
col.Name = name
cols = append(cols, col)
} else if aconst, ok := n.Defresult.(*ast.A_Const); ok {
Expand Down Expand Up @@ -358,7 +361,10 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
name = *res.Name
}
// TODO Validate column names
col := toColumn(n.TypeName)
col, err := toColumn(n.TypeName)
if err != nil {
return nil, err
}
col.Name = name
// TODO Add correct, real type inference
if constant, ok := n.Arg.(*ast.A_Const); ok {
Expand Down
5 changes: 4 additions & 1 deletion internal/compiler/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,10 @@ func (comp *Compiler) resolveCatalogRefs(qc *QueryCatalog, rvs []*ast.RangeVar,
if n.TypeName == nil {
return nil, fmt.Errorf("*ast.TypeCast has nil type name")
}
col := toColumn(n.TypeName)
col, err := toColumn(n.TypeName)
if err != nil {
return nil, err
}
defaultP := named.NewInferredParam(col.Name, col.NotNull)
p, _ := params.FetchMerge(ref.ref.Number, defaultP)

Expand Down
9 changes: 5 additions & 4 deletions internal/compiler/to_column.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"fmt"
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
Expand All @@ -14,13 +15,13 @@ func arrayDims(n *ast.TypeName) int {
return len(n.ArrayBounds.Items)
}

func toColumn(n *ast.TypeName) *Column {
func toColumn(n *ast.TypeName) (*Column, error) {
if n == nil {
panic("can't build column for nil type name")
return nil, fmt.Errorf("can't build column for nil type name")
}
typ, err := ParseTypeName(n)
if err != nil {
panic("toColumn: " + err.Error())
return nil, fmt.Errorf("toColumn: %w", err)
}
arrayDims := arrayDims(n)
return &Column{
Expand All @@ -29,5 +30,5 @@ func toColumn(n *ast.TypeName) *Column {
NotNull: true, // XXX: How do we know if this should be null?
IsArray: arrayDims > 0,
ArrayDims: arrayDims,
}
}, nil
}
Loading