String sql = ServletRequestUtils.getStringParameter(request, "sql", null);
if (sql == null || sql.equals("") || sql.trim().equals("")) {
request.setAttribute("errorMessage", getMessageSourceAccessor().getMessage("probe.src.dataSourceTest.sql.required"));
return new ModelAndView(getViewName());
}
int maxRows = ServletRequestUtils.getIntParameter(request, "maxRows", 0);
int rowsPerPage = ServletRequestUtils.getIntParameter(request, "rowsPerPage", 0);
int historySize = ServletRequestUtils.getIntParameter(request, "historySize", 0);
// store current option values and query history in a session attribute
HttpSession sess = request.getSession();
DataSourceTestInfo sessData = (DataSourceTestInfo) sess.getAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR);
synchronized(sess) {
if (sessData == null) {
sessData = new DataSourceTestInfo();
sess.setAttribute(DataSourceTestInfo.DS_TEST_SESS_ATTR, sessData);
}
sessData.setMaxRows(maxRows);
sessData.setRowsPerPage(rowsPerPage);
sessData.setHistorySize(historySize);
sessData.addQueryToHistory(sql);
}
DataSource dataSource = null;
try {
dataSource = getContainerWrapper().getResourceResolver().lookupDataSource(context, resourceName);
} catch (NamingException e) {
request.setAttribute("errorMessage", getMessageSourceAccessor().getMessage("probe.src.dataSourceTest.resource.lookup.failure", new Object[]{resourceName}));
}
if (dataSource == null) {
request.setAttribute("errorMessage", getMessageSourceAccessor().getMessage("probe.src.dataSourceTest.resource.lookup.failure", new Object[]{resourceName}));
} else {
List results = null;
int rowsAffected = 0;
try {
// TODO: use Spring's jdbc template?
Connection conn = dataSource.getConnection();
try {
conn.setAutoCommit(true);
PreparedStatement stmt = conn.prepareStatement(sql);
try {
boolean hasResultSet = stmt.execute();
if (! hasResultSet) {
rowsAffected = stmt.getUpdateCount();
} else {
results = new ArrayList();
ResultSet rs = stmt.getResultSet();
try {
ResultSetMetaData metaData = rs.getMetaData();
while(rs.next() && (maxRows < 0 || results.size() < maxRows)) {
Map record = new LinkedHashMap();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String value = rs.getString(i);
if (rs.wasNull()) {
value = getMessageSourceAccessor().getMessage("probe.src.dataSourceTest.sql.null");
} else {
value = HtmlUtils.htmlEscape(value);
}
// a work around for IE browsers bug of not displaying
// a border around an empty table column
if (value.equals("")) {
value = " ";
}
record.put(HtmlUtils.htmlEscape(metaData.getColumnName(i)), value);
}
results.add(record);
}
} finally {
rs.close();
}
rowsAffected = results.size();
}
} finally {
stmt.close();
}
} finally {
conn.close();
}
// store the query results in the session attribute in order
// to support a result set pagination feature without re-executing the query
synchronized(sess) {
sessData.setResults(results);
}
ModelAndView mv = new ModelAndView(getViewName(), "results", results);
mv.addObject("rowsAffected", String.valueOf(rowsAffected));
mv.addObject("rowsPerPage", String.valueOf(rowsPerPage));
return mv;
} catch (SQLException e) {
String message = getMessageSourceAccessor().getMessage("probe.src.dataSourceTest.sql.failure", new Object[] { e.getMessage() });
logger.error(message, e);
request.setAttribute("errorMessage", message);
}
}
return new ModelAndView(getViewName());
}