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

org.simpleflatmapper.jdbc.NameBasedResultSetGetterFactory Maven / Gradle / Ivy

package org.simpleflatmapper.jdbc;


import org.simpleflatmapper.map.mapper.AbstractColumnNameDiscriminatorMapperFactory;
import org.simpleflatmapper.reflect.Getter;

import java.sql.ResultSet;

public final class NameBasedResultSetGetterFactory implements AbstractColumnNameDiscriminatorMapperFactory.DiscriminatorNamedGetterFactory {
	public static final NameBasedResultSetGetterFactory INSTANCE = new NameBasedResultSetGetterFactory();

	private NameBasedResultSetGetterFactory() {
	}

	@Override
	public  AbstractColumnNameDiscriminatorMapperFactory.DiscriminatorNamedGetter newGetter(final Class type) {
		return new AbstractColumnNameDiscriminatorMapperFactory.DiscriminatorNamedGetter() {
			@Override
			public T get(ResultSet resultSet, String discriminatorColumn) throws Exception {
				try {
					return resultSet.getObject(discriminatorColumn, type);
				} catch (NoSuchMethodError e) {
					throw jdbc40Error(resultSet, e);
				} catch (UnsupportedOperationException e) {
					throw jdbc40Error(resultSet, e);
				}
			}
		};
	}

	private Exception jdbc40Error(ResultSet target, Throwable e) {
		String message = "The ResultSet " + target.getClass().getName() + " does not support the T getObject(String, Class) method, " +
				"you will need to use the JdbcMapperFactory.discriminator(Class commonType, final String discriminatorColumn, CheckedBiFunction discriminatorFieldAccessor, Consumer> consumer) call passing ResultSet::getXXX as the discriminatorFieldAccessor reason : " + e.getMessage();
		return new UnsupportedOperationException(message, e);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy