diff --git a/go.mod b/go.mod index d592265c61..11bc4c0951 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/jinzhu/inflection v1.0.0 github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.16 - github.com/pganalyze/pg_query_go/v4 v4.2.0 + github.com/pganalyze/pg_query_go/v4 v4.2.1 github.com/riza-io/grpc-go v0.2.0 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 530542a3e4..d7c5ac6767 100644 --- a/go.sum +++ b/go.sum @@ -118,8 +118,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= -github.com/pganalyze/pg_query_go/v4 v4.2.0 h1:67hSBZXYfABNYisEu/Xfu6R2gupnQwaoRhQicy0HSnQ= -github.com/pganalyze/pg_query_go/v4 v4.2.0/go.mod h1:aEkDNOXNM5j0YGzaAapwJ7LB3dLNj+bvbWcLv1hOVqA= +github.com/pganalyze/pg_query_go/v4 v4.2.1 h1:id/vuyIQccb9f6Yx3pzH5l4QYrxE3v6/m8RPlgMrprc= +github.com/pganalyze/pg_query_go/v4 v4.2.1/go.mod h1:aEkDNOXNM5j0YGzaAapwJ7LB3dLNj+bvbWcLv1hOVqA= github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 h1:+FZIDR/D97YOPik4N4lPDaUcLDF/EQPogxtlHB2ZZRM= github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg= diff --git a/internal/endtoend/testdata/syntax_errors/postgresql/stderr.txt b/internal/endtoend/testdata/syntax_errors/postgresql/stderr.txt index dc5dd9f3b1..8f766d68a4 100644 --- a/internal/endtoend/testdata/syntax_errors/postgresql/stderr.txt +++ b/internal/endtoend/testdata/syntax_errors/postgresql/stderr.txt @@ -1,4 +1,4 @@ # package querytest -query/from.sql:1:1: syntax error at or near "from" -query/select.sql:1:1: syntax error at or near "select" -query/typo.sql:1:1: syntax error at or near "selectt" +query/from.sql:2:35: syntax error at or near "from" +query/select.sql:2:29: syntax error at or near "select" +query/typo.sql:2:2: syntax error at or near "selectt" diff --git a/internal/engine/postgresql/parse.go b/internal/engine/postgresql/parse.go index 7ee35cb589..fbad31bd29 100644 --- a/internal/engine/postgresql/parse.go +++ b/internal/engine/postgresql/parse.go @@ -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 { @@ -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 @@ -184,6 +187,21 @@ func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) { return stmts, nil } +func normalizeErr(err error) error { + //TODO: errors.As complains that *parser.Error does not implement error + if pErr, ok := err.(*parser.Error); ok { + 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{