Search in sources :

Example 1 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite by apache.

the class CalciteConnectionImpl method enumerable.

public <T> Enumerable<T> enumerable(Meta.StatementHandle handle, CalcitePrepare.CalciteSignature<T> signature) throws SQLException {
    Map<String, Object> map = Maps.newLinkedHashMap();
    AvaticaStatement statement = lookupStatement(handle);
    final List<TypedValue> parameterValues = TROJAN.getParameterValues(statement);
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
        throw new SQLException("exception while executing query: unbound parameter");
    }
    for (Ord<TypedValue> o : Ord.zip(parameterValues)) {
        map.put("?" + o.i, o.e.toLocal());
    }
    map.putAll(signature.internalParameters);
    final AtomicBoolean cancelFlag;
    try {
        cancelFlag = getCancelFlag(handle);
    } catch (NoSuchStatementException e) {
        throw new RuntimeException(e);
    }
    map.put(DataContext.Variable.CANCEL_FLAG.camelName, cancelFlag);
    final DataContext dataContext = createDataContext(map, signature.rootSchema);
    return signature.enumerable(dataContext);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContext(org.apache.calcite.DataContext) SQLException(java.sql.SQLException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 2 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite by apache.

the class CalciteMetaImpl method prepare.

@Override
public StatementHandle prepare(ConnectionHandle ch, String sql, long maxRowCount) {
    final StatementHandle h = createStatement(ch);
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalciteServerStatement statement;
    try {
        statement = calciteConnection.server.getStatement(h);
    } catch (NoSuchStatementException e) {
        // Not possible. We just created a statement.
        throw new AssertionError("missing statement", e);
    }
    final Context context = statement.createPrepareContext();
    final CalcitePrepare.Query<Object> query = toQuery(context, sql);
    h.signature = calciteConnection.parseQuery(query, context, maxRowCount);
    statement.setSignature(h.signature);
    return h;
}
Also used : DataContext(org.apache.calcite.DataContext) Context(org.apache.calcite.jdbc.CalcitePrepare.Context) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 3 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite-avatica by apache.

the class LocalService method apply.

public ExecuteResponse apply(ExecuteRequest request) {
    try (final Context ignore = executeTimer.start()) {
        try {
            final Meta.ExecuteResult executeResult = meta.execute(request.statementHandle, request.parameterValues, AvaticaUtils.toSaturatedInt(request.maxRowCount));
            final List<ResultSetResponse> results = new ArrayList<>(executeResult.resultSets.size());
            for (Meta.MetaResultSet metaResultSet : executeResult.resultSets) {
                results.add(toResponse(metaResultSet));
            }
            return new ExecuteResponse(results, false, serverLevelRpcMetadata);
        } catch (NoSuchStatementException e) {
            return new ExecuteResponse(null, true, serverLevelRpcMetadata);
        }
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) Meta(org.apache.calcite.avatica.Meta) ArrayList(java.util.ArrayList) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 4 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite-avatica by apache.

the class JdbcMeta method execute.

@Override
public ExecuteResult execute(StatementHandle h, List<TypedValue> parameterValues, int maxRowsInFirstFrame) throws NoSuchStatementException {
    try {
        if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
            throw new SQLException("exception while executing query: unbound parameter");
        }
        final StatementInfo statementInfo = statementCache.getIfPresent(h.id);
        if (null == statementInfo) {
            throw new NoSuchStatementException(h);
        }
        final List<MetaResultSet> resultSets;
        final PreparedStatement preparedStatement = (PreparedStatement) statementInfo.statement;
        if (parameterValues != null) {
            for (int i = 0; i < parameterValues.size(); i++) {
                TypedValue o = parameterValues.get(i);
                preparedStatement.setObject(i + 1, o.toJdbc(calendar));
            }
        }
        if (preparedStatement.execute()) {
            final Signature signature2;
            if (preparedStatement.isWrapperFor(AvaticaPreparedStatement.class)) {
                signature2 = h.signature;
            } else {
                h.signature = signature(preparedStatement.getMetaData(), preparedStatement.getParameterMetaData(), h.signature.sql, Meta.StatementType.SELECT);
                signature2 = h.signature;
            }
            // Make sure we set this for subsequent fetch()'s to find the result set.
            statementInfo.setResultSet(preparedStatement.getResultSet());
            if (statementInfo.getResultSet() == null) {
                resultSets = Collections.<MetaResultSet>singletonList(JdbcResultSet.empty(h.connectionId, h.id, signature2));
            } else {
                resultSets = Collections.<MetaResultSet>singletonList(JdbcResultSet.create(h.connectionId, h.id, statementInfo.getResultSet(), maxRowsInFirstFrame, signature2));
            }
        } else {
            resultSets = Collections.<MetaResultSet>singletonList(JdbcResultSet.count(h.connectionId, h.id, preparedStatement.getUpdateCount()));
        }
        return new ExecuteResult(resultSets);
    } catch (SQLException e) {
        throw propagate(e);
    }
}
Also used : SQLException(java.sql.SQLException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) PreparedStatement(java.sql.PreparedStatement) AvaticaPreparedStatement(org.apache.calcite.avatica.AvaticaPreparedStatement) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Example 5 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite-avatica by apache.

the class JdbcMeta method prepareAndExecuteBatch.

@Override
public ExecuteBatchResult prepareAndExecuteBatch(StatementHandle h, List<String> sqlCommands) throws NoSuchStatementException {
    try {
        // Get the statement
        final StatementInfo info = statementCache.getIfPresent(h.id);
        if (info == null) {
            throw new NoSuchStatementException(h);
        }
        // addBatch() for each sql command
        final Statement stmt = info.statement;
        for (String sqlCommand : sqlCommands) {
            stmt.addBatch(sqlCommand);
        }
        // Execute the batch and return the results
        return new ExecuteBatchResult(AvaticaUtils.executeLargeBatch(stmt));
    } catch (SQLException e) {
        throw propagate(e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) AvaticaPreparedStatement(org.apache.calcite.avatica.AvaticaPreparedStatement) Statement(java.sql.Statement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Aggregations

NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)16 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)6 AvaticaPreparedStatement (org.apache.calcite.avatica.AvaticaPreparedStatement)6 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 NoSuchConnectionException (org.apache.calcite.avatica.NoSuchConnectionException)3 TypedValue (org.apache.calcite.avatica.remote.TypedValue)3 CalciteServerStatement (org.apache.calcite.server.CalciteServerStatement)3 DataContext (org.apache.calcite.DataContext)2 Meta (org.apache.calcite.avatica.Meta)2 Context (org.apache.calcite.avatica.metrics.Timer.Context)2 ISE (org.apache.druid.java.util.common.ISE)2 AuthenticationResult (org.apache.druid.server.security.AuthenticationResult)2 ForbiddenException (org.apache.druid.server.security.ForbiddenException)2 Connection (java.sql.Connection)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Nonnull (javax.annotation.Nonnull)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 Common (org.apache.calcite.avatica.proto.Common)1