org.sfm.datastax.DatastaxBinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-datastax Show documentation
Show all versions of sfm-datastax Show documentation
Cassandra Datastax SFM supports.
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);
}
}