Skip to content

Commit

Permalink
Merge pull request #9 from actiontech/fix_issue704
Browse files Browse the repository at this point in the history
Fix issue704
  • Loading branch information
sjjian authored Jul 28, 2022
2 parents ba839f4 + f1dc755 commit 8483c9f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 10 additions & 5 deletions ast/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ func (n *ChooseNode) GetStmt(ctx *Context) (string, error) {
// https://github.com/actiontech/sqle/issues/639
// otherwise can be not defined. so ChooseNode -> Otherwise may be nil.
/*
<choose>
<when test="state == 1">
where name = #{name1}
</when>
</choose>
<choose>
<when test="state == 1">
where name = #{name1}
</when>
</choose>
*/
if n.Otherwise != nil {
data, err := n.Otherwise.GetStmt(ctx)
Expand Down Expand Up @@ -178,6 +178,7 @@ type TrimNode struct {
*ChildrenNode
Name string
Prefix string
Suffix string
PrefixOverrides []string
SuffixOverrides []string
}
Expand All @@ -202,6 +203,9 @@ func (n *TrimNode) Scan(start *xml.StartElement) error {
if attr.Name.Local == "prefix" {
n.Prefix = attr.Value
}
if attr.Name.Local == "suffix" {
n.Suffix = attr.Value
}
if attr.Name.Local == "prefixOverrides" {
n.PrefixOverrides = strings.Split(attr.Value, "|")
}
Expand Down Expand Up @@ -233,6 +237,7 @@ func (n *TrimNode) GetStmt(ctx *Context) (string, error) {
buff.WriteString(n.Prefix)
buff.WriteString(" ")
buff.WriteString(body)
buff.WriteString(n.Suffix)
return buff.String(), nil
}

Expand Down
33 changes: 32 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,4 +963,35 @@ func TestParserChoose_issue639(t *testing.T) {
</mapper>`,
"SELECT `name`,`category`,`price` FROM `fruits` WHERE `name`=? AND `category`=? AND `price`=? AND `name`=1;",
)
}
}

func TestParserTrimSuffix_issue704(t *testing.T) {
testParser(t, `
<mapper namespace="Test">
<insert id="insertSelective" parameterType="com.paylabs.merchant.pojo.Agent">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into agent
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="agentNo != null">
agent_no,
</if>
<if test="agentName != null">
agent_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="agentNo != null">
#{agentNo,jdbcType=VARCHAR},
</if>
<if test="agentName != null">
#{agentName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
</mapper>
`, "INSERT INTO `agent` (`agent_no`,`agent_name`) VALUES (?,?);",
)
}

0 comments on commit 8483c9f

Please sign in to comment.