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

org.sfm.jdbc.impl.DynamicJdbcMapper Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.jdbc.impl;

import org.sfm.jdbc.*;
import org.sfm.jdbc.impl.getter.ResultSetGetterFactory;
import org.sfm.map.*;
import org.sfm.map.impl.ColumnsMapperKey;
import org.sfm.map.impl.FieldMapperColumnDefinition;
import org.sfm.map.impl.MapperCache;
import org.sfm.reflect.meta.ClassMeta;
import org.sfm.reflect.meta.PropertyNameMatcherFactory;
import org.sfm.utils.RowHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END


public final class DynamicJdbcMapper implements JdbcMapper {

	private final ClassMeta classMeta;

	private final FieldMapperErrorHandler fieldMapperErrorHandler;

	private final MapperBuilderErrorHandler mapperBuilderErrorHandler;
	private final ColumnDefinitionProvider, JdbcColumnKey> columnDefinitions;
	private final PropertyNameMatcherFactory propertyNameMatcherFactory;
	private final RowHandlerErrorHandler rowHandlerErrorHandler;
    private final boolean failOnAsm;

    private final MapperCache> mapperCache = new MapperCache>();

	public DynamicJdbcMapper(final ClassMeta classMeta,
							 final FieldMapperErrorHandler fieldMapperErrorHandler,
							 final MapperBuilderErrorHandler mapperBuilderErrorHandler,
							 RowHandlerErrorHandler rowHandlerErrorHandler,
							 final ColumnDefinitionProvider, JdbcColumnKey> columnDefinitions,
							 PropertyNameMatcherFactory propertyNameMatcherFactory,
                             boolean failOnAsm) {
		this.classMeta = classMeta;
		this.fieldMapperErrorHandler = fieldMapperErrorHandler;
		this.mapperBuilderErrorHandler = mapperBuilderErrorHandler;
		this.columnDefinitions = columnDefinitions;
		this.propertyNameMatcherFactory = propertyNameMatcherFactory;
		this.rowHandlerErrorHandler = rowHandlerErrorHandler;
        this.failOnAsm = failOnAsm;
	}


    @Override
    public final T map(ResultSet source) throws MappingException {
        return map(source, newMappingContext(source));
    }

    @Override
	public final T map(final ResultSet source, final MappingContext mappingContext) throws MappingException {
		try {
			final JdbcMapper mapper = getMapper(source);
			return mapper.map(source, mappingContext);
		} catch(SQLException e) {
			throw new SQLMappingException(e.getMessage(), e);
		}
	}

    @Override
    public final void mapTo(final ResultSet source, final T target, final MappingContext mappingContext) throws MappingException {
        try {
            final JdbcMapper mapper = getMapper(source);
            mapper.mapTo(source, target, mappingContext);
        } catch(SQLException e) {
            throw new SQLMappingException(e.getMessage(), e);
        } catch(Exception e) {
            throw new MappingException(e.getMessage(), e);
        }
    }

	@Override
	public final > H forEach(final ResultSet rs, final H handle)
			throws SQLException, MappingException {
		final JdbcMapper mapper = getMapper(rs);
		return mapper.forEach(rs, handle);
	}
	
	@Override
    @Deprecated
	public final Iterator iterate(final ResultSet rs)
			throws SQLException, MappingException {
		final JdbcMapper mapper = getMapper(rs);
		return mapper.iterator(rs);
	}

	@Override
    @SuppressWarnings("deprecation")
    public final Iterator iterator(final ResultSet rs)
			throws SQLException, MappingException {
		return iterate(rs);
	}
	
	//IFJAVA8_START
	@Override
	public Stream stream(ResultSet rs) throws SQLException, MappingException {
		final JdbcMapper mapper = getMapper(rs);
		return mapper.stream(rs);
	}
    //IFJAVA8_END

    @Override
    public MappingContext newMappingContext(ResultSet source) throws MappingException {
        try {
            return getMapper(source).newMappingContext(source);
        } catch (SQLException e) {
            throw new SQLMappingException(e.getMessage(), e);
        }
    }

	public JdbcMapper getMapper(final ResultSet rs) throws MapperBuildingException, SQLException {
        return getMapper(rs.getMetaData());
    }

    public JdbcMapper getMapper(final ResultSetMetaData metaData) throws MapperBuildingException, SQLException {
        final ColumnsMapperKey key = mapperKey(metaData);
		
		JdbcMapper mapper = mapperCache.get(key);
		
		if (mapper == null) {
			final JdbcMapperBuilder builder = new JdbcMapperBuilder(classMeta, mapperBuilderErrorHandler,columnDefinitions, propertyNameMatcherFactory, new ResultSetGetterFactory(), failOnAsm, new JdbcMappingContextFactoryBuilder());

			builder.jdbcMapperErrorHandler(rowHandlerErrorHandler);
			builder.fieldMapperErrorHandler(fieldMapperErrorHandler);
			builder.addMapping(metaData);
			
			mapper = builder.mapper();
			mapperCache.add(key, mapper);
		}
		return mapper;
	}
	
	private static ColumnsMapperKey mapperKey(final ResultSetMetaData metaData) throws SQLException {
		final String[] columns = new String[metaData.getColumnCount()];
		
		for(int i = 0; i < columns.length; i++) {
			columns[i] = metaData.getColumnLabel(i + 1);
		}
		
		return new ColumnsMapperKey(columns);
	}

    @Override
    public String toString() {
        return "DynamicJdbcMapper{target=" + classMeta.getType()
                +  ", " + mapperCache +
                '}';
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy