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.postgresql.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.PostgreSQLStatementParser.AlterRoleContext;
23  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.AlterUserContext;
24  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.CreateGroupContext;
25  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.CreateRoleContext;
26  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.CreateUserContext;
27  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.DropRoleContext;
28  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.DropUserContext;
29  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.GrantContext;
30  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.PrivilegeClauseContext;
31  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ReassignOwnedContext;
32  import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.RevokeContext;
33  import org.apache.shardingsphere.sql.parser.postgresql.visitor.statement.PostgreSQLStatementVisitor;
34  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
35  import org.apache.shardingsphere.sql.parser.sql.common.value.collection.CollectionValue;
36  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLAlterRoleStatement;
37  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLAlterUserStatement;
38  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLCreateGroupStatement;
39  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLCreateRoleStatement;
40  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLCreateUserStatement;
41  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLDropRoleStatement;
42  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLDropUserStatement;
43  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLGrantStatement;
44  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLReassignOwnedStatement;
45  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.dcl.PostgreSQLRevokeStatement;
46  
47  import java.util.Collection;
48  
49  /**
50   * DCL statement visitor for PostgreSQL.
51   */
52  public final class PostgreSQLDCLStatementVisitor extends PostgreSQLStatementVisitor implements DCLStatementVisitor {
53      
54      @Override
55      public ASTNode visitGrant(final GrantContext ctx) {
56          PostgreSQLGrantStatement result = new PostgreSQLGrantStatement();
57          if (containsTableSegment(ctx.privilegeClause())) {
58              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
59          }
60          return result;
61      }
62      
63      @Override
64      public ASTNode visitRevoke(final RevokeContext ctx) {
65          PostgreSQLRevokeStatement result = new PostgreSQLRevokeStatement();
66          if (containsTableSegment(ctx.privilegeClause())) {
67              result.getTables().addAll(getTableSegments(ctx.privilegeClause()));
68          }
69          return result;
70      }
71      
72      private boolean containsTableSegment(final PrivilegeClauseContext ctx) {
73          return null != ctx && null != ctx.onObjectClause() && null != ctx.onObjectClause().privilegeLevel() && null != ctx.onObjectClause().privilegeLevel().tableNames();
74      }
75      
76      @SuppressWarnings("unchecked")
77      private Collection<SimpleTableSegment> getTableSegments(final PrivilegeClauseContext ctx) {
78          return ((CollectionValue<SimpleTableSegment>) visit(ctx.onObjectClause().privilegeLevel().tableNames())).getValue();
79      }
80      
81      @Override
82      public ASTNode visitCreateUser(final CreateUserContext ctx) {
83          return new PostgreSQLCreateUserStatement();
84      }
85      
86      @Override
87      public ASTNode visitDropUser(final DropUserContext ctx) {
88          return new PostgreSQLDropUserStatement();
89      }
90      
91      @Override
92      public ASTNode visitAlterUser(final AlterUserContext ctx) {
93          return new PostgreSQLAlterUserStatement();
94      }
95      
96      @Override
97      public ASTNode visitCreateRole(final CreateRoleContext ctx) {
98          return new PostgreSQLCreateRoleStatement();
99      }
100     
101     @Override
102     public ASTNode visitAlterRole(final AlterRoleContext ctx) {
103         return new PostgreSQLAlterRoleStatement();
104     }
105     
106     @Override
107     public ASTNode visitDropRole(final DropRoleContext ctx) {
108         return new PostgreSQLDropRoleStatement();
109     }
110     
111     @Override
112     public ASTNode visitReassignOwned(final ReassignOwnedContext ctx) {
113         return new PostgreSQLReassignOwnedStatement();
114     }
115     
116     @Override
117     public ASTNode visitCreateGroup(final CreateGroupContext ctx) {
118         return new PostgreSQLCreateGroupStatement();
119     }
120 }