org.simpleflatmapper.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.simpleflatmapper.datastax;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.ColumnDefinitions;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.SettableByIndexData;
import com.datastax.driver.core.Statement;
import org.simpleflatmapper.datastax.impl.DatastaxMapperKeyComparator;
import org.simpleflatmapper.datastax.impl.SettableDataSetterFactory;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactoryImpl;
import org.simpleflatmapper.map.mapper.MapperCache;
import org.simpleflatmapper.map.mapper.MapperKey;
import org.simpleflatmapper.reflect.meta.ClassMeta;
public class DatastaxBinder {
private final MapperConfig config;
private final ClassMeta classMeta;
private final MapperCache> cache =
new MapperCache>(DatastaxMapperKeyComparator.INSTANCE);
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,
ConstantTargetFieldMapperFactoryImpl.newInstance(new SettableDataSetterFactory(config, classMeta.getReflectionService()), SettableByIndexData.class));
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);
}
}