package org.sfm.benchmark.db.sql2o;

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

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.sfm.beans.SmallBenchmarkObject;
import org.sfm.benchmark.db.jmh.ConnectionParam;
import org.sfm.benchmark.db.jmh.DbTarget;
import org.sfm.benchmark.db.jmh.LimitParam;
import org.sql2o.Sql2o;

@State(Scope.Benchmark)
public class Sql2OBenchmark {

	private Sql2o sql2o;
	
	@Param(value="MOCK")
	DbTarget db;
	
	@Setup
	public void init() throws Exception  {
		ConnectionParam connParam = new ConnectionParam();
		connParam.db = db;
		connParam.init();
		sql2o = new Sql2o(connParam.dataSource);
	}
	
	@Benchmark
	public void testQuery(LimitParam limit, final Blackhole blackhole) throws Exception {
		
		org.sql2o.Connection conn = sql2o.open();
		try  {
			List<?> list = conn.createQuery("SELECT id, name, email, year_started FROM test_small_benchmark_object LIMIT :limit")
					.addColumnMapping("YEAR_STARTED", "yearStarted")
					.addParameter("limit", limit.limit)
					.executeAndFetch(SmallBenchmarkObject.class);
			for(Object o : list) {
				blackhole.consume(o);
			}
		} finally {
			conn.close();
		}
	}
	

	
}

@@ -532,137 +532,476 @@
 rg.s
-ql2o.Sql2o;%0A%0A@State(Scope.Benchmark)%0Apublic class Sql2OBenchmark %7B%0A%0A%09private Sql2o sql2o;%0A%09%0A%09@Param(value=%22MOCK%22)%0A%09DbTarget db;%0A%09
+fm.sql2o.SfmResultSetHandlerFactory;%0Aimport org.sfm.sql2o.SfmResultSetHandlerFactoryBuilder;%0Aimport org.sql2o.ResultSetHandlerFactory;%0Aimport org.sql2o.Sql2o;%0A%0A@State(Scope.Benchmark)%0Apublic class Sql2OBenchmark %7B%0A%0A%09private final ResultSetHandlerFactory%3CSmallBenchmarkObject%3E factory;%0A%09private Sql2o sql2o;%0A%09%0A%09@Param(value=%22MOCK%22)%0A%09DbTarget db;%0A%0A%09public Sql2OBenchmark() %7B%0A%09%09factory = new SfmResultSetHandlerFactoryBuilder().newFactory(SmallBenchmarkObject.class);%0A%09%7D%0A
 %0A%09@S
@@ -1708,11 +1708,498 @@
 %0A%09%7D%0A
-%09
 %0A%0A%09
+@Benchmark%0A%09public void sfm(LimitParam limit, final Blackhole blackhole) throws Exception %7B%0A%0A%09%09org.sql2o.Connection conn = sql2o.open();%0A%09%09try  %7B%0A%09%09%09List%3C?%3E list = conn.createQuery(%22SELECT id, name, email, year_started FROM test_small_benchmark_object LIMIT :limit%22)%0A%09%09%09%09%09.addColumnMapping(%22YEAR_STARTED%22, %22yearStarted%22)%0A%09%09%09%09%09.addParameter(%22limit%22, limit.limit)%0A%09%09%09%09%09.executeAndFetch(factory);%0A%09%09%09for(Object o : list) %7B%0A%09%09%09%09blackhole.consume(o);%0A%09%09%09%7D%0A%09%09%7D finally %7B%0A%09%09%09conn.close();%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add sql2 with sfm bench