Skip to content

Commit

Permalink
support values fn call
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyq101 committed Oct 17, 2024
1 parent d0078f9 commit fbb44b2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
15 changes: 15 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (*StringLit) node() {}
func (*TableName) node() {}
func (*Type) node() {}
func (*UnaryExpr) node() {}
func (*FnCallExpr) node() {}
func (*UpdateStatement) node() {}
func (*UpsertClause) node() {}
func (*UsingConstraint) node() {}
Expand Down Expand Up @@ -93,6 +94,7 @@ func (*QualifiedRef) expr() {}
func (*Range) expr() {}
func (*StringLit) expr() {}
func (*UnaryExpr) expr() {}
func (*FnCallExpr) expr() {}

// ExprString returns the string representation of expr.
// Returns a blank string if expr is nil.
Expand Down Expand Up @@ -362,6 +364,19 @@ func (expr *UnaryExpr) String() string {
}
}

type FnCallExpr struct {
Fn Token // function
X Expr // param expression
}

func (expr *FnCallExpr) String() string {
paran, ok := expr.X.(*ParenExpr)
if ok {
return expr.Fn.String() + "(" + paran.X.String() + ")"
}
return expr.Fn.String() + "(" + expr.X.String() + ")"
}

type BinaryExpr struct {
X Expr // lhs
Op Token // operator
Expand Down
22 changes: 14 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sqlparser

import (
"errors"
"fmt"
"io"
"strings"
)
Expand Down Expand Up @@ -221,8 +220,6 @@ func (p *Parser) parseUpsertClause() (_ *UpsertClause, err error) {
return &clause, p.errorExpected(p.pos, p.tok, "CONFLICT or DUPLICATE")
}
p.lex()
fmt.Printf("-------- on duplicate key: %v\n", clause.DuplicateKey)

// Parse optional indexed column list & WHERE conditional.
if p.peek() == LP {
p.lex()
Expand Down Expand Up @@ -937,12 +934,21 @@ func (p *Parser) ParseExpr() (expr Expr, err error) {

func (p *Parser) parseOperand() (expr Expr, err error) {
_, tok, lit := p.lex()
//////////////////
if tok == VALUES {
_, 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
}
}
return &FnCallExpr{Fn: VALUES, X: expr}, nil

case IDENT, QIDENT:
ident := identByNameAndTok(lit, tok)
if p.peek() == DOT {
Expand Down
6 changes: 5 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlparser_test

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -1018,8 +1019,11 @@ func AssertParseExprError(tb testing.TB, s string, want string) {

func TestSql_1(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`)"
_, err := sqlparser.NewParser(strings.NewReader(sql)).ParseStatement()
stmt, err := sqlparser.NewParser(strings.NewReader(sql)).ParseStatement()
if err != nil {
t.Fatal(err)
}
insert := stmt.(*sqlparser.InsertStatement)
result := insert.String()
fmt.Println(result)
}

0 comments on commit fbb44b2

Please sign in to comment.