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.exception.dialect.exception.syntax.database.UnknownDatabaseException;
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.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.MySQLShowCreateDatabaseStatement;
35  
36  import java.sql.Types;
37  import java.util.Arrays;
38  import java.util.LinkedList;
39  import java.util.List;
40  
41  /**
42   * Show create database executor.
43   */
44  @RequiredArgsConstructor
45  @Getter
46  public final class ShowCreateDatabaseExecutor implements DatabaseAdminQueryExecutor {
47      
48      private static final String CREATE_DATABASE_PATTERN = "CREATE DATABASE `%s`;";
49      
50      private static final String DATABASE = "Database";
51      
52      private static final String CREATE_DATABASE = "CREATE DATABASE ";
53      
54      private final MySQLShowCreateDatabaseStatement showCreateDatabaseStatement;
55      
56      private QueryResultMetaData queryResultMetaData;
57      
58      private MergedResult mergedResult;
59      
60      @Override
61      public void execute(final ConnectionSession connectionSession) {
62          queryResultMetaData = createQueryResultMetaData();
63          mergedResult = new TransparentMergedResult(getQueryResult(showCreateDatabaseStatement.getDatabaseName()));
64      }
65      
66      private QueryResult getQueryResult(final String databaseName) {
67          if (!ProxyContext.getInstance().databaseExists(databaseName)) {
68              throw new UnknownDatabaseException(databaseName);
69          }
70          List<MemoryQueryResultDataRow> rows = new LinkedList<>();
71          rows.add(new MemoryQueryResultDataRow(Arrays.asList(databaseName, String.format(CREATE_DATABASE_PATTERN, databaseName))));
72          return new RawMemoryQueryResult(queryResultMetaData, rows);
73      }
74      
75      private QueryResultMetaData createQueryResultMetaData() {
76          List<RawQueryResultColumnMetaData> columnMetaData = Arrays.asList(new RawQueryResultColumnMetaData("", DATABASE, DATABASE, Types.VARCHAR, "VARCHAR", 255, 0),
77                  new RawQueryResultColumnMetaData("", CREATE_DATABASE, CREATE_DATABASE, Types.VARCHAR, "VARCHAR", 255, 0));
78          return new RawQueryResultMetaData(columnMetaData);
79      }
80  }