Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(mybatis): support mybatis xml `foreach` dynamic sql node (#5876)

h3n4leb7d932dc45e
diff --git a/backend/plugin/parser/mybatis/ast/dymamic_node.go b/backend/plugin/parser/mybatis/ast/dymamic_node.go
index 69f719bd1a..b6953d6dac 100644
--- a/backend/plugin/parser/mybatis/ast/dymamic_node.go
+++ b/backend/plugin/parser/mybatis/ast/dymamic_node.go
@@ -17,6 +17,7 @@ var (
_ Node = (*WhereNode)(nil)
_ Node = (*SetNode)(nil)
_ Node = (*TrimNode)(nil)
+ _ Node = (*ForEachNode)(nil)
)
// IfNode represents a if node in mybatis mapper xml likes <if test="condition">...</if>.
@@ -297,3 +298,87 @@ func (n *SetNode) RestoreSQL(w io.Writer) error {
func (n *SetNode) AddChild(child Node) {
n.trimNode.AddChild(child)
}
+
+// ForEachNode represents a foreach node in mybatis mapper xml likes <foreach collection="collection" item="item" index="index" open="open" close="close" separator="separator">...</foreach>.
+type ForEachNode struct {
+ Collection string
+ Item string
+ Separator string
+ Open string
+ Close string
+ Index string
+ Children []Node
+}
+
+// NewForeachNode creates a new foreach node.
+func NewForeachNode(startElement *xml.StartElement) *ForEachNode {
+ var eachNode ForEachNode
+ for _, attr := range startElement.Attr {
+ switch attr.Name.Local {
+ case "collection":
+ eachNode.Collection = attr.Value
+ case "item":
+ eachNode.Item = attr.Value
+ case "index":
+ eachNode.Index = attr.Value
+ case "open":
+ eachNode.Open = attr.Value
+ case "close":
+ eachNode.Close = attr.Value
+ case "separator":
+ eachNode.Separator = attr.Value
+ }
+ }
+ return &eachNode
+}
+
+// AddChild adds a child to the foreach node.
+func (n *ForEachNode) AddChild(child Node) {
+ n.Children = append(n.Children, child)
+}
+
+// RestoreSQL implements Node interface.
+func (n *ForEachNode) RestoreSQL(w io.Writer) error {
+ var partBuilder strings.Builder
+ for _, node := range n.Children {
+ if err := node.RestoreSQL(&partBuilder); err != nil {
+ return err
+ }
+ }
+ part := strings.TrimSpace(partBuilder.String())
+ if len(part) == 0 {
+ return nil
+ }
+ if _, err := w.Write([]byte(" ")); err != nil {
+ return err
+ }
+ if len(n.Open) > 0 {
+ if _, err := w.Write([]byte(n.Open)); err != nil {
+ return err
+ }
+ }
+ // We write the part twice.
+ if _, err := w.Write([]byte(part)); err != nil {
+ return err
+ }
+ if len(n.Separator) > 0 {
+ if _, err := w.Write([]byte(" ")); err != nil {
+ return err
+ }
+ if _, err := w.Write([]byte(n.Separator)); err != nil {
+ return err
+ }
+ if _, err := w.Write([]byte(" ")); err != nil {
+ return err
+ }
+ if _, err := w.Write([]byte(part)); err != nil {
+ return err
+ }
+ }
+ if len(n.Close) > 0 {
+ if _, err := w.Write([]byte(n.Close)); err != nil {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/backend/plugin/parser/mybatis/parser.go b/backend/plugin/parser/mybatis/parser.go
index 9f6288fa61..90508f3bde 100644
--- a/backend/plugin/parser/mybatis/parser.go
+++ b/backend/plugin/parser/mybatis/parser.go
@@ -121,6 +121,8 @@ func (*Parser) newNodeByStartElement(startElement *xml.StartElement) ast.Node {
return ast.NewSetNode(startElement)
case "trim":
return ast.NewTrimNode(startElement)
+ case "foreach":
+ return ast.NewForeachNode(startElement)
}
return ast.NewEmptyNode()
}
diff --git a/backend/plugin/parser/mybatis/test-data/test_dynamic_sql_mapper.yaml b/backend/plugin/parser/mybatis/test-data/test_dynamic_sql_mapper.yaml
index 3ffe7b2d5a..b985d11991 100644
--- a/backend/plugin/parser/mybatis/test-data/test_dynamic_sql_mapper.yaml
+++ b/backend/plugin/parser/mybatis/test-data/test_dynamic_sql_mapper.yaml
@@ -175,3 +175,27 @@
</mapper>
sql: |
select users_id, name, age, sex from users where name like ? and age >= ? and users_id is not null group by users_id;
+- xml: |-
+ <mapper namespace="com.bytebase.test">
+ <select id="testForeach">
+ SELECT
+ name,
+ category,
+ price
+ FROM
+ fruits
+ <where>
+ category = 'apple' AND
+ <foreach collection="apples" item="name" open="(" close=")" separator="OR">
+ name = #{name}
+ </foreach>
+ </where>
+ </select>
+ </mapper>
+ sql: |
+ SELECT
+ name,
+ category,
+ price
+ FROM
+ fruits WHERE category = 'apple' AND (name = ? OR name = ?);