package org.sfm.benchmark.sfm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.sfm.beans.DbObject;
import org.sfm.benchmark.BenchmarkRunner;
import org.sfm.benchmark.ForEachListener;
import org.sfm.benchmark.QueryExecutor;
import org.sfm.benchmark.SysOutBenchmarkListener;
import org.sfm.jdbc.DbHelper;
import org.sfm.jdbc.JdbcMapperFactory;
import org.sfm.reflect.Instantiator;
import org.sfm.reflect.InstantiatorFactory;
import org.sfm.reflect.asm.AsmFactory;

public class DynamicJdbcMapperMapBenchmark extends AbstractMapperQueryExecutor implements QueryExecutor {
	
	Instantiator<DbObject> instantiator;
	PreparedStatement ps;
	ResultSet rs;
	
	public DynamicJdbcMapperMapBenchmark(Connection conn) throws NoSuchMethodException, SecurityException, SQLException {
		super(JdbcMapperFactory.newInstance().newMapper(DbObject.class), conn);
		instantiator = new InstantiatorFactory(new AsmFactory()).getInstantiator(DbObject.class);
	}
	
	@Override
	public void forEach(final ForEachListener ql) throws Exception {
		ql.start();
		
		while(rs.next()) {
			DbObject o = instantiator.newInstance();
			mapper.map(rs, o);
			ql.object(o);
		}
		
		ql.start();
	}

	public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException, Exception {
		new BenchmarkRunner(-1, new DynamicJdbcMapperMapBenchmark(DbHelper.benchmarkDb())).run(new SysOutBenchmarkListener(DynamicJdbcMapperMapBenchmark.class, "BigQuery"));
		new BenchmarkRunner(1, new DynamicJdbcMapperMapBenchmark(DbHelper.benchmarkDb())).run(new SysOutBenchmarkListener(DynamicJdbcMapperMapBenchmark.class, "SmallQuery"));
	}

}

@@ -57,70 +57,8 @@
 on;%0A
-import java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0A
 impo
@@ -83,16 +83,16 @@
 eption;%0A
+
 %0Aimport 
@@ -629,46 +629,8 @@
 or;%0A
-%09PreparedStatement ps;%0A%09ResultSet rs;%0A
 %09%0A%09p
@@ -936,16 +936,22 @@
 %09public 
+final 
 void for
@@ -1004,25 +1004,8 @@
 n %7B%0A
-%09%09ql.start();%0A%09%09%0A
 %09%09wh
@@ -1108,29 +1108,12 @@
 o);%0A
+
 %09%09%7D%0A
-%09%09%0A%09%09ql.start();%0A
 %09%7D%0A%0A

fix npe in benchmark