diff --git a/README.md b/README.md index 6f95077..7e956f9 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,8 @@ CasserSession session = Casser.init(getSession()).showCql().add(Timeline.class). Select information: ``` session.select(timeline::userId, timeline::timestamp, timeline::text) - .where(timeline::userId, eq(userId)) - .orderBy(desc(timeline::timestamp)).limit(5).sync() + .where(timeline::userId, Query.eq(userId)) + .orderBy(Query.desc(timeline::timestamp)).limit(5).sync() .forEach(System.out::println); ``` diff --git a/src/main/java/com/noorq/casser/Example.java b/src/main/java/com/noorq/casser/Example.java index 8f0b95b..81749d5 100644 --- a/src/main/java/com/noorq/casser/Example.java +++ b/src/main/java/com/noorq/casser/Example.java @@ -20,6 +20,7 @@ import static com.noorq.casser.core.Query.eq; import com.datastax.driver.core.Cluster; import com.noorq.casser.core.Casser; import com.noorq.casser.core.CasserSession; +import com.noorq.casser.core.Operator; import com.noorq.casser.core.operation.PreparedStreamOperation; import com.noorq.casser.core.tuple.Tuple1; import com.noorq.casser.core.tuple.Tuple2; @@ -96,7 +97,7 @@ public class Example { session.delete(User.class).where(user::id, eq(100L)).async(); - PreparedStreamOperation> ps = session.select(user::name).where(user::id, "==", null).prepare(); + PreparedStreamOperation> ps = session.select(user::name).where(user::id, Operator.EQ, null).prepare(); long cnt = ps.bind(100L).sync().count(); diff --git a/src/main/java/com/noorq/casser/core/Filter.java b/src/main/java/com/noorq/casser/core/Filter.java index 488cdf2..b8414d0 100644 --- a/src/main/java/com/noorq/casser/core/Filter.java +++ b/src/main/java/com/noorq/casser/core/Filter.java @@ -21,23 +21,12 @@ import com.datastax.driver.core.querybuilder.Clause; import com.noorq.casser.core.reflect.CasserPropertyNode; import com.noorq.casser.mapping.MappingUtil; import com.noorq.casser.mapping.value.ColumnValuePreparer; -import com.noorq.casser.support.CasserMappingException; public final class Filter { private final CasserPropertyNode node; private final Postulate postulate; - private Filter(CasserPropertyNode node, Operator op, V value) { - this.node = node; - this.postulate = new Postulate(op, value); - } - - private Filter(CasserPropertyNode node, Operator op, V[] values) { - this.node = node; - this.postulate = new Postulate(op, values); - } - private Filter(CasserPropertyNode node, Postulate postulate) { this.node = node; this.postulate = postulate; @@ -55,7 +44,7 @@ public final class Filter { return create(getter, Operator.EQ, val); } - public static Filter in(Getter getter, V[] vals) { + public static Filter in(Getter getter, V... vals) { Objects.requireNonNull(getter, "empty getter"); Objects.requireNonNull(vals, "empty values"); @@ -69,7 +58,9 @@ public final class Filter { CasserPropertyNode node = MappingUtil.resolveMappingProperty(getter); - return new Filter(node, Operator.IN, vals); + Postulate postulate = Postulate.of(Operator.IN, vals); + + return new Filter(node, postulate); } public static Filter greaterThan(Getter getter, V val) { @@ -97,18 +88,6 @@ public final class Filter { return new Filter(node, postulate); } - public static Filter create(Getter getter, String operator, V val) { - Objects.requireNonNull(operator, "empty operator"); - - Operator fo = Operator.findByOperator(operator); - - if (fo == null) { - throw new CasserMappingException("invalid operator " + operator); - } - - return create(getter, fo, val); - } - public static Filter create(Getter getter, Operator op, V val) { Objects.requireNonNull(getter, "empty getter"); Objects.requireNonNull(op, "empty op"); @@ -120,7 +99,9 @@ public final class Filter { CasserPropertyNode node = MappingUtil.resolveMappingProperty(getter); - return new Filter(node, op, val); + Postulate postulate = Postulate.of(op, val); + + return new Filter(node, postulate); } @Override diff --git a/src/main/java/com/noorq/casser/core/Postulate.java b/src/main/java/com/noorq/casser/core/Postulate.java index d66d754..bb782de 100644 --- a/src/main/java/com/noorq/casser/core/Postulate.java +++ b/src/main/java/com/noorq/casser/core/Postulate.java @@ -24,36 +24,24 @@ import com.noorq.casser.support.CasserMappingException; public final class Postulate { private final Operator operator; - private final V value; private final V[] values; - protected Postulate(Operator op, V value) { - this.operator = op; - this.value = value; - this.values = null; - - if (op == Operator.IN) { - throw new IllegalArgumentException("invalid usage of the 'in' operator"); - } - } - protected Postulate(Operator op, V[] values) { this.operator = op; - this.value = null; this.values = values; - - if (op != Operator.IN) { - throw new IllegalArgumentException("invalid usage of the non 'in' operator"); - } } + public static Postulate of(Operator op, V... values) { + return new Postulate(op, values); + } + public Clause getClause(CasserPropertyNode node, ColumnValuePreparer valuePreparer) { switch(operator) { case EQ: return QueryBuilder.eq(node.getColumnName(), - valuePreparer.prepareColumnValue(value, node.getProperty())); + valuePreparer.prepareColumnValue(values[0], node.getProperty())); case IN: Object[] preparedValues = new Object[values.length]; @@ -64,19 +52,19 @@ public final class Postulate { case LT: return QueryBuilder.lt(node.getColumnName(), - valuePreparer.prepareColumnValue(value, node.getProperty())); + valuePreparer.prepareColumnValue(values[0], node.getProperty())); case LTE: return QueryBuilder.lte(node.getColumnName(), - valuePreparer.prepareColumnValue(value, node.getProperty())); + valuePreparer.prepareColumnValue(values[0], node.getProperty())); case GT: return QueryBuilder.gt(node.getColumnName(), - valuePreparer.prepareColumnValue(value, node.getProperty())); + valuePreparer.prepareColumnValue(values[0], node.getProperty())); case GTE: return QueryBuilder.gte(node.getColumnName(), - valuePreparer.prepareColumnValue(value, node.getProperty())); + valuePreparer.prepareColumnValue(values[0], node.getProperty())); default: throw new CasserMappingException("unknown filter operation " + operator); @@ -91,7 +79,7 @@ public final class Postulate { return "in(" + values + ")"; } - return operator.getName() + value; + return operator.getName() + values[0]; } diff --git a/src/main/java/com/noorq/casser/core/Query.java b/src/main/java/com/noorq/casser/core/Query.java index 522e9ee..a4788ab 100644 --- a/src/main/java/com/noorq/casser/core/Query.java +++ b/src/main/java/com/noorq/casser/core/Query.java @@ -49,23 +49,23 @@ public final class Query { } public static Postulate eq(V val) { - return new Postulate(Operator.EQ, val); + return Postulate.of(Operator.EQ, val); } public static Postulate lt(V val) { - return new Postulate(Operator.LT, val); + return Postulate.of(Operator.LT, val); } public static Postulate lte(V val) { - return new Postulate(Operator.LTE, val); + return Postulate.of(Operator.LTE, val); } public static Postulate gt(V val) { - return new Postulate(Operator.GT, val); + return Postulate.of(Operator.GT, val); } public static Postulate gte(V val) { - return new Postulate(Operator.GTE, val); + return Postulate.of(Operator.GTE, val); } public static Postulate in(Getter getter, V[] vals) { diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractFilterOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractFilterOperation.java index 1a3300a..97c8ba6 100644 --- a/src/main/java/com/noorq/casser/core/operation/AbstractFilterOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/AbstractFilterOperation.java @@ -40,14 +40,6 @@ public abstract class AbstractFilterOperation O where(Getter getter, String operator, V val) { - - addFilter(Filter.create(getter, operator, val)); - - return (O) this; - } - public O where(Getter getter, Operator operator, V val) { addFilter(Filter.create(getter, operator, val)); @@ -69,13 +61,6 @@ public abstract class AbstractFilterOperation O and(Getter getter, String operator, V val) { - - addFilter(Filter.create(getter, operator, val)); - - return (O) this; - } - public O and(Getter getter, Operator operator, V val) { addFilter(Filter.create(getter, operator, val)); @@ -97,14 +82,6 @@ public abstract class AbstractFilterOperation O onlyIf(Getter getter, String operator, V val) { - - addIfFilter(Filter.create(getter, operator, val)); - - return (O) this; - } - public O onlyIf(Getter getter, Operator operator, V val) { addIfFilter(Filter.create(getter, operator, val)); diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractFilterStreamOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractFilterStreamOperation.java index 42aefbd..e6bb380 100644 --- a/src/main/java/com/noorq/casser/core/operation/AbstractFilterStreamOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/AbstractFilterStreamOperation.java @@ -39,13 +39,6 @@ public abstract class AbstractFilterStreamOperation O where(Getter getter, String operator, V val) { - - addFilter(Filter.create(getter, operator, val)); - - return (O) this; - } public O where(Getter getter, Operator operator, V val) { @@ -68,13 +61,6 @@ public abstract class AbstractFilterStreamOperation O and(Getter getter, String operator, V val) { - - addFilter(Filter.create(getter, operator, val)); - - return (O) this; - } - public O and(Getter getter, Operator operator, V val) { addFilter(Filter.create(getter, operator, val)); @@ -96,14 +82,6 @@ public abstract class AbstractFilterStreamOperation O onlyIf(Getter getter, String operator, V val) { - - addIfFilter(Filter.create(getter, operator, val)); - - return (O) this; - } - public O onlyIf(Getter getter, Operator operator, V val) { addIfFilter(Filter.create(getter, operator, val)); diff --git a/src/main/java/com/noorq/casser/core/operation/SelectOperation.java b/src/main/java/com/noorq/casser/core/operation/SelectOperation.java index 7a1e8ed..2ab601f 100644 --- a/src/main/java/com/noorq/casser/core/operation/SelectOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/SelectOperation.java @@ -17,7 +17,6 @@ package com.noorq.casser.core.operation; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.Spliterator; import java.util.Spliterators; @@ -93,11 +92,6 @@ public final class SelectOperation extends AbstractFilterStreamOperation(this, fn); } - public SelectOperation orderBy(Getter getter, String direction) { - Objects.requireNonNull(direction, "direction is null"); - return orderBy(getter, OrderingDirection.parseString(direction)); - } - public SelectOperation orderBy(Getter getter, OrderingDirection direction) { getOrCreateOrdering().add(new Ordered(getter, direction).getOrdering()); return this; diff --git a/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java b/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java index d5462d9..9798da9 100644 --- a/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java +++ b/src/main/java/com/noorq/casser/mapping/CasserMappingProperty.java @@ -150,6 +150,10 @@ public final class CasserMappingProperty implements CasserProperty { Class javaType = (Class) getJavaType(); + if (UDTValue.class.isAssignableFrom(javaType)) { + return null; + } + return TypedConverter.create( UDTValue.class, javaType, @@ -196,6 +200,10 @@ public final class CasserMappingProperty implements CasserProperty { if (columnType.isRight()) { + if (UDTValue.class.isAssignableFrom(javaType)) { + return null; + } + Class javaType = (Class) getJavaType(); UserType userType = repository.findUserType(columnType.getRight().getName()); diff --git a/src/test/java/com/noorq/casser/test/integration/core/compound/CompondKeyTest.java b/src/test/java/com/noorq/casser/test/integration/core/compound/CompondKeyTest.java index f982a27..015934b 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/compound/CompondKeyTest.java +++ b/src/test/java/com/noorq/casser/test/integration/core/compound/CompondKeyTest.java @@ -24,7 +24,10 @@ import org.junit.Test; import com.noorq.casser.core.Casser; import com.noorq.casser.core.CasserSession; +import com.noorq.casser.core.Operator; + import static com.noorq.casser.core.Query.*; + import com.noorq.casser.support.Mutable; import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest; @@ -89,7 +92,7 @@ public class CompondKeyTest extends AbstractEmbeddedCassandraTest { final Mutable c = new Mutable(0); session.select(timeline::userId, timeline::timestamp, timeline::text) - .where(timeline::userId, "==", userId) + .where(timeline::userId, Operator.EQ, userId) .orderBy(desc(timeline::timestamp)).limit(5).sync() .forEach(t -> { diff --git a/src/test/java/com/noorq/casser/test/integration/core/simple/SimpleUserTest.java b/src/test/java/com/noorq/casser/test/integration/core/simple/SimpleUserTest.java index 37a4e59..361e2ea 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/simple/SimpleUserTest.java +++ b/src/test/java/com/noorq/casser/test/integration/core/simple/SimpleUserTest.java @@ -80,13 +80,13 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest { session.update(user::name, "albert") .set(user::age, 35) - .where(user::id, "==", 123L).sync(); + .where(user::id, Operator.EQ, 123L).sync(); long cnt = session.count(user).where(user::id, Operator.EQ, 123L).sync(); Assert.assertEquals(1L, cnt); String name = session.select(user::name) - .where(user::id, "==", 123L) + .where(user::id, Operator.EQ, 123L) .map(t -> "_" + t._1) .sync() .findFirst() @@ -100,7 +100,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest { Assert.assertEquals("albert", u.name()); Assert.assertEquals(Integer.valueOf(35), u.age()); - session.delete(user).where(user::id, "==", 123L).sync(); + session.delete(user).where(user::id, Operator.EQ, 123L).sync(); } diff --git a/src/test/java/com/noorq/casser/test/integration/core/usertype/Account.java b/src/test/java/com/noorq/casser/test/integration/core/usertype/Account.java index 56d37f1..242a58d 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/usertype/Account.java +++ b/src/test/java/com/noorq/casser/test/integration/core/usertype/Account.java @@ -29,6 +29,6 @@ public interface Account { Address address(); @UserTypeName("address0") - UDTValue address2(); + UDTValue addressNoMapping(); } diff --git a/src/test/java/com/noorq/casser/test/integration/core/usertype/UserDefinedTypeTest.java b/src/test/java/com/noorq/casser/test/integration/core/usertype/UserDefinedTypeTest.java index 3ec5714..4a5e439 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/usertype/UserDefinedTypeTest.java +++ b/src/test/java/com/noorq/casser/test/integration/core/usertype/UserDefinedTypeTest.java @@ -17,6 +17,7 @@ package com.noorq.casser.test.integration.core.usertype; import java.util.UUID; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,6 +36,7 @@ import com.datastax.driver.core.schemabuilder.CreateType; import com.datastax.driver.core.schemabuilder.SchemaBuilder; import com.noorq.casser.core.Casser; import com.noorq.casser.core.CasserSession; +import com.noorq.casser.core.Query; import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest; public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest { @@ -43,13 +45,13 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest { CasserSession csession; - UserType fullname; public static class AccountImpl implements Account { long id; Address address; + UDTValue addressNoMapping; @Override public long id() { @@ -62,8 +64,8 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest { } @Override - public UDTValue address2() { - return null; + public UDTValue addressNoMapping() { + return addressNoMapping; } } @@ -165,6 +167,30 @@ public class UserDefinedTypeTest extends AbstractEmbeddedCassandraTest { } @Test + public void testNoMapping() { + + String ks = getSession().getLoggedKeyspace(); + UserType addressType = getSession().getCluster().getMetadata().getKeyspace(ks).getUserType("address0"); + + UDTValue addressNoMapping = addressType.newValue(); + addressNoMapping.setString("line_1", "Lundy Ave"); + addressNoMapping.setString("city", "San Jose"); + + AccountImpl acc = new AccountImpl(); + acc.id = 777L; + acc.addressNoMapping = addressNoMapping; + + csession.upsert(acc).sync(); + + UDTValue found = csession.select(account::addressNoMapping).where(account::id, Query.eq(777L)).sync().findFirst().get()._1; + + Assert.assertEquals(addressNoMapping.getType(), found.getType()); + Assert.assertEquals(addressNoMapping.getString("line_1"), found.getString("line_1")); + Assert.assertEquals(addressNoMapping.getString("city"), found.getString("city")); + + } + + //@Test public void testUDT() {