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

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

package org.simpleflatmapper.jooq;

import org.jooq.Context;
import org.jooq.Record;
import org.jooq.SQLDialect;
import org.jooq.impl.CustomField;
import org.jooq.impl.DefaultDataType;
import org.simpleflatmapper.map.SourceMapper;
import org.simpleflatmapper.map.MapperBuildingException;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.context.MappingContextFactory;
import org.simpleflatmapper.map.getter.ContextualGetterFactoryAdapter;
import org.simpleflatmapper.map.mapper.ConstantSourceMapperBuilder;
import org.simpleflatmapper.map.mapper.KeyFactory;
import org.simpleflatmapper.map.mapper.MapperSource;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.context.MappingContextFactoryBuilder;
import org.simpleflatmapper.map.mapper.MapperSourceImpl;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.meta.ClassMeta;

import java.lang.reflect.Type;


public class JooqMapperBuilder {

	public static final MapperSource FIELD_MAPPER_SOURCE = new MapperSourceImpl(Record.class, new ContextualGetterFactoryAdapter(new RecordGetterFactory()));
	private static final KeyFactory KEY_FACTORY = new KeyFactory() {
		@Override
		public JooqFieldKey newKey(String name, int i) {
			return new JooqFieldKey(new FakeField(name), i);
		}
	};

	private final ConstantSourceMapperBuilder constantSourceMapperBuilder;

	public JooqMapperBuilder(final Type target) throws MapperBuildingException {
		this(target, ReflectionService.newInstance());
	}

	@SuppressWarnings("unchecked")
	public JooqMapperBuilder(final Type target, ReflectionService reflectService) throws MapperBuildingException {
		this(reflectService.getClassMeta(target), new JooqMappingContextFactoryBuilder(true));
	}

	public JooqMapperBuilder(final ClassMeta classMeta,
							 MappingContextFactoryBuilder mappingContextFactoryBuilder) throws MapperBuildingException {
		this(classMeta, mappingContextFactoryBuilder, MapperConfig.fieldMapperConfig());
	}

	public JooqMapperBuilder(final ClassMeta classMeta,
							 MappingContextFactoryBuilder mappingContextFactoryBuilder,
							 MapperConfig mapperConfig) throws MapperBuildingException {
		constantSourceMapperBuilder =
				ConstantSourceMapperBuilder.newConstantSourceMapperBuilder(
						FIELD_MAPPER_SOURCE,
						classMeta,
						mapperConfig,
						mappingContextFactoryBuilder,
						KEY_FACTORY);
	}

		public JooqMapperBuilder addField(JooqFieldKey key) {
		constantSourceMapperBuilder.addMapping(key, FieldMapperColumnDefinition.identity());
		return this;
	}

	public SourceMapper mapper() {
		return constantSourceMapperBuilder.mapper();
	}

	public MappingContextFactory contextFactory() {
		return constantSourceMapperBuilder.contextFactory();
	}

	private static class FakeField extends CustomField {

        protected FakeField(String name) {
            super(name, new DefaultDataType(SQLDialect.DEFAULT, Object.class, "varchar"));
        }

		public void accept(Context context) {
			throw new UnsupportedOperationException("Fake field not supposed to be used in query generation");
		}
	}
}