use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class EntityTypeParserTest method testGetKeyDataCompoundKey.
@Test
public void testGetKeyDataCompoundKey() {
SimpleKey sk = new SimpleKey();
sk.setName("name");
sk.setRank(10);
CompositeKey id = new CompositeKey();
Date date = new Date();
id.setKey(sk);
id.setCreated(date);
id.setEmail("email");
EntityTypeMetadata meta = EntityTypeParser.getEntityMetadata(EntityWithCompositeKey.class);
List<String> cols = meta.getPkColumns();
assertEquals(6, cols.size());
assertEquals("name", cols.get(0));
assertEquals("rank", cols.get(1));
assertEquals("t1", cols.get(2));
assertEquals("t2", cols.get(3));
assertEquals("created", cols.get(4));
assertEquals("email", cols.get(5));
List<Object> vals = meta.getIdValues(id);
assertEquals(6, vals.size());
assertEquals("name", vals.get(0));
assertEquals(10, vals.get(1));
assertEquals(date, vals.get(4));
assertEquals("email", vals.get(5));
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method prepareRemoveItemsFromSetOrList.
public static BoundStatement prepareRemoveItemsFromSetOrList(Object id, Class<?> clazz, String propertyName, Object item, String keyspace, Session session) {
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
Update update = QueryBuilder.update(keyspace, emeta.getTableName());
if (item instanceof Set<?> && fmeta.getType() == Set.class) {
Set<?> set = (Set<?>) item;
if (set.size() == 0)
return null;
update.with(QueryBuilder.removeAll(fmeta.getColumnName(), set));
} else if (item instanceof List<?> && fmeta.getType() == List.class) {
List<?> list = (List<?>) item;
if (list.size() == 0)
return null;
update.with(QueryBuilder.discardAll(fmeta.getColumnName(), list));
} else if (fmeta.getType() == Set.class) {
update.with(QueryBuilder.remove(fmeta.getColumnName(), item));
} else if (fmeta.getType() == List.class) {
update.with(QueryBuilder.discard(fmeta.getColumnName(), item));
}
return prepareUpdate(id, emeta, update, session);
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method buildUpdate.
/**
* Statement to persist an entity in Cassandra
*
* @param entity to be inserted
* @return com.datastax.driver.core.BoundStatement
*/
public static <E> BuiltStatement buildUpdate(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()];
Update update = QueryBuilder.update(keyspace, table);
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);
update.onlyIf(eq(verField.getColumnName(), oldVersion));
}
for (int i = 0; i < fields.size(); i++) {
EntityFieldMetaData field = fields.get(i);
String colName = field.getColumnName();
Object colVal = null;
if (pkCols.contains(colName)) {
int idx = pkCols.indexOf(colName);
colVal = pkVals.get(idx);
update.where(eq(colName, colVal));
continue;
} else {
colVal = field.getValue(entity);
}
columns[i] = colName;
if (field.equals(verField)) {
values[i] = newVersion;
} else {
values[i] = colVal;
}
update.with(set(colName, colVal));
}
applyOptions(options, update, entityMetadata);
return update;
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method buildDelete.
public static <T> BuiltStatement buildDelete(Class<T> clazz, Object id, String keyspace) {
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
List<String> pkCols = entityMetadata.getPkColumns();
Object[] values = entityMetadata.getIdValues(id).toArray(new Object[pkCols.size()]);
Delete delete = buildDelete(entityMetadata, pkCols, values, keyspace);
return delete;
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method prepareSelect.
/**
* Prepare BoundStatement to select row by id
*/
public static <T> BoundStatement prepareSelect(Class<T> clazz, Object id, final ReadOptions options, final String keyspace, final Session session) {
EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(clazz);
final List<EntityFieldMetaData> fields = entityMetadata.getFields();
final List<String> pkCols = entityMetadata.getPkColumns();
final String table = entityMetadata.getTableName();
// get prepared statement
PreparedStatement ps;
try {
ps = statementCache.get(getSelectCacheKey(table, session, fields), new Callable<PreparedStatement>() {
@Override
public PreparedStatement call() throws Exception {
Select stmt = buildSelectAll(table, pkCols, options, keyspace, fields);
return session.prepare(stmt);
}
});
} catch (ExecutionException e) {
// if the error caused by prepare the client will get it as is,
// otherwise process will not blow and statement will not be cached.
Select stmt = buildSelectAll(table, pkCols, options, keyspace, fields);
ps = session.prepare(stmt);
}
// bind parameters
Object[] values = entityMetadata.getIdValues(id).toArray(new Object[pkCols.size()]);
BoundStatement bs = ps.bind(values);
return bs;
}
Aggregations