org.apache.calcite.avatica.jdbc.JdbcResultSet Maven / Gradle / Ivy
/*
* 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
*
* 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.
*/
package org.apache.calcite.avatica.jdbc;
import org.apache.calcite.avatica.AvaticaStatement;
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.ColumnMetaData.ArrayType;
import org.apache.calcite.avatica.ColumnMetaData.AvaticaType;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.SqlType;
import org.apache.calcite.avatica.util.DateTimeUtils;
import com.google.common.base.Optional;
import java.sql.Array;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
/** Implementation of {@link org.apache.calcite.avatica.Meta.MetaResultSet}
* upon a JDBC {@link java.sql.ResultSet}.
*
* @see org.apache.calcite.avatica.jdbc.JdbcMeta */
class JdbcResultSet extends Meta.MetaResultSet {
protected JdbcResultSet(String connectionId, int statementId,
boolean ownStatement, Meta.Signature signature, Meta.Frame firstFrame) {
this(connectionId, statementId, ownStatement, signature, firstFrame, -1L);
}
protected JdbcResultSet(String connectionId, int statementId,
boolean ownStatement, Meta.Signature signature, Meta.Frame firstFrame,
long updateCount) {
super(connectionId, statementId, ownStatement, signature, firstFrame, updateCount);
}
/** Creates a result set. */
public static JdbcResultSet create(String connectionId, int statementId,
ResultSet resultSet) {
// -1 still limits to 100 but -2 does not limit to any number
return create(connectionId, statementId, resultSet,
JdbcMeta.UNLIMITED_COUNT);
}
/** Creates a result set with maxRowCount.
*
* If {@code maxRowCount} is -2 ({@link JdbcMeta#UNLIMITED_COUNT}),
* returns an unlimited number of rows in a single frame; any other
* negative value (typically -1) returns an unlimited number of rows
* in frames of the default frame size. */
public static JdbcResultSet create(String connectionId, int statementId,
ResultSet resultSet, int maxRowCount) {
try {
Meta.Signature sig = JdbcMeta.signature(resultSet.getMetaData());
return create(connectionId, statementId, resultSet, maxRowCount, sig);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static JdbcResultSet create(String connectionId, int statementId,
ResultSet resultSet, int maxRowCount, Meta.Signature signature) {
try {
final Calendar calendar = DateTimeUtils.calendar();
final int fetchRowCount;
if (maxRowCount == JdbcMeta.UNLIMITED_COUNT) {
fetchRowCount = -1;
} else if (maxRowCount < 0L) {
fetchRowCount = AvaticaStatement.DEFAULT_FETCH_SIZE;
} else if (maxRowCount > AvaticaStatement.DEFAULT_FETCH_SIZE) {
fetchRowCount = AvaticaStatement.DEFAULT_FETCH_SIZE;
} else {
fetchRowCount = maxRowCount;
}
final Meta.Frame firstFrame = frame(null, resultSet, 0, fetchRowCount, calendar,
Optional.of(signature));
if (firstFrame.done) {
resultSet.close();
}
return new JdbcResultSet(connectionId, statementId, true, signature,
firstFrame);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/** Creates a empty result set with empty frame */
public static JdbcResultSet empty(String connectionId, int statementId,
Meta.Signature signature) {
return new JdbcResultSet(connectionId, statementId, true, signature,
Meta.Frame.EMPTY);
}
/** Creates a result set that only has an update count. */
public static JdbcResultSet count(String connectionId, int statementId,
int updateCount) {
return new JdbcResultSet(connectionId, statementId, true, null, null, updateCount);
}
/** Creates a frame containing a given number or unlimited number of rows
* from a result set. */
static Meta.Frame frame(StatementInfo info, ResultSet resultSet, long offset,
int fetchMaxRowCount, Calendar calendar, Optional sig) throws SQLException {
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
final int[] types = new int[columnCount];
Set arrayOffsets = new HashSet<>();
for (int i = 0; i < types.length; i++) {
types[i] = metaData.getColumnType(i + 1);
if (Types.ARRAY == types[i]) {
arrayOffsets.add(i);
}
}
final List