Skip to content

Commit 8483c9f

Browse files
authored
Merge pull request #9 from actiontech/fix_issue704
Fix issue704
2 parents ba839f4 + f1dc755 commit 8483c9f

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

ast/dynamic.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ func (n *ChooseNode) GetStmt(ctx *Context) (string, error) {
9292
// https://github.com/actiontech/sqle/issues/639
9393
// otherwise can be not defined. so ChooseNode -> Otherwise may be nil.
9494
/*
95-
<choose>
96-
<when test="state == 1">
97-
where name = #{name1}
98-
</when>
99-
</choose>
95+
<choose>
96+
<when test="state == 1">
97+
where name = #{name1}
98+
</when>
99+
</choose>
100100
*/
101101
if n.Otherwise != nil {
102102
data, err := n.Otherwise.GetStmt(ctx)
@@ -178,6 +178,7 @@ type TrimNode struct {
178178
*ChildrenNode
179179
Name string
180180
Prefix string
181+
Suffix string
181182
PrefixOverrides []string
182183
SuffixOverrides []string
183184
}
@@ -202,6 +203,9 @@ func (n *TrimNode) Scan(start *xml.StartElement) error {
202203
if attr.Name.Local == "prefix" {
203204
n.Prefix = attr.Value
204205
}
206+
if attr.Name.Local == "suffix" {
207+
n.Suffix = attr.Value
208+
}
205209
if attr.Name.Local == "prefixOverrides" {
206210
n.PrefixOverrides = strings.Split(attr.Value, "|")
207211
}
@@ -233,6 +237,7 @@ func (n *TrimNode) GetStmt(ctx *Context) (string, error) {
233237
buff.WriteString(n.Prefix)
234238
buff.WriteString(" ")
235239
buff.WriteString(body)
240+
buff.WriteString(n.Suffix)
236241
return buff.String(), nil
237242
}
238243

parser_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,4 +963,35 @@ func TestParserChoose_issue639(t *testing.T) {
963963
</mapper>`,
964964
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `name`=? AND `category`=? AND `price`=? AND `name`=1;",
965965
)
966-
}
966+
}
967+
968+
func TestParserTrimSuffix_issue704(t *testing.T) {
969+
testParser(t, `
970+
<mapper namespace="Test">
971+
<insert id="insertSelective" parameterType="com.paylabs.merchant.pojo.Agent">
972+
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
973+
SELECT LAST_INSERT_ID()
974+
</selectKey>
975+
insert into agent
976+
<trim prefix="(" suffix=")" suffixOverrides=",">
977+
<if test="agentNo != null">
978+
agent_no,
979+
</if>
980+
<if test="agentName != null">
981+
agent_name,
982+
</if>
983+
</trim>
984+
985+
<trim prefix="values (" suffix=")" suffixOverrides=",">
986+
<if test="agentNo != null">
987+
#{agentNo,jdbcType=VARCHAR},
988+
</if>
989+
<if test="agentName != null">
990+
#{agentName,jdbcType=VARCHAR},
991+
</if>
992+
</trim>
993+
</insert>
994+
</mapper>
995+
`, "INSERT INTO `agent` (`agent_no`,`agent_name`) VALUES (?,?);",
996+
)
997+
}

0 commit comments

Comments
 (0)