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.proxy.backend.mysql.handler.admin.executor.show;
19  
20  import lombok.Getter;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.authority.checker.AuthorityChecker;
23  import org.apache.shardingsphere.authority.rule.AuthorityRule;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
25  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
27  import org.apache.shardingsphere.infra.merge.result.MergedResult;
28  import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataMergedResult;
29  import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
30  import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
31  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
32  import org.apache.shardingsphere.infra.util.regex.RegexUtils;
33  import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
34  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
35  import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.database.MySQLShowDatabasesStatement;
36  
37  import java.sql.Types;
38  import java.util.Collection;
39  import java.util.Collections;
40  import java.util.Optional;
41  import java.util.stream.Collectors;
42  
43  /**
44   * Show databases executor for MySQL.
45   */
46  @RequiredArgsConstructor
47  @Getter
48  public final class MySQLShowDatabasesExecutor implements DatabaseAdminQueryExecutor {
49      
50      private final MySQLShowDatabasesStatement sqlStatement;
51      
52      private MergedResult mergedResult;
53      
54      @Override
55      public void execute(final ConnectionSession connectionSession, final ShardingSphereMetaData metaData) {
56          mergedResult = new LocalDataMergedResult(getDatabaseNames(connectionSession, metaData));
57      }
58      
59      private Collection<LocalDataQueryResultRow> getDatabaseNames(final ConnectionSession connectionSession, final ShardingSphereMetaData metaData) {
60          AuthorityRule authorityRule = metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
61          AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, connectionSession.getConnectionContext().getGrantee());
62          return metaData.getAllDatabases().stream().map(ShardingSphereDatabase::getName).sorted()
63                  .filter(each -> checkLikePattern(each) && authorityChecker.isAuthorized(each)).map(LocalDataQueryResultRow::new).collect(Collectors.toList());
64      }
65      
66      private boolean checkLikePattern(final String databaseName) {
67          if (sqlStatement.getFilter().isPresent()) {
68              Optional<String> pattern = sqlStatement.getFilter().get().getLike().map(optional -> RegexUtils.convertLikePatternToRegex(optional.getPattern()));
69              return !pattern.isPresent() || databaseName.matches(pattern.get());
70          }
71          return true;
72      }
73      
74      @Override
75      public QueryResultMetaData getQueryResultMetaData() {
76          return new RawQueryResultMetaData(Collections.singletonList(new RawQueryResultColumnMetaData("SCHEMATA", "SCHEMA_NAME", "Database", Types.VARCHAR, "VARCHAR", 255, 0)));
77      }
78  }