Search in sources :

Example 1 with JdbcQueryExecuteResult

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";
}
Also used : JdbcBulkLoadAckResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadAckResult) JdbcQueryExecuteRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteRequest) JdbcResultInfo(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResultInfo) JdbcQueryExecuteMultipleStatementsResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteMultipleStatementsResult) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) JdbcQueryExecuteResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult) ArrayList(java.util.ArrayList) List(java.util.List) JdbcResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand)

Example 2 with JdbcQueryExecuteResult

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);
    }
}
Also used : JdbcBulkLoadBatchRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest) BufferedInputStream(java.io.BufferedInputStream) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JdbcQueryExecuteResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult) JdbcResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult) FileInputStream(java.io.FileInputStream) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) BatchUpdateException(java.sql.BatchUpdateException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 3 with JdbcQueryExecuteResult

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);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SQLException(java.sql.SQLException) JdbcQueryExecuteResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult) IOException(java.io.IOException)

Aggregations

SQLException (java.sql.SQLException)3 JdbcQueryExecuteResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult)3 JdbcResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 JdbcBulkLoadAckResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadAckResult)1 JdbcBulkLoadBatchRequest (org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest)1 JdbcQueryExecuteMultipleStatementsResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteMultipleStatementsResult)1 JdbcQueryExecuteRequest (org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteRequest)1 JdbcResultInfo (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResultInfo)1 SqlParseException (org.apache.ignite.internal.sql.SqlParseException)1 SqlCommand (org.apache.ignite.internal.sql.command.SqlCommand)1