Skip to content

Commit

Permalink
Merge pull request #10 from actiontech/fix-issue-1193
Browse files Browse the repository at this point in the history
  • Loading branch information
sjjian authored Dec 13, 2022
2 parents 8483c9f + d5d3594 commit 3163799
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
40 changes: 4 additions & 36 deletions ast/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,51 +129,19 @@ func (n *WhenNode) Scan(start *xml.StartElement) error {
}

type OtherwiseNode struct {
Data *MyBatisData
*ChildrenNode
}

func NewOtherwiseNode() *OtherwiseNode {
return &OtherwiseNode{}
n := &OtherwiseNode{}
n.ChildrenNode = NewNode()
return n
}

func (n *OtherwiseNode) Scan(start *xml.StartElement) error {
return nil
}

func (n *OtherwiseNode) AddChildren(ns ...Node) error {
err := fmt.Errorf(`<ohterwise> data is invalid`)
if len(ns) != 1 {
return err
}
switch d := ns[0].(type) {
case *MyBatisData:
n.Data = d
default:
return err
}
return nil
}

func (n *OtherwiseNode) GetStmt(ctx *Context) (string, error) {
// fix: https://github.com/actiontech/sqle/issues/563
// the label <otherwise> may be empty.
// <when>
/*
<when "case 1">
case 1
</when>
<when "case 2">
case 2
</when>
<otherwise> #no default case
</otherwise>
*/
if n.Data != nil {
return n.Data.GetStmt(ctx)
}
return "", nil
}

type TrimNode struct {
*ChildrenNode
Name string
Expand Down
51 changes: 51 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,54 @@ func TestParserTrimSuffix_issue704(t *testing.T) {
`, "INSERT INTO `agent` (`agent_no`,`agent_name`) VALUES (?,?);",
)
}

func TestOtherwise_issue1193(t *testing.T) {
testParser(t, `
<mapper namespace="Test">
<select id="testChoose">
SELECT
*
FROM
fruits
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<otherwise>
<if test="price != null and price !=''">
AND price = ${price}
</if>
</otherwise>
</choose>
</where>
</select>
</mapper>
`, "SELECT * FROM `fruits` WHERE `name`=? AND `price`=?;",
)

testParser(t, `
<mapper namespace="Test">
<select id="testChoose">
SELECT
*
FROM
fruits
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<otherwise>
<if test="price != null and price !=''">
AND price = ${price}
</if>
AND category = #{category}
</otherwise>
</choose>
</where>
</select>
</mapper>
`, "SELECT * FROM `fruits` WHERE `name`=? AND `price`=? AND `category`=?;",
)
}

0 comments on commit 3163799

Please sign in to comment.