Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.simpleflatmapper.jdbc.JdbcMapperBuilder Maven / Gradle / Ivy
Go to download
Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.
package org.simpleflatmapper.jdbc;
import org.simpleflatmapper.jdbc.impl.JdbcKeySourceGetter;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.map.SourceFieldMapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.MappingContext;
import org.simpleflatmapper.map.context.MappingContextFactory;
import org.simpleflatmapper.map.getter.ContextualGetterFactory;
import org.simpleflatmapper.map.getter.ContextualGetterFactoryAdapter;
import org.simpleflatmapper.map.mapper.ColumnDefinition;
import org.simpleflatmapper.map.mapper.DefaultSetRowMapperBuilder;
import org.simpleflatmapper.map.mapper.MapperBuilder;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.mapper.KeyFactory;
import org.simpleflatmapper.map.mapper.MapperSourceImpl;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.getter.GetterFactory;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.CheckedConsumer;
import org.simpleflatmapper.util.Function;
import org.simpleflatmapper.util.TypeReference;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.util.Enumerable;
import org.simpleflatmapper.util.UnaryFactory;
import org.simpleflatmapper.jdbc.impl.ResultSetEnumerable;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END
/**
* @param the targeted type of the jdbcMapper
*/
public final class JdbcMapperBuilder extends MapperBuilder, JdbcMapper, JdbcMapperBuilder> {
private static final MapperSourceImpl FIELD_MAPPER_SOURCE =
new MapperSourceImpl(ResultSet.class, new ContextualGetterFactoryAdapter(ResultSetGetterFactory.INSTANCE));
private static final KeyFactory KEY_FACTORY = new KeyFactory() {
@Override
public JdbcColumnKey newKey(String name, int i) {
return new JdbcColumnKey(name, i);
}
};
public static final Function> COLUMN_DEFINITION_FACTORY = FieldMapperColumnDefinition.factory();
private final MappingContextFactoryBuilder mappingContextFactoryBuilder;
/**
* Build a new JdbcMapperBuilder targeting the type specified by the TypeReference. The TypeReference
* allow you to provide a generic type with check of T
* new TypeReference<List<String>>() {}
*
* @param target the TypeReference to the type T to map to
*/
public JdbcMapperBuilder(final TypeReference target) {
this(target.getType());
}
/**
* Build a new JdbcMapperBuilder targeting the specified type.
*
* @param target the type
*/
public JdbcMapperBuilder(final Type target) {
this(target, ReflectionService.newInstance());
}
/**
* Build a new JdbcMapperBuilder targeting the specified type with the specified ReflectionService.
*
* @param target the type
* @param reflectService the ReflectionService
*/
public JdbcMapperBuilder(final Type target, ReflectionService reflectService) {
this(reflectService.getClassMeta(target),
MapperConfig.fieldMapperConfig(),
(ContextualGetterFactory)ResultSetGetterFactory.INSTANCE,
new JdbcMappingContextFactoryBuilder(!MapperConfig.fieldMapperConfig().unorderedJoin()));
}
/**
* @param classMeta the meta for the target class.
* @param mapperConfig the mapperConfig.
* @param getterFactory the Getter factory.
* @param parentBuilder the parent builder, null if none.
*/
public JdbcMapperBuilder(
final ClassMeta classMeta,
final MapperConfig mapperConfig,
final GetterFactory getterFactory,
final MappingContextFactoryBuilder parentBuilder) {
this(classMeta, mapperConfig, new ContextualGetterFactoryAdapter(getterFactory), parentBuilder);
}
/**
* @param classMeta the meta for the target class.
* @param mapperConfig the mapperConfig.
* @param getterFactory the Getter factory.
* @param parentBuilder the parent builder, null if none.
*/
public JdbcMapperBuilder(
final ClassMeta classMeta,
final MapperConfig mapperConfig,
final ContextualGetterFactory super ResultSet, JdbcColumnKey> getterFactory,
final MappingContextFactoryBuilder parentBuilder) {
super(KEY_FACTORY,
new DefaultSetRowMapperBuilder(
classMeta,
parentBuilder,
mapperConfig,
FIELD_MAPPER_SOURCE.getterFactory(getterFactory),
KEY_FACTORY,
new ResultSetEnumerableFactory(),
JdbcKeySourceGetter.INSTANCE),
new BiFunction, List, JdbcMapper>() {
@Override
public JdbcMapper apply(SetRowMapper setRowMapper, List keys) {
return new JdbcMapperImpl(setRowMapper, parentBuilder.build());
}
},
COLUMN_DEFINITION_FACTORY,
1 );
this.mappingContextFactoryBuilder = parentBuilder;
}
/**
* add a new mapping to the specified property with the specified index and the specified type.
*
* @param column the property name
* @param index the property index
* @param sqlType the property type, @see java.sql.Types
* @return the current builder
*/
public JdbcMapperBuilder addMapping(final String column, final int index, final int sqlType) {
addMapping(column, index, sqlType, FieldMapperColumnDefinition.identity());
return this;
}
/**
* add a new mapping to the specified property with the specified index, the specified type.
*
* @param column the property name
* @param index the property index
* @param sqlType the property type, @see java.sql.Types
* @param columnDefinition the property definition
* @return the current builder
*/
public JdbcMapperBuilder addMapping(final String column, final int index, final int sqlType, FieldMapperColumnDefinition columnDefinition) {
return addMapping(new JdbcColumnKey(column, index, sqlType), columnDefinition);
}
/**
* add a new mapping to the specified property with the specified index, the specified type.
*
* @param column the property name
* @param index the property index
* @param sqlType the property type, @see java.sql.Types
* @param properties the property properties
* @return the current builder
*/
public JdbcMapperBuilder addMapping(final String column, final int index, final int sqlType, Object... properties) {
return addMapping(new JdbcColumnKey(column, index, sqlType), properties);
}
/**
* add the all the property present in the metaData
*
* @param metaData the metaDAta
* @return the current builder
* @throws SQLException when an error occurs getting the metaData
*/
public JdbcMapperBuilder addMapping(final ResultSetMetaData metaData) throws SQLException {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
addMapping(metaData.getColumnLabel(i), i, metaData.getColumnType(i));
}
return this;
}
public JdbcSourceFieldMapper newSourceFieldMapper() {
return new JdbcSourceFieldMapperImpl(super.sourceFieldMapper(), mappingContextFactoryBuilder.build());
}
private static class JdbcSourceFieldMapperImpl implements JdbcSourceFieldMapper {
private final SourceFieldMapper sourceFieldMapper;
private final MappingContextFactory super ResultSet> mappingContextFactory;
private JdbcSourceFieldMapperImpl(SourceFieldMapper sourceFieldMapper, MappingContextFactory super ResultSet> mappingContextFactory) {
this.sourceFieldMapper = sourceFieldMapper;
this.mappingContextFactory = mappingContextFactory;
}
@Override
public void mapTo(ResultSet source, T target, MappingContext super ResultSet> context) throws Exception {
sourceFieldMapper.mapTo(source, target, context);
}
@Override
public T map(ResultSet source, MappingContext super ResultSet> context) throws MappingException {
return sourceFieldMapper.map(source, context);
}
@Override
public T map(ResultSet source) throws MappingException {
return sourceFieldMapper.map(source, mappingContextFactory.newContext());
}
@Override
public MappingContext super ResultSet> newMappingContext(ResultSet resultSet) throws SQLException {
return mappingContextFactory.newContext();
}
}
private static class JdbcMapperImpl implements JdbcMapper {
private final SetRowMapper setRowMapper;
private final MappingContextFactory super ResultSet> mappingContextFactory;
private JdbcMapperImpl(SetRowMapper setRowMapper, MappingContextFactory super ResultSet> mappingContextFactory) {
this.setRowMapper = setRowMapper;
this.mappingContextFactory = mappingContextFactory;
}
@Override
public T map(ResultSet source) throws MappingException {
return setRowMapper.map(source);
}
@Override
public T map(ResultSet source, MappingContext super ResultSet> context) throws MappingException {
return setRowMapper.map(source, context);
}
@Override
public > H forEach(ResultSet source, H handler) throws SQLException, MappingException {
return setRowMapper.forEach(source, handler);
}
@Override
public Iterator iterator(ResultSet source) throws SQLException, MappingException {
return setRowMapper.iterator(source);
}
@Override
public Enumerable enumerate(ResultSet source) throws SQLException, MappingException {
return setRowMapper.enumerate(source);
}
//IFJAVA8_START
@Override
public Stream stream(ResultSet source) throws SQLException, MappingException {
return setRowMapper.stream(source);
}
//IFJAVA8_END
@Override
public MappingContext super ResultSet> newMappingContext(ResultSet resultSet) throws SQLException {
return mappingContextFactory.newContext();
}
@Override
public String toString() {
return "JdbcMapperImpl{" +
"setRowMapper=" + setRowMapper +
", mappingContextFactory=" + mappingContextFactory +
'}';
}
}
private static class ResultSetEnumerableFactory implements UnaryFactory> {
@Override
public Enumerable newInstance(ResultSet rows) {
return new ResultSetEnumerable(rows);
}
}
}