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
}
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));
}
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);
}
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);
}
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);
}
}
Aggregations