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.connector.sane;
19  
20  import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
21  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
22  import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
23  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
25  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.type.RawMemoryQueryResult;
27  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
28  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
29  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
30  
31  import java.sql.SQLException;
32  import java.sql.Types;
33  import java.util.Collections;
34  import java.util.Optional;
35  
36  /**
37   * Sane query result engine.
38   */
39  public final class SaneQueryResultEngine {
40      
41      private final DialectSaneQueryResultEngine dialectEngine;
42      
43      public SaneQueryResultEngine(final DatabaseType databaseType) {
44          dialectEngine = DatabaseTypedSPILoader.findService(DialectSaneQueryResultEngine.class, databaseType).orElse(null);
45      }
46      
47      /**
48       * Get sane query result.
49       *
50       * @param sqlStatement SQL statement
51       * @param ex SQL exception
52       * @return sane execute result
53       */
54      public Optional<ExecuteResult> getSaneQueryResult(final SQLStatement sqlStatement, final SQLException ex) {
55          if (null == dialectEngine) {
56              return sqlStatement instanceof SelectStatement ? Optional.of(getDefaultQueryResult()) : Optional.empty();
57          }
58          return dialectEngine.getSaneQueryResult(sqlStatement, ex);
59      }
60      
61      private QueryResult getDefaultQueryResult() {
62          RawQueryResultColumnMetaData queryResultColumnMetaData = new RawQueryResultColumnMetaData("", "", "", Types.VARCHAR, "VARCHAR", 255, 0);
63          MemoryQueryResultDataRow resultDataRow = new MemoryQueryResultDataRow(Collections.singletonList("1"));
64          return new RawMemoryQueryResult(new RawQueryResultMetaData(Collections.singletonList(queryResultColumnMetaData)), Collections.singletonList(resultDataRow));
65      }
66  }