Search in sources :

Example 1 with QueryExecuteResult

use of org.apache.ignite.client.proto.query.event.QueryExecuteResult in project ignite-3 by apache.

the class JdbcQueryEventHandlerImpl method queryAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<QueryExecuteResult> queryAsync(QueryExecuteRequest req) {
    if (req.pageSize() <= 0) {
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Invalid fetch size : [fetchSize=" + req.pageSize() + ']'));
    }
    List<SqlCursor<List<?>>> cursors;
    try {
        QueryContext context = createQueryContext(req.getStmtType());
        List<SqlCursor<List<?>>> queryCursors = processor.query(context, req.schemaName(), req.sqlQuery(), req.arguments() == null ? OBJECT_EMPTY_ARRAY : req.arguments());
        cursors = queryCursors.stream().map(cursor -> new JdbcQueryCursor<>(req.maxRows(), cursor)).collect(Collectors.toList());
    } catch (Exception e) {
        StringWriter sw = getWriterWithStackTrace(e);
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Exception while executing query " + req.sqlQuery() + ". Error message: " + sw));
    }
    if (cursors.isEmpty()) {
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "At least one cursor is expected for query " + req.sqlQuery()));
    }
    List<QuerySingleResult> results = new ArrayList<>();
    try {
        for (SqlCursor<List<?>> cur : cursors) {
            QuerySingleResult res = createJdbcResult(cur, req);
            results.add(res);
        }
    } catch (Exception ex) {
        StringWriter sw = getWriterWithStackTrace(ex);
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Failed to fetch results for query " + req.sqlQuery() + ". Error message: " + sw));
    }
    return CompletableFuture.completedFuture(new QueryExecuteResult(results));
}
Also used : SqlCursor(org.apache.ignite.internal.sql.engine.SqlCursor) StringWriter(java.io.StringWriter) QueryExecuteResult(org.apache.ignite.client.proto.query.event.QueryExecuteResult) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) IntList(it.unimi.dsi.fastutil.ints.IntList) QueryContext(org.apache.ignite.internal.sql.engine.QueryContext) QuerySingleResult(org.apache.ignite.client.proto.query.event.QuerySingleResult) QueryValidationException(org.apache.ignite.internal.sql.engine.exec.QueryValidationException)

Example 2 with QueryExecuteResult

use of org.apache.ignite.client.proto.query.event.QueryExecuteResult in project ignite-3 by apache.

the class JdbcStatement method execute0.

/**
 * Execute the query with given parameters.
 *
 * @param sql  Sql query.
 * @param args Query parameters.
 * @throws SQLException Onj error.
 */
protected void execute0(JdbcStatementType stmtType, String sql, List<Object> args) throws SQLException {
    ensureNotClosed();
    closeResults();
    if (sql == null || sql.isEmpty()) {
        throw new SQLException("SQL query is empty.");
    }
    QueryExecuteRequest req = new QueryExecuteRequest(stmtType, schema, pageSize, maxRows, sql, args == null ? ArrayUtils.OBJECT_EMPTY_ARRAY : args.toArray());
    QueryExecuteResult res = conn.handler().queryAsync(req).join();
    if (!res.hasResults()) {
        throw IgniteQueryErrorCode.createJdbcSqlException(res.err(), res.status());
    }
    for (QuerySingleResult jdbcRes : res.results()) {
        if (!jdbcRes.hasResults()) {
            throw IgniteQueryErrorCode.createJdbcSqlException(jdbcRes.err(), jdbcRes.status());
        }
    }
    resSets = new ArrayList<>(res.results().size());
    for (QuerySingleResult jdbcRes : res.results()) {
        resSets.add(new JdbcResultSet(this, jdbcRes.cursorId(), pageSize, jdbcRes.last(), jdbcRes.items(), jdbcRes.isQuery(), false, jdbcRes.updateCount(), closeOnCompletion, conn.handler()));
    }
    assert !resSets.isEmpty() : "At least one results set is expected";
}
Also used : SQLException(java.sql.SQLException) QueryExecuteResult(org.apache.ignite.client.proto.query.event.QueryExecuteResult) QueryExecuteRequest(org.apache.ignite.client.proto.query.event.QueryExecuteRequest) QuerySingleResult(org.apache.ignite.client.proto.query.event.QuerySingleResult)

Aggregations

QueryExecuteResult (org.apache.ignite.client.proto.query.event.QueryExecuteResult)2 QuerySingleResult (org.apache.ignite.client.proto.query.event.QuerySingleResult)2 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 StringWriter (java.io.StringWriter)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 QueryExecuteRequest (org.apache.ignite.client.proto.query.event.QueryExecuteRequest)1 QueryContext (org.apache.ignite.internal.sql.engine.QueryContext)1 SqlCursor (org.apache.ignite.internal.sql.engine.SqlCursor)1 QueryValidationException (org.apache.ignite.internal.sql.engine.exec.QueryValidationException)1