Skip to content

Commit 8f72da7

Browse files
committed
Add syntax error tests
1 parent 86b9796 commit 8f72da7

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

internal/dolphin/parse.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package dolphin
22

33
import (
4+
"errors"
45
"io"
56
"io/ioutil"
7+
"regexp"
8+
"strconv"
69
"strings"
710

8-
"github.com/davecgh/go-spew/spew"
911
"github.com/pingcap/parser"
1012
_ "github.com/pingcap/tidb/types/parser_driver"
1113

1214
"github.com/kyleconroy/sqlc/internal/metadata"
1315
"github.com/kyleconroy/sqlc/internal/sql/ast"
16+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1417
)
1518

1619
func NewParser() *Parser {
@@ -21,15 +24,39 @@ type Parser struct {
2124
pingcap *parser.Parser
2225
}
2326

27+
var lineColumn = regexp.MustCompile(`^line (\d+) column (\d+) (.*)`)
28+
29+
func normalizeErr(err error) error {
30+
if err == nil {
31+
return err
32+
}
33+
parts := strings.Split(err.Error(), "\n")
34+
msg := strings.TrimSpace(parts[0] + "\"")
35+
out := lineColumn.FindStringSubmatch(msg)
36+
if len(out) == 4 {
37+
line, lineErr := strconv.Atoi(out[1])
38+
col, colErr := strconv.Atoi(out[2])
39+
if lineErr != nil || colErr != nil {
40+
return errors.New(msg)
41+
}
42+
return &sqlerr.Error{
43+
Message: "syntax error",
44+
Err: errors.New(out[3]),
45+
Line: line,
46+
Column: col,
47+
}
48+
}
49+
return errors.New(msg)
50+
}
51+
2452
func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
2553
blob, err := ioutil.ReadAll(r)
2654
if err != nil {
2755
return nil, err
2856
}
2957
stmtNodes, _, err := p.pingcap.Parse(string(blob), "", "")
3058
if err != nil {
31-
spew.Dump(err)
32-
return nil, err
59+
return nil, normalizeErr(err)
3360
}
3461
var stmts []ast.Statement
3562
for i := range stmtNodes {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"experimental_parser_only": true
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# package querytest
2+
query/from.sql:2:38: syntax error near "from where id = ?;"
3+
query/select.sql:2:34: syntax error near "select id;"
4+
query/typo.sql:2:8: syntax error near "selectt id, first_name from users;"

internal/multierr/error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/kyleconroy/sqlc/internal/pg"
77
"github.com/kyleconroy/sqlc/internal/source"
8+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
89
)
910

1011
type FileError struct {
@@ -30,6 +31,14 @@ func (e *Error) Add(filename, in string, loc int, err error) {
3031
loc = lerr.Location
3132
}
3233
}
34+
if lerr, ok := err.(*sqlerr.Error); ok {
35+
if lerr.Location != 0 {
36+
loc = lerr.Location
37+
} else if lerr.Line != 0 && lerr.Column != 0 {
38+
line = lerr.Line
39+
column = lerr.Column
40+
}
41+
}
3342
if in != "" && loc != 0 {
3443
line, column = source.LineNumber(in, loc)
3544
}

internal/sql/sqlerr/errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type Error struct {
1414
Code string
1515
Message string
1616
Location int
17+
Line int
18+
Column int
1719
// Hint string
1820
}
1921

0 commit comments

Comments
 (0)