Search in sources :

Example 6 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement 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)

Example 7 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteMetaImpl method prepareAndExecuteBatch.

@Override
public ExecuteBatchResult prepareAndExecuteBatch(final StatementHandle h, List<String> sqlCommands) throws NoSuchStatementException {
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalciteServerStatement statement = calciteConnection.server.getStatement(h);
    final List<Long> updateCounts = new ArrayList<>();
    final Meta.PrepareCallback callback = new Meta.PrepareCallback() {

        long updateCount;

        Signature signature;

        public Object getMonitor() {
            return statement;
        }

        public void clear() throws SQLException {
        }

        public void assign(Meta.Signature signature, Meta.Frame firstFrame, long updateCount) throws SQLException {
            this.signature = signature;
            this.updateCount = updateCount;
        }

        public void execute() throws SQLException {
            if (signature.statementType.canUpdate()) {
                final Iterable<Object> iterable = _createIterable(h, signature, ImmutableList.<TypedValue>of(), null);
                final Iterator<Object> iterator = iterable.iterator();
                updateCount = ((Number) iterator.next()).longValue();
            }
            updateCounts.add(updateCount);
        }
    };
    for (String sqlCommand : sqlCommands) {
        Util.discard(prepareAndExecute(h, sqlCommand, -1L, -1, callback));
    }
    return new ExecuteBatchResult(Longs.toArray(updateCounts));
}
Also used : Meta(org.apache.calcite.avatica.Meta) ArrayList(java.util.ArrayList) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement)

Example 8 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteMetaImpl method closeStatement.

@Override
public void closeStatement(StatementHandle h) {
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalciteServerStatement stmt;
    try {
        stmt = calciteConnection.server.getStatement(h);
    } catch (NoSuchStatementException e) {
        // statement is not valid; nothing to do
        return;
    }
    // stmt.close(); // TODO: implement
    calciteConnection.server.removeStatement(h);
}
Also used : CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 9 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class CalciteStatement method prepare.

protected <T> CalcitePrepare.CalciteSignature<T> prepare(Queryable<T> queryable) {
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalcitePrepare prepare = calciteConnection.prepareFactory.apply();
    final CalciteServerStatement serverStatement;
    try {
        serverStatement = calciteConnection.server.getStatement(handle);
    } catch (NoSuchStatementException e) {
        throw new AssertionError("invalid statement", e);
    }
    final CalcitePrepare.Context prepareContext = serverStatement.createPrepareContext();
    return prepare.prepareQueryable(prepareContext, queryable);
}
Also used : CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 10 with CalciteServerStatement

use of org.apache.calcite.server.CalciteServerStatement in project calcite by apache.

the class Frameworks method withPrepare.

/**
 * Initializes a container then calls user-specified code with a planner
 * and statement.
 *
 * @param action Callback containing user-specified code
 * @return Return value from action
 */
public static <R> R withPrepare(PrepareAction<R> action) {
    try {
        final Properties info = new Properties();
        if (action.config.getTypeSystem() != RelDataTypeSystem.DEFAULT) {
            info.setProperty(CalciteConnectionProperty.TYPE_SYSTEM.camelName(), action.config.getTypeSystem().getClass().getName());
        }
        Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
        final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
        return new CalcitePrepareImpl().perform(statement, action);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CalcitePrepareImpl(org.apache.calcite.prepare.CalcitePrepareImpl) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) Connection(java.sql.Connection) Properties(java.util.Properties)

Aggregations

CalciteServerStatement (org.apache.calcite.server.CalciteServerStatement)10 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)3 Properties (java.util.Properties)2 DataContext (org.apache.calcite.DataContext)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 Meta (org.apache.calcite.avatica.Meta)2 CalcitePrepare (org.apache.calcite.jdbc.CalcitePrepare)2 Context (org.apache.calcite.jdbc.CalcitePrepare.Context)2 ImmutableList (com.google.common.collect.ImmutableList)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 RelOptRule (org.apache.calcite.plan.RelOptRule)1 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)1 CalcitePrepareImpl (org.apache.calcite.prepare.CalcitePrepareImpl)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1