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.infra.merge.result.impl.memory;
19  
20  import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
21  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
22  import org.apache.shardingsphere.infra.merge.result.MergedResult;
23  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
24  import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
25  import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
26  
27  import java.io.InputStream;
28  import java.io.Reader;
29  import java.sql.Blob;
30  import java.sql.Clob;
31  import java.sql.SQLException;
32  import java.sql.SQLFeatureNotSupportedException;
33  import java.sql.SQLXML;
34  import java.util.Arrays;
35  import java.util.Calendar;
36  import java.util.Collection;
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * Memory merged result.
43   *
44   * @param <T> type of rule
45   */
46  public abstract class MemoryMergedResult<T extends ShardingSphereRule> implements MergedResult {
47      
48      private static final Collection<Class<?>> INVALID_MEMORY_TYPES = new HashSet<>(Arrays.asList(Blob.class, Clob.class, Reader.class, InputStream.class, SQLXML.class));
49      
50      private final Iterator<MemoryQueryResultRow> memoryResultSetRows;
51      
52      private MemoryQueryResultRow currentResultSetRow;
53      
54      private boolean wasNull;
55      
56      protected MemoryMergedResult(final T rule, final ShardingSphereSchema schema, final SQLStatementContext sqlStatementContext, final List<QueryResult> queryResults) throws SQLException {
57          List<MemoryQueryResultRow> memoryQueryResultRows = init(rule, schema, sqlStatementContext, queryResults);
58          memoryResultSetRows = memoryQueryResultRows.iterator();
59          if (!memoryQueryResultRows.isEmpty()) {
60              currentResultSetRow = memoryQueryResultRows.get(0);
61          }
62      }
63      
64      protected abstract List<MemoryQueryResultRow> init(T rule, ShardingSphereSchema schema, SQLStatementContext sqlStatementContext, List<QueryResult> queryResults) throws SQLException;
65      
66      @Override
67      public final boolean next() {
68          if (memoryResultSetRows.hasNext()) {
69              currentResultSetRow = memoryResultSetRows.next();
70              return true;
71          }
72          return false;
73      }
74      
75      @Override
76      public final Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
77          ShardingSpherePreconditions.checkNotContains(INVALID_MEMORY_TYPES, type, () -> new SQLFeatureNotSupportedException(String.format("Get value from `%s`", type.getName())));
78          Object result = currentResultSetRow.getCell(columnIndex);
79          wasNull = null == result;
80          return result;
81      }
82      
83      @Override
84      public final Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
85          // TODO implement with calendar
86          Object result = currentResultSetRow.getCell(columnIndex);
87          wasNull = null == result;
88          return result;
89      }
90      
91      @Override
92      public final InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
93          throw new SQLFeatureNotSupportedException(String.format("Get input stream from `%s`", type));
94      }
95      
96      @Override
97      public Reader getCharacterStream(final int columnIndex) throws SQLException {
98          throw new SQLFeatureNotSupportedException("Get Character stream");
99      }
100     
101     @Override
102     public final boolean wasNull() {
103         return wasNull;
104     }
105 }