Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyq101 committed Oct 17, 2024
1 parent eb949f7 commit d9b18f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
20 changes: 18 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sqlparser_test

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -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")
}
}

0 comments on commit d9b18f8

Please sign in to comment.