Search in sources :

Example 1 with SinkKeyValueStreamListener

use of org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener in project elasticsearch-jdbc by jprante.

the class StandardSource method executeWithParameter.

/**
     * Execute SQL query command with parameter binding.
     *
     * @param command the SQL command
     * @throws SQLException when SQL execution gives an error
     * @throws IOException  when input/output error occurs
     */
private void executeWithParameter(SQLCommand command) throws Exception {
    PreparedStatement statement = null;
    ResultSet results = null;
    try {
        if (command.isQuery()) {
            statement = prepareQuery(command.getSQL());
            bind(statement, command.getParameters());
            logger.info("execute sql is {} ", statement.toString());
            results = executeQuery(statement);
            SinkKeyValueStreamListener<Object, Object> listener = new SinkKeyValueStreamListener<Object, Object>().output(context.getSink()).shouldIgnoreNull(shouldIgnoreNull()).shouldDetectGeo(shouldDetectGeo()).shouldDetectJson(shouldDetectJson());
            merge(command, results, listener);
        } else {
            statement = prepareUpdate(command.getSQL());
            bind(statement, command.getParameters());
            executeUpdate(statement);
        }
    } finally {
        close(results);
        close(statement);
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SinkKeyValueStreamListener(org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener)

Example 2 with SinkKeyValueStreamListener

use of org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener in project elasticsearch-jdbc by jprante.

the class StandardSource method execute.

/**
     * Execute SQL query command without parameter binding.
     *
     * @param command the SQL command
     * @throws SQLException when SQL execution gives an error
     * @throws IOException  when input/output error occurs
     */
private void execute(SQLCommand command) throws Exception {
    Statement statement = null;
    ResultSet results = null;
    try {
        if (command.isQuery()) {
            // use read connection
            // we must not use prepareStatement for Postgresql!
            // Postgresql requires direct use of executeQuery(sql) for cursor with fetchsize set.
            Connection connection = getConnectionForReading();
            if (connection != null) {
                logger.debug("{} using read connection {} for executing query", this, connection);
                statement = connection.createStatement();
                try {
                    statement.setQueryTimeout(getQueryTimeout());
                } catch (SQLFeatureNotSupportedException e) {
                    // Postgresql does not support setQueryTimeout()
                    logger.warn("driver does not support setQueryTimeout(), skipped");
                }
                results = executeQuery(statement, command.getSQL());
                if (shouldPrepareResultSetMetadata()) {
                    prepare(results.getMetaData());
                }
                SinkKeyValueStreamListener<Object, Object> listener = new SinkKeyValueStreamListener<Object, Object>().output(context.getSink()).shouldIgnoreNull(shouldIgnoreNull()).shouldDetectGeo(shouldDetectGeo()).shouldDetectJson(shouldDetectJson());
                merge(command, results, listener);
            }
        } else {
            // use write connection
            Connection connection = getConnectionForWriting();
            if (connection != null) {
                logger.debug("{} using write connection {} for executing insert/update", this, connection);
                statement = connection.createStatement();
                executeUpdate(statement, command.getSQL());
            }
        }
    } finally {
        close(results);
        close(statement);
    }
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) SinkKeyValueStreamListener(org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener)

Aggregations

PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SinkKeyValueStreamListener (org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener)2 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 Statement (java.sql.Statement)1