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.infra.database.core.metadata.database.system.SystemDatabase;
23  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
25  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
27  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
28  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.type.RawMemoryQueryResult;
29  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
30  import org.apache.shardingsphere.infra.merge.result.MergedResult;
31  import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
32  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
33  import org.apache.shardingsphere.infra.util.regex.RegexUtils;
34  import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
35  import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
36  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
37  import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
38  
39  import java.sql.Types;
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.Comparator;
43  import java.util.LinkedList;
44  import java.util.List;
45  import java.util.Optional;
46  import java.util.regex.Pattern;
47  import java.util.stream.Collectors;
48  
49  /**
50   * Show tables executor.
51   */
52  @RequiredArgsConstructor
53  public final class ShowTablesExecutor implements DatabaseAdminQueryExecutor {
54      
55      private final MySQLShowTablesStatement showTablesStatement;
56      
57      private final DatabaseType databaseType;
58      
59      @Getter
60      private QueryResultMetaData queryResultMetaData;
61      
62      @Getter
63      private MergedResult mergedResult;
64      
65      @Override
66      public void execute(final ConnectionSession connectionSession) {
67          String databaseName = showTablesStatement.getFromSchema().map(schema -> schema.getSchema().getIdentifier().getValue()).orElseGet(connectionSession::getDatabaseName);
68          queryResultMetaData = createQueryResultMetaData(databaseName);
69          mergedResult = new TransparentMergedResult(getQueryResult(databaseName));
70      }
71      
72      private QueryResultMetaData createQueryResultMetaData(final String databaseName) {
73          List<RawQueryResultColumnMetaData> columnNames = new LinkedList<>();
74          String tableColumnName = String.format("Tables_in_%s", databaseName);
75          columnNames.add(new RawQueryResultColumnMetaData("", tableColumnName, tableColumnName, Types.VARCHAR, "VARCHAR", 255, 0));
76          if (showTablesStatement.isContainsFull()) {
77              columnNames.add(new RawQueryResultColumnMetaData("", "Table_type", "Table_type", Types.VARCHAR, "VARCHAR", 20, 0));
78          }
79          return new RawQueryResultMetaData(columnNames);
80      }
81      
82      private QueryResult getQueryResult(final String databaseName) {
83          SystemDatabase systemDatabase = new SystemDatabase(databaseType);
84          if (!systemDatabase.getSystemSchemas().contains(databaseName) && !ProxyContext.getInstance().getContextManager().getDatabase(databaseName).isComplete()) {
85              return new RawMemoryQueryResult(queryResultMetaData, Collections.emptyList());
86          }
87          List<MemoryQueryResultDataRow> rows = getTables(databaseName).stream().map(each -> {
88              List<Object> rowValues = new LinkedList<>();
89              rowValues.add(each.getName());
90              if (showTablesStatement.isContainsFull()) {
91                  rowValues.add(each.getType());
92              }
93              return new MemoryQueryResultDataRow(rowValues);
94          }).collect(Collectors.toList());
95          return new RawMemoryQueryResult(queryResultMetaData, rows);
96      }
97      
98      private Collection<ShardingSphereTable> getTables(final String databaseName) {
99          Collection<ShardingSphereTable> tables = ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getSchema(databaseName).getTables().values();
100         Optional<Pattern> likePattern = getLikePattern();
101         return likePattern.isPresent()
102                 ? tables.stream().filter(each -> likePattern.get().matcher(each.getName()).matches()).sorted(Comparator.comparing(ShardingSphereTable::getName)).collect(Collectors.toList())
103                 : tables;
104     }
105     
106     private Optional<Pattern> getLikePattern() {
107         if (!showTablesStatement.getFilter().isPresent()) {
108             return Optional.empty();
109         }
110         Optional<String> pattern = showTablesStatement.getFilter().get().getLike().map(optional -> RegexUtils.convertLikePatternToRegex(optional.getPattern()));
111         return pattern.map(optional -> Pattern.compile(RegexUtils.convertLikePatternToRegex(optional), Pattern.CASE_INSENSITIVE));
112     }
113 }