Added update(draft).
This commit is contained in:
parent
84bb6b5fae
commit
7ac9288eb8
5 changed files with 111 additions and 16 deletions
2
pom.xml
2
pom.xml
|
@ -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>
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
@ -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,6 +169,34 @@ 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)
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue