Skip to content

Commit 3163799

Browse files
authored
Merge pull request #10 from actiontech/fix-issue-1193
fix actiontech/sqle#1193
2 parents 8483c9f + d5d3594 commit 3163799

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

ast/dynamic.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -129,51 +129,19 @@ func (n *WhenNode) Scan(start *xml.StartElement) error {
129129
}
130130

131131
type OtherwiseNode struct {
132-
Data *MyBatisData
132+
*ChildrenNode
133133
}
134134

135135
func NewOtherwiseNode() *OtherwiseNode {
136-
return &OtherwiseNode{}
136+
n := &OtherwiseNode{}
137+
n.ChildrenNode = NewNode()
138+
return n
137139
}
138140

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

143-
func (n *OtherwiseNode) AddChildren(ns ...Node) error {
144-
err := fmt.Errorf(`<ohterwise> data is invalid`)
145-
if len(ns) != 1 {
146-
return err
147-
}
148-
switch d := ns[0].(type) {
149-
case *MyBatisData:
150-
n.Data = d
151-
default:
152-
return err
153-
}
154-
return nil
155-
}
156-
157-
func (n *OtherwiseNode) GetStmt(ctx *Context) (string, error) {
158-
// fix: https://github.com/actiontech/sqle/issues/563
159-
// the label <otherwise> may be empty.
160-
// <when>
161-
/*
162-
<when "case 1">
163-
case 1
164-
</when>
165-
<when "case 2">
166-
case 2
167-
</when>
168-
<otherwise> #no default case
169-
</otherwise>
170-
*/
171-
if n.Data != nil {
172-
return n.Data.GetStmt(ctx)
173-
}
174-
return "", nil
175-
}
176-
177145
type TrimNode struct {
178146
*ChildrenNode
179147
Name string

parser_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,3 +995,54 @@ func TestParserTrimSuffix_issue704(t *testing.T) {
995995
`, "INSERT INTO `agent` (`agent_no`,`agent_name`) VALUES (?,?);",
996996
)
997997
}
998+
999+
func TestOtherwise_issue1193(t *testing.T) {
1000+
testParser(t, `
1001+
<mapper namespace="Test">
1002+
<select id="testChoose">
1003+
SELECT
1004+
*
1005+
FROM
1006+
fruits
1007+
<where>
1008+
<choose>
1009+
<when test="name != null">
1010+
AND name = #{name}
1011+
</when>
1012+
<otherwise>
1013+
<if test="price != null and price !=''">
1014+
AND price = ${price}
1015+
</if>
1016+
</otherwise>
1017+
</choose>
1018+
</where>
1019+
</select>
1020+
</mapper>
1021+
`, "SELECT * FROM `fruits` WHERE `name`=? AND `price`=?;",
1022+
)
1023+
1024+
testParser(t, `
1025+
<mapper namespace="Test">
1026+
<select id="testChoose">
1027+
SELECT
1028+
*
1029+
FROM
1030+
fruits
1031+
<where>
1032+
<choose>
1033+
<when test="name != null">
1034+
AND name = #{name}
1035+
</when>
1036+
<otherwise>
1037+
<if test="price != null and price !=''">
1038+
AND price = ${price}
1039+
</if>
1040+
AND category = #{category}
1041+
</otherwise>
1042+
</choose>
1043+
</where>
1044+
</select>
1045+
</mapper>
1046+
`, "SELECT * FROM `fruits` WHERE `name`=? AND `price`=? AND `category`=?;",
1047+
)
1048+
}

0 commit comments

Comments
 (0)