Added update(draft).

This commit is contained in:
Greg Burd 2017-08-25 16:08:31 -04:00
parent 84bb6b5fae
commit 7ac9288eb8
5 changed files with 111 additions and 16 deletions

View file

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.helenus</groupId>
<artifactId>helenus-core</artifactId>
<version>2.0.21-SNAPSHOT</version>
<version>2.0.23-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helenus</name>

View file

@ -23,23 +23,27 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.io.Closeable;
import java.io.PrintStream;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import net.helenus.core.operation.*;
import net.helenus.core.reflect.Drafted;
import net.helenus.core.reflect.DslExportable;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.HelenusProperty;
import net.helenus.mapping.MappingUtil;
import net.helenus.mapping.value.*;
import net.helenus.support.DslPropertyException;
import net.helenus.support.Fun;
import net.helenus.support.Fun.Tuple1;
import net.helenus.support.Fun.Tuple2;
import net.helenus.support.Fun.Tuple6;
import static net.helenus.core.Query.eq;
public final class HelenusSession extends AbstractSessionOperations implements Closeable {
private final int MAX_CACHE_SIZE = 10000;
@ -385,6 +389,53 @@ public final class HelenusSession extends AbstractSessionOperations implements C
return new UpdateOperation(this);
}
public <T> UpdateOperation update(Drafted<T> draft) {
UpdateOperation update = new UpdateOperation(this);
Map<String, Object> map = draft.toMap();
HelenusEntity entity = draft.getEntity();
Set<String> mutatedProperties = draft.mutated();
// Add all the mutated values contained in the draft.
entity.getOrderedProperties().forEach(property -> {
switch (property.getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
break;
default:
String propertyName = property.getPropertyName();
if (mutatedProperties.contains(propertyName)) {
Object value = map.get(propertyName);
Getter<Object> getter = new Getter<Object>() {
@Override
public Object get() {
throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty()));
}
};
update.set(getter, value);
}
}
});
// Add the partition and clustering keys if they were in the draft (normally the case).
entity.getOrderedProperties().forEach(property -> {
switch (property.getColumnType()) {
case PARTITION_KEY:
case CLUSTERING_COLUMN:
String propertyName = property.getPropertyName();
Object value = map.get(propertyName);
Getter<Object> getter = new Getter<Object>() {
@Override
public Object get() {
throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty()));
}
};
update.where(getter, eq(value));
}
});
return update;
}
public <V> UpdateOperation update(Getter<V> getter, V v) {
Objects.requireNonNull(getter, "field is empty");
Objects.requireNonNull(v, "value is empty");

View file

@ -1,9 +1,13 @@
package net.helenus.core.reflect;
import net.helenus.mapping.HelenusEntity;
import java.util.Set;
public interface Drafted<T> extends MapExportable {
HelenusEntity getEntity();
Set<String> mutated();
T build();

View file

@ -20,12 +20,19 @@ import static net.helenus.core.Query.eq;
import net.helenus.core.Helenus;
import net.helenus.core.HelenusSession;
import net.helenus.core.Operator;
import net.helenus.core.reflect.Drafted;
import net.helenus.mapping.HelenusEntity;
import net.helenus.support.Fun;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
@ -85,13 +92,13 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
// select row and map to entity
User actual =
session
.selectAll(User.class)
.mapTo(User.class)
.where(user::id, eq(100L))
.sync()
.findFirst()
.get();
session
.selectAll(User.class)
.mapTo(User.class)
.where(user::id, eq(100L))
.sync()
.findFirst()
.get();
assertUsers(newUser, actual);
// select as object
@ -150,7 +157,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
name =
(String)
session
.select()
.<Fun.ArrayTuple>select()
.column(user::name)
.where(user::id, eq(100L))
.sync()
@ -162,11 +169,39 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
// UPDATE
session.update(new Drafted<User>() {
@Override
public HelenusEntity getEntity() { return Helenus.entity(User.class); }
@Override
public Map<String, Object> toMap() {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "joeseph");
map.put("age", Integer.valueOf(45));
map.put("id", 100L);
return map;
}
@Override
public Set<String> mutated() {
Set<String> set = new HashSet<String>();
set.add("name");
set.add("age");
return set;
}
@Override
public User build() {
return null;
}
}).sync();
session
.update(user::name, "albert")
.set(user::age, 35)
.where(user::id, Operator.EQ, 100L)
.sync();
.update(user::name, "albert")
.set(user::age, 35)
.where(user::id, Operator.EQ, 100L)
.sync();
long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync();
Assert.assertEquals(1L, cnt);

View file

@ -19,6 +19,7 @@ import java.util.Date;
import java.util.Map;
import java.util.Set;
import net.helenus.core.reflect.Drafted;
import net.helenus.mapping.HelenusEntity;
import net.helenus.mapping.annotation.*;
@Table
@ -40,6 +41,10 @@ public interface Account {
}
class Draft implements Drafted { //TODO
@Override
public HelenusEntity getEntity() { return null; }
@Override
public Set<String> mutated() {
return null;