use of org.springframework.jdbc.core.ColumnMapRowMapper in project camel by apache.
the class DefaultSqlEndpoint method queryForStreamList.
@SuppressWarnings("unchecked")
public ResultSetIterator queryForStreamList(Connection connection, Statement statement, ResultSet rs) throws SQLException {
if (outputClass == null) {
RowMapper rowMapper = new ColumnMapRowMapper();
return new ResultSetIterator(connection, statement, rs, rowMapper);
} else {
Class<?> outputClzz = getCamelContext().getClassResolver().resolveClass(outputClass);
RowMapper rowMapper = new BeanPropertyRowMapper(outputClzz);
return new ResultSetIterator(connection, statement, rs, rowMapper);
}
}
use of org.springframework.jdbc.core.ColumnMapRowMapper in project libresonic by Libresonic.
the class DBController method handleRequestInternal.
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String query = request.getParameter("query");
if (query != null) {
map.put("query", query);
try {
List<?> result = daoHelper.getJdbcTemplate().query(query, new ColumnMapRowMapper());
map.put("result", result);
} catch (DataAccessException x) {
map.put("error", ExceptionUtils.getRootCause(x).getMessage());
}
}
return new ModelAndView("db", "model", map);
}
use of org.springframework.jdbc.core.ColumnMapRowMapper in project uPortal by Jasig.
the class SqlQueryPortletController method handleRenderRequest.
@Override
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response) throws Exception {
// find the configured SQL statement
PortletPreferences preferences = request.getPreferences();
String sqlQuery = preferences.getValue(SQL_QUERY_PARAM_NAME, null);
String dsName = preferences.getValue(DATASOURCE_BEAN_NAME_PARAM_NAME, BasePortalJpaDao.PERSISTENCE_UNIT_NAME);
String viewName = preferences.getValue(VIEW_PARAM_NAME, "jsp/SqlQuery/results");
// Allow substituting attributes from the request and userInfo objects using the SPEL ${} notation..
String spelSqlQuery = evaluateSpelExpression(sqlQuery, request);
List<Map<String, Object>> results = null;
String cacheKey = createCacheKey(spelSqlQuery, dsName);
Cache cache = getCache(request);
if (cache != null) {
Element cachedElement = cache.get(cacheKey);
if (cachedElement != null) {
log.debug("Cache hit. Returning item for query: {}, substituted query: {}, from cache {} for key {}", sqlQuery, spelSqlQuery, cache.getName(), cacheKey);
results = (List<Map<String, Object>>) cachedElement.getObjectValue();
}
}
if (results == null) {
// generate a JDBC template for the requested data source
DataSource ds = (DataSource) getApplicationContext().getBean(dsName);
JdbcTemplate template = new JdbcTemplate(ds);
// Execute the SQL query and build a results object. This result will consist of one
// rowname -> rowvalue map for each row in the result set
results = template.query(spelSqlQuery, new ColumnMapRowMapper());
log.debug("found {} results for query {}", results.size(), spelSqlQuery);
if (cache != null) {
log.debug("Adding SQL results to cache {}, query: {}, substituted query: {}", cache.getName(), sqlQuery, spelSqlQuery);
Element cachedElement = new Element(cacheKey, results);
cache.put(cachedElement);
}
}
// build the model
ModelAndView modelandview = new ModelAndView(viewName);
modelandview.addObject("results", results);
return modelandview;
}
use of org.springframework.jdbc.core.ColumnMapRowMapper in project camel by apache.
the class DefaultSqlEndpoint method queryForObject.
@SuppressWarnings("unchecked")
public Object queryForObject(ResultSet rs) throws SQLException {
Object result = null;
if (outputClass == null) {
RowMapper rowMapper = new ColumnMapRowMapper();
RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
List<Map<String, Object>> data = mapper.extractData(rs);
if (data.size() > 1) {
throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
} else if (data.size() == 1) {
// Set content depend on number of column from query result
Map<String, Object> row = data.get(0);
if (row.size() == 1) {
result = row.values().iterator().next();
} else {
result = row;
}
}
} else {
Class<?> outputClzz = getCamelContext().getClassResolver().resolveClass(outputClass);
RowMapper rowMapper = new BeanPropertyRowMapper(outputClzz);
RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
List<?> data = mapper.extractData(rs);
if (data.size() > 1) {
throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
} else if (data.size() == 1) {
result = data.get(0);
}
}
// If data.size is zero, let result be null.
return result;
}
use of org.springframework.jdbc.core.ColumnMapRowMapper in project camel by apache.
the class DefaultSqlEndpoint method queryForList.
@SuppressWarnings("unchecked")
public List<?> queryForList(ResultSet rs, boolean allowMapToClass) throws SQLException {
if (allowMapToClass && outputClass != null) {
Class<?> outputClazz = getCamelContext().getClassResolver().resolveClass(outputClass);
RowMapper rowMapper = new BeanPropertyRowMapper(outputClazz);
RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
List<?> data = mapper.extractData(rs);
return data;
} else {
ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
List<Map<String, Object>> data = mapper.extractData(rs);
return data;
}
}
Aggregations