Merge branch '2.0.15-SNAPSHOT' into develop

This commit is contained in:
Greg Burd 2017-08-10 16:10:31 -04:00
commit 7f5cc4add6
8 changed files with 48 additions and 33 deletions

View file

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

View file

@ -344,38 +344,36 @@ public final class HelenusSession extends AbstractSessionOperations implements C
return new UpdateOperation(this, p, v);
}
public InsertOperation insert() {
return new InsertOperation(this, true);
}
public <T> InsertOperation<T> insert() { return new InsertOperation<T>(this, true); }
public InsertOperation insert(Object pojo) {
return insert(pojo, null);
}
public <T> InsertOperation<T> insert(Object pojo) { return this.<T>insert(pojo, null); }
public InsertOperation insert(Drafted draft) {
return insert(draft.build(), draft.mutated());
}
public <T> InsertOperation<T> insert(Drafted draft) { return this.<T>insert(draft.build(), draft.mutated()); }
public InsertOperation insert(Object pojo, Set<String> mutations) {
public <T> InsertOperation<T> insert(Object pojo, Set<String> mutations) {
Objects.requireNonNull(pojo, "pojo is empty");
Class<?> iface = MappingUtil.getMappingInterface(pojo);
HelenusEntity entity = Helenus.entity(iface);
return new InsertOperation(this, entity, pojo, mutations, true);
return new InsertOperation<T>(this, entity, pojo, mutations, true);
}
public InsertOperation upsert() {
return new InsertOperation(this, false);
public <T> InsertOperation<T> upsert() {
return new InsertOperation<T>(this, false);
}
public InsertOperation upsert(Object pojo) {
public <T> InsertOperation<T> upsert(Drafted draft) { return this.<T>upsert(draft.build(), draft.mutated()); }
public <T> InsertOperation<T> upsert(Object pojo) { return this.<T>upsert(pojo, null); }
public <T> InsertOperation<T> upsert(Object pojo, Set<String> mutations) {
Objects.requireNonNull(pojo, "pojo is empty");
Class<?> iface = MappingUtil.getMappingInterface(pojo);
HelenusEntity entity = Helenus.entity(iface);
return new InsertOperation(this, entity, pojo, null, false);
return new InsertOperation<T>(this, entity, pojo, mutations, false);
}
public DeleteOperation delete() {

View file

@ -21,6 +21,7 @@ import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import net.helenus.core.AbstractSessionOperations;
import net.helenus.core.Getter;
import net.helenus.core.Helenus;
import net.helenus.core.reflect.HelenusPropertyNode;
import net.helenus.core.reflect.MapExportable;
import net.helenus.mapping.HelenusEntity;
@ -32,12 +33,13 @@ import net.helenus.support.HelenusMappingException;
import java.util.*;
public final class InsertOperation extends AbstractOperation<ResultSet, InsertOperation> {
public final class InsertOperation<T> extends AbstractOperation<T, InsertOperation<T>> {
private HelenusEntity entity;
private final List<Fun.Tuple2<HelenusPropertyNode, Object>> values = new ArrayList<Fun.Tuple2<HelenusPropertyNode, Object>>();
private boolean ifNotExists;
private Object pojo;
private int[] ttl;
private long[] timestamp;
@ -53,9 +55,10 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
super(sessionOperations);
this.entity = entity;
this.pojo = pojo;
this.ifNotExists = ifNotExists;
Collection<HelenusProperty> properties = entity.getOrderedProperties();
Set<String> keys = (mutations == null) ? ((MapExportable) pojo).toMap().keySet() : mutations;
Set<String> keys = (mutations == null) ? null : mutations;
for (HelenusProperty prop : properties) {
@ -75,17 +78,17 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
}
public InsertOperation ifNotExists() {
public InsertOperation<T> ifNotExists() {
this.ifNotExists = true;
return this;
}
public InsertOperation ifNotExists(boolean enable) {
public InsertOperation<T> ifNotExists(boolean enable) {
this.ifNotExists = enable;
return this;
}
public <V> InsertOperation value(Getter<V> getter, V val) {
public <V> InsertOperation<T> value(Getter<V> getter, V val) {
Objects.requireNonNull(getter, "getter is empty");
@ -134,17 +137,26 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
}
@Override
public ResultSet transform(ResultSet resultSet) {
return resultSet;
public T transform(ResultSet resultSet) {
if (pojo != null && ((T) pojo).getClass().isAssignableFrom(ResultSet.class)) {
return (T) pojo;
} else {
if (values.size() > 0) {
Map<String, Object> backingMap = new HashMap<String, Object>(values.size());
values.forEach(t -> backingMap.put(t._1.getProperty().getPropertyName(), t._2));
pojo = Helenus.map(values.get(0)._1.getEntity().getMappingInterface(), backingMap);
}
}
return (T) pojo;
}
public InsertOperation usingTtl(int ttl) {
public InsertOperation<T> usingTtl(int ttl) {
this.ttl = new int[1];
this.ttl[0] = ttl;
return this;
}
public InsertOperation usingTimestamp(long timestamp) {
public InsertOperation<T> usingTimestamp(long timestamp) {
this.timestamp = new long[1];
this.timestamp[0] = timestamp;
return this;

View file

@ -101,8 +101,6 @@ public class MapperInvocationHandler<E> implements InvocationHandler {
return type.getDefaultValue();
} else if (returnType.isEnum()) {
throw new HelenusException("missing default type for enum user type " + returnType);
}
}

View file

@ -18,6 +18,7 @@ package net.helenus.test.integration.core.prepared;
import static net.helenus.core.Query.marker;
import java.math.BigDecimal;
import java.util.Optional;
import net.helenus.core.HelenusSession;
import net.helenus.core.Query;
@ -57,7 +58,7 @@ public class PreparedStatementTest extends AbstractEmbeddedCassandraTest {
session = Helenus.init(getSession()).showCql().add(Car.class).autoCreateDrop().get();
car = Helenus.dsl(Car.class, session.getMetadata());
insertOp = session.insert()
insertOp = session.<ResultSet>insert()
.value(car::make, Query.marker())
.value(car::model, Query.marker())
.value(car::year, 2004)

View file

@ -48,7 +48,7 @@ public class InsertPartialTest extends AbstractEmbeddedCassandraTest {
Long id = rnd.nextLong();
map.put("id", id);
map.put("age", 5);
InsertOperation insert = session.insert(Helenus.map(User.class, map));
InsertOperation<User> insert = session.<User>insert(Helenus.map(User.class, map));
String cql = "INSERT INTO simple_users (id,age) VALUES (" + id.toString() + ",5) IF NOT EXISTS;";
Assert.assertEquals(cql, insert.cql());
insert.sync();

View file

@ -28,6 +28,8 @@ import net.helenus.core.Operator;
import net.helenus.support.Fun;
import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest;
import java.util.Optional;
public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
static User user;
@ -164,6 +166,12 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
Assert.assertEquals(Integer.valueOf(35), u.age());
// INSERT
User greg = session.<User>insert()
.value(user::name, "greg")
.value(user::age, 44)
.value(user::type, UserType.USER)
.value(user::id, 1234L)
.sync();
session.update()
.set(user::name, null)

View file

@ -143,8 +143,7 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
Assert.assertNull(actual.country());
Assert.assertEquals(0, actual.zip());
// INSERT
// INSERT using UPDATE
session.update().set(account::address, null).where(account::id, Query.eq(123L)).sync();
Address adrNull = session.select(account::address).where(account::id, Query.eq(123L)).sync().findFirst().get()._1;
@ -200,8 +199,7 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest {
Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city"));
// INSERT
// INSERT using UPDATE
session.update().set(account::addressNoMapping, null).where(account::id, Query.eq(777L)).sync();
found = session.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst().get()._1;