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

org.sfm.datastax.DatastaxBinder Maven / Gradle / Ivy

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

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Statement;
import org.sfm.datastax.impl.SettableDataSetterFactory;
import org.sfm.map.MapperConfig;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.ConstantTargetFieldMapperFactorImpl;
import org.sfm.map.mapper.MapperCache;
import org.sfm.map.mapper.MapperKey;
import org.sfm.reflect.meta.ClassMeta;

public class DatastaxBinder {
    private final MapperConfig> config;
    private final ClassMeta classMeta;

    private final MapperCache> cache = new MapperCache>();

    public DatastaxBinder(ClassMeta classMeta, MapperConfig> config) {
        this.classMeta = classMeta;
        this.config = config;
    }

    public BoundStatementMapper mapTo(PreparedStatement statement) {
        return mapTo(statement.getVariables());
    }

    public BoundStatementMapper mapTo(ColumnDefinitions variables) {
        MapperKey mapperKey = DatastaxColumnKey.mapperKey(variables);
        BoundStatementMapper mapper = cache.get(mapperKey);

        if (mapper == null) {
            mapper = createMapper(mapperKey);
        }
        return mapper;
    }

    protected BoundStatementMapper createMapper(MapperKey mapperKey) {
        BoundStatementMapper mapper;SettableDataMapperBuilder settableDataMapperBuilder = new SettableDataMapperBuilder(classMeta, config,
                ConstantTargetFieldMapperFactorImpl.instance(new SettableDataSetterFactory(config, classMeta.getReflectionService())));
        for(DatastaxColumnKey columnKey : mapperKey.getColumns()) {
            settableDataMapperBuilder.addColumn(columnKey);
        }
        mapper = new BoundStatementMapper(settableDataMapperBuilder.mapper());
        cache.add(mapperKey, mapper);
        return mapper;
    }

    public Statement mapTo(T value, PreparedStatement preparedStatement) {
        BoundStatementMapper statementMapper = mapTo(preparedStatement);
        BoundStatement boundStatement = preparedStatement.bind();
        return statementMapper.mapTo(value, boundStatement);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy