Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support if choose when otherwise dynamic sql (#5865)
h3n4le9a3a7559548
diff --git a/backend/plugin/parser/mybatis/ast/dymamic_node.go b/backend/plugin/parser/mybatis/ast/dymamic_node.gonew file mode 100644index 0000000000..c2facbe426--- /dev/null+++ b/backend/plugin/parser/mybatis/ast/dymamic_node.go@@ -0,0 +1,143 @@+// Package ast defines the abstract syntax tree of mybatis mapper xml.+package ast++import (+ "encoding/xml"+ "io"+)++var (+ _ Node = (*IfNode)(nil)+ _ Node = (*ChooseNode)(nil)+ _ Node = (*WhenNode)(nil)+ _ Node = (*OtherwiseNode)(nil)+)++// IfNode represents a if node in mybatis mapper xml likes <if test="condition">...</if>.+type IfNode struct {+ Test string+ Children []Node+}++// NewIfNode creates a new if node.+func NewIfNode(startElement *xml.StartElement) *IfNode {+ node := &IfNode{}+ for _, attr := range startElement.Attr {+ if attr.Name.Local == "test" {+ node.Test = attr.Value+ }+ }+ return node+}++// RestoreSQL implements Node interface, the if condition will be ignored.+func (n *IfNode) RestoreSQL(w io.Writer) error {+ if len(n.Children) > 0 {+ if _, err := w.Write([]byte(" ")); err != nil {+ return err+ }+ }+ for _, node := range n.Children {+ if err := node.RestoreSQL(w); err != nil {+ return err+ }+ }+ return nil+}++// AddChild adds a child to the if node.+func (n *IfNode) AddChild(child Node) {+ n.Children = append(n.Children, child)+}++// ChooseNode represents a choose node in mybatis mapper xml likes <choose>...</choose>.+type ChooseNode struct {+ Children []Node+}++// NewChooseNode creates a new choose node.+func NewChooseNode(_ *xml.StartElement) *ChooseNode {+ return &ChooseNode{}+}++// RestoreSQL implements Node interface.+func (n *ChooseNode) RestoreSQL(w io.Writer) error {+ for _, node := range n.Children {+ if err := node.RestoreSQL(w); err != nil {+ return err+ }+ }+ return nil+}++// AddChild implements Node interface.+func (n *ChooseNode) AddChild(child Node) {+ n.Children = append(n.Children, child)+}++// WhenNode represents a when node in mybatis mapper xml select node likes <select><when test="condition">...</when></select>.+type WhenNode struct {+ Test string+ Children []Node+}++// NewWhenNode creates a new when node.+func NewWhenNode(startElement *xml.StartElement) *WhenNode {+ node := &WhenNode{}+ for _, attr := range startElement.Attr {+ if attr.Name.Local == "test" {+ node.Test = attr.Value+ }+ }+ return node+}++// RestoreSQL implements Node interface, the when condition will be ignored.+func (n *WhenNode) RestoreSQL(w io.Writer) error {+ if len(n.Children) > 0 {+ if _, err := w.Write([]byte(" ")); err != nil {+ return err+ }+ }+ for _, node := range n.Children {+ if err := node.RestoreSQL(w); err != nil {+ return err+ }+ }+ return nil+}++// AddChild adds a child to the when node.+func (n *WhenNode) AddChild(child Node) {+ n.Children = append(n.Children, child)+}++// OtherwiseNode represents a otherwise node in mybatis mapper xml select node likes <select><otherwise>...</otherwise></select>.+type OtherwiseNode struct {+ Children []Node+}++// NewOtherwiseNode creates a new otherwise node.+func NewOtherwiseNode(_ *xml.StartElement) *OtherwiseNode {+ return &OtherwiseNode{}+}++// RestoreSQL implements Node interface.+func (n *OtherwiseNode) RestoreSQL(w io.Writer) error {+ if len(n.Children) > 0 {+ if _, err := w.Write([]byte(" ")); err != nil {+ return err+ }+ }+ for _, node := range n.Children {+ if err := node.RestoreSQL(w); err != nil {+ return err+ }+ }+ return nil+}++// AddChild adds a child to the otherwise node.+func (n *OtherwiseNode) AddChild(child Node) {+ n.Children = append(n.Children, child)+}diff --git a/backend/plugin/parser/mybatis/parser.go b/backend/plugin/parser/mybatis/parser.goindex d25e692040..ea217cf4fd 100644--- a/backend/plugin/parser/mybatis/parser.go+++ b/backend/plugin/parser/mybatis/parser.go@@ -107,6 +107,14 @@ func (*Parser) newNodeByStartElement(startElement *xml.StartElement) ast.Node {return ast.NewMapperNode(startElement)case "select", "update", "insert", "delete":return ast.NewQueryNode(startElement)+ case "if":+ return ast.NewIfNode(startElement)+ case "choose":+ return ast.NewChooseNode(startElement)+ case "when":+ return ast.NewWhenNode(startElement)+ case "otherwise":+ return ast.NewOtherwiseNode(startElement)}return ast.NewEmptyNode()}diff --git a/backend/plugin/parser/mybatis/parser_test.go b/backend/plugin/parser/mybatis/parser_test.goindex 67ab18ada1..3d9dd14ef8 100644--- a/backend/plugin/parser/mybatis/parser_test.go+++ b/backend/plugin/parser/mybatis/parser_test.go@@ -61,6 +61,7 @@ func runTest(t *testing.T, filepath string, record bool) {func TestParser(t *testing.T) {testFileList := []string{"test-data/test_simple_mapper.yaml",+ "test-data/test_dynamic_sql_mapper.yaml",}for _, filepath := range testFileList {runTest(t, filepath, false)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.yamlnew file mode 100644index 0000000000..671ad33d71--- /dev/null+++ b/backend/plugin/parser/mybatis/test-data/test_dynamic_sql_mapper.yaml@@ -0,0 +1,65 @@+- xml: |-+ <mapper namespace="com.bytebase.test">+ <select id="testIf">+ SELECT+ name,+ category,+ price+ FROM+ fruits+ WHERE+ 1=1+ <if test="category != null and category !=''">+ AND category = #{category}+ </if>+ <if test="price != null and price !=''">+ AND price = ${price}+ <if test="price >= 400">+ AND name = 'Fuji'+ </if>+ </if>+ </select>+ </mapper>+ sql: |+ SELECT+ name,+ category,+ price+ FROM+ fruits+ WHERE+ 1=1 AND category = ? AND price = ? AND name = 'Fuji';+- xml: |-+ <mapper namespace="com.bytebase.test">+ <select id="testChoose">+ SELECT+ name,+ category,+ price+ FROM+ fruits+ WHERE category = 'apple'+ <choose>+ <when test="name != null">+ AND name = #{name}+ </when>+ <when test="category == 'banana'">+ AND category = #{category}+ <if test="price != null and price !=''">+ AND price = ${price}+ </if>+ </when>+ <otherwise>+ AND category = 'apple'+ </otherwise>+ </choose>+ </select>+ </mapper>+ sql: |+ SELECT+ name,+ category,+ price+ FROM+ fruits+ WHERE category = 'apple' AND name = ? AND category = ? AND price = ? AND category = 'apple';diff --git a/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml b/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yamlindex 08e0fb887d..03e52885fa 100644--- a/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml+++ b/backend/plugin/parser/mybatis/test-data/test_simple_mapper.yaml@@ -36,7 +36,7 @@delete from user where id = ?;- xml: |-<mapper namespace="com.bytebase.test">- <!--Queyr user by id-->+ <!--Query user by id--><select id="selectUser" parameterType="int" resultType="hashmap">select * from user where id = #{id}</select>