use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult in project ignite by apache.
the class JdbcThinStatement method execute0.
/**
* @param stmtType Expected statement type.
* @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.");
checkStatementBatchEmpty();
SqlCommand nativeCmd = null;
if (stmtType != JdbcStatementType.SELECT_STATEMENT_TYPE && isEligibleForNativeParsing(sql))
nativeCmd = tryParseNative(sql);
if (nativeCmd != null) {
conn.executeNative(sql, nativeCmd);
resultSets = Collections.singletonList(resultSetForUpdate(0));
// as an ordinary batch citizen.
return;
}
if (conn.isStream()) {
if (stmtType == JdbcStatementType.SELECT_STATEMENT_TYPE)
throw new SQLException("executeQuery() method is not allowed in streaming mode.", SqlStateCode.INTERNAL_ERROR, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
conn.addBatch(sql, args);
resultSets = Collections.singletonList(resultSetForUpdate(0));
return;
}
JdbcResult res0 = conn.sendRequest(new JdbcQueryExecuteRequest(stmtType, schema, pageSize, maxRows, sql, args == null ? null : args.toArray(new Object[args.size()])));
assert res0 != null;
if (res0 instanceof JdbcBulkLoadAckResult)
res0 = sendFile((JdbcBulkLoadAckResult) res0);
if (res0 instanceof JdbcQueryExecuteResult) {
JdbcQueryExecuteResult res = (JdbcQueryExecuteResult) res0;
resultSets = Collections.singletonList(new JdbcThinResultSet(this, res.getQueryId(), pageSize, res.last(), res.items(), res.isQuery(), conn.autoCloseServerCursor(), res.updateCount(), closeOnCompletion));
} else if (res0 instanceof JdbcQueryExecuteMultipleStatementsResult) {
JdbcQueryExecuteMultipleStatementsResult res = (JdbcQueryExecuteMultipleStatementsResult) res0;
List<JdbcResultInfo> resInfos = res.results();
resultSets = new ArrayList<>(resInfos.size());
boolean firstRes = true;
for (JdbcResultInfo rsInfo : resInfos) {
if (!rsInfo.isQuery())
resultSets.add(resultSetForUpdate(rsInfo.updateCount()));
else {
if (firstRes) {
firstRes = false;
resultSets.add(new JdbcThinResultSet(this, rsInfo.queryId(), pageSize, res.isLast(), res.items(), true, conn.autoCloseServerCursor(), -1, closeOnCompletion));
} else {
resultSets.add(new JdbcThinResultSet(this, rsInfo.queryId(), pageSize, false, null, true, conn.autoCloseServerCursor(), -1, closeOnCompletion));
}
}
}
} else
throw new SQLException("Unexpected result [res=" + res0 + ']');
assert resultSets.size() > 0 : "At least one results set is expected";
}
use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult in project ignite by apache.
the class JdbcThinStatement method sendFile.
/**
* Sends a file to server in batches via multiple {@link JdbcBulkLoadBatchRequest}s.
*
* @param cmdRes Result of invoking COPY command: contains server-parsed
* bulk load parameters, such as file name and batch size.
* @return Bulk load result.
* @throws SQLException On error.
*/
private JdbcResult sendFile(JdbcBulkLoadAckResult cmdRes) throws SQLException {
String fileName = cmdRes.params().localFileName();
int batchSize = cmdRes.params().packetSize();
int batchNum = 0;
try {
try (InputStream input = new BufferedInputStream(new FileInputStream(fileName))) {
byte[] buf = new byte[batchSize];
int readBytes;
while ((readBytes = input.read(buf)) != -1) {
if (readBytes == 0)
continue;
JdbcResult res = conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_CONTINUE, readBytes == buf.length ? buf : Arrays.copyOf(buf, readBytes)));
if (!(res instanceof JdbcQueryExecuteResult))
throw new SQLException("Unknown response sent by the server: " + res);
}
return conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_FINISHED_EOF));
}
} catch (Exception e) {
try {
conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum, JdbcBulkLoadBatchRequest.CMD_FINISHED_ERROR));
} catch (SQLException e1) {
throw new SQLException("Cannot send finalization request: " + e1.getMessage(), e);
}
if (e instanceof SQLException)
throw (SQLException) e;
else
throw new SQLException("Failed to read file: '" + fileName + "'", SqlStateCode.INTERNAL_ERROR, e);
}
}
use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult in project ignite by apache.
the class JdbcThinStatement method execute0.
/**
* @param sql Sql query.
* @param args Query parameters.
*
* @throws SQLException Onj error.
*/
protected void execute0(String sql, List<Object> args) throws SQLException {
ensureNotClosed();
if (rs != null) {
rs.close();
rs = null;
}
alreadyRead = false;
if (sql == null || sql.isEmpty())
throw new SQLException("SQL query is empty.");
try {
JdbcQueryExecuteResult res = conn.io().queryExecute(conn.getSchema(), pageSize, maxRows, sql, args);
assert res != null;
rs = new JdbcThinResultSet(this, res.getQueryId(), pageSize, res.last(), res.items(), res.isQuery(), res.updateCount());
} catch (IOException e) {
conn.close();
throw new SQLException("Failed to query Ignite.", e);
} catch (IgniteCheckedException e) {
throw new SQLException("Failed to query Ignite.", e);
}
}
Aggregations