use of com.datastax.driver.mapping.meta.EntityFieldMetaData in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method getFromRow.
/**
* Convert individual ResultSet Row into Entity instance
*/
public static <T> T getFromRow(Class<T> clazz, Row row) {
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
T entity = null;
Object primaryKey = null;
Object partitionKey = null;
// create PK
try {
entity = clazz.newInstance();
PrimaryKeyMetadata pkmeta = entityMetadata.getPrimaryKeyMetadata();
if (pkmeta.isCompound()) {
EntityFieldMetaData pkField = pkmeta.getOwnField();
primaryKey = pkField.getType().newInstance();
pkField.setValue(entity, primaryKey);
if (pkmeta.hasPartitionKey()) {
PrimaryKeyMetadata partmeta = pkmeta.getPartitionKey();
EntityFieldMetaData partField = partmeta.getOwnField();
partitionKey = partField.getType().newInstance();
partField.setValue(primaryKey, partitionKey);
}
}
} catch (Exception e) {
// skip error to support any-2-any
}
// set properties' values
for (EntityFieldMetaData field : entityMetadata.getFields()) {
Object value = getValueFromRow(row, field);
try {
if (value != null) {
if (field.isPartition()) {
field.setValue(partitionKey, value);
} else if (field.isPrimary()) {
field.setValue(primaryKey, value);
} else {
field.setValue(entity, value);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return entity;
}
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;
}
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 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 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());
}
Aggregations