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

org.sfm.datastax.impl.getter.DatastaxUDTGetter Maven / Gradle / Ivy

There is a newer version: 8.2.3
Show newest version
package org.sfm.datastax.impl.getter;

import com.datastax.driver.core.GettableByIndexData;
import com.datastax.driver.core.UserType;
import org.sfm.datastax.DatastaxColumnKey;
import org.sfm.datastax.DatastaxMapperFactory;
import org.sfm.datastax.impl.DatastaxMappingContextFactoryBuilder;
import org.sfm.datastax.impl.RowGetterFactory;
import org.sfm.map.Mapper;
import org.sfm.map.MapperConfig;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.FieldMapperMapperBuilder;
import org.sfm.map.mapper.MapperSourceImpl;
import org.sfm.reflect.Getter;
import org.sfm.reflect.meta.ClassMeta;

import java.lang.reflect.Type;
import java.util.Iterator;

public class DatastaxUDTGetter implements Getter {
    private final Mapper mapper;
    private final int index;

    public DatastaxUDTGetter(Mapper mapper, int index) {
        this.mapper = mapper;
        this.index = index;
    }

    @Override
    public T get(GettableByIndexData target) throws Exception {
        return mapper.map(target.getUDTValue(index));
    }

    @SuppressWarnings("unchecked")
    public static 

Getter newInstance(DatastaxMapperFactory factory, Type target, UserType tt, int index) { Mapper mapper = newUDTMapper(target, tt, factory); return new DatastaxUDTGetter

(mapper, index); } public static

Mapper newUDTMapper(Type target, UserType tt, DatastaxMapperFactory factory) { FieldMapperMapperBuilder builder = newFieldMapperBuilder(factory, target); Iterator iterator = tt.iterator(); int i = 0; while(iterator.hasNext()) { UserType.Field f = iterator.next(); FieldMapperColumnDefinition identity = FieldMapperColumnDefinition.identity(); builder.addMapping(new DatastaxColumnKey(f.getName(), i, f.getType()), identity); i ++; } return builder.mapper(); } public static

FieldMapperMapperBuilder newFieldMapperBuilder(DatastaxMapperFactory factory, Type target) { MapperConfig> config = factory.mapperConfig(); MapperSourceImpl mapperSource = new MapperSourceImpl(GettableByIndexData.class, new RowGetterFactory(factory)); ClassMeta

classMeta = factory.getClassMeta(target); return new FieldMapperMapperBuilder( mapperSource, classMeta, config, new DatastaxMappingContextFactoryBuilder() ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy