diff --git a/src/main/java/com/noorq/casser/core/Casser.java b/src/main/java/com/noorq/casser/core/Casser.java index 998945c..ab87065 100644 --- a/src/main/java/com/noorq/casser/core/Casser.java +++ b/src/main/java/com/noorq/casser/core/Casser.java @@ -47,8 +47,10 @@ public final class Casser { return Objects.requireNonNull(session, "session is not initialized"); } - protected static void register(CasserSession newSession) { - session = newSession; + protected static synchronized void register(CasserSession newSession) { + if (session == null) { + session = newSession; + } } public static CasserSettings settings() { diff --git a/src/main/java/com/noorq/casser/core/Postulate.java b/src/main/java/com/noorq/casser/core/Postulate.java index 5698350..3b97dbf 100644 --- a/src/main/java/com/noorq/casser/core/Postulate.java +++ b/src/main/java/com/noorq/casser/core/Postulate.java @@ -15,12 +15,13 @@ */ package com.noorq.casser.core; +import java.util.Arrays; + import com.datastax.driver.core.querybuilder.Clause; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.noorq.casser.core.reflect.CasserPropertyNode; import com.noorq.casser.mapping.value.ColumnValuePreparer; import com.noorq.casser.support.CasserMappingException; -import com.noorq.casser.support.Requires; public final class Postulate { @@ -28,7 +29,6 @@ public final class Postulate { private final V[] values; protected Postulate(Operator op, V[] values) { - Requires.nonNullArray(values); this.operator = op; this.values = values; } @@ -78,7 +78,22 @@ public final class Postulate { public String toString() { if (operator == Operator.IN) { - return "in(" + values + ")"; + + if (values == null) { + return "in()"; + } + + int len = values.length; + StringBuilder b = new StringBuilder(); + b.append("in("); + for (int i = 0; i != len; i++) { + if (b.length() > 3) { + b.append(", "); + } + b.append(String.valueOf(values[i])); + } + return b.append(')').toString(); + } return operator.getName() + values[0]; diff --git a/src/main/java/com/noorq/casser/core/SessionInitializer.java b/src/main/java/com/noorq/casser/core/SessionInitializer.java index e4c874e..8fa496d 100644 --- a/src/main/java/com/noorq/casser/core/SessionInitializer.java +++ b/src/main/java/com/noorq/casser/core/SessionInitializer.java @@ -166,10 +166,7 @@ public final class SessionInitializer extends AbstractSessionOperations { return this; } - public synchronized void register() { - if (Casser.session() != null) { - return; - } + public void register() { Casser.register(get()); } diff --git a/src/main/java/com/noorq/casser/core/operation/UpdateOperation.java b/src/main/java/com/noorq/casser/core/operation/UpdateOperation.java index 25ef568..2073dae 100644 --- a/src/main/java/com/noorq/casser/core/operation/UpdateOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/UpdateOperation.java @@ -339,7 +339,7 @@ public final class UpdateOperation extends AbstractFilterOperation actual = Casser.session().select(Message.class) + .where(message::id, eq(123)).sync() + .collect(Collectors.toList()); + + Assert.assertEquals(2, actual.size()); + + Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get(); + assertMessages(msg, toCraig); + + // UPDATE + + Casser.session().update().set(message::from, "Albert") + .where(message::id, eq(123)) + .onlyIf(message::from, eq("Alex")) + .sync(); + + long cnt = Casser.session().select(message::from) + .where(message::id, eq(123)).sync() + .filter(t -> t._1.equals("Albert")) + .count(); + + Assert.assertEquals(2, cnt); + + // INSERT + + Casser.session().update().set(message::from, null) + .where(message::id, eq(123)) + .sync(); + + Casser.session().select(message::from) + .where(message::id, eq(123)) + .sync() + .map(t -> t._1) + .forEach(Assert::assertNull); + + Casser.session().update().set(message::from, "Alex") + .where(message::id, eq(123)) + .onlyIf(message::from, eq(null)).sync(); + + // DELETE + + Casser.session().delete().where(message::id, eq(123)).sync(); + + cnt = Casser.session().count().where(message::id, eq(123)).sync(); + Assert.assertEquals(0, cnt); + } + + private void assertMessages(Message expected, Message actual) { + Assert.assertEquals(expected.id(), actual.id()); + Assert.assertEquals(expected.from(), actual.from()); + Assert.assertEquals(expected.timestamp(), actual.timestamp()); + Assert.assertEquals(expected.to(), actual.to()); + Assert.assertEquals(expected.message(), actual.message()); + } + +}