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