All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.ugli.habanero.j.internal.PrepareArgumentsCommand Maven / Gradle / Ivy

There is a newer version: 1.8.1.1
Show newest version
package se.ugli.habanero.j.internal;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import se.ugli.habanero.j.HabaneroException;
import se.ugli.habanero.j.TypeAdaptor;
import se.ugli.habanero.j.TypeRegister;
import se.ugli.habanero.j.util.Option;

public class PrepareArgumentsCommand {

	public static PrepareArgumentsCommand apply(final PreparedStatement statement) {
		return new PrepareArgumentsCommand(statement);
	}

	private final PreparedStatement statement;

	private PrepareArgumentsCommand(final PreparedStatement statement) {
		this.statement = statement;
	}

	public void exec(final Object... args) throws SQLException {
		int parameterIndex = 1;
		for (final Object arg : args)
			statement.setObject(parameterIndex++, convertArgument(arg));
	}

	private Object convertArgument(final Object object) {
		final Class type = object.getClass();
		final Option typeAdaptorOpt = TypeRegister.get(type);
		if (typeAdaptorOpt.isDefined())
			return typeAdaptorOpt.get().toJdbcValue(object);
		throw new HabaneroException(type.getName() + " isn't registered.");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy