package org.jooby.jdbi;

import java.util.List;

import org.jooby.test.ServerFeature;
import org.junit.Test;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.logging.FormattedLog;
import org.skife.jdbi.v2.util.StringMapper;

import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class JdbiInClauseFeature extends ServerFeature {

  {

    use(ConfigFactory.empty()
        .withValue("db.url", ConfigValueFactory.fromAnyRef("jdbc:mysql://localhost/test"))
        .withValue("db.user", ConfigValueFactory.fromAnyRef("root")));

    use(new Jdbi().doWith((dbi, conf) -> {
      dbi.setSQLLog(new FormattedLog() {

        @Override
        protected void log(final String msg) {
          System.out.println(msg);
        }

        @Override
        protected boolean isEnabled() {
          return true;
        }
      });
    }));

    get("/named", req -> {
      try (Handle h = req.require(Handle.class)) {
        h.execute("drop table members");

        h.execute("create table members (id int primary key, name varchar(100))");

        h.execute("insert into members (id, name) values (?, ?)", 1, "Jooby");
        h.execute("insert into members (id, name) values (?, ?)", 2, "Rock");

        List<String> name = h.createQuery("select name from members where id in (:id)")
            .bind("id", req.param("id").toList(Integer.class))
            .map(StringMapper.FIRST)
            .list();

        return name;
      }
    });

    get("/pos", req -> {
      try (Handle h = req.require(Handle.class)) {
        h.execute("drop table members");

        h.execute("create table members (id int primary key, name varchar(100))");

        h.execute("insert into members (id, name) values (?, ?)", 1, "Jooby");
        h.execute("insert into members (id, name) values (?, ?)", 2, "Rock");

        List<String> name = h.createQuery("select name from members where id in (?)")
            .bind(0, req.param("id").toList(Integer.class).toArray(new Integer[0]))
            .map(StringMapper.FIRST)
            .list();

        return name;
      }
    });
  }

  @Test
  public void named() throws Exception {
    request()
        .get("/named?id=1&id=2")
        .expect("[Jooby, Rock]");
  }

  @Test
  public void pos() throws Exception {
    request()
        .get("/pos?id=1&id=2")
        .expect("[Jooby, Rock]");
  }
}

@@ -139,55 +139,8 @@
 le;%0A
-import org.skife.jdbi.v2.logging.FormattedLog;%0A
 impo
@@ -389,12 +389,8 @@
 (%22db
-.url
 %22, C
@@ -423,413 +423,35 @@
 ef(%22
-jdbc:mysql://localhost/test%22))%0A        .withValue(%22db.user%22, ConfigValueFactory.fromAnyRef(%22root%22)));%0A%0A    use(new Jdbi().doWith((dbi, conf) -%3E %7B%0A      dbi.setSQLLog(new FormattedLog() %7B%0A%0A        @Override%0A        protected void log(final String msg) %7B%0A          System.out.println(msg);%0A        %7D%0A%0A        @Override%0A        protected boolean isEnabled() %7B%0A          return true;%0A        %7D%0A      %7D);%0A    %7D
+mem%22)));%0A%0A    use(new Jdbi(
 ));%0A
@@ -533,50 +533,8 @@
 ) %7B%0A
-        h.execute(%22drop table members%22);%0A%0A
     
@@ -1099,292 +1099,8 @@
 ) %7B%0A
-        h.execute(%22drop table members%22);%0A%0A        h.execute(%22create table members (id int primary key, name varchar(100))%22);%0A%0A        h.execute(%22insert into members (id, name) values (?, ?)%22, 1, %22Jooby%22);%0A        h.execute(%22insert into members (id, name) values (?, ?)%22, 2, %22Rock%22);%0A%0A
     
@@ -1500,59 +1500,8 @@
 %22);%0A
-  %7D%0A%0A  @Test%0A  public void pos() throws Exception %7B
 %0A   
@@ -1580,10 +1580,11 @@
 %22);%0A  %7D%0A
+%0A
 %7D%0A

remove mysql test from jdbi and use h2.mem db