/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 Maksim Gramin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.github.mgramin.sqlboot.sql.impl;

import com.github.mgramin.sqlboot.exceptions.BootException;
import com.github.mgramin.sqlboot.sql.SqlQuery;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;

/**
 * Execute SQL-query through Jdbc.
 *
 * @author Maksim Gramin (mgramin@gmail.com)
 * @version $Id$
 * @since 0.1
 */
public final class JdbcSqlQuery implements SqlQuery {

    /**
     * Data source.
     */
    private final DataSource dataSource;

    /**
     * Ctor.
     *
     * @param datasource Data source
     */
    public JdbcSqlQuery(final DataSource datasource) {
        this.dataSource = datasource;
    }

    @Override
    public List<Map<String, String>> select(final String sql)
    throws BootException {
        final List<Map<String, String>> result = new ArrayList<>();
        try (Connection connection = dataSource.getConnection()) {
            final Statement statement = connection.createStatement();
            try (ResultSet resultSet = statement.executeQuery(sql)) {
                final ResultSetMetaData rsMetaData = resultSet.getMetaData();
                final int columnCount = rsMetaData.getColumnCount();
                while (resultSet.next()) {
                    final Map<String, String> map = new LinkedHashMap<>();
                    for (int i = 1; i < columnCount + 1; i++) {
                        map.put(rsMetaData.getColumnName(i),
                                resultSet.getString(i));
                    }
                    result.add(map);
                }
            }
        } catch (SQLException e) {
            throw new BootException("SQL Exception", e);
        }
        return result;
    }

    @Override
    public void dbHealth() {
        try {
            dataSource.getConnection();
        } catch (SQLException e) {
            throw new BootException(e);
        }
    }

}

@@ -148,19 +148,16 @@
 g a copy
-%0A *
  of this
@@ -169,16 +169,19 @@
 ware and
+%0A *
  associa
@@ -229,19 +229,16 @@
  to deal
-%0A *
  in the 
@@ -266,16 +266,19 @@
 riction,
+%0A *
  includi
@@ -309,19 +309,16 @@
 e rights
-%0A *
  to use,
@@ -359,16 +359,19 @@
 tribute,
+%0A *
  sublice
@@ -386,19 +386,16 @@
 /or sell
-%0A *
  copies 
@@ -594,19 +594,16 @@
 included
-%0A *
  in all 
@@ -611,16 +611,19 @@
 opies or
+%0A *
  substan
@@ -733,19 +733,16 @@
 PRESS OR
-%0A *
  IMPLIED
@@ -756,16 +756,19 @@
 DING BUT
+%0A *
  NOT LIM
@@ -809,19 +809,16 @@
 ABILITY,
-%0A *
  FITNESS
@@ -846,16 +846,19 @@
 POSE AND
+%0A *
  NON-INF
@@ -889,19 +889,16 @@
 HALL THE
-%0A *
  AUTHORS
@@ -943,16 +943,19 @@
 Y CLAIM,
+%0A *
  DAMAGES
@@ -963,19 +963,16 @@
 OR OTHER
-%0A *
  LIABILI
@@ -1122,19 +1122,16 @@
 S IN THE
-%0A *
  SOFTWAR
@@ -1195,282 +1195,207 @@
 ort 
-com.github.mgramin.sqlboot.exceptions.BootException;%0Aimport com.github.mgramin.sqlboot.sql.SqlQuery;%0Aimport java.sql.Connection;%0Aimport java.sql.ResultSet;%0Aimport java.sql.ResultSetMetaData;%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement;%0Aimport java.util.ArrayList
+static java.util.stream.Collectors.toList;%0Aimport static java.util.stream.Collectors.toMap;%0A%0Aimport com.github.mgramin.sqlboot.exceptions.BootException;%0Aimport com.github.mgramin.sqlboot.sql.SqlQuery
 ;%0Aim
@@ -1465,24 +1465,52 @@
 a.util.Map;%0A
+import java.util.Map.Entry;%0A
 import javax
@@ -1525,16 +1525,67 @@
 aSource;
+%0Aimport org.springframework.jdbc.core.JdbcTemplate;
 %0A%0A/**%0A *
@@ -1611,16 +1611,26 @@
 through 
+plain old 
 Jdbc.%0A *
@@ -2092,16 +2092,20 @@
 ng sql)%0A
+    
     thro
@@ -2135,204 +2135,70 @@
     
-final List%3CMap%3CString, String%3E%3E result = new ArrayList%3C%3E();%0A        try (Connection connection = dataSource.getConnection()) %7B%0A            final Statement statement = connection.createStatement
+return new JdbcTemplate(dataSource).queryForList(sql).stream
 ()
-;
 %0A   
@@ -2210,212 +2210,43 @@
     
-try (ResultSet resultSet = statement.executeQuery(sql)) %7B%0A                final ResultSetMetaData rsMetaData = resultSet.getMetaData();%0A                final int columnCount = rsMetaData.getColumnCount
+.map(map -%3E map.entrySet().stream
 ()
-;
 %0A   
@@ -2262,696 +2262,238 @@
     
-while (resultSet.next()) %7B%0A                    final Map%3CString, String%3E map = new LinkedHashMap%3C%3E();%0A                    for (int i = 1; i %3C columnCount + 1; i++) %7B%0A                        map.put(rsMetaData.getColumnName(i),%0A                                resultSet.getString(i));%0A                    %7D%0A                    result.add(map);%0A                %7D%0A            %7D%0A        %7D catch (SQLException e) %7B%0A            throw new BootException(%22SQL Exception%22, e);%0A        %7D%0A        return result;%0A    %7D%0A%0A    @Override%0A    public void dbHealth() %7B%0A        try %7B%0A            dataSource.getConnection();%0A        %7D catch (SQLException e) %7B%0A            throw new BootException(e);%0A        %7D
+.collect(toMap(Entry::getKey, v -%3E v.getValue().toString(), (a, b) -%3E a,%0A                LinkedHashMap::new)))%0A            .collect(toList());%0A    %7D%0A%0A    @Override%0A    public void dbHealth() %7B%0A        new JdbcTemplate(dataSource);
 %0A   

fix #81