Skip to content

Commit

Permalink
Fix for INTO keyword is optional in INSERT statement for MySQL sy…
Browse files Browse the repository at this point in the history
…ntax.

* mysql insert can ignore keyword `into`

* Update parser.go

---------

Co-authored-by: Jason Lee <[email protected]>
  • Loading branch information
sunfuze and huacnlee authored Jan 9, 2024
1 parent 02d9768 commit c96041c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 6 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"strings"
)

var (
// ErrNotImplemented not implemented
ErrNotImplemented = errors.New("not implemented")
)
// ErrNotImplemented not implemented
var ErrNotImplemented = errors.New("not implemented")

// Parser represents a SQL parser.
type Parser struct {
Expand Down Expand Up @@ -97,10 +95,11 @@ func (p *Parser) parseInsertStatement() (_ *InsertStatement, err error) {

var stmt InsertStatement

if p.peek() != INTO {
return &stmt, p.errorExpected(p.pos, p.tok, "INTO")
// In MySQL the `INSERT INTO`, the `INTO` is optional.
// https://dev.mysql.com/doc/refman/8.0/en/insert.html
if p.peek() == INTO {
p.lex()
}
p.lex()

// Parse table name & optional alias.
if stmt.TableName, err = p.parseTableName(); err != nil {
Expand Down Expand Up @@ -967,7 +966,6 @@ func (p *Parser) parseBinaryExpr(prec1 int) (expr Expr, err error) {
x = &BinaryExpr{X: x, Op: op, Y: y}
}
}

}

func (p *Parser) parseExprs() (_ *Exprs, err error) {
Expand Down
3 changes: 2 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/go-test/deep"

"github.com/longbridgeapp/sqlparser"
)

Expand Down Expand Up @@ -468,7 +469,7 @@ func TestParser_ParseStatement(t *testing.T) {
},
})

AssertParseStatementError(t, `INSERT`, `1:6: expected INTO, found 'EOF'`)
AssertParseStatementError(t, `INSERT`, `1:6: expected table name, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO`, `1:11: expected table name, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl AS`, `1:18: expected table alias, found 'EOF'`)
AssertParseStatementError(t, `INSERT INTO tbl `, `1:16: expected DEFAULT VALUES, VALUES or SELECT, found 'EOF'`)
Expand Down

0 comments on commit c96041c

Please sign in to comment.