if (targetDS == null) return;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
CodeBlockTrace trace = null;
try {
// Get the connection.
conn = targetDS.getConnection();
// Execute the query.
lastExecutedStmt = createSQLStatament();
trace = new SQLStatementTrace(lastExecutedStmt.getSQLSentence()).begin();
trace.addRuntimeConstraint(new DataSetLoadConstraints(this));
log.debug("Load data set from datasource=" + dataSource + " SQL=" + lastExecutedStmt.getSQLSentence());
stmt = lastExecutedStmt.getPreparedStatement(conn);
rs = stmt.executeQuery();
// Get the properties definition.
ResultSetMetaData meta = rs.getMetaData();
int propsSize = meta.getColumnCount();
SQLDataSet.this.setPropertySize(propsSize);
for (int i = 0; i < propsSize; i++) {
SQLDataProperty dp = createSQLProperty();
String propId = StringUtils.isNotBlank(meta.getColumnLabel(i + 1)) ? meta.getColumnLabel(i + 1) : meta.getColumnName(i + 1);
dp.setPropertyId(propId.toLowerCase());
// dp.setPropertyId(meta.getColumnName(i + 1).toLowerCase());
dp.setType(meta.getColumnType(i + 1));
dp.setTableName(meta.getTableName(i + 1));
dp.setColumnName(meta.getColumnName(i + 1));
addProperty(dp, i);
}
// Get rows and populate the data set values.
int index = 0;
while (rs.next()) {
Object[] row = new Object[propsSize];
for (int i = 0; i < propsSize; i++) row[i] = rs.getObject(i + 1);
SQLDataSet.this.addRowValues(row);
// Check load constraints (every 10,000 rows)
if (++index == 10000) {
trace.checkRuntimeConstraints();
index = 0;
}
}
// Once we got the dataset initialized then calculate the domain for each property.
for (int i = 0; i < properties.length; i++) {
SQLDataProperty property = (SQLDataProperty) properties[i];
property.calculateDomain();
}
}
catch (Exception e) {
if (lastExecutedStmt != null) {
log.error("Error in load() SQLDataset. SQL = " + lastExecutedStmt.getSQLSentence(), e);
}
throw e;
}
finally {
try {
if (rs != null) rs.close();
} catch (Exception e) {
log.warn("Error closing ResultSet: ", e);
}
try {
if (stmt != null) stmt.close();
} catch (Exception e) {
log.warn("Error closing PreparedStatement: ", e);
}
if (conn != null) {
conn.close();
}
if (trace != null) {
trace.end();
}
}
}