Skip to content

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

feat(mybatis): support mybatis xml `property` element (#5900)

h3n4l562f885f20b5
diff --git a/backend/plugin/parser/mybatis/ast/data_node.go b/backend/plugin/parser/mybatis/ast/data_node.go
index 89baa4b7b1..67b39f037e 100644
--- a/backend/plugin/parser/mybatis/ast/data_node.go
+++ b/backend/plugin/parser/mybatis/ast/data_node.go
@@ -25,18 +25,18 @@ func (n *TextNode) RestoreSQL(_ *RestoreContext, w io.Writer) error {
if len(n.Text) == 0 {
return nil
}
-
- if _, err := w.Write([]byte(" ")); err != nil {
- return err
- }
if _, err := w.Write([]byte(n.Text)); err != nil {
return err
}
return nil
}
+func (*TextNode) isChildAcceptable(Node) bool {
+ return false
+}
+
// AddChild implements Node interface, text node does not have child.
-func (*TextNode) AddChild(_ Node) {}
+func (*TextNode) AddChild(Node) {}
// ParameterNode represents a parameter node in mybatis mapper xml likes #{param}.
type ParameterNode struct {
@@ -52,8 +52,13 @@ func (*ParameterNode) RestoreSQL(_ *RestoreContext, w io.Writer) error {
return nil
}
+// isChildAcceptable implements Node interface, parameter node does not accept child.
+func (*ParameterNode) isChildAcceptable(Node) bool {
+ return false
+}
+
// AddChild implements Node interface, parameter node does not have child.
-func (*ParameterNode) AddChild(_ Node) {}
+func (*ParameterNode) AddChild(Node) {}
// VariableNode represents a variable node in mybatis mapper xml likes ${variable}.
type VariableNode struct {
@@ -75,8 +80,13 @@ func (v *VariableNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+// isChildAcceptable implements Node interface, variable node does not accept child.
+func (*VariableNode) isChildAcceptable(Node) bool {
+ return false
+}
+
// AddChild implements Node interface.
-func (*VariableNode) AddChild(_ Node) {}
+func (*VariableNode) AddChild(Node) {}
// DataNode represents a data node which contains plain text, parameter or variable.
type DataNode struct {
@@ -87,6 +97,12 @@ type DataNode struct {
// RestoreSQL implements Node interface.
func (d *DataNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
+ if len(d.Children) == 0 {
+ return nil
+ }
+ if _, err := w.Write([]byte(" ")); err != nil {
+ return err
+ }
for _, node := range d.Children {
if err := node.RestoreSQL(ctx, w); err != nil {
return err
@@ -102,8 +118,22 @@ func NewDataNode(data []byte) *DataNode {
}
}
+func (*DataNode) isChildAcceptable(node Node) bool {
+ switch node.(type) {
+ case *ParameterNode, *VariableNode, *TextNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild implements Node interface.
-func (*DataNode) AddChild(_ Node) {}
+func (d *DataNode) AddChild(n Node) {
+ if !d.isChildAcceptable(n) {
+ return
+ }
+ d.Children = append(d.Children, n)
+}
// Scan scans the data node from the bytes given in NewDataNode.
func (d *DataNode) Scan() error {
@@ -166,7 +196,7 @@ func (d *DataNode) clearBufToTextNode() {
if len(d.buf) == 0 {
return
}
- d.Children = append(d.Children, &TextNode{
+ d.AddChild(&TextNode{
Text: string(d.buf),
})
d.buf = d.buf[:0]
@@ -211,7 +241,7 @@ func (d *DataNode) scanParameter() error {
if r == '}' {
// Skip the prefix '#{' and suffix '}'.
partBuf := string(d.buf[2 : len(d.buf)-1])
- d.Children = append(d.Children, &ParameterNode{
+ d.AddChild(&ParameterNode{
Name: string(partBuf),
})
d.buf = d.buf[:0]
@@ -241,7 +271,7 @@ func (d *DataNode) scanVariable() error {
if r == '}' {
// Skip the prefix '${' and suffix '}'.
partBuf := string(d.buf[2 : len(d.buf)-1])
- d.Children = append(d.Children, &VariableNode{
+ d.AddChild(&VariableNode{
Name: string(partBuf),
})
d.buf = d.buf[:0]
diff --git a/backend/plugin/parser/mybatis/ast/dymamic_node.go b/backend/plugin/parser/mybatis/ast/dymamic_node.go
index 45075a4a9b..349976dc47 100644
--- a/backend/plugin/parser/mybatis/ast/dymamic_node.go
+++ b/backend/plugin/parser/mybatis/ast/dymamic_node.go
@@ -4,6 +4,7 @@ package ast
import (
"encoding/xml"
"io"
+ "regexp"
"strings"
"github.com/pkg/errors"
@@ -20,6 +21,10 @@ var (
_ Node = (*SetNode)(nil)
_ Node = (*TrimNode)(nil)
_ Node = (*ForEachNode)(nil)
+
+ _ Node = (*SQLNode)(nil)
+ _ Node = (*IncludeNode)(nil)
+ _ Node = (*PropertyNode)(nil)
)
// IfNode represents a if node in mybatis mapper xml likes <if test="condition">...</if>.
@@ -54,8 +59,21 @@ func (n *IfNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+func (*IfNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L290
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *IfNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the if node.
func (n *IfNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -84,8 +102,20 @@ func (n *ChooseNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+func (*ChooseNode) isChildAcceptable(child Node) bool {
+ switch child.(type) {
+ case *WhenNode, *OtherwiseNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild implements Node interface.
func (n *ChooseNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -126,8 +156,21 @@ func (n *WhenNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+func (*WhenNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#LL284C1-L284C1
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *IfNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the when node.
func (n *WhenNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -156,8 +199,21 @@ func (n *OtherwiseNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+func (*OtherwiseNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L288
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *IfNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the otherwise node.
func (n *OtherwiseNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -252,8 +308,21 @@ func (n *TrimNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return nil
}
+func (*TrimNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L262
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *IfNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the trim node.
func (n *TrimNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -274,6 +343,11 @@ func (n *WhereNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return n.trimNode.RestoreSQL(ctx, w)
}
+func (n *WhereNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L269
+ return n.trimNode.isChildAcceptable(child)
+}
+
// AddChild adds a child to the where node.
func (n *WhereNode) AddChild(child Node) {
n.trimNode.AddChild(child)
@@ -296,6 +370,11 @@ func (n *SetNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
return n.trimNode.RestoreSQL(ctx, w)
}
+func (n *SetNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L270
+ return n.trimNode.isChildAcceptable(child)
+}
+
// AddChild adds a child to the set node.
func (n *SetNode) AddChild(child Node) {
n.trimNode.AddChild(child)
@@ -334,8 +413,21 @@ func NewForeachNode(startElement *xml.StartElement) *ForEachNode {
return &eachNode
}
+func (*ForEachNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L272
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *IfNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the foreach node.
func (n *ForEachNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -405,8 +497,21 @@ func NewSQLNode(startElement *xml.StartElement) *SQLNode {
}
}
+func (*SQLNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L255
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *IfNode, *ChooseNode:
+ return true
+ default:
+ return false
+ }
+}
+
// AddChild adds a child to the sql node.
func (n *SQLNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
@@ -432,7 +537,7 @@ type IncludeNode struct {
RefID string
// IncludeNode can only contains property node.
// https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L244
- Children []Node
+ PropertyChildren []*PropertyNode
}
// NewIncludeNode creates a new include node.
@@ -451,15 +556,46 @@ func NewIncludeNode(startElement *xml.StartElement) *IncludeNode {
// AddChild adds a child to the include node.
func (n *IncludeNode) AddChild(child Node) {
- n.Children = append(n.Children, child)
+ if !n.isChildAcceptable(child) {
+ return
+ }
+ n.PropertyChildren = append(n.PropertyChildren, child.(*PropertyNode))
+}
+
+func (*IncludeNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L244
+ if _, ok := child.(*PropertyNode); ok {
+ return true
+ }
+ return false
}
// RestoreSQL implements Node interface.
func (n *IncludeNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
- sqlNode, ok := ctx.SQLMap[n.RefID]
+ variableCatcher := regexp.MustCompile(`\${([a-zA-Z0-9_]+)}`)
+ // We need to replace the variable in the refID.
+ refID := variableCatcher.ReplaceAllStringFunc(n.RefID, func(s string) string {
+ matches := variableCatcher.FindStringSubmatch(s)
+ if len(matches) != 2 {
+ return s
+ }
+ name := matches[1]
+ return ctx.Variable[name]
+ })
+
+ sqlNode, ok := ctx.SQLMap[refID]
if !ok {
return errors.Errorf("refID %s not found", n.RefID)
}
+
+ // Set all the properties.
+ // It is safe we don't check whether the variable exists, because property element can only be child of include element,
+ // and include element can only refers one sql element. The property element was covered by another property element
+ // is not a problem, because the outer include element will not use the outer property element any more.
+ for _, propertyNode := range n.PropertyChildren {
+ ctx.Variable[propertyNode.Name] = propertyNode.Value
+ }
+
sqlString, err := sqlNode.String(ctx)
if err != nil {
return err
@@ -474,5 +610,46 @@ func (n *IncludeNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
if _, err := w.Write([]byte(trimmed)); err != nil {
return err
}
+
+ // Unset all the properties.
+ for _, propertyNode := range n.PropertyChildren {
+ delete(ctx.Variable, propertyNode.Name)
+ }
+
+ return nil
+}
+
+// PropertyNode represents a property node in mybatis mapper xml likes <property name="name" value="value" />.
+type PropertyNode struct {
+ Name string
+ Value string
+}
+
+// NewPropertyNode creates a new property node.
+func NewPropertyNode(startElement *xml.StartElement) *PropertyNode {
+ var name, value string
+ for _, attr := range startElement.Attr {
+ if attr.Name.Local == "name" {
+ name = attr.Value
+ } else if attr.Name.Local == "value" {
+ value = attr.Value
+ }
+ }
+ return &PropertyNode{
+ Name: name,
+ Value: value,
+ }
+}
+
+func (*PropertyNode) isChildAcceptable(Node) bool {
+ return false
+}
+
+// AddChild adds a child to the property node.
+func (*PropertyNode) AddChild(Node) {
+}
+
+// RestoreSQL implements Node interface.
+func (*PropertyNode) RestoreSQL(*RestoreContext, io.Writer) error {
return nil
}
diff --git a/backend/plugin/parser/mybatis/ast/mapper_node.go b/backend/plugin/parser/mybatis/ast/mapper_node.go
index d3acbee5e0..61494b2555 100644
--- a/backend/plugin/parser/mybatis/ast/mapper_node.go
+++ b/backend/plugin/parser/mybatis/ast/mapper_node.go
@@ -39,5 +39,18 @@ func NewMapperNode(startElement *xml.StartElement) *MapperNode {
// AddChild adds a child to the mapper node.
func (n *MapperNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
+
+func (*MapperNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L19
+ switch child.(type) {
+ case *SQLNode, *QueryNode:
+ return true
+ default:
+ return false
+ }
+}
diff --git a/backend/plugin/parser/mybatis/ast/node.go b/backend/plugin/parser/mybatis/ast/node.go
index 967bf12a2c..30f465f334 100644
--- a/backend/plugin/parser/mybatis/ast/node.go
+++ b/backend/plugin/parser/mybatis/ast/node.go
@@ -11,6 +11,8 @@ type Node interface {
RestoreSQL(ctx *RestoreContext, w io.Writer) error
// AddChild adds a child to the node.
AddChild(child Node)
+ // isChildAcceptable checks whether the child is acceptable.
+ isChildAcceptable(child Node) bool
}
// RestoreContext is the context for restoring SQL statement.
@@ -48,8 +50,14 @@ func (n *RootNode) AddChild(child Node) {
n.Children = append(n.Children, child)
}
+// isChildAcceptable implements Node interface.
+func (*RootNode) isChildAcceptable(Node) bool {
+ return true
+}
+
// EmptyNode represents an unacceptable nodes in mybatis mapper xml.
-type EmptyNode struct{}
+type EmptyNode struct {
+}
// NewEmptyNode returns a new empty node.
func NewEmptyNode() *EmptyNode {
@@ -64,3 +72,8 @@ func (*EmptyNode) RestoreSQL(*RestoreContext, io.Writer) error {
// AddChild implements Node interface.
func (*EmptyNode) AddChild(Node) {
}
+
+// isChildAcceptable implements Node interface.
+func (*EmptyNode) isChildAcceptable(Node) bool {
+ return false
+}
diff --git a/backend/plugin/parser/mybatis/ast/query_node.go b/backend/plugin/parser/mybatis/ast/query_node.go
index bfc463e283..cdbd7c089f 100644
--- a/backend/plugin/parser/mybatis/ast/query_node.go
+++ b/backend/plugin/parser/mybatis/ast/query_node.go
@@ -9,7 +9,6 @@ import (
var (
_ Node = (*QueryNode)(nil)
- _ Node = (*TextNode)(nil)
)
// QueryNodeType is the type of the query node.
@@ -61,9 +60,22 @@ func (n *QueryNode) RestoreSQL(ctx *RestoreContext, w io.Writer) error {
// AddChild adds a child to the query node.
func (n *QueryNode) AddChild(child Node) {
+ if !n.isChildAcceptable(child) {
+ return
+ }
n.Children = append(n.Children, child)
}
+func (*QueryNode) isChildAcceptable(child Node) bool {
+ // https://github.com/mybatis/mybatis-3/blob/master/src/main/resources/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd#L19
+ switch child.(type) {
+ case *DataNode, *IncludeNode, *TrimNode, *WhereNode, *SetNode, *ForEachNode, *ChooseNode, *SQLNode, *IfNode:
+ default:
+ return false
+ }
+ return true
+}
+
// NewQueryNode creates a new query node.
func NewQueryNode(startEle *xml.StartElement) *QueryNode {
n := &QueryNode{}
diff --git a/backend/plugin/parser/mybatis/parser.go b/backend/plugin/parser/mybatis/parser.go
index 1566722e09..18e2c5edaa 100644
--- a/backend/plugin/parser/mybatis/parser.go
+++ b/backend/plugin/parser/mybatis/parser.go
@@ -141,6 +141,8 @@ func (*Parser) newNodeByStartElement(startElement *xml.StartElement) ast.Node {
return ast.NewSQLNode(startElement)
case "include":
return ast.NewIncludeNode(startElement)
+ case "property":
+ return ast.NewPropertyNode(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 f53b6af673..dad044d3b1 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
@@ -152,7 +152,7 @@
</mapper>
sql: |
UPDATE
- fruits SET category = ? , price = ? WHERE
+ fruits SET category = ?, price = ? WHERE
name = ?;
- xml: |-
<mapper namespace="com.bytebase.test">
@@ -174,7 +174,7 @@
</select>
</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;
+ 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">
@@ -224,3 +224,24 @@
price
FROM fruits WHERE category = 'apple'
AND price = 200;
+- xml: |-
+ <mapper namespace="com.bytebase.test">
+ <sql id="sometable">
+ ${prefix}Table
+ </sql>
+ <sql id="someinclude">
+ from
+ <include refid="${include_target}"/>
+ </sql>
+ <select id="select" resultType="map">
+ select
+ field1, field2, field3
+ <include refid="someinclude">
+ <property name="prefix" value="Some"/>
+ <property name="include_target" value="sometable"/>
+ </include>
+ </select>
+ </mapper>
+ sql: |
+ select
+ field1, field2, field3 from SomeTable;
diff --git a/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml b/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml
index e73a7fd6e3..88b8e00e0a 100644
--- a/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml
+++ b/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml
@@ -31,8 +31,8 @@
</mapper>
sql: |
select * from users where id = ?;
- insert into users (id, name) values (? , ? );
- update users set name = ? where id = ?;
+ insert into users (id, name) values (?, ?);
+ update users set name = ? where id = ?;
delete from users where id = ?;
- xml: |-
<mapper namespace="com.bytebase.test">