From d9b18f8ecc2443cc810925d677b9e1ec4a6eb38f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E8=8B=B1=E5=BC=BA?= Date: Thu, 17 Oct 2024 13:41:21 +0800 Subject: [PATCH] add tests --- parser.go | 3 ++- parser_test.go | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/parser.go b/parser.go index 05d4ca2..d425df3 100644 --- a/parser.go +++ b/parser.go @@ -936,7 +936,6 @@ func (p *Parser) parseOperand() (expr Expr, err error) { _, tok, lit := p.lex() switch tok { case VALUES: - // 左括号 _, tok, lit = p.lex() var expr Expr if tok == LP { @@ -947,6 +946,8 @@ func (p *Parser) parseOperand() (expr Expr, err error) { return nil, err } expr = expr.(*ParenExpr).X + } else { + return nil, p.errorExpected(p.pos, p.tok, "left paren") } return &Call{Name: &Ident{Name: "VALUES"}, Args: []Expr{expr}}, nil diff --git a/parser_test.go b/parser_test.go index 1373777..42e27fc 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1,7 +1,6 @@ package sqlparser_test import ( - "fmt" "strings" "testing" @@ -1023,7 +1022,24 @@ func TestSql_support_values_call(t *testing.T) { if err != nil { t.Fatal(err) } + insert := stmt.(*sqlparser.InsertStatement) result := insert.String() - fmt.Println(result) + expected := "INSERT INTO `daily_asset` (`trade_date`, `fund_account`, `client_id`, `day_income`, `hold_income`, `acc_income`, `day_income_ratio`, `acc_income_sw_ratio`, `acc_income_nav_ratio`, `market_value`, `fund_asset`, `total_asset`, `sw_total_net`, `bank_transfer_in`, `bank_transfer_out`, `acc_bank_transfer_in`, `acc_bank_transfer_out`, `net_in_balance`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `fund_asset` = VALUES(`fund_asset`), `hold_income` = VALUES(`hold_income`), `market_value` = VALUES(`market_value`), `total_asset` = VALUES(`total_asset`), `bank_transfer_in` = VALUES(`bank_transfer_in`), `bank_transfer_out` = VALUES(`bank_transfer_out`), `net_in_balance` = VALUES(`net_in_balance`)" + if result != expected { + t.Fatal("sql not equal") + } +} + +func TestSql_support_values_error(t *testing.T) { + sql := "INSERT INTO `daily_asset` (`trade_date`,`fund_account`,`client_id`,`day_income`,`hold_income`,`acc_income`,`day_income_ratio`,`acc_income_sw_ratio`,`acc_income_nav_ratio`,`market_value`,`fund_asset`,`total_asset`,`sw_total_net`,`bank_transfer_in`,`bank_transfer_out`,`acc_bank_transfer_in`,`acc_bank_transfer_out`,`net_in_balance`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE `fund_asset`=VALUES `fund_asset`" + _, err := sqlparser.NewParser(strings.NewReader(sql)).ParseStatement() + if err == nil { + t.Fatal(err) + } + msg := err.Error() + expected := "1:422: expected left paren, found `fund_asset`" + if msg != expected { + t.Fatal("err msg not equal") + } }