@Override public void close() { // Note: No already-closed exception for close(). super.close(); }
@Override protected void cancel() { if (cursor instanceof DrillCursor) { hasPendingCancellationNotification = true; ((DrillCursor) cursor).cancel(); } else { super.cancel(); } }
public void execute() throws SQLException { if (statement.openResultSet != null) { statement.openResultSet.execute(); isUpdateCapable(statement); } } };
@Override protected ResultSet newInstance() throws Exception { UnregisteredDriver driver = new TestDriver(); AvaticaConnection connection = new AvaticaConnection(driver, driver.createFactory(), "jdbc:avatica", new Properties()) { }; StatementHandle handle = mock(StatementHandle.class); AvaticaStatement statement = new AvaticaStatement(connection, handle, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT) { }; Signature signature = new Signature(Collections.emptyList(), "", Collections.emptyList(), Collections.emptyMap(), null, Meta.StatementType.SELECT); AvaticaResultSet resultSet = new AvaticaResultSet(statement, new QueryState(""), signature, null, DateTimeUtils.UTC_ZONE, null); resultSet.close(); assertTrue("Resultset is not closed", resultSet.isClosed()); return resultSet; } }
@Override public boolean isClosed() throws SQLException { // Note: No already-closed exception for isClosed(). return super.isClosed(); }
/** Returns whether a a statement is capable of updates and if so, * and the statement's {@code updateCount} is still -1, proceeds to * get updateCount value from statement's resultSet. * * <p>Handles "ROWCOUNT" object as Number or List * * @param statement Statement * @throws SQLException on error */ private void isUpdateCapable(final AvaticaStatement statement) throws SQLException { Meta.Signature signature = statement.getSignature(); if (signature == null || signature.statementType == null) { return; } if (signature.statementType.canUpdate() && statement.updateCount == -1) { statement.openResultSet.next(); Object obj = statement.openResultSet.getObject(ROWCOUNT_COLUMN_NAME); if (obj instanceof Number) { statement.updateCount = ((Number) obj).intValue(); } else if (obj instanceof List) { statement.updateCount = ((Number) ((List<Object>) obj).get(0)).intValue(); } else { throw helper.createException("Not a valid return result."); } statement.openResultSet = null; } }
@Override public ResultSet create(AvaticaType elementType, Iterable<Object> elements) throws SQLException { // The ColumnMetaData for offset "1" in the ResultSet for an Array. ScalarType arrayOffsetType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.PRIMITIVE_INT); // Two columns (types) in the ResultSet we will create List<ColumnMetaData> types = Arrays.asList(ColumnMetaData.dummy(arrayOffsetType, false), ColumnMetaData.dummy(elementType, true)); List<List<Object>> rows = createResultSetRowsForArrayData(elements); // `(List<Object>) rows` is a compile error. @SuppressWarnings({ "unchecked", "rawtypes" }) List<Object> untypedRows = (List<Object>) ((List) rows); try (ListIteratorCursor cursor = new ListIteratorCursor(rows.iterator())) { final String sql = "MOCKED"; QueryState state = new QueryState(sql); Meta.Signature signature = new Meta.Signature(types, sql, Collections.<AvaticaParameter>emptyList(), Collections.<String, Object>emptyMap(), Meta.CursorFactory.LIST, Meta.StatementType.SELECT); AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, sql, signature); Meta.Frame frame = new Meta.Frame(0, true, untypedRows); AvaticaResultSet resultSet = new AvaticaResultSet(null, state, signature, resultSetMetaData, timeZone, frame); resultSet.execute2(cursor, types); return resultSet; } }
@Override public boolean next() throws SQLException { checkOpen(); // TODO: Resolve following comments (possibly obsolete because of later // addition of preceding call to checkOpen. Also, NOTE that the // following check, and maybe some checkOpen() calls, probably must // synchronize on the statement, per the comment on AvaticaStatement's // openResultSet: // Next may be called after close has been called (for example after a user // cancellation) which in turn sets the cursor to null. So we must check // before we call next. // TODO: handle next() after close is called in the Avatica code. if (cursor != null) { return super.next(); } else { return false; } }
@Override public Object getObject( String columnLabel ) throws SQLException { throwIfClosed(); return super.getObject( columnLabel ); }
public AvaticaResultSet newResultSet(AvaticaStatement statement, QueryState state, Meta.Signature signature, TimeZone timeZone, Meta.Frame firstFrame) throws SQLException { final ResultSetMetaData metaData = newResultSetMetaData(statement, signature); return new AvaticaResultSet(statement, state, signature, metaData, timeZone, firstFrame); }
/** A means for anyone who has a trojan to call the protected method * {@link org.apache.calcite.avatica.AvaticaResultSet#execute()}. * @throws SQLException if execute fails for some reason. */ public ResultSet execute(AvaticaResultSet resultSet) throws SQLException { return resultSet.execute(); }
@Override protected ResultSet newInstance() throws Exception { UnregisteredDriver driver = new TestDriver(); AvaticaConnection connection = new AvaticaConnection(driver, driver.createFactory(), "jdbc:avatica", new Properties()) { }; StatementHandle handle = mock(StatementHandle.class); AvaticaStatement statement = new AvaticaStatement(connection, handle, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT) { }; Signature signature = new Signature(Collections.emptyList(), "", Collections.emptyList(), Collections.emptyMap(), null, Meta.StatementType.SELECT); AvaticaResultSet resultSet = new AvaticaResultSet(statement, new QueryState(""), signature, null, DateTimeUtils.UTC_ZONE, null); resultSet.close(); assertTrue("Resultset is not closed", resultSet.isClosed()); return resultSet; } }
protected void checkOpen() throws SQLException { if (isClosed()) { throw AvaticaConnection.HELPER.createException("ResultSet closed"); } } /**
/** Returns whether a a statement is capable of updates and if so, * and the statement's {@code updateCount} is still -1, proceeds to * get updateCount value from statement's resultSet. * * <p>Handles "ROWCOUNT" object as Number or List * * @param statement Statement * @throws SQLException on error */ private void isUpdateCapable(final AvaticaStatement statement) throws SQLException { Meta.Signature signature = statement.getSignature(); if (signature == null || signature.statementType == null) { return; } if (signature.statementType.canUpdate() && statement.updateCount == -1) { statement.openResultSet.next(); Object obj = statement.openResultSet.getObject(ROWCOUNT_COLUMN_NAME); if (obj instanceof Number) { statement.updateCount = ((Number) obj).intValue(); } else if (obj instanceof List) { @SuppressWarnings("unchecked") final List<Number> numbers = (List<Number>) obj; statement.updateCount = numbers.get(0).intValue(); } else { throw HELPER.createException("Not a valid return result."); } statement.openResultSet = null; } }
@Override public ResultSet create(AvaticaType elementType, Iterable<Object> elements) throws SQLException { // The ColumnMetaData for offset "1" in the ResultSet for an Array. ScalarType arrayOffsetType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.PRIMITIVE_INT); // Two columns (types) in the ResultSet we will create List<ColumnMetaData> types = Arrays.asList(ColumnMetaData.dummy(arrayOffsetType, false), ColumnMetaData.dummy(elementType, true)); List<List<Object>> rows = createResultSetRowsForArrayData(elements); // `(List<Object>) rows` is a compile error. @SuppressWarnings({ "unchecked", "rawtypes" }) List<Object> untypedRows = (List<Object>) ((List) rows); try (ListIteratorCursor cursor = new ListIteratorCursor(rows.iterator())) { final String sql = "MOCKED"; QueryState state = new QueryState(sql); Meta.Signature signature = new Meta.Signature(types, sql, Collections.<AvaticaParameter>emptyList(), Collections.<String, Object>emptyMap(), Meta.CursorFactory.LIST, Meta.StatementType.SELECT); AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, sql, signature); Meta.Frame frame = new Meta.Frame(0, true, untypedRows); AvaticaResultSet resultSet = new AvaticaResultSet(null, state, signature, resultSetMetaData, timeZone, frame); resultSet.execute2(cursor, types); return resultSet; } }
@Override public boolean next() throws SQLException { throwIfClosed(); // TODO: Resolve following comments (possibly obsolete because of later // addition of preceding call to throwIfClosed. Also, NOTE that the // following check, and maybe some throwIfClosed() calls, probably must // synchronize on the statement, per the comment on AvaticaStatement's // openResultSet: // Next may be called after close has been called (for example after a user // cancellation) which in turn sets the cursor to null. So we must check // before we call next. // TODO: handle next() after close is called in the Avatica code. if (cursor != null) { return super.next(); } else { return false; } }
@Override public <T> T getObject( int columnIndex, Class<T> type ) throws SQLException { throwIfClosed(); return super.getObject( columnIndex, type ); }