Search in sources :

Example 1 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class DmlStatementsProcessor method updateSqlFieldsBatched.

/**
 * Execute DML statement, possibly with few re-attempts in case of concurrent data modifications.
 *
 * @param schemaName Schema.
 * @param conn Connection.
 * @param prepared Prepared statement.
 * @param fieldsQry Original query.
 * @param loc Query locality flag.
 * @param filters Cache name and key filter.
 * @param cancel Cancel.
 * @return Update result (modified items count and failed keys).
 * @throws IgniteCheckedException if failed.
 */
private Collection<UpdateResult> updateSqlFieldsBatched(String schemaName, Connection conn, Prepared prepared, SqlFieldsQueryEx fieldsQry, boolean loc, IndexingQueryFilter filters, GridQueryCancel cancel) throws IgniteCheckedException {
    List<Object[]> argss = fieldsQry.batchedArguments();
    UpdatePlan plan = getPlanForStatement(schemaName, conn, prepared, fieldsQry, loc, null);
    if (plan.hasRows() && plan.mode() == UpdateMode.INSERT) {
        GridCacheContext<?, ?> cctx = plan.cacheContext();
        CacheOperationContext opCtx = setKeepBinaryContext(cctx);
        try {
            List<List<List<?>>> cur = plan.createRows(argss);
            List<UpdateResult> res = processDmlSelectResultBatched(plan, cur, fieldsQry.getPageSize());
            return res;
        } finally {
            cctx.operationContextPerCall(opCtx);
        }
    } else {
        // Fallback to previous mode.
        Collection<UpdateResult> ress = new ArrayList<>(argss.size());
        SQLException batchException = null;
        int[] cntPerRow = new int[argss.size()];
        int cntr = 0;
        for (Object[] args : argss) {
            SqlFieldsQueryEx qry0 = (SqlFieldsQueryEx) fieldsQry.copy();
            qry0.clearBatchedArgs();
            qry0.setArgs(args);
            UpdateResult res;
            try {
                res = updateSqlFields(schemaName, conn, prepared, qry0, loc, filters, cancel);
                cntPerRow[cntr++] = (int) res.counter();
                ress.add(res);
            } catch (Exception e) {
                String sqlState;
                int code;
                if (e instanceof IgniteSQLException) {
                    sqlState = ((IgniteSQLException) e).sqlState();
                    code = ((IgniteSQLException) e).statusCode();
                } else {
                    sqlState = SqlStateCode.INTERNAL_ERROR;
                    code = IgniteQueryErrorCode.UNKNOWN;
                }
                batchException = chainException(batchException, new SQLException(e.getMessage(), sqlState, code, e));
                cntPerRow[cntr++] = Statement.EXECUTE_FAILED;
            }
        }
        if (batchException != null) {
            BatchUpdateException e = new BatchUpdateException(batchException.getMessage(), batchException.getSQLState(), batchException.getErrorCode(), cntPerRow, batchException);
            throw new IgniteCheckedException(e);
        }
        return ress;
    }
}
Also used : CacheOperationContext(org.apache.ignite.internal.processors.cache.CacheOperationContext) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) IgniteQueryErrorCode.createJdbcSqlException(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.createJdbcSqlException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) BatchUpdateException(java.sql.BatchUpdateException) EntryProcessorException(javax.cache.processor.EntryProcessorException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) List(java.util.List) ArrayList(java.util.ArrayList) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan) BatchUpdateException(java.sql.BatchUpdateException)

Example 2 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class JdbcRequestHandler method getParametersMeta.

/**
 * @param req Request.
 * @return Response.
 */
