package eu.dnetlib.dlms.jdbc;

import java.net.URL;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;

import eu.dnetlib.dlms.jdbc.server.ParamInfo;
import eu.dnetlib.dlms.lowlevel.objects.structures.BooleanBasicValue;
import eu.dnetlib.dlms.lowlevel.objects.structures.DateBasicValue;
import eu.dnetlib.dlms.lowlevel.objects.structures.DoubleBasicValue;
import eu.dnetlib.dlms.lowlevel.objects.structures.IntBasicValue;
import eu.dnetlib.dlms.lowlevel.objects.structures.StringBasicValue;

/**
 * Class that provides support for named parameters in DOLPreparedStatement.
 * 
 * @author alessia
 * 
 */
public class ExtendedPreparedStatement extends DOLPreparedStatement {
	/**
	 * Constructor.
	 * 
	 * @param connection
	 *            a DorotyConnection
	 * @param query
	 *            DOl string to execute
	 * @throws SQLException
	 */
	public ExtendedPreparedStatement(final DorotyConnection connection, final String query) throws SQLException {
		super(connection, query);
		this.setDolString(query);
		this.setResultSetFactory(connection.getResultSetFactory());
	}

	/**
	 * Sets a parameter
	 * 
	 * @param name
	 * @param value
	 * @throws SQLException
	 *             if an error occurred
	 * @see PreparedStatement#setBoolean(int, boolean)
	 */
	public void setBoolean(final String name, final boolean value) throws SQLException {
		this.setObjectBase(new BooleanBasicValue(value), DorotyObjectEnum.d_boolean, name);
	}

	public void setDate(final String name, final Date x) throws SQLException {
		this.setObjectBase(new DateBasicValue(x), DorotyObjectEnum.d_date, name);
	}

	public void setDate(final String name, final Date x, final Calendar cal) throws SQLException {
		// TODO: what to do with the calendar??
		this.setObjectBase(new DateBasicValue(x), DorotyObjectEnum.d_date, name);
	}

	public void setDouble(final String name, final double x) throws SQLException {
		this.setObjectBase(new DoubleBasicValue(x), DorotyObjectEnum.d_double, name);
	}

	public void setInt(final String name, final int x) throws SQLException {
		this.setObjectBase(new IntBasicValue(x), DorotyObjectEnum.d_int, name);
	}

	public void setLong(final String name, final long x) throws SQLException {
		this.setObjectBase(x, DorotyObjectEnum.std_long, name);
	}

	public void setObject(final String name, final Object x, final int targetType) throws SQLException {
		this.setObjectBase(x, DorotyObjectEnum.getEnumFromCode(targetType), name);
	}

	public void setString(final String name, final String x) throws SQLException {
		this.setObjectBase(new StringBasicValue(x), DorotyObjectEnum.d_string, name);
	}

	public void setURL(final String name, final URL x) throws SQLException {
		this.setObjectBase(x, DorotyObjectEnum.std_url, name);

	}

	@Override
	void setObjectBase(final Object x, final DorotyObjectEnum dorotyType, final String parameterName) throws SQLException {
		ParamInfo paramInfo = this.createParamInfo(x, dorotyType, parameterName);
		this.getParameters().put(parameterName, paramInfo);
	}

}
