diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index dda58bd..9216a6d 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -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 InsertOperation insert() { return new InsertOperation(this, true); } - public InsertOperation insert(Object pojo) { - return insert(pojo, null); - } + public InsertOperation insert(Object pojo) { return this.insert(pojo, null); } - public InsertOperation insert(Drafted draft) { - return insert(draft.build(), draft.mutated()); - } + public InsertOperation insert(Drafted draft) { return this.insert(draft.build(), draft.mutated()); } - public InsertOperation insert(Object pojo, Set mutations) { + public InsertOperation insert(Object pojo, Set 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(this, entity, pojo, mutations, true); } - public InsertOperation upsert() { - return new InsertOperation(this, false); + public InsertOperation upsert() { + return new InsertOperation(this, false); } - public InsertOperation upsert(Object pojo) { + public InsertOperation upsert(Drafted draft) { return this.upsert(draft.build(), draft.mutated()); } + + public InsertOperation upsert(Object pojo) { return this.upsert(pojo, null); } + + public InsertOperation upsert(Object pojo, Set 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(this, entity, pojo, mutations, false); } public DeleteOperation delete() { diff --git a/src/main/java/net/helenus/core/operation/InsertOperation.java b/src/main/java/net/helenus/core/operation/InsertOperation.java index ddbcb57..a9b4528 100644 --- a/src/main/java/net/helenus/core/operation/InsertOperation.java +++ b/src/main/java/net/helenus/core/operation/InsertOperation.java @@ -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 { +public final class InsertOperation extends AbstractOperation> { private HelenusEntity entity; private final List> values = new ArrayList>(); private boolean ifNotExists; + private Object pojo; private int[] ttl; private long[] timestamp; @@ -53,9 +55,10 @@ public final class InsertOperation extends AbstractOperation properties = entity.getOrderedProperties(); - Set keys = (mutations == null) ? ((MapExportable) pojo).toMap().keySet() : mutations; + Set keys = (mutations == null) ? null : mutations; for (HelenusProperty prop : properties) { @@ -75,17 +78,17 @@ public final class InsertOperation extends AbstractOperation ifNotExists() { this.ifNotExists = true; return this; } - public InsertOperation ifNotExists(boolean enable) { + public InsertOperation ifNotExists(boolean enable) { this.ifNotExists = enable; return this; } - public InsertOperation value(Getter getter, V val) { + public InsertOperation value(Getter getter, V val) { Objects.requireNonNull(getter, "getter is empty"); @@ -134,17 +137,26 @@ public final class InsertOperation extends AbstractOperation 0) { + Map backingMap = new HashMap(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 usingTtl(int ttl) { this.ttl = new int[1]; this.ttl[0] = ttl; return this; } - public InsertOperation usingTimestamp(long timestamp) { + public InsertOperation usingTimestamp(long timestamp) { this.timestamp = new long[1]; this.timestamp[0] = timestamp; return this; diff --git a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java index 467c857..1f2182b 100644 --- a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java +++ b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java @@ -101,8 +101,6 @@ public class MapperInvocationHandler implements InvocationHandler { return type.getDefaultValue(); - } else if (returnType.isEnum()) { - throw new HelenusException("missing default type for enum user type " + returnType); } } diff --git a/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java b/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java index 8ba5484..d1bb7a2 100644 --- a/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java +++ b/src/test/java/net/helenus/test/integration/core/prepared/PreparedStatementTest.java @@ -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.insert() .value(car::make, Query.marker()) .value(car::model, Query.marker()) .value(car::year, 2004) diff --git a/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java b/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java index a0ecbba..143fc18 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/InsertPartialTest.java @@ -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 insert = session.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(); diff --git a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java index f34a3fb..9484656 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java @@ -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.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) diff --git a/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java b/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java index bf1af4c..b77d9b8 100644 --- a/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java +++ b/src/test/java/net/helenus/test/integration/core/usertype/UserDefinedTypeTest.java @@ -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;