use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.
the class JdbcPreparedStatement method parameterMetaData.
/**
* Fetches parameters metadata of the specified query.
*/
private ParameterMetaData parameterMetaData() throws SQLException {
SqlFieldsQueryEx qry = new SqlFieldsQueryEx(sql, null);
setupQuery(qry);
try {
List<JdbcParameterMeta> params = conn.ignite().context().query().getIndexing().parameterMetaData(conn.schemaName(), qry);
return new JdbcThinParameterMetadata(params);
} catch (IgniteSQLException ex) {
throw ex.toJdbcException();
} catch (SqlCacheException e) {
throw e.toJdbcException();
}
}
use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.
the class JdbcUtils method convertToSqlException.
/**
* Convert exception to {@link SQLException}.
*
* @param e Converted Exception.
* @param msgForUnknown Message for non-convertable exception.
* @param sqlStateForUnknown SQLSTATE for non-convertable exception.
* @return JDBC {@link SQLException}.
* @see IgniteQueryErrorCode
*/
public static SQLException convertToSqlException(Exception e, String msgForUnknown, String sqlStateForUnknown) {
SQLException sqlEx = null;
Throwable t = e;
while (sqlEx == null && t != null) {
if (t instanceof SQLException) {
if (t.getCause() instanceof IgniteSQLException)
return ((IgniteSQLException) t.getCause()).toJdbcException();
else if (t.getCause() instanceof SqlCacheException)
return ((SqlCacheException) t.getCause()).toJdbcException();
else
return (SQLException) t;
} else if (t instanceof IgniteSQLException)
return ((IgniteSQLException) t).toJdbcException();
else if (t instanceof SecurityException)
return new SQLException(t.getMessage(), sqlStateForUnknown, t);
else if (t instanceof SqlCacheException)
return ((SqlCacheException) t).toJdbcException();
t = t.getCause();
}
return new SQLException(msgForUnknown, sqlStateForUnknown, e);
}
use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.
the class DmlUtils method doInsertBatched.
/**
* Execute INSERT statement plan.
*
* @param plan Plan to execute.
* @param cursor Cursor to take inserted data from. I.e. list of batch arguments for each query.
* @param pageSize Batch size for streaming, anything <= 0 for single page operations.
* @return Number of items affected.
* @throws IgniteCheckedException if failed, particularly in case of duplicate keys.
*/
private static List<UpdateResult> doInsertBatched(UpdatePlan plan, List<List<List<?>>> cursor, int pageSize) throws IgniteCheckedException {
GridCacheContext cctx = plan.cacheContext();
DmlBatchSender snd = new DmlBatchSender(cctx, pageSize, cursor.size());
int rowNum = 0;
SQLException resEx = null;
for (List<List<?>> qryRow : cursor) {
for (List<?> row : qryRow) {
try {
final IgniteBiTuple keyValPair = plan.processRow(row);
snd.add(keyValPair.getKey(), new DmlStatementsProcessor.InsertEntryProcessor(keyValPair.getValue()), rowNum);
} catch (Exception e) {
String sqlState;
int code;
if (e instanceof IgniteSQLException) {
sqlState = ((IgniteSQLException) e).sqlState();
code = ((IgniteSQLException) e).statusCode();
} else if (e instanceof SqlCacheException) {
sqlState = ((SqlCacheException) e).sqlState();
code = ((SqlCacheException) e).statusCode();
} else {
sqlState = SqlStateCode.INTERNAL_ERROR;
code = IgniteQueryErrorCode.UNKNOWN;
}
resEx = chainException(resEx, new SQLException(e.getMessage(), sqlState, code, e));
snd.setFailed(rowNum);
}
}
rowNum++;
}
try {
snd.flush();
} catch (Exception e) {
resEx = chainException(resEx, new SQLException(e.getMessage(), SqlStateCode.INTERNAL_ERROR, IgniteQueryErrorCode.UNKNOWN, e));
}
resEx = chainException(resEx, snd.error());
if (!F.isEmpty(snd.failedKeys())) {
SQLException e = new SQLException("Failed to INSERT some keys because they are already in cache [keys=" + snd.failedKeys() + ']', SqlStateCode.CONSTRAINT_VIOLATION, DUPLICATE_KEY);
resEx = chainException(resEx, e);
}
if (resEx != null) {
BatchUpdateException e = new BatchUpdateException(resEx.getMessage(), resEx.getSQLState(), resEx.getErrorCode(), snd.perRowCounterAsArray(), resEx);
throw new IgniteCheckedException(e);
}
int[] cntPerRow = snd.perRowCounterAsArray();
List<UpdateResult> res = new ArrayList<>(cntPerRow.length);
for (int i = 0; i < cntPerRow.length; i++) {
int cnt = cntPerRow[i];
res.add(new UpdateResult(cnt, X.EMPTY_OBJECT_ARRAY));
}
return res;
}
use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.
the class GridReduceQueryExecutor method onNextPage.
/**
* @param node Node.
* @param msg Message.
*/
public void onNextPage(final ClusterNode node, final GridQueryNextPageResponse msg) {
try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_PAGE_RESP, MTC.span()))) {
final long qryReqId = msg.queryRequestId();
final int qry = msg.query();
final int seg = msg.segmentId();
final ReduceQueryRun r = runs.get(qryReqId);
if (// Already finished with error or canceled.
r == null)
return;
final int pageSize = r.pageSize();
Reducer idx = r.reducers().get(msg.query());
ReduceResultPage page;
try {
page = new ReduceResultPage(ctx, node.id(), msg) {
@Override
public void fetchNextPage() {
if (r.hasErrorOrRetry()) {
if (r.exception() != null)
throw r.exception();
assert r.retryCause() != null;
throw new CacheException(r.retryCause());
}
try {
GridQueryNextPageRequest msg0 = new GridQueryNextPageRequest(qryReqId, qry, seg, pageSize, (byte) GridH2QueryRequest.setDataPageScanEnabled(0, r.isDataPageScanEnabled()));
if (node.isLocal())
h2.mapQueryExecutor().onNextPageRequest(node, msg0);
else
ctx.io().sendToGridTopic(node, GridTopic.TOPIC_QUERY, msg0, GridIoPolicy.QUERY_POOL);
} catch (IgniteCheckedException e) {
throw new CacheException("Failed to fetch data from node: " + node.id(), e);
}
}
};
} catch (Exception e) {
U.error(log, "Error in message.", e);
MTC.span().addTag(ERROR, e::getMessage);
int errCode = 0;
IgniteSQLException sqlCause = X.cause(e, IgniteSQLException.class);
if (sqlCause != null)
errCode = sqlCause.statusCode();
else {
SqlCacheException sqlCacheException = X.cause(e, SqlCacheException.class);
if (sqlCacheException != null)
errCode = sqlCacheException.statusCode();
}
fail(r, node.id(), "Error in message.", GridQueryFailResponse.GENERAL_ERROR, errCode);
return;
}
idx.addPage(page);
if (msg.retry() != null)
r.setStateOnRetry(node.id(), msg.retry(), msg.retryCause());
else if (// Count down only on each first page received.
msg.page() == 0)
r.onFirstPage();
}
}
use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.
the class QueryParser method prepareDmlStatement.
/**
* Prepare DML statement.
*
* @param planKey Plan key.
* @param prepared Prepared.
* @return Statement.
*/
private QueryParserResultDml prepareDmlStatement(QueryDescriptor planKey, Prepared prepared) {
if (F.eq(QueryUtils.sysSchemaName(), planKey.schemaName()))
throw new IgniteSQLException("DML statements are not supported on " + planKey.schemaName() + " schema", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
// Prepare AST.
GridSqlQueryParser parser = new GridSqlQueryParser(false, log);
GridSqlStatement stmt = parser.parse(prepared);
List<GridH2Table> tbls = parser.tablesForDml();
// available on local node.
for (GridH2Table h2tbl : tbls) H2Utils.checkAndStartNotStartedCache(idx.kernalContext(), h2tbl);
// Check MVCC mode.
GridCacheContextInfo ctx = null;
boolean mvccEnabled = false;
for (GridH2Table h2tbl : tbls) {
GridCacheContextInfo curCtx = h2tbl.cacheInfo();
boolean curMvccEnabled = curCtx.config().getAtomicityMode() == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT;
if (ctx == null) {
ctx = curCtx;
mvccEnabled = curMvccEnabled;
} else if (curMvccEnabled != mvccEnabled)
MvccUtils.throwAtomicityModesMismatchException(ctx.config(), curCtx.config());
}
// Get streamer info.
GridH2Table streamTbl = null;
if (GridSqlQueryParser.isStreamableInsertStatement(prepared)) {
GridSqlInsert insert = (GridSqlInsert) stmt;
streamTbl = DmlAstUtils.gridTableForElement(insert.into()).dataTable();
}
// Create update plan.
UpdatePlan plan;
try {
plan = UpdatePlanBuilder.planForStatement(planKey, stmt, mvccEnabled, idx, log, forceFillAbsentPKsWithDefaults);
} catch (Exception e) {
if (e instanceof IgniteSQLException)
throw (IgniteSQLException) e;
else if (e instanceof SqlCacheException)
throw (SqlCacheException) e;
else
throw new IgniteSQLException("Failed to prepare update plan.", e);
}
return new QueryParserResultDml(stmt, mvccEnabled, streamTbl, plan);
}
Aggregations