Search in sources :

Example 1 with ColumnMapRowMapper

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);
    }
}
Also used : BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Example 2 with ColumnMapRowMapper

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);
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) DataAccessException(org.springframework.dao.DataAccessException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ColumnMapRowMapper

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;
}
Also used : Element(net.sf.ehcache.Element) ModelAndView(org.springframework.web.portlet.ModelAndView) PortletPreferences(javax.portlet.PortletPreferences) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Map(java.util.Map) Cache(net.sf.ehcache.Cache) DataSource(javax.sql.DataSource)

Example 4 with ColumnMapRowMapper

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;
}
Also used : ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) SQLDataException(java.sql.SQLDataException) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Example 5 with ColumnMapRowMapper

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;
    }
}
Also used : BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Aggregations

ColumnMapRowMapper (org.springframework.jdbc.core.ColumnMapRowMapper)6 Map (java.util.Map)4 BeanPropertyRowMapper (org.springframework.jdbc.core.BeanPropertyRowMapper)3 RowMapper (org.springframework.jdbc.core.RowMapper)3 RowMapperResultSetExtractor (org.springframework.jdbc.core.RowMapperResultSetExtractor)3 SQLDataException (java.sql.SQLDataException)1 HashMap (java.util.HashMap)1 PortletPreferences (javax.portlet.PortletPreferences)1 DataSource (javax.sql.DataSource)1 Cache (net.sf.ehcache.Cache)1 Element (net.sf.ehcache.Element)1 DataAccessException (org.springframework.dao.DataAccessException)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.portlet.ModelAndView)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1