Skip to content

Commit

Permalink
Normalize PostgreSQL parse error reporting as we do Dolphin
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
StevenACoffman committed May 28, 2023
1 parent 8b88586 commit e608477
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion internal/engine/postgresql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strings"

nodes "github.com/pganalyze/pg_query_go/v4"
"github.com/pganalyze/pg_query_go/v4/parser"

"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/sql/ast"
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
)

func stringSlice(list *nodes.List) []string {
Expand Down Expand Up @@ -158,7 +160,8 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
}
tree, err := nodes.Parse(string(contents))
if err != nil {
return nil, err
pErr := normalizeErr(err)
return nil, pErr
}

var stmts []ast.Statement
Expand All @@ -184,6 +187,20 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
return stmts, nil
}

func normalizeErr(err error) error {
pErr := parser.Error{}
if errors.As(err, &pErr) {
sErr := &sqlerr.Error{
Message: pErr.Message,
Err: &pErr,
Line: pErr.Lineno,
Location: pErr.Cursorpos,
}
return sErr
}
return err
}

// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-COMMENTS
func (p *Parser) CommentSyntax() metadata.CommentSyntax {
return metadata.CommentSyntax{
Expand Down

0 comments on commit e608477

Please sign in to comment.