Skip to content

Commit e608477

Browse files
Normalize PostgreSQL parse error reporting as we do Dolphin
Signed-off-by: Steve Coffman <[email protected]>
1 parent 8b88586 commit e608477

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

internal/engine/postgresql/parse.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"strings"
1111

1212
nodes "github.com/pganalyze/pg_query_go/v4"
13+
"github.com/pganalyze/pg_query_go/v4/parser"
1314

1415
"github.com/kyleconroy/sqlc/internal/metadata"
1516
"github.com/kyleconroy/sqlc/internal/sql/ast"
17+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1618
)
1719

1820
func stringSlice(list *nodes.List) []string {
@@ -158,7 +160,8 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
158160
}
159161
tree, err := nodes.Parse(string(contents))
160162
if err != nil {
161-
return nil, err
163+
pErr := normalizeErr(err)
164+
return nil, pErr
162165
}
163166

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

190+
func normalizeErr(err error) error {
191+
pErr := parser.Error{}
192+
if errors.As(err, &pErr) {
193+
sErr := &sqlerr.Error{
194+
Message: pErr.Message,
195+
Err: &pErr,
196+
Line: pErr.Lineno,
197+
Location: pErr.Cursorpos,
198+
}
199+
return sErr
200+
}
201+
return err
202+
}
203+
187204
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-COMMENTS
188205
func (p *Parser) CommentSyntax() metadata.CommentSyntax {
189206
return metadata.CommentSyntax{

0 commit comments

Comments
 (0)