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.binder.context.segment.select.projection.engine.ProjectionEngine;
23  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
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.executor.sql.execute.result.query.impl.raw.type.RawMemoryQueryResult;
28  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
29  import org.apache.shardingsphere.infra.merge.result.MergedResult;
30  import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
31  import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
32  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
33  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
34  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
35  
36  import java.sql.Types;
37  import java.util.ArrayList;
38  import java.util.Collection;
39  import java.util.Collections;
40  import java.util.LinkedList;
41  import java.util.List;
42  import java.util.Optional;
43  import java.util.stream.Collectors;
44  
45  /**
46   * No resource show executor.
47   */
48  @Getter
49  @RequiredArgsConstructor
50  public final class NoResourceShowExecutor implements DatabaseAdminQueryExecutor {
51      
52      private MergedResult mergedResult;
53      
54      private final SelectStatement sqlStatement;
55      
56      private Collection<Object> expressions = Collections.emptyList();
57      
58      @Override
59      public void execute(final ConnectionSession connectionSession) {
60          expressions = sqlStatement.getProjections().getProjections().stream().filter(each -> !(each instanceof ShorthandProjectionSegment))
61                  .map(each -> new ProjectionEngine(null).createProjection(each))
62                  .filter(Optional::isPresent).map(each -> each.get().getAlias().isPresent() ? each.get().getAlias().get() : each.get().getExpression()).collect(Collectors.toList());
63          mergedResult = new TransparentMergedResult(getQueryResult());
64      }
65      
66      private QueryResult getQueryResult() {
67          List<MemoryQueryResultDataRow> rows = new LinkedList<>();
68          if (expressions.isEmpty()) {
69              rows.add(new MemoryQueryResultDataRow(Collections.singletonList("")));
70              return new RawMemoryQueryResult(getQueryResultMetaData(), rows);
71          }
72          List<Object> row = new ArrayList<>(expressions);
73          Collections.fill(row, "");
74          rows.add(new MemoryQueryResultDataRow(row));
75          return new RawMemoryQueryResult(getQueryResultMetaData(), rows);
76      }
77      
78      @Override
79      public QueryResultMetaData getQueryResultMetaData() {
80          if (expressions.isEmpty()) {
81              RawQueryResultColumnMetaData defaultColumnMetaData = new RawQueryResultColumnMetaData("", "", "", Types.VARCHAR, "VARCHAR", 100, 0);
82              return new RawQueryResultMetaData(Collections.singletonList(defaultColumnMetaData));
83          }
84          List<RawQueryResultColumnMetaData> columns = expressions.stream().map(each -> new RawQueryResultColumnMetaData("", each.toString(), each.toString(), Types.VARCHAR, "VARCHAR", 100, 0))
85                  .collect(Collectors.toList());
86          return new RawQueryResultMetaData(columns);
87      }
88  }