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;
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.util.regex.RegexUtils;
31  import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
32  import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
33  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
34  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
35  
36  import java.sql.Types;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.Optional;
40  import java.util.stream.Collectors;
41  
42  /**
43   * Show databases executor.
44   */
45  @RequiredArgsConstructor
46  @Getter
47  public final class ShowDatabasesExecutor implements DatabaseAdminQueryExecutor {
48      
49      private final MySQLShowDatabasesStatement showDatabasesStatement;
50      
51      private MergedResult mergedResult;
52      
53      @Override
54      public void execute(final ConnectionSession connectionSession) {
55          mergedResult = new LocalDataMergedResult(getDatabaseNames(connectionSession));
56      }
57      
58      private Collection<LocalDataQueryResultRow> getDatabaseNames(final ConnectionSession connectionSession) {
59          AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
60          AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, connectionSession.getGrantee());
61          return ProxyContext.getInstance().getAllDatabaseNames().stream().sorted()
62                  .filter(each -> checkLikePattern(each) && authorityChecker.isAuthorized(each)).map(LocalDataQueryResultRow::new).collect(Collectors.toList());
63      }
64      
65      private boolean checkLikePattern(final String databaseName) {
66          if (showDatabasesStatement.getFilter().isPresent()) {
67              Optional<String> pattern = showDatabasesStatement.getFilter().get().getLike().map(optional -> RegexUtils.convertLikePatternToRegex(optional.getPattern()));
68              return !pattern.isPresent() || databaseName.matches(pattern.get());
69          }
70          return true;
71      }
72      
73      @Override
74      public QueryResultMetaData getQueryResultMetaData() {
75          return new RawQueryResultMetaData(Collections.singletonList(new RawQueryResultColumnMetaData("SCHEMATA", "SCHEMA_NAME", "Database", Types.VARCHAR, "VARCHAR", 255, 0)));
76      }
77  }