private JdbcResponse getParametersMeta(JdbcMetaParamsRequest req) {
    String schemaName = prepareSchemaName(req.schemaName());
    SqlFieldsQueryEx qry = new SqlFieldsQueryEx(req.sql(), null);
    setupQuery(qry, schemaName);
    try {
        List<JdbcParameterMeta> meta = connCtx.kernalContext().query().getIndexing().parameterMetaData(schemaName, qry);
        JdbcMetaParamsResult res = new JdbcMetaParamsResult(meta);
        return resultToResonse(res);
    } catch (Exception e) {
        U.error(log, "Failed to get parameters metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
        return exceptionToResult(e);
    }
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BatchUpdateException(java.sql.BatchUpdateException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 3 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class OdbcRequestHandler method getParamsMeta.

/**
 * {@link OdbcQueryGetQueryMetaRequest} command handler.
 * Returns metadata for the parameters to be set.
 *
 * @param req Get params metadata request.
 * @return Response.
 */
private ClientListenerResponse getParamsMeta(OdbcQueryGetParamsMetaRequest req) {
    try {
        String sql = OdbcEscapeUtils.parse(req.query());
        String schema = OdbcUtils.prepareSchema(req.schema());
        SqlFieldsQueryEx qry = makeQuery(schema, sql);
        List<JdbcParameterMeta> params = ctx.query().getIndexing().parameterMetaData(schema, qry);
        byte[] typeIds = new byte[params.size()];
        for (int i = 0; i < params.size(); ++i) {
            int sqlType = params.get(i).type();
            typeIds[i] = sqlTypeToBinary(sqlType);
        }
        OdbcQueryGetParamsMetaResult res = new OdbcQueryGetParamsMetaResult(typeIds);
        return new OdbcResponse(res);
    } catch (Exception e) {
        U.error(log, "Failed to get params metadata [reqId=" + req.requestId() + ", req=" + req + ']', e);
        return exceptionToResult(e);
    }
}
Also used : JdbcParameterMeta(org.apache.ignite.internal.processors.odbc.jdbc.JdbcParameterMeta) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) BatchUpdateException(java.sql.BatchUpdateException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 4 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class OdbcRequestHandler method makeQuery.

/**
 * Make query considering handler configuration.
 * @param schema Schema.
 * @param sql SQL request.
 * @return Query instance.
 */
private SqlFieldsQueryEx makeQuery(String schema, String sql) {
    SqlFieldsQueryEx qry = new SqlFieldsQueryEx(sql, null);
    qry.setDistributedJoins(cliCtx.isDistributedJoins());
    qry.setEnforceJoinOrder(cliCtx.isEnforceJoinOrder());
    qry.setReplicatedOnly(cliCtx.isReplicatedOnly());
    qry.setCollocated(cliCtx.isCollocated());
    qry.setLazy(cliCtx.isLazy());
    qry.setSchema(OdbcUtils.prepareSchema(schema));
    qry.setSkipReducerOnUpdate(cliCtx.isSkipReducerOnUpdate());
    qry.setNestedTxMode(nestedTxMode);
    qry.setQueryInitiatorId(connCtx.clientDescriptor());
    return qry;
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)

Example 5 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class OdbcRequestHandler method makeQuery.

/**
 * Make query considering handler configuration.
 * @param schema Schema.
 * @param sql SQL request.
 * @param args Arguments.
 * @param autoCommit Autocommit transaction.
 * @param timeout Query timeout.
 * @return Query instance.
 */
private SqlFieldsQueryEx makeQuery(String schema, String sql, Object[] args, int timeout, boolean autoCommit) {
    SqlFieldsQueryEx qry = makeQuery(schema, sql);
    qry.setArgs(args);
    qry.setAutoCommit(autoCommit);
    QueryUtils.withQueryTimeout(qry, timeout, TimeUnit.SECONDS);
    return qry;
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)

Aggregations

SqlFieldsQueryEx (org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)42 Test (org.junit.Test)23 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)17 List (java.util.List)11 ArrayList (java.util.ArrayList)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)8 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 BatchUpdateException (java.sql.BatchUpdateException)6 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)6 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)5 SQLException (java.sql.SQLException)4 IgniteException (org.apache.ignite.IgniteException)4 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 BulkLoadContextCursor (org.apache.ignite.cache.query.BulkLoadContextCursor)3 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)3 IgniteLogger (org.apache.ignite.IgniteLogger)2 QueryCursor (org.apache.ignite.cache.query.QueryCursor)2 GridKernalContext (org.apache.ignite.internal.GridKernalContext)2