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.infra.database.core.type.DatabaseType;
21  import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
22  import org.apache.shardingsphere.sql.parser.api.ASTNode;
23  import org.apache.shardingsphere.sql.parser.api.visitor.statement.type.DCLStatementVisitor;
24  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterRoleContext;
25  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterUserContext;
26  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CreateRoleContext;
27  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CreateUserContext;
28  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DropRoleContext;
29  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DropUserContext;
30  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.GrantContext;
31  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.PrivilegeClauseContext;
32  import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.RevokeContext;
33  import org.apache.shardingsphere.sql.parser.opengauss.visitor.statement.OpenGaussStatementVisitor;
34  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
35  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement;
36  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.RevokeStatement;
37  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.role.AlterRoleStatement;
38  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.role.CreateRoleStatement;
39  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.role.DropRoleStatement;
40  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.user.AlterUserStatement;
41  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.user.CreateUserStatement;
42  import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.user.DropUserStatement;
43  import org.apache.shardingsphere.sql.parser.statement.core.value.collection.CollectionValue;
44  
45  import java.util.Collection;
46  import java.util.Collections;
47  
48  /**
49   * DCL statement visitor for openGauss.
50   */
51  public final class OpenGaussDCLStatementVisitor extends OpenGaussStatementVisitor implements DCLStatementVisitor {
52      
53      private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "openGauss");
54      
55      @Override
56      public ASTNode visitGrant(final GrantContext ctx) {
57          GrantStatement result = new GrantStatement(databaseType);
58          if (containsTableSegment(ctx.privilegeClause())) {
59              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
60          }
61          return result;
62      }
63      
64      @Override
65      public ASTNode visitRevoke(final RevokeContext ctx) {
66          RevokeStatement result = new RevokeStatement(databaseType);
67          if (containsTableSegment(ctx.privilegeClause())) {
68              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
69          }
70          return result;
71      }
72      
73      private boolean containsTableSegment(final PrivilegeClauseContext ctx) {
74          return null != ctx && null != ctx.onObjectClause() && null != ctx.onObjectClause().privilegeLevel() && null != ctx.onObjectClause().privilegeLevel().tableNames();
75      }
76      
77      @SuppressWarnings("unchecked")
78      private Collection<SimpleTableSegment> getTableSegments(final PrivilegeClauseContext ctx) {
79          return ((CollectionValue<SimpleTableSegment>) visit(ctx.onObjectClause().privilegeLevel().tableNames())).getValue();
80      }
81      
82      @Override
83      public ASTNode visitCreateUser(final CreateUserContext ctx) {
84          return new CreateUserStatement(databaseType);
85      }
86      
87      @Override
88      public ASTNode visitDropUser(final DropUserContext ctx) {
89          return new DropUserStatement(databaseType, Collections.emptyList());
90      }
91      
92      @Override
93      public ASTNode visitAlterUser(final AlterUserContext ctx) {
94          return new AlterUserStatement(databaseType, null);
95      }
96      
97      @Override
98      public ASTNode visitCreateRole(final CreateRoleContext ctx) {
99          return new CreateRoleStatement(databaseType);
100     }
101     
102     @Override
103     public ASTNode visitAlterRole(final AlterRoleContext ctx) {
104         return new AlterRoleStatement(databaseType);
105     }
106     
107     @Override
108     public ASTNode visitDropRole(final DropRoleContext ctx) {
109         return new DropRoleStatement(databaseType);
110     }
111 }