package de.henninglanghorst.functional;

import de.henninglanghorst.functional.model.Person;
import de.henninglanghorst.functional.util.Either;
import org.h2.jdbcx.JdbcConnectionPool;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import static de.henninglanghorst.functional.sql.DatabaseOperations.*;
import static java.util.Arrays.asList;

/**
 * Example program.
 *
 * @author Henning Langhorst
 */
public class Main {

    public static void main(String[] args) {

        final JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:~/testdb", "sa", "");

        doInDatabase(cp::getConnection, databaseUpdate(statement("drop table Person")))
                .handleResult(objects -> System.out.println("Success " + objects), Throwable::printStackTrace);

        doInDatabase(
                cp::getConnection,
                withinTransaction(
                        databaseUpdate(
                                statement("create table Person (id integer primary key, firstName varchar2, lastName varchar2, birthday date)"))))
                .handleResult(objects -> System.out.println("Success " + objects), Throwable::printStackTrace);

        doInDatabase(cp::getConnection, multipleDatabaseUpdates(
                asList(
                        statement(
                                "insert into Person values (?, ?, ?, ?);",
                                1, "Carl", "Carlsson", Date.valueOf("1972-04-02")),
                        statement("insert into Person values (?, ?, ?, ?);",
                                2, "Lenny", "Leonard", Date.valueOf("1981-04-02")))
                )
        ).handleResult(
                objects -> System.out.println("Inserted rows: " + Arrays.toString(objects)),
                Throwable::printStackTrace);



        final Either<List<Person>, SQLException> result = doInDatabase(
                cp::getConnection,
                databaseQuery(
                        statement("select * from Person where id = ?", 1),
                        resultSet -> new Person(
                                resultSet.getInt("id"),
                                resultSet.getString("firstName"),
                                resultSet.getString("lastName"),
                                resultSet.getDate("birthday").toLocalDate())));


        System.out.println(result.left().get());
    }


}

@@ -176,16 +176,73 @@
 ionPool;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -524,24 +524,104 @@
 ass Main %7B%0A%0A
+    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);%0A%0A%0A
     public s
@@ -879,34 +879,27 @@
 ects -%3E 
-System.out.println
+LOGGER.info
 (%22Succes
@@ -918,34 +918,22 @@
 s), 
-Throwable::printStackTrace
+Main::logError
 );%0A%0A
@@ -1252,34 +1252,27 @@
 ects -%3E 
-System.out.println
+LOGGER.info
 (%22Succes
@@ -1291,34 +1291,22 @@
 s), 
-Throwable::printStackTrace
+Main::logError
 );%0A%0A
@@ -1818,34 +1818,27 @@
 ects -%3E 
-System.out.println
+LOGGER.info
 (%22Insert
@@ -1897,34 +1897,22 @@
     
-Throwable::printStackTrace
+Main::logError
 );%0A%0A
@@ -2456,46 +2456,173 @@
     
-System.out.println(result.left().get()
+LOGGER.info(%22DB Result: %22 + result.left());%0A    %7D%0A%0A    private static void logError(final SQLException e) %7B%0A        LOGGER.error(%22Error during database operation%22, e
 );%0A 

Use Logger instead of System.out.println()