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

org.simpleflatmapper.datastax.DatastaxMapperBuilder Maven / Gradle / Ivy

There is a newer version: 8.2.3
Show newest version
package org.simpleflatmapper.datastax;

import com.datastax.driver.core.*;
import com.datastax.driver.core.exceptions.DriverException;
import org.simpleflatmapper.map.ConsumerErrorHandler;
import org.simpleflatmapper.reflect.getter.GetterFactory;
import org.simpleflatmapper.datastax.impl.ResultSetEnumarable;
import org.simpleflatmapper.map.Mapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.context.MappingContextFactory;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.mapper.AbstractMapperBuilder;
import org.simpleflatmapper.map.mapper.JoinMapper;
import org.simpleflatmapper.map.mapper.KeyFactory;
import org.simpleflatmapper.map.mapper.MapperSourceImpl;
import org.simpleflatmapper.map.mapper.StaticSetRowMapper;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.util.Enumarable;
import org.simpleflatmapper.util.UnaryFactory;

/**
 * @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 property with the specified index,  the specified type.
     *
     * @param column           the property name
     * @param index            the property index
     * @param dataType          the property type, @see java.sql.Types
     * @param properties the property properties
     * @return the current builder
     */
    public DatastaxMapperBuilder addMapping(final String column, final int index, final DataType dataType, Object... properties) {
        return addMapping(new DatastaxColumnKey(column, index, dataType), properties);
    }

    /**
     * add the all the property present in the metaData
     *
     * @param metaData the metaDAta
     * @return the current builder
     */
    public DatastaxMapperBuilder addMapping(final ColumnDefinitions metaData) {
        for (int i = 1; i <= metaData.size(); i++) {
            addMapping(metaData.getName(i), i, metaData.getType(i), new Object[0]);
        }

        return this;
    }

    @Override
    protected DatastaxMapper newJoinMapper(Mapper mapper) {
        return new JoinDatastaxMapper(mapper, mapperConfig.consumerErrorHandler(), mappingContextFactoryBuilder.newFactory());
    }

    private static class JoinDatastaxMapper extends JoinMapper implements DatastaxMapper {
        public JoinDatastaxMapper(Mapper mapper, ConsumerErrorHandler errorHandler, MappingContextFactory 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 newStaticMapper(Mapper mapper) {
        return new StaticDatastaxMapper(mapper, mapperConfig.consumerErrorHandler(), mappingContextFactoryBuilder.newFactory());
    }

    public static class StaticDatastaxMapper extends StaticSetRowMapper implements DatastaxMapper {

        public StaticDatastaxMapper(Mapper mapper, ConsumerErrorHandler errorHandler, MappingContextFactory mappingContextFactory) {
            super(mapper, errorHandler, mappingContextFactory, new ResultSetEnumarableFactory());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy