use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class EntityTypeParserTest method testGetEntityMetadataOverrideDataType.
@Test
public void testGetEntityMetadataOverrideDataType() {
EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityOverrideDataType.class);
assertEquals(2, meta.getFields().size());
EntityFieldMetaData fd = meta.getFieldMetadata("uid");
assertEquals(DataType.Name.TIMEUUID, fd.getDataType());
fd = meta.getFieldMetadata("name");
assertEquals(DataType.Name.VARCHAR, fd.getDataType());
}
use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class EntityTypeParserTest method testGetEntityMetadataOverrideCollectionType.
@Test
public void testGetEntityMetadataOverrideCollectionType() {
EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityWithCollectionsOverride.class);
assertEquals(4, meta.getFields().size());
EntityFieldMetaData fd = meta.getFieldMetadata("rates");
assertEquals(TreeMap.class, fd.getCollectionType());
fd = meta.getFieldMetadata("refs");
assertEquals(TreeSet.class, fd.getCollectionType());
fd = meta.getFieldMetadata("trades");
assertEquals(LinkedList.class, fd.getCollectionType());
}
use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class MappingSessionTest method getByQueryTest.
@Test
public void getByQueryTest() throws Exception {
for (int i = 0; i < 3; i++) {
EntityWithIndexes obj = new EntityWithIndexes();
obj.setCount(100);
obj.setEmail("email@test");
obj.setName("test" + i);
obj.setTimeStamp(new Date());
obj.setUuid(UUID.randomUUID());
target.save(obj);
}
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
EntityFieldMetaData fdata = emeta.getFieldMetadata("email");
Statement query = QueryBuilder.select().all().from(keyspace, emeta.getTableName()).where(eq(fdata.getColumnName(), "email@test"));
List<EntityWithIndexes> items = target.getByQuery(EntityWithIndexes.class, query);
assertEquals(3, items.size());
}
use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method prepareReplaceAt.
public static BoundStatement prepareReplaceAt(Object id, Class<?> clazz, String propertyName, Object item, int idx, WriteOptions options, String keyspace, Session session) {
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
Update update = QueryBuilder.update(keyspace, emeta.getTableName());
if (fmeta.getType() == List.class) {
update.with(QueryBuilder.setIdx(fmeta.getColumnName(), idx, item));
}
applyOptions(options, update, null);
return prepareUpdate(id, emeta, update, session);
}
use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method buildInsert.
/**
* Statement to persist an entity in Cassandra
*
* @param entity to be inserted
* @return com.datastax.driver.core.BoundStatement
*/
public static <E> BuiltStatement buildInsert(E entity, WriteOptions options, String keyspace) {
Class<?> clazz = entity.getClass();
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
String table = entityMetadata.getTableName();
List<EntityFieldMetaData> fields = entityMetadata.getFields();
List<String> pkCols = entityMetadata.getPkColumns();
List<Object> pkVals = entityMetadata.getEntityPKValues(entity);
String[] columns = new String[fields.size()];
Object[] values = new Object[fields.size()];
EntityFieldMetaData verField = null;
Object newVersion = null;
Object oldVersion = null;
// increment and set @Version field
if (entityMetadata.hasVersion()) {
verField = entityMetadata.getVersionField();
oldVersion = verField.getValue(entity);
newVersion = incVersion(oldVersion);
verField.setValue(entity, newVersion);
}
for (int i = 0; i < fields.size(); i++) {
EntityFieldMetaData f = fields.get(i);
String colName = f.getColumnName();
Object colVal = null;
if (pkCols.contains(colName)) {
int idx = pkCols.indexOf(colName);
colVal = pkVals.get(idx);
if (colVal == null && f.isAutoGenerate()) {
if (f.getDataType() == DataType.Name.TIMEUUID) {
colVal = QueryBuilder.fcall("now");
} else if (f.getDataType() == DataType.Name.UUID) {
colVal = QueryBuilder.fcall("uuid");
}
}
} else {
colVal = f.getValue(entity);
}
columns[i] = colName;
if (f.equals(verField)) {
values[i] = newVersion;
} else {
values[i] = colVal;
}
}
Insert insert = insertInto(keyspace, table).values(columns, values);
if (verField != null) {
insert.ifNotExists();
}
applyOptions(options, insert, entityMetadata);
return insert;
}
Aggregations