Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support VALUES call on update #7

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.swp
16 changes: 16 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,22 @@ func (p *Parser) ParseExpr() (expr Expr, err error) {
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 {
p.unlex()
var err error
expr, err = p.parseParenExpr()
if err != nil {
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

case IDENT, QIDENT:
ident := identByNameAndTok(lit, tok)
if p.peek() == DOT {
Expand Down
28 changes: 28 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,3 +1015,31 @@ func AssertParseExprError(tb testing.TB, s string, want string) {
tb.Fatalf("ParseExpr()=%q, want %q", err, want)
}
}

func TestSql_support_values_call(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`),`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`)"
stmt, err := sqlparser.NewParser(strings.NewReader(sql)).ParseStatement()
if err != nil {
t.Fatal(err)
}

insert := stmt.(*sqlparser.InsertStatement)
result := insert.String()
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")
}
}
Loading