org.sfm.datastax.DatastaxMapperBuilder Maven / Gradle / Ivy
package org.sfm.datastax;
import com.datastax.driver.core.*;
import com.datastax.driver.core.exceptions.DriverException;
import org.sfm.datastax.impl.ResultSetEnumarable;
import org.sfm.map.*;
import org.sfm.map.column.ColumnProperty;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.context.MappingContextFactory;
import org.sfm.map.context.MappingContextFactoryBuilder;
import org.sfm.map.mapper.AbstractMapperBuilder;
import org.sfm.map.mapper.JoinMapperImpl;
import org.sfm.map.mapper.KeyFactory;
import org.sfm.map.mapper.MapperSourceImpl;
import org.sfm.map.mapper.StaticSetRowMapper;
import org.sfm.reflect.meta.ClassMeta;
import org.sfm.utils.Enumarable;
import org.sfm.utils.UnaryFactory;
import java.sql.SQLException;
/**
* @see DatastaxMapperFactory
* @param the targeted type of the jdbcMapper
*/
public final class DatastaxMapperBuilder extends AbstractMapperBuilder, DatastaxMapperBuilder> {
public static final KeyFactory KEY_FACTORY = new KeyFactory() {
@Override
public DatastaxColumnKey newKey(String name, int i) {
return new DatastaxColumnKey(name, i);
}
};
/**
* @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 DatastaxMapperBuilder(
final ClassMeta classMeta,
MapperConfig> mapperConfig,
GetterFactory getterFactory,
MappingContextFactoryBuilder parentBuilder) {
super(classMeta, parentBuilder, mapperConfig, new MapperSourceImpl(GettableByIndexData.class, getterFactory), KEY_FACTORY, 0);
}
/**
* add a new mapping to the specified column with the specified index, the specified type.
*
* @param column the column name
* @param index the column index
* @param dataType the column type, @see java.sql.Types
* @param properties the column properties
* @return the current builder
*/
public DatastaxMapperBuilder addMapping(final String column, final int index, final DataType dataType, ColumnProperty... properties) {
return addMapping(new DatastaxColumnKey(column, index, dataType), properties);
}
/**
* add the all the column present in the metaData
*
* @param metaData the metaDAta
* @return the current builder
* @throws SQLException when an error occurs getting the metaData
*/
public DatastaxMapperBuilder addMapping(final ColumnDefinitions metaData) throws SQLException {
for (int i = 1; i <= metaData.size(); i++) {
addMapping(metaData.getName(i), i, metaData.getType(i));
}
return this;
}
@Override
protected DatastaxColumnKey key(String column, int index) {
return new DatastaxColumnKey(column, index);
}
@Override
protected DatastaxMapper newJoinJdbcMapper(Mapper mapper) {
return new JoinDatastaxMapper(mapper, mapperConfig.rowHandlerErrorHandler(), mappingContextFactoryBuilder.newFactory());
}
private static class JoinDatastaxMapper extends JoinMapperImpl implements DatastaxMapper {
public JoinDatastaxMapper(Mapper mapper, RowHandlerErrorHandler errorHandler, MappingContextFactory super Row> mappingContextFactory) {
super(mapper, errorHandler, mappingContextFactory, new ResultSetEnumarableFactory());
}
}
private static class ResultSetEnumarableFactory implements UnaryFactory> {
@Override
public Enumarable newInstance(ResultSet rows) {
return new ResultSetEnumarable(rows);
}
}
@Override
protected DatastaxMapper newStaticJdbcMapper(Mapper mapper) {
return new StaticDatastaxMapper(mapper, mapperConfig.rowHandlerErrorHandler(), mappingContextFactoryBuilder.newFactory());
}
public static class StaticDatastaxMapper extends StaticSetRowMapper implements DatastaxMapper {
public StaticDatastaxMapper(Mapper mapper, RowHandlerErrorHandler errorHandler, MappingContextFactory super Row> mappingContextFactory) {
super(mapper, errorHandler, mappingContextFactory, new ResultSetEnumarableFactory());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy