package de.henninglanghorst.functional;

import de.henninglanghorst.functional.model.Person;
import de.henninglanghorst.functional.sql.function.Function;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

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

/**
 * Contains database function relating to database table {@code Person}.
 *
 * @author Henning Langhorst
 */
public final class PersonDbFunctions {

    private PersonDbFunctions() {
    }

    public static Function<Connection, Integer> dropTablePerson() {
        return databaseUpdate(statement("drop table Person"));
    }

    public static Function<Connection, Integer> createTablePerson() {
        return databaseUpdate(
                statement("create table Person (" +
                        "id integer primary key, firstName varchar2, " +
                        "lastName varchar2, birthday date)"));
    }

    public static Function<Connection, int[]> insertPersons() {
        return withinTransaction(
                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")))
                ));
    }


    public static Function<Connection, List<Person>> selectAllPersons() {
        return databaseQuery(
                statement("select * from Person"),
                resultSet -> new Person(
                        resultSet.getInt("id"),
                        resultSet.getString("firstName"),
                        resultSet.getString("lastName"),
                        resultSet.getDate("birthday").toLocalDate()));
    }

    public static Function<Connection, Person> selectPersonWithId(int id) {
        return databaseQuerySingleRow(
                statement("select * from Person where id = ?", id),
                resultSet -> new Person(
                        resultSet.getInt("id"),
                        resultSet.getString("firstName"),
                        resultSet.getString("lastName"),
                        resultSet.getDate("birthday").toLocalDate()));
    }


}

@@ -198,16 +198,73 @@
 l.Date;%0A
+import java.sql.ResultSet;%0Aimport java.sql.SQLException;%0A
 import j
@@ -1787,264 +1787,47 @@
     
-resultSet -%3E new Person(%0A                        resultSet.getInt(%22id%22),%0A                        resultSet.getString(%22firstName%22),%0A                        resultSet.getString(%22lastName%22),%0A                        resultSet.getDate(%22birthday%22).toLocalDate())
+PersonDbFunctions::mapResultSetToPerson
 );%0A 
@@ -2035,42 +2035,180 @@
     
-resultSet -%3E new Person(%0A         
+PersonDbFunctions::mapResultSetToPerson);%0A    %7D%0A%0A    private static Person mapResultSetToPerson(final ResultSet resultSet) throws SQLException %7B%0A        return new Person(%0A
     
@@ -2210,32 +2210,33 @@
 %0A               
+ 
 resultSet.getInt
@@ -2239,32 +2239,24 @@
 tInt(%22id%22),%0A
-        
             
@@ -2305,32 +2305,24 @@
             
-    
-    
 resultSet.ge
@@ -2338,32 +2338,24 @@
 lastName%22),%0A
-        
             
@@ -2402,17 +2402,16 @@
 lDate())
-)
 ;%0A    %7D%0A

Extract duplicate code to mapResultSetToPerson()