Search in sources :

Example 1 with DefaultQueryResult

use of com.thinkbiganalytics.discovery.model.DefaultQueryResult in project kylo by Teradata.

the class QueryRunner method query.

/**
 * Executes the specified SELECT query and returns the results.
 *
 * @param query the SELECT query
 * @return the query result
 * @throws DataAccessException if the query cannot be executed
 */
public QueryResult query(String query) {
    // Validate the query
    if (!validateQuery(query)) {
        throw new DataRetrievalFailureException("Invalid query: " + query);
    }
    // Execute the query
    final DefaultQueryResult queryResult = new DefaultQueryResult(query);
    jdbcTemplate.query(query, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            // First-time initialization
            if (queryResult.isEmpty()) {
                QueryRunner.this.initQueryResult(queryResult, rs.getMetaData());
            }
            // Add row to the result
            final Map<String, Object> row = new LinkedHashMap<>();
            for (final QueryResultColumn column : queryResult.getColumns()) {
                row.put(column.getDisplayName(), rs.getObject(column.getHiveColumnLabel()));
            }
            queryResult.addRow(row);
        }
    });
    return queryResult;
}
Also used : DefaultQueryResult(com.thinkbiganalytics.discovery.model.DefaultQueryResult) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) DefaultQueryResultColumn(com.thinkbiganalytics.discovery.model.DefaultQueryResultColumn) QueryResultColumn(com.thinkbiganalytics.discovery.schema.QueryResultColumn) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DefaultQueryResult (com.thinkbiganalytics.discovery.model.DefaultQueryResult)1 DefaultQueryResultColumn (com.thinkbiganalytics.discovery.model.DefaultQueryResultColumn)1 QueryResultColumn (com.thinkbiganalytics.discovery.schema.QueryResultColumn)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)1 RowCallbackHandler (org.springframework.jdbc.core.RowCallbackHandler)1