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

org.simpleflatmapper.jooq.JooqJdbcMapperBuilder Maven / Gradle / Ivy

package org.simpleflatmapper.jooq;

import org.simpleflatmapper.jdbc.JdbcColumnKey;
import org.simpleflatmapper.jdbc.ResultSetEnumerable;
import org.simpleflatmapper.jdbc.ResultSetGetterFactory;
import org.simpleflatmapper.map.*;
import org.simpleflatmapper.map.context.KeySourceGetter;
import org.simpleflatmapper.map.getter.ContextualGetter;
import org.simpleflatmapper.map.getter.ContextualGetterFactory;
import org.simpleflatmapper.map.getter.ContextualGetterFactoryAdapter;
import org.simpleflatmapper.map.mapper.*;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.reflect.Getter;
import org.simpleflatmapper.reflect.getter.GetterFactory;
import org.simpleflatmapper.util.BiFunction;
import org.simpleflatmapper.util.Function;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.util.Enumerable;
import org.simpleflatmapper.util.UnaryFactory;

import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;
/**
 * @param  the targeted type of the jdbcMapper
 */
final class JooqJdbcMapperBuilder extends MapperBuilder, SetRowMapper, JooqJdbcMapperBuilder> {

    public static final ResultSetJooqFieldKeyGetterFactory GETTER_FACTORY = new ResultSetJooqFieldKeyGetterFactory();
    private static final MapperSourceImpl FIELD_MAPPER_SOURCE =
            new MapperSourceImpl(ResultSet.class,  new ContextualGetterFactoryAdapter(
                    GETTER_FACTORY
            ));
    public static final KeySourceGetter KEY_SOURCE_GETTER = new KeySourceGetter() {
        @Override
        public Object getValue(JooqFieldKey key, ResultSet source) throws Exception {
            return source.getObject(key.getIndex());
        }
    };

    static JdbcColumnKey toJdbcColumnKey(JooqFieldKey key) {
        return new JdbcColumnKey(key.getName(), key.getIndex(), key.getField().getDataType().getSQLType(), key.getParent() != null ? toJdbcColumnKey(key.getParent()) : null);
    }


    public static final Function> COLUMN_DEFINITION_FACTORY = FieldMapperColumnDefinition.factory();
    private static final KeyFactory KEY_FACTORY = new KeyFactory() {
        @Override
        public JooqFieldKey newKey(String name, int i) {
            throw new UnsupportedOperationException();
        }
    };


    JooqJdbcMapperBuilder(
            final ClassMeta classMeta,
            final MapperConfig mapperConfig) {
        this(classMeta, mapperConfig,
                (ContextualGetterFactory)GETTER_FACTORY,
                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.
     */
    JooqJdbcMapperBuilder(
             final ClassMeta classMeta,
             final MapperConfig mapperConfig,
             final ContextualGetterFactory getterFactory,
             final MappingContextFactoryBuilder parentBuilder) {

        super(KEY_FACTORY, 
                new DefaultSetRowMapperBuilder(
                        classMeta,
                        parentBuilder,
                        mapperConfig,
                        FIELD_MAPPER_SOURCE.getterFactory(getterFactory),
                        KEY_FACTORY,
                        new ResultSetEnumerableFactory(),
                        KEY_SOURCE_GETTER),
                new BiFunction, List, SetRowMapper>() {
                    @Override
                    public SetRowMapper apply(SetRowMapper setRowMapper, List keys) {
                        return setRowMapper;
                    }
                }, 
                COLUMN_DEFINITION_FACTORY,
                1 );
    }



    private static class ResultSetEnumerableFactory implements UnaryFactory> {
        @Override
        public Enumerable newInstance(ResultSet rows) {
            return new ResultSetEnumerable(rows);
        }
    }


    private static class JdbcMappingContextFactoryBuilder extends MappingContextFactoryBuilder {
        private JdbcMappingContextFactoryBuilder(boolean ignoreRootKey) {
            super(KEY_SOURCE_GETTER, ignoreRootKey);
        }

    }

    private static class ResultSetJooqFieldKeyGetterFactory implements GetterFactory, ContextualGetterFactory {

        private final ResultSetGetterFactory delegate = ResultSetGetterFactory.INSTANCE;

        @Override
        public 

Getter newGetter(Type target, JooqFieldKey key, Object... properties) { return delegate.newGetter(target, toJdbcColumnKey(key), properties); } @Override public

ContextualGetter newGetter(Type target, JooqFieldKey key, MappingContextFactoryBuilder> mappingContextFactoryBuilder, Object... properties) { ContextualGetterFactory delegate = this.delegate; return delegate.newGetter(target, toJdbcColumnKey(key), mappingContextFactoryBuilder, properties); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy