use of org.skife.jdbi.v2.tweak.BaseStatementCustomizer in project irontest by zheng-wang.
the class DBTeststepRunner method run.
protected BasicTeststepRun run(Teststep teststep) throws Exception {
BasicTeststepRun basicTeststepRun = new BasicTeststepRun();
DBAPIResponse response = new DBAPIResponse();
String request = (String) teststep.getRequest();
Endpoint endpoint = teststep.getEndpoint();
DBI jdbi = new DBI(endpoint.getUrl(), endpoint.getUsername(), getDecryptedEndpointPassword());
// get SQL statements (trimmed and without comments) and JDBI script object
List<String> statements = IronTestUtils.getStatements(request);
sanityCheckTheStatements(statements);
Handle handle = jdbi.open();
if (SQLStatementType.isSelectStatement(statements.get(0))) {
// the request is a select statement
RetainingColumnOrderResultSetMapper resultSetMapper = new RetainingColumnOrderResultSetMapper();
// use statements.get(0) instead of the raw request, as Oracle does not support trailing semicolon in select statement
Query<Map<String, Object>> query = handle.createQuery(statements.get(0)).map(resultSetMapper);
// obtain columnNames in case the query returns no row
final List<String> columnNames = new ArrayList<String>();
query.addStatementCustomizer(new BaseStatementCustomizer() {
public void afterExecution(PreparedStatement stmt, StatementContext ctx) throws SQLException {
ResultSetMetaData metaData = stmt.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
columnNames.add(metaData.getColumnLabel(i).toLowerCase());
}
}
});
// limit the number of returned rows
List<Map<String, Object>> rows = query.list(5000);
response.setColumnNames(columnNames);
response.setRowsJSON(jacksonObjectMapper.writeValueAsString(rows));
} else {
// the request is one or more non-select statements
Script script = handle.createScript(request);
int[] returnValues = script.execute();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < returnValues.length; i++) {
String statementType = SQLStatementType.getByStatement(statements.get(i)).toString();
sb.append(returnValues[i]).append(" row(s) ").append(statementType.toLowerCase()).append(statementType.endsWith("E") ? "d" : "ed").append("\n");
response.setStatementExecutionResults(sb.toString());
}
}
handle.close();
basicTeststepRun.setResponse(response);
return basicTeststepRun;
}
Aggregations