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