Search in sources :

Example 51 with IgniteSQLException

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

the class H2Utils method checkQuery.

/**
 * Check if query is valid.
 *
 * @param idx Indexing.
 * @param cacheIds Cache IDs.
 * @param tbls Tables.
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
public static void checkQuery(IgniteH2Indexing idx, List<Integer> cacheIds, Collection<QueryTable> tbls) {
    GridCacheSharedContext sharedCtx = idx.kernalContext().cache().context();
    // Check query parallelism.
    int expectedParallelism = 0;
    for (int i = 0; i < cacheIds.size(); i++) {
        Integer cacheId = cacheIds.get(i);
        GridCacheContext cctx = sharedCtx.cacheContext(cacheId);
        if (cctx == null) {
            throw new IgniteSQLException("Failed to find cache [cacheId=" + cacheId + ']', IgniteQueryErrorCode.TABLE_NOT_FOUND);
        }
        if (!cctx.isPartitioned())
            continue;
        if (expectedParallelism == 0)
            expectedParallelism = cctx.config().getQueryParallelism();
        else if (cctx.config().getQueryParallelism() != expectedParallelism) {
            throw new IllegalStateException("Using indexes with different parallelism levels in same query is " + "forbidden.");
        }
    }
    // Check for joins between system views and normal tables.
    if (!F.isEmpty(tbls)) {
        for (QueryTable tbl : tbls) {
            if (QueryUtils.SCHEMA_SYS.equals(tbl.schema())) {
                if (!F.isEmpty(cacheIds)) {
                    throw new IgniteSQLException("Normal tables and system views cannot be used in the same query.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
                } else
                    return;
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) QueryTable(org.apache.ignite.internal.processors.cache.query.QueryTable)

Example 52 with IgniteSQLException

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

the class IgniteH2Indexing method querySqlFields.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "StringEquality" })
@Override
public List<FieldsQueryCursor<List<?>>> querySqlFields(String schemaName, SqlFieldsQuery qry, @Nullable SqlClientContext cliCtx, boolean keepBinary, boolean failOnMultipleStmts, GridQueryCancel cancel) {
    try {
        List<FieldsQueryCursor<List<?>>> res = new ArrayList<>(1);
        SqlFieldsQuery remainingQry = qry;
        while (remainingQry != null) {
            Span qrySpan = ctx.tracing().create(SQL_QRY, MTC.span()).addTag(SQL_SCHEMA, () -> schemaName);
            try (TraceSurroundings ignored = MTC.supportContinual(qrySpan)) {
                // Parse.
                QueryParserResult parseRes = parser.parse(schemaName, remainingQry, !failOnMultipleStmts);
                qrySpan.addTag(SQL_QRY_TEXT, () -> parseRes.queryDescriptor().sql());
                remainingQry = parseRes.remainingQuery();
                // Get next command.
                QueryDescriptor newQryDesc = parseRes.queryDescriptor();
                QueryParameters newQryParams = parseRes.queryParameters();
                // since they pass parameters differently.
                if (!newQryDesc.batched()) {
                    int qryParamsCnt = F.isEmpty(newQryParams.arguments()) ? 0 : newQryParams.arguments().length;
                    if (qryParamsCnt < parseRes.parametersCount())
                        throw new IgniteSQLException("Invalid number of query parameters [expected=" + parseRes.parametersCount() + ", actual=" + qryParamsCnt + ']');
                }
                // Check if cluster state is valid.
                checkClusterState(parseRes);
                // Execute.
                if (parseRes.isCommand()) {
                    QueryParserResultCommand cmd = parseRes.command();
                    assert cmd != null;
                    FieldsQueryCursor<List<?>> cmdRes = executeCommand(newQryDesc, newQryParams, cliCtx, cmd);
                    res.add(cmdRes);
                } else if (parseRes.isDml()) {
                    QueryParserResultDml dml = parseRes.dml();
                    assert dml != null;
                    List<? extends FieldsQueryCursor<List<?>>> dmlRes = executeDml(newQryDesc, newQryParams, dml, cancel);
                    res.addAll(dmlRes);
                } else {
                    assert parseRes.isSelect();
                    QueryParserResultSelect select = parseRes.select();
                    assert select != null;
                    List<? extends FieldsQueryCursor<List<?>>> qryRes = executeSelect(newQryDesc, newQryParams, select, keepBinary, cancel);
                    res.addAll(qryRes);
                }
            } catch (Throwable th) {
                qrySpan.addTag(ERROR, th::getMessage).end();
                throw th;
            }
        }
        return res;
    } catch (RuntimeException | Error e) {
        GridNearTxLocal tx = ctx.cache().context().tm().tx();
        if (tx != null && tx.mvccSnapshot() != null && (!(e instanceof IgniteSQLException) || /* Parsing errors should not rollback Tx. */
        ((IgniteSQLException) e).sqlState() != SqlStateCode.PARSING_EXCEPTION))
            tx.setRollbackOnly();
        throw e;
    }
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ArrayList(java.util.ArrayList) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Span(org.apache.ignite.internal.processors.tracing.Span) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 53 with IgniteSQLException

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

