Search in sources :

Example 31 with EntityTypeMetadata

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));
}
Also used : CompositeKey(com.datastax.driver.mapping.entity.CompositeKey) EntityWithCompositeKey(com.datastax.driver.mapping.entity.EntityWithCompositeKey) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Date(java.util.Date) SimpleKey(com.datastax.driver.mapping.entity.SimpleKey) Test(org.junit.Test)

Example 32 with EntityTypeMetadata

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);
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 33 with EntityTypeMetadata

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;
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 34 with EntityTypeMetadata

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;
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 35 with EntityTypeMetadata

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;
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Aggregations

EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)40 Test (org.junit.Test)18 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)13 TableMetadata (com.datastax.driver.core.TableMetadata)8 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)6 PrimaryKeyMetadata (com.datastax.driver.mapping.meta.PrimaryKeyMetadata)4 RegularStatement (com.datastax.driver.core.RegularStatement)3 EntityWithCompositeKey (com.datastax.driver.mapping.entity.EntityWithCompositeKey)2 SimpleKey (com.datastax.driver.mapping.entity.SimpleKey)2 ExecutionException (java.util.concurrent.ExecutionException)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 Cluster (com.datastax.driver.core.Cluster)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 Statement (com.datastax.driver.core.Statement)1 BuiltStatement (com.datastax.driver.core.querybuilder.BuiltStatement)1 TableProperties (com.datastax.driver.mapping.annotation.TableProperties)1 TableProperty (com.datastax.driver.mapping.annotation.TableProperty)1