com.qubole.quark.fatjdbc.QuarkMetaResultSet Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2015. Qubole Inc
* Licensed 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 com.qubole.quark.fatjdbc;
import org.apache.calcite.avatica.AvaticaStatement;
import org.apache.calcite.avatica.ColumnMetaData;
import org.apache.calcite.avatica.Meta;
import org.apache.calcite.avatica.util.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Array;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
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.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
/**
* Implementation of {@link org.apache.calcite.avatica.Meta.MetaResultSet}
* upon a JDBC {@link java.sql.ResultSet}.
*
* @see QuarkMetaImpl
*/
public class QuarkMetaResultSet extends Meta.MetaResultSet {
protected static final Logger LOG = LoggerFactory.getLogger(QuarkMetaResultSet.class);
protected QuarkMetaResultSet(String connectionId,
int statementId,
boolean ownStatement,
Meta.Signature signature,
Meta.Frame firstFrame) {
this(connectionId, statementId, ownStatement, signature, firstFrame, -1L);
}
protected QuarkMetaResultSet(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 QuarkMetaResultSet 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,
QuarkMetaImpl.UNLIMITED_COUNT);
}
/**
* Creates a result set with maxRowCount.
* If {@code maxRowCount} is -2 ({@link QuarkMetaImpl#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 QuarkMetaResultSet create(String connectionId, int statementId,
ResultSet resultSet, long maxRowCount) {
try {
Meta.Signature sig = QuarkMetaImpl.signature(resultSet.getMetaData());
return create(connectionId, statementId, resultSet, maxRowCount, sig);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static QuarkMetaResultSet create(String connectionId, int statementId,
Iterator iterator,
long maxRowCount, Meta.Signature signature) {
try {
final Calendar calendar = Calendar.getInstance(DateTimeUtils.GMT_ZONE);
final int fetchRowCount;
if (maxRowCount == QuarkMetaImpl.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 = (int) maxRowCount;
}
final Meta.Frame firstFrame;
if (!iterator.hasNext()) {
firstFrame = Meta.Frame.EMPTY;
} else {
firstFrame = frame(iterator, signature.columns, 0, fetchRowCount, calendar);
}
return new QuarkMetaResultSet(connectionId, statementId, true, signature,
firstFrame);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static QuarkMetaResultSet create(String connectionId, int statementId,
ResultSet resultSet,
long maxRowCount, Meta.Signature signature) {
try {
final Calendar calendar = Calendar.getInstance(DateTimeUtils.GMT_ZONE);
final int fetchRowCount;
if (maxRowCount == QuarkMetaImpl.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 = (int) maxRowCount;
}
final Meta.Frame firstFrame = frame(resultSet, 0, fetchRowCount, calendar);
if (firstFrame.done) {
resultSet.close();
}
return new QuarkMetaResultSet(connectionId, statementId, true, signature,
firstFrame);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Creates a empty result set with empty frame
*/
public static QuarkMetaResultSet empty(String connectionId, int statementId,
Meta.Signature signature) {
return new QuarkMetaResultSet(connectionId, statementId, true, signature,
Meta.Frame.EMPTY);
}
/**
* Creates a result set that only has an update count.
*/
public static QuarkMetaResultSet count(String connectionId, int statementId,
int updateCount) {
return new QuarkMetaResultSet(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(ResultSet resultSet, long offset,
int fetchMaxRowCount, Calendar calendar) throws SQLException {
final ResultSetMetaData metaData = resultSet.getMetaData();
final int columnCount = metaData.getColumnCount();
final int[] types = new int[columnCount];
for (int i = 0; i < types.length; i++) {
types[i] = metaData.getColumnType(i + 1);
}
final List