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.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import se.ugli.habanero.j.Habanero;
import se.ugli.habanero.j.OutParam;
import se.ugli.habanero.j.TypeAdaptor;

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)
			if (arg instanceof OutParam && statement instanceof CallableStatement) {
				final OutParam outParam = (OutParam) arg;
				final CallableStatement callableStatement = (CallableStatement) statement;
				callableStatement.registerOutParameter(parameterIndex, outParam.sqlType);
			} else
				statement.setObject(parameterIndex++, convertArgument(arg));
	}

	private Object convertArgument(final Object object) {
		final Class type = object.getClass();
		final TypeAdaptor typeAdaptor = Habanero.getTypeAdaptor(type);
		return typeAdaptor.toJdbcValue(object);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy