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