MemoryMergedResult.java 3.8 KB
Newer Older
1
/*
2 3 4 5 6 7
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
8 9 10 11 12 13 14 15 16 17
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

18
package org.apache.shardingsphere.infra.merge.result.impl.memory;
19

20 21 22
import org.apache.shardingsphere.infra.executor.sql.QueryResult;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
23
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
24
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
25 26 27 28 29 30 31 32 33

import java.io.InputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLXML;
import java.util.Calendar;
34 35
import java.util.Iterator;
import java.util.List;
36 37

/**
T
terrymanu 已提交
38
 * Memory merged result.
39
 *
L
Liang Zhang 已提交
40
 * @param <T> type of rule
41
 */
L
Liang Zhang 已提交
42
public abstract class MemoryMergedResult<T extends ShardingSphereRule> implements MergedResult {
43
    
44
    private final Iterator<MemoryQueryResultRow> memoryResultSetRows;
45
    
T
terrymanu 已提交
46
    private MemoryQueryResultRow currentResultSetRow;
47 48 49
    
    private boolean wasNull;
    
50 51
    protected MemoryMergedResult(final T rule, final ShardingSphereSchema schema, final SQLStatementContext sqlStatementContext, final List<QueryResult> queryResults) throws SQLException {
        List<MemoryQueryResultRow> memoryQueryResultRowList = init(rule, schema, sqlStatementContext, queryResults);
52 53 54 55 56 57
        memoryResultSetRows = memoryQueryResultRowList.iterator();
        if (!memoryQueryResultRowList.isEmpty()) {
            currentResultSetRow = memoryQueryResultRowList.get(0);
        }
    }
    
58
    protected abstract List<MemoryQueryResultRow> init(T rule, ShardingSphereSchema schema, SQLStatementContext sqlStatementContext, List<QueryResult> queryResults) throws SQLException;
59 60 61 62 63 64 65 66
    
    @Override
    public final boolean next() {
        if (memoryResultSetRows.hasNext()) {
            currentResultSetRow = memoryResultSetRows.next();
            return true;
        }
        return false;
67 68
    }
    
69
    @Override
T
terrymanu 已提交
70
    public final Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
71
        if (Blob.class == type || Clob.class == type || Reader.class == type || InputStream.class == type || SQLXML.class == type) {
T
terrymanu 已提交
72
            throw new SQLFeatureNotSupportedException(String.format("Get value from `%s`", type.getName()));
73 74 75 76 77 78 79
        }
        Object result = currentResultSetRow.getCell(columnIndex);
        wasNull = null == result;
        return result;
    }
    
    @Override
T
terrymanu 已提交
80
    public final Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
T
terrymanu 已提交
81
        // TODO implement with calendar
82 83 84 85 86 87
        Object result = currentResultSetRow.getCell(columnIndex);
        wasNull = null == result;
        return result;
    }
    
    @Override
T
terrymanu 已提交
88
    public final InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
T
terrymanu 已提交
89
        throw new SQLFeatureNotSupportedException(String.format("Get input stream from `%s`", type));
90 91 92
    }
    
    @Override
T
terrymanu 已提交
93
    public final boolean wasNull() {
94 95 96
        return wasNull;
    }
}