IN PROGRESS - issue DATACASS-32: Implement the TemplateAPI for CQL
https://jira.springsource.org/browse/DATACASS-32 Implemented remove()
This commit is contained in:
@@ -17,7 +17,10 @@ package org.springframework.data.cassandra.convert;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.cassandra.util.CQLUtils;
|
||||
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
|
||||
import org.springframework.data.mapping.model.PropertyValueProvider;
|
||||
import org.springframework.data.mapping.model.SpELExpressionEvaluator;
|
||||
@@ -32,6 +35,8 @@ import com.datastax.driver.core.Row;
|
||||
* @author Alex Shvid
|
||||
*/
|
||||
public class CassandraPropertyValueProvider implements PropertyValueProvider<CassandraPersistentProperty> {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CassandraPropertyValueProvider.class);
|
||||
|
||||
private final Row source;
|
||||
private final SpELExpressionEvaluator evaluator;
|
||||
@@ -68,6 +73,8 @@ public class CassandraPropertyValueProvider implements PropertyValueProvider<Cas
|
||||
}
|
||||
DataType columnType = source.getColumnDefinitions().getType(columnName);
|
||||
|
||||
log.info(columnType.getName().name());
|
||||
|
||||
/*
|
||||
* Dave Webb - Added handler for text since getBytes was throwing
|
||||
* InvalidTypeException when using getBytes on a text column.
|
||||
@@ -76,6 +83,9 @@ public class CassandraPropertyValueProvider implements PropertyValueProvider<Cas
|
||||
if (columnType.equals(DataType.text())) {
|
||||
return (T) source.getString(columnName);
|
||||
}
|
||||
if (columnType.equals(DataType.cint())) {
|
||||
return (T) new Integer(source.getInt(columnName));
|
||||
}
|
||||
|
||||
ByteBuffer bytes = source.getBytes(columnName);
|
||||
return (T) columnType.deserialize(bytes);
|
||||
|
||||
@@ -152,10 +152,9 @@ public class CassandraTemplate implements CassandraOperations {
|
||||
protected CassandraPersistentEntity<?> getEntity(Object o) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = null;
|
||||
|
||||
try {
|
||||
String entityClassName = o.getClass().getName();
|
||||
Class<?> entityClass = ClassUtils.forName(entityClassName, this.beanClassLoader);
|
||||
Class<?> entityClass = ClassUtils.forName(entityClassName, beanClassLoader);
|
||||
entity = mappingContext.getPersistentEntity(entityClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
@@ -397,7 +396,8 @@ public class CassandraTemplate implements CassandraOperations {
|
||||
*/
|
||||
@Override
|
||||
public void remove(Object object) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
remove(object, determineTableName(object.getClass()));
|
||||
|
||||
}
|
||||
|
||||
@@ -406,8 +406,38 @@ public class CassandraTemplate implements CassandraOperations {
|
||||
*/
|
||||
@Override
|
||||
public void remove(Object object, String tableName) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
CassandraPersistentEntity<?> entityClass = getEntity(object);
|
||||
|
||||
Assert.notNull(entityClass);
|
||||
|
||||
doRemove(object, tableName);
|
||||
|
||||
}
|
||||
|
||||
protected <T> void doRemove(final Object objectToRemove, final String tableName) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = getEntity(objectToRemove);
|
||||
|
||||
Assert.notNull(entity);
|
||||
|
||||
try {
|
||||
|
||||
final Query q = CQLUtils.toDeleteQuery(keyspace.getKeyspace(), tableName, objectToRemove, entity);
|
||||
log.info(q.toString());
|
||||
|
||||
execute(new SessionCallback<ResultSet>() {
|
||||
|
||||
public ResultSet doInSession(Session s) throws DataAccessException {
|
||||
|
||||
return s.execute(q);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
} catch (EntityWriterException e) {
|
||||
throw exceptionTranslator.translateExceptionIfPossible(new RuntimeException("Failed to translate Object to Query", e));
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -416,7 +446,6 @@ public class CassandraTemplate implements CassandraOperations {
|
||||
@Override
|
||||
public void createTable(Class<?> entityClass) {
|
||||
|
||||
|
||||
try {
|
||||
|
||||
final CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
|
||||
|
||||
@@ -11,13 +11,14 @@ import org.springframework.data.cassandra.exception.EntityWriterException;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.datastax.driver.core.ColumnMetadata;
|
||||
import com.datastax.driver.core.DataType;
|
||||
import com.datastax.driver.core.Query;
|
||||
import com.datastax.driver.core.TableMetadata;
|
||||
import com.datastax.driver.core.querybuilder.Clause;
|
||||
import com.datastax.driver.core.querybuilder.Delete;
|
||||
import com.datastax.driver.core.querybuilder.Delete.Where;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
|
||||
@@ -222,7 +223,8 @@ public abstract class CQLUtils {
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
Object o = (String)prop.getGetter().invoke(objectToSave, new Object[0]);
|
||||
|
||||
Object o = prop.getGetter().invoke(objectToSave, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
@@ -248,6 +250,60 @@ public abstract class CQLUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param keyspace
|
||||
* @param tableName
|
||||
* @param objectToRemove
|
||||
* @param entity
|
||||
* @return
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static Query toDeleteQuery(String keyspace, String tableName,
|
||||
final Object objectToRemove, CassandraPersistentEntity<?> entity) throws EntityWriterException {
|
||||
|
||||
final Delete.Selection ds = QueryBuilder.delete();
|
||||
final Delete q = ds.from(keyspace, tableName);
|
||||
final Where w = q.where();
|
||||
|
||||
final Exception innerException = new Exception();
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
/*
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
|
||||
if (prop.isIdProperty()) {
|
||||
Object o = (String)prop.getGetter().invoke(objectToRemove, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
if (o != null) {
|
||||
w.and(QueryBuilder.eq(prop.getColumnName(), o));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
innerException.initCause(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (innerException.getCause() != null) {
|
||||
throw new EntityWriterException("Failed to convert Persistent Entity to CQL/Query", innerException.getCause());
|
||||
}
|
||||
|
||||
return q;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate the CQL for insert
|
||||
*
|
||||
@@ -313,5 +369,4 @@ public abstract class CQLUtils {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import static org.junit.Assert.assertNotNull;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
@@ -96,6 +98,7 @@ public class CassandraOperationsTest {
|
||||
* <li>insert()</li>
|
||||
* <li>selectOne()</li>
|
||||
* <li>select()</li>
|
||||
* <li>remove()</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Test
|
||||
@@ -105,6 +108,7 @@ public class CassandraOperationsTest {
|
||||
u.setUsername("cassandra");
|
||||
u.setFirstName("Apache");
|
||||
u.setLastName("Cassnadra");
|
||||
u.setAge(40);
|
||||
|
||||
cassandraTemplate.insert(u, "users");
|
||||
|
||||
@@ -122,6 +126,15 @@ public class CassandraOperationsTest {
|
||||
log.debug(x.getLastName());
|
||||
}
|
||||
|
||||
cassandraTemplate.remove(u);
|
||||
|
||||
User delUser = cassandraTemplate.selectOne("select * from test.users where username='cassandra';" , User.class);
|
||||
|
||||
log.info("delUser => " + delUser);
|
||||
|
||||
Assert.assertNull(delUser);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
@@ -62,6 +62,11 @@ public class User {
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/*
|
||||
* Age
|
||||
*/
|
||||
private int age;
|
||||
|
||||
/*
|
||||
* Following other users in userline
|
||||
*/
|
||||
@@ -136,4 +141,18 @@ public class User {
|
||||
this.friends = friends;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the age.
|
||||
*/
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param age The age to set.
|
||||
*/
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user