Search in sources :

Example 1 with Context

use of org.apache.calcite.jdbc.CalcitePrepare.Context 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 2 with Context

use of org.apache.calcite.jdbc.CalcitePrepare.Context in project calcite by apache.

the class CalciteMetaImpl method prepareAndExecute.

@Override
public ExecuteResult prepareAndExecute(StatementHandle h, String sql, long maxRowCount, int maxRowsInFirstFrame, PrepareCallback callback) throws NoSuchStatementException {
    final CalcitePrepare.CalciteSignature<Object> signature;
    try {
        synchronized (callback.getMonitor()) {
            callback.clear();
            final CalciteConnectionImpl calciteConnection = getConnection();
            final CalciteServerStatement statement = calciteConnection.server.getStatement(h);
            final Context context = statement.createPrepareContext();
            final CalcitePrepare.Query<Object> query = toQuery(context, sql);
            signature = calciteConnection.parseQuery(query, context, maxRowCount);
            statement.setSignature(signature);
            final int updateCount;
            switch(signature.statementType) {
                case CREATE:
                case DROP:
                case ALTER:
                case OTHER_DDL:
                    // DDL produces no result set
                    updateCount = 0;
                    break;
                default:
                    // SELECT and DML produces result set
                    updateCount = -1;
                    break;
            }
            callback.assign(signature, null, updateCount);
        }
        callback.execute();
        final MetaResultSet metaResultSet = MetaResultSet.create(h.connectionId, h.id, false, signature, null);
        return new ExecuteResult(ImmutableList.of(metaResultSet));
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
// TODO: share code with prepare and createIterable
}
Also used : DataContext(org.apache.calcite.DataContext) Context(org.apache.calcite.jdbc.CalcitePrepare.Context) SQLException(java.sql.SQLException) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement)

Aggregations

DataContext (org.apache.calcite.DataContext)2 Context (org.apache.calcite.jdbc.CalcitePrepare.Context)2 CalciteServerStatement (org.apache.calcite.server.CalciteServerStatement)2 SQLException (java.sql.SQLException)1 NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)1