This is an automated email from the ASF dual-hosted git repository.

miaoliyao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new dc39e4c  feat: support shadow distSQL parse to AST
     new 8c27caa  Merge pull request #385 from wbtlb/feat-shadow
dc39e4c is described below

commit dc39e4cb18cae67188f337d21afb851ef0865405
Author: wangbo <[email protected]>
AuthorDate: Wed May 31 09:46:45 2023 +0000

    feat: support shadow distSQL parse to AST
---
 .../pkg/distsql/ast/shadow_rdl_ast.go              | 179 ++++++++++++++
 .../pkg/distsql/visitor/shadow_rdl_visitor.go      | 271 +++++++++++++++++++++
 2 files changed, 450 insertions(+)

diff --git a/shardingsphere-operator/pkg/distsql/ast/shadow_rdl_ast.go 
b/shardingsphere-operator/pkg/distsql/ast/shadow_rdl_ast.go
new file mode 100644
index 0000000..ecc617d
--- /dev/null
+++ b/shardingsphere-operator/pkg/distsql/ast/shadow_rdl_ast.go
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ast
+
+import (
+       "fmt"
+       "strings"
+)
+
+type CreateShadowRule struct {
+       IfNotExists             *IfNotExists
+       AllShadowRuleDefinition []*ShadowRuleDefinition
+}
+
+func (createShadowRule *CreateShadowRule) ToString() string {
+       var (
+               distSQL = "CREATE SHADOW RULE"
+               allRule = []string{}
+       )
+       if createShadowRule.IfNotExists != nil {
+               distSQL = fmt.Sprintf("%s %s", distSQL, 
createShadowRule.IfNotExists)
+       }
+       if createShadowRule.AllShadowRuleDefinition != nil {
+               for _, r := range createShadowRule.AllShadowRuleDefinition {
+                       allRule = append(allRule, r.ToString())
+               }
+       }
+       return fmt.Sprintf("%s %s", distSQL, strings.Join(allRule, ","))
+}
+
+type ShadowRuleDefinition struct {
+       RuleName           *CommonIdentifier
+       Source             *CommonIdentifier
+       Shadow             *CommonIdentifier
+       AllShadowTableRule []*ShadowTableRule
+}
+
+func (shadowRuleDefinition *ShadowRuleDefinition) ToString() string {
+       var (
+               distSQL          = ""
+               shadowTableRules = []string{}
+       )
+       if shadowRuleDefinition.RuleName != nil {
+               distSQL = fmt.Sprintf("%s (", 
shadowRuleDefinition.RuleName.ToString())
+       }
+
+       if shadowRuleDefinition.Source != nil {
+               distSQL = fmt.Sprintf("%s SOURCE = %s, ", distSQL, 
shadowRuleDefinition.ToString())
+       }
+
+       if shadowRuleDefinition.Shadow != nil {
+               distSQL = fmt.Sprintf("%s SHADOW = %s, ", distSQL, 
shadowRuleDefinition.Shadow.ToString())
+       }
+
+       if shadowRuleDefinition.AllShadowTableRule != nil {
+               for _, r := range shadowRuleDefinition.AllShadowTableRule {
+                       shadowTableRules = append(shadowTableRules, 
r.ToString())
+               }
+       }
+       return fmt.Sprintf("%s %s)", distSQL, strings.Join(shadowTableRules, 
","))
+}
+
+type ShadowTableRule struct {
+       TableName              *CommonIdentifier
+       AllAlgorithmDefinition []*AlgorithmDefinition
+}
+
+func (shadowTableRule *ShadowTableRule) ToString() string {
+       var (
+               tableName           = ""
+               algorithmDefinition = []string{}
+       )
+       if shadowTableRule.TableName != nil {
+               tableName = shadowTableRule.TableName.ToString()
+       }
+       if shadowTableRule.AllAlgorithmDefinition != nil {
+               for _, algo := range shadowTableRule.AllAlgorithmDefinition {
+                       algorithmDefinition = append(algorithmDefinition, 
algo.ToString())
+               }
+       }
+       return fmt.Sprintf("%s (%s)", tableName, 
strings.Join(algorithmDefinition, ","))
+}
+
+type AlterShadowRule struct {
+       AllShadowRuleDefinition []*ShadowRuleDefinition
+}
+
+func (alterShadowRule *AlterShadowRule) ToString() string {
+       var allrules []string
+       if alterShadowRule.AllShadowRuleDefinition != nil {
+               for _, r := range alterShadowRule.AllShadowRuleDefinition {
+                       allrules = append(allrules, r.ToString())
+               }
+       }
+       return fmt.Sprintf("ALTER SHADOW RULE %s", strings.Join(allrules, ","))
+}
+
+type DropShadowRule struct {
+       IfExists    *IfExists
+       AllRuleName []*CommonIdentifier
+}
+
+func (dropShadowRule *DropShadowRule) ToString() string {
+       var (
+               ifExists    = ""
+               allRuleName = []string{}
+       )
+       if dropShadowRule.IfExists != nil {
+               ifExists = dropShadowRule.IfExists.ToString()
+       }
+       for _, r := range dropShadowRule.AllRuleName {
+               allRuleName = append(allRuleName, r.ToString())
+       }
+       return fmt.Sprintf("DROP SHADOW RULE %s %s", ifExists, 
strings.Join(allRuleName, ","))
+}
+
+type DropShadowAlgorithm struct {
+       IfExists         *IfExists
+       AllAlgorithmName []*CommonIdentifier
+}
+
+func (dropShadowAlgorithm *DropShadowAlgorithm) ToString() string {
+       var (
+               ifExists         = ""
+               allAlgorithmName = []string{}
+       )
+
+       if dropShadowAlgorithm.IfExists != nil {
+               ifExists = dropShadowAlgorithm.IfExists.ToString()
+       }
+
+       if dropShadowAlgorithm.AllAlgorithmName != nil {
+               for _, algo := range dropShadowAlgorithm.AllAlgorithmName {
+                       allAlgorithmName = append(allAlgorithmName, 
algo.ToString())
+               }
+       }
+
+       return fmt.Sprintf("DROP SHADOW ALGORITHM %s %s", ifExists, 
strings.Join(allAlgorithmName, ","))
+}
+
+type CreateDefaultShadowAlgorithm struct {
+       IfNotExists         *IfNotExists
+       AlgorithmDefinition *AlgorithmDefinition
+}
+
+func (createDefaultShadowAlgorithm *CreateDefaultShadowAlgorithm) ToString() 
string {
+       return fmt.Sprintf("CREATE DEFAULT SHADOW ALGORITHM %s %s", 
createDefaultShadowAlgorithm.IfNotExists.ToString(), 
createDefaultShadowAlgorithm.AlgorithmDefinition.ToString())
+}
+
+type DropDefaultShadowAlgorithm struct {
+       IfExists *IfExists
+}
+
+func (dropDefaultShadowAlgorithm *DropDefaultShadowAlgorithm) ToString() 
string {
+       return fmt.Sprintf("DROP DEFAULT SHADOW ALGORITHM %s", 
dropDefaultShadowAlgorithm.IfExists.ToString())
+}
+
+type AlterDefaultShadowAlgorithm struct {
+       AlgorithmDefinition *AlgorithmDefinition
+}
+
+func (alterDefaultShadowAlgorithm *AlterDefaultShadowAlgorithm) ToString() 
string {
+       return fmt.Sprintf("ALTER DEFAULT SHADOW ALGORITHM %s", 
alterDefaultShadowAlgorithm.AlgorithmDefinition.ToString())
+}
diff --git a/shardingsphere-operator/pkg/distsql/visitor/shadow_rdl_visitor.go 
b/shardingsphere-operator/pkg/distsql/visitor/shadow_rdl_visitor.go
new file mode 100644
index 0000000..3006e0e
--- /dev/null
+++ b/shardingsphere-operator/pkg/distsql/visitor/shadow_rdl_visitor.go
@@ -0,0 +1,271 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package visitor
+
+import (
+       "fmt"
+
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/distsql/ast"
+       parser 
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/distsql/visitor_parser/shadow"
+)
+
+type ShadowVisitor struct {
+       parser.BaseRDLStatementVisitor
+}
+
+func (v *ShadowVisitor) VisitCreateShadowRule(ctx 
*parser.CreateShadowRuleContext) *ast.CreateShadowRule {
+       stmt := &ast.CreateShadowRule{}
+       if ctx.IfNotExists() != nil {
+               stmt.IfNotExists = 
v.VisitIfNotExists(ctx.IfNotExists().(*parser.IfNotExistsContext))
+       }
+       if ctx.AllShadowRuleDefinition() != nil {
+               for _, r := range ctx.AllShadowRuleDefinition() {
+                       stmt.AllShadowRuleDefinition = 
append(stmt.AllShadowRuleDefinition, 
v.VisitShadowRuleDefinition(r.(*parser.ShadowRuleDefinitionContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitAlterShadowRule(ctx 
*parser.AlterShadowRuleContext) *ast.AlterShadowRule {
+       stmt := &ast.AlterShadowRule{}
+       if ctx.AllShadowRuleDefinition() != nil {
+               for _, r := range ctx.AllShadowRuleDefinition() {
+                       stmt.AllShadowRuleDefinition = 
append(stmt.AllShadowRuleDefinition, 
v.VisitShadowRuleDefinition(r.(*parser.ShadowRuleDefinitionContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitDropShadowRule(ctx *parser.DropShadowRuleContext) 
*ast.DropShadowRule {
+       stmt := &ast.DropShadowRule{}
+       if ctx.IfExists() != nil {
+               stmt.IfExists = 
v.VisitIfExists(ctx.IfExists().(*parser.IfExistsContext))
+       }
+       if ctx.AllRuleName() != nil {
+               for _, rn := range ctx.AllRuleName() {
+                       stmt.AllRuleName = append(stmt.AllRuleName, 
v.VisitRuleName(rn.(*parser.RuleNameContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitDropShadowAlgorithm(ctx 
*parser.DropShadowAlgorithmContext) *ast.DropShadowAlgorithm {
+       stmt := &ast.DropShadowAlgorithm{}
+       if ctx.IfExists() != nil {
+               stmt.IfExists = 
v.VisitIfExists(ctx.IfExists().(*parser.IfExistsContext))
+       }
+       if ctx.AllAlgorithmName() != nil {
+               for _, algorithm := range ctx.AllAlgorithmName() {
+                       stmt.AllAlgorithmName = append(stmt.AllAlgorithmName, 
v.VisitAlgorithmName(algorithm.(*parser.AlgorithmNameContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitCreateDefaultShadowAlgorithm(ctx 
*parser.CreateDefaultShadowAlgorithmContext) *ast.CreateDefaultShadowAlgorithm {
+       stmt := &ast.CreateDefaultShadowAlgorithm{}
+       if ctx.IfNotExists() != nil {
+               stmt.IfNotExists = 
v.VisitIfNotExists(ctx.IfNotExists().(*parser.IfNotExistsContext))
+       }
+       if ctx.AlgorithmDefinition() != nil {
+               stmt.AlgorithmDefinition = 
v.VisitAlgorithmDefinition(ctx.AlgorithmDefinition().(*parser.AlgorithmDefinitionContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitDropDefaultShadowAlgorithm(ctx 
*parser.DropDefaultShadowAlgorithmContext) *ast.DropDefaultShadowAlgorithm {
+       stmt := &ast.DropDefaultShadowAlgorithm{}
+       if ctx.IfExists() != nil {
+               stmt.IfExists = 
v.VisitIfExists(ctx.IfExists().(*parser.IfExistsContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitAlterDefaultShadowAlgorithm(ctx 
*parser.AlterDefaultShadowAlgorithmContext) *ast.AlterDefaultShadowAlgorithm {
+       stmt := &ast.AlterDefaultShadowAlgorithm{}
+       if ctx.AlgorithmDefinition() != nil {
+               stmt.AlgorithmDefinition = 
v.VisitAlgorithmDefinition(ctx.AlgorithmDefinition().(*parser.AlgorithmDefinitionContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitShadowRuleDefinition(ctx 
*parser.ShadowRuleDefinitionContext) *ast.ShadowRuleDefinition {
+       stmt := &ast.ShadowRuleDefinition{}
+       if ctx.RuleName() != nil {
+               stmt.RuleName = 
v.VisitRuleName(ctx.RuleName().(*parser.RuleNameContext))
+       }
+       if ctx.Source() != nil {
+               stmt.Source = 
v.VisitSource(ctx.Source().(*parser.SourceContext))
+       }
+       if ctx.SHADOW() != nil {
+               stmt.Shadow = 
v.VisitShadow(ctx.Shadow().(*parser.ShadowContext))
+       }
+       if ctx.AllShadowTableRule() != nil {
+               for _, r := range ctx.AllShadowTableRule() {
+                       stmt.AllShadowTableRule = 
append(stmt.AllShadowTableRule, 
v.VisitShadowTableRule(r.(*parser.ShadowTableRuleContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitShadowTableRule(ctx 
*parser.ShadowTableRuleContext) *ast.ShadowTableRule {
+       stmt := &ast.ShadowTableRule{}
+       if ctx.TableName() != nil {
+               stmt.TableName = 
v.VisitTableName(ctx.TableName().(*parser.TableNameContext))
+       }
+       if ctx.AllAlgorithmDefinition() != nil {
+               for _, algorithm := range ctx.AllAlgorithmDefinition() {
+                       stmt.AllAlgorithmDefinition = 
append(stmt.AllAlgorithmDefinition, 
v.VisitAlgorithmDefinition(algorithm.(*parser.AlgorithmDefinitionContext)))
+               }
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitSource(ctx *parser.SourceContext) 
*ast.CommonIdentifier {
+       stmt := &ast.CommonIdentifier{}
+       if ctx.IDENTIFIER_() != nil {
+               stmt.Identifier = ctx.IDENTIFIER_().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitShadow(ctx *parser.ShadowContext) 
*ast.CommonIdentifier {
+       stmt := &ast.CommonIdentifier{}
+       if ctx.IDENTIFIER_() != nil {
+               stmt.Identifier = ctx.IDENTIFIER_().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitTableName(ctx *parser.TableNameContext) 
*ast.CommonIdentifier {
+       stmt := &ast.CommonIdentifier{}
+       if ctx.IDENTIFIER_() != nil {
+               stmt.Identifier = ctx.IDENTIFIER_().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitAlgorithmName(ctx *parser.AlgorithmNameContext) 
*ast.CommonIdentifier {
+       stmt := &ast.CommonIdentifier{}
+       if ctx.IDENTIFIER_() != nil {
+               stmt.Identifier = ctx.IDENTIFIER_().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitIfExists(ctx *parser.IfExistsContext) 
*ast.IfExists {
+       return &ast.IfExists{
+               IfExists: fmt.Sprintf("%s %s", ctx.IF().GetText(), 
ctx.EXISTS().GetText()),
+       }
+}
+
+func (v *ShadowVisitor) VisitIfNotExists(ctx *parser.IfNotExistsContext) 
*ast.IfNotExists {
+       return &ast.IfNotExists{
+               IfNotExists: fmt.Sprintf("%s %s %s", ctx.IF().GetText(), 
ctx.NOT().GetText(), ctx.EXISTS().GetText()),
+       }
+}
+
+// nolint
+func (v *ShadowVisitor) VisitLiteral(ctx *parser.LiteralContext) *ast.Literal {
+       stmt := &ast.Literal{}
+       switch {
+       case ctx.STRING_() != nil:
+               stmt.Literal = ctx.STRING_().GetText()
+       case ctx.MINUS_() != nil:
+               stmt.Literal = ctx.MINUS_().GetText()
+       case ctx.INT_() != nil:
+               stmt.Literal = ctx.INT_().GetText()
+       case ctx.TRUE() != nil:
+               stmt.Literal = ctx.TRUE().GetText()
+       case ctx.FALSE() != nil:
+               stmt.Literal = ctx.FALSE().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitAlgorithmDefinition(ctx 
*parser.AlgorithmDefinitionContext) *ast.AlgorithmDefinition {
+       stmt := &ast.AlgorithmDefinition{}
+       if ctx.AlgorithmTypeName() != nil {
+               stmt.AlgorithmTypeName = 
v.VisitAlgorithmTypeName(ctx.AlgorithmTypeName().(*parser.AlgorithmTypeNameContext))
+       }
+
+       if ctx.PropertiesDefinition() != nil {
+               stmt.PropertiesDefinition = 
v.VisitPropertiesDefinition(ctx.PropertiesDefinition().(*parser.PropertiesDefinitionContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitAlgorithmTypeName(ctx 
*parser.AlgorithmTypeNameContext) *ast.AlgorithmTypeName {
+       stmt := &ast.AlgorithmTypeName{}
+       switch {
+       case ctx.STRING_() != nil:
+               stmt.String = ctx.STRING_().GetText()
+       case ctx.BuildInShadowAlgorithmType() != nil:
+               stmt.BuildinAlgorithmTypeName = 
v.VisitBuildInShadowAlgorithmType(ctx.BuildInShadowAlgorithmType().(*parser.BuildInShadowAlgorithmTypeContext))
+       }
+       return stmt
+}
+
+// nolint
+func (v *ShadowVisitor) VisitBuildInShadowAlgorithmType(ctx 
*parser.BuildInShadowAlgorithmTypeContext) *ast.BuildinAlgorithmTypeName {
+       stmt := &ast.BuildinAlgorithmTypeName{}
+       switch {
+       case ctx.VALUE_MATCH() != nil:
+               stmt.AlgorithmTypeName = ctx.VALUE_MATCH().GetText()
+       case ctx.REGEX_MATCH() != nil:
+               stmt.AlgorithmTypeName = ctx.REGEX_MATCH().GetText()
+       case ctx.SQL_HINT() != nil:
+               stmt.AlgorithmTypeName = ctx.SQL_HINT().GetText()
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitPropertiesDefinition(ctx 
*parser.PropertiesDefinitionContext) *ast.PropertiesDefinition {
+       stmt := &ast.PropertiesDefinition{}
+       if ctx.Properties() != nil {
+               stmt.Properties = 
v.VisitProperties(ctx.Properties().(*parser.PropertiesContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitProperties(ctx *parser.PropertiesContext) 
*ast.Properties {
+       stmt := &ast.Properties{}
+       for _, p := range ctx.AllProperty() {
+               stmt.Properties = append(stmt.Properties, 
v.VisitProperty(p.(*parser.PropertyContext)))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitProperty(ctx *parser.PropertyContext) 
*ast.Property {
+       stmt := &ast.Property{}
+       if ctx.STRING_() != nil {
+               stmt.Key = ctx.STRING_().GetText()
+       }
+       if ctx.Literal() != nil {
+               stmt.Literal = 
v.VisitLiteral(ctx.Literal().(*parser.LiteralContext))
+       }
+       return stmt
+}
+
+func (v *ShadowVisitor) VisitRuleName(ctx *parser.RuleNameContext) 
*ast.CommonIdentifier {
+       stmt := &ast.CommonIdentifier{}
+       if ctx.IDENTIFIER_() != nil {
+               stmt.Identifier = ctx.IDENTIFIER_().GetText()
+       }
+       return stmt
+}

Reply via email to