View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.type;
19  
20  import org.apache.shardingsphere.sql.parser.api.ASTNode;
21  import org.apache.shardingsphere.sql.parser.api.visitor.statement.type.DCLStatementVisitor;
22  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterRoleContext;
23  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterUserContext;
24  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CreateRoleContext;
25  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CreateUserContext;
26  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DropRoleContext;
27  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DropUserContext;
28  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.GrantContext;
29  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.PrivilegeClauseContext;
30  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.RevokeContext;
31  import org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.OpenGaussStatementVisitor;
32  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
33  import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
34  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussAlterRoleStatement;
35  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussAlterUserStatement;
36  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussCreateRoleStatement;
37  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussCreateUserStatement;
38  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussDropRoleStatement;
39  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussDropUserStatement;
40  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussGrantStatement;
41  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dcl.OpenGaussRevokeStatement;
42  
43  import java.util.Collection;
44  
45  /**
46   * DCL statement visitor for openGauss.
47   */
48  public final class OpenGaussDCLStatementVisitor extends OpenGaussStatementVisitor implements DCLStatementVisitor {
49      
50      @Override
51      public ASTNode visitGrant(final GrantContext ctx) {
52          OpenGaussGrantStatement result = new OpenGaussGrantStatement();
53          if (containsTableSegment(ctx.privilegeClause())) {
54              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
55          }
56          return result;
57      }
58      
59      @Override
60      public ASTNode visitRevoke(final RevokeContext ctx) {
61          OpenGaussRevokeStatement result = new OpenGaussRevokeStatement();
62          if (containsTableSegment(ctx.privilegeClause())) {
63              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
64          }
65          return result;
66      }
67      
68      private boolean containsTableSegment(final PrivilegeClauseContext ctx) {
69          return null != ctx && null != ctx.onObjectClause() && null != ctx.onObjectClause().privilegeLevel() && null != ctx.onObjectClause().privilegeLevel().tableNames();
70      }
71      
72      @SuppressWarnings("unchecked")
73      private Collection<SimpleTableSegment> getTableSegments(final PrivilegeClauseContext ctx) {
74          return ((CollectionValue<SimpleTableSegment>) visit(ctx.onObjectClause().privilegeLevel().tableNames())).getValue();
75      }
76      
77      @Override
78      public ASTNode visitCreateUser(final CreateUserContext ctx) {
79          return new OpenGaussCreateUserStatement();
80      }
81      
82      @Override
83      public ASTNode visitDropUser(final DropUserContext ctx) {
84          return new OpenGaussDropUserStatement();
85      }
86      
87      @Override
88      public ASTNode visitAlterUser(final AlterUserContext ctx) {
89          return new OpenGaussAlterUserStatement();
90      }
91      
92      @Override
93      public ASTNode visitCreateRole(final CreateRoleContext ctx) {
94          return new OpenGaussCreateRoleStatement();
95      }
96      
97      @Override
98      public ASTNode visitAlterRole(final AlterRoleContext ctx) {
99          return new OpenGaussAlterRoleStatement();
100     }
101     
102     @Override
103     public ASTNode visitDropRole(final DropRoleContext ctx) {
104         return new OpenGaussDropRoleStatement();
105     }
106 }