package com.github.arteam.jdit.domain;

import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.util.LongMapper;
import org.skife.jdbi.v2.util.StringMapper;

import java.util.Date;
import java.util.List;

/**
 * Date: 1/27/15
 * Time: 11:03 PM
 *
 * @author Artem Prigoda
 */
public abstract class BasePlayerDao {

    public abstract DBI dbi();

    public Long createPlayer(String firstName, String lastName, Date birthDate, int height, int weight) {
        Handle handle = dbi().open();
        try {
            return handle.createStatement("insert into players(first_name, last_name, birth_date, weight, height) values" +
                    "(:first_name, :last_name, :birth_date, :weight, :height)")
                    .bind("first_name", firstName)
                    .bind("last_name", lastName)
                    .bind("birth_date", birthDate)
                    .bind("height", height)
                    .bind("weight", weight)
                    .executeAndReturnGeneratedKeys(LongMapper.FIRST)
                    .first();
        } finally {
            handle.close();
        }
    }

    public List<String> getLastNames() {
        Handle handle = dbi().open();
        try {
            return handle.createQuery("select last_name from players")
                    .map(StringMapper.FIRST)
                    .list();
        } finally {
            handle.close();
        }
    }
}

@@ -478,32 +478,37 @@
 ight) %7B%0A        
+try (
 Handle handle = 
@@ -515,37 +515,25 @@
 dbi().open()
-;%0A        try
+)
  %7B%0A         
@@ -1069,56 +1069,8 @@
 ();%0A
-        %7D finally %7B%0A            handle.close();%0A
     
@@ -1131,16 +1131,21 @@
         
+try (
 Handle h
@@ -1168,21 +1168,9 @@
 en()
-;%0A        try
+)
  %7B%0A 
@@ -1317,56 +1317,8 @@
 ();%0A
-        %7D finally %7B%0A            handle.close();%0A
     

Use try-with-resources