package org.seamframework.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;

@RequestScoped
public class DefaultPreparedStatementFactory implements PreparedStatementFactory {
	
	private List<PreparedStatement> statements = new ArrayList<PreparedStatement>();
	
	public PreparedStatement createStatement(Connection connection, String sql) {
		try {
			return connection.prepareStatement(sql);
		}
		catch (SQLException sqle) {
			throw new RuntimeException(sqle);
		}
	}
	
	@PreDestroy
	public void closeStatements() {
		for (PreparedStatement ps: statements) {
			try {
				 ps.close();
			}
			catch (SQLException sqle) {
				//logger.warn(sqle);
			}
		}
	}
	
}
