package org.seamframework.sql;

import java.lang.annotation.Annotation;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.enterprise.inject.spi.InjectionPoint;
import javax.inject.Inject;

public class Session {
	
	private final ConnectionFactory cf;
	private final PreparedStatementFactory psf;
	private final Annotation qualifier;
	
	@Inject
	Session(InjectionPoint ip, ConnectionFactory cf, PreparedStatementFactory psf) {
		this( ip.getQualifiers().iterator().next(), cf, psf );
	}
	
	public Session(Annotation qualifier, ConnectionFactory cf, PreparedStatementFactory psf) {
		this.qualifier = qualifier;
		this.cf = cf;
		this.psf = psf;
	}
	
	Annotation getQualifier() {
		return qualifier;
	}
	
	public PreparedStatement createStatement(String sql) {
		Connection connection = cf.getConnection(qualifier);
		return psf.createStatement(connection,sql);
	}
	
	public ExecutableUpdate prepare(Update<?> update) {
		return new ExecutableUpdate(update, this);
	}
	
	public ExecutableInsert prepare(Insert<?> insert) {
		return new ExecutableInsert(insert, this);
	}
	
	public ExecutableQuery prepare(Query query) {
		return new ExecutableQuery(query, this);
	}
	
	public ExecutableStatement prepare(Statement statement) {
		return new ExecutableStatement(statement, this);
	}
	
	public ExecutableQuery prepareQuery(String query) {
		return new ExecutableQuery(query, this);
	}
	
	public ExecutableUpdate prepareUpdate(String update) {
		return new ExecutableUpdate(update, this);
	}
	
	public ExecutableInsert prepareInsert(String insert) {
		return new ExecutableInsert(insert, this);
	}
	
	public ExecutableStatement prepare(String statement) {
		return new ExecutableStatement(statement, this);
	}
	
}