the class IgniteH2Indexing method executeCommand.

/**
 * Execute command.
 *
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param cliCtx CLient context.
 * @param cmd Command (native).
 * @return Result.
 */
private FieldsQueryCursor<List<?>> executeCommand(QueryDescriptor qryDesc, QueryParameters qryParams, @Nullable SqlClientContext cliCtx, QueryParserResultCommand cmd) {
    if (cmd.noOp())
        return zeroCursor();
    SqlCommand cmdNative = cmd.commandNative();
    GridSqlStatement cmdH2 = cmd.commandH2();
    if (qryDesc.local()) {
        throw new IgniteSQLException("DDL statements are not supported for LOCAL caches", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    long qryId = registerRunningQuery(qryDesc, qryParams, null, null);
    CommandResult res = null;
    Exception failReason = null;
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_CMD_QRY_EXECUTE, MTC.span()))) {
        res = cmdProc.runCommand(qryDesc.sql(), cmdNative, cmdH2, qryParams, cliCtx, qryId);
        return res.cursor();
    } catch (IgniteException e) {
        failReason = e;
        throw e;
    } catch (IgniteCheckedException e) {
        failReason = e;
        throw new IgniteSQLException("Failed to execute DDL statement [stmt=" + qryDesc.sql() + ", err=" + e.getMessage() + ']', e);
    } finally {
        if (res == null || res.unregisterRunningQuery())
            runningQryMgr.unregister(qryId, failReason);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) BatchUpdateException(java.sql.BatchUpdateException) ClusterTopologyServerNotFoundException(org.apache.ignite.internal.cluster.ClusterTopologyServerNotFoundException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteClusterReadOnlyException(org.apache.ignite.internal.processors.cache.distributed.dht.IgniteClusterReadOnlyException) CacheServerNotFoundException(org.apache.ignite.cache.CacheServerNotFoundException) SQLException(java.sql.SQLException) IgniteException(org.apache.ignite.IgniteException) CacheException(javax.cache.CacheException) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand)

Example 54 with IgniteSQLException

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

the class ConnectionManager method connection.

/**
 * @return H2 connection wrapper.
 */
public H2PooledConnection connection() {
    try {
        H2Connection conn = connPool.borrow();
        if (conn == null)
            conn = newConnection();
        H2PooledConnection connWrp = new H2PooledConnection(conn, this);
        usedConns.add(conn);
        assert !conn.connection().isClosed() : "Connection is closed [conn=" + conn + ']';
        return connWrp;
    } catch (SQLException e) {
        throw new IgniteSQLException("Failed to initialize DB connection: " + dbUrl, e);
    }
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 55 with IgniteSQLException

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

the class H2Connection method schema.

/**
 * @param schema Schema name set on this connection.
 */
void schema(@Nullable String schema) {
    if (schema != null && !F.eq(this.schema, schema)) {
        try {
            if (schema.trim().isEmpty()) {
                throw new IgniteSQLException("Failed to set schema for DB connection. " + "Schema name could not be an empty string");
            }
            this.schema = schema;
            conn.setSchema(schema);
        } catch (SQLException e) {
            throw new IgniteSQLException("Failed to set schema for DB connection for thread [schema=" + schema + "]", e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Aggregations

IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)112 SQLException (java.sql.SQLException)38 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)37 ArrayList (java.util.ArrayList)34 List (java.util.List)27 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)19 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)18 Column (org.h2.table.Column)18 IgniteException (org.apache.ignite.IgniteException)16 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)16 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)12 BatchUpdateException (java.sql.BatchUpdateException)11 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)11 LinkedHashMap (java.util.LinkedHashMap)10 CacheException (javax.cache.CacheException)9 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)9 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)9 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)9 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8