support no mapping for UDTValue type

This commit is contained in:
Albert Shift 2015-04-09 14:28:54 -07:00
parent 05dc642c41
commit bde9ffd7a0
13 changed files with 71 additions and 115 deletions

View file

@ -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);
```

View file

@ -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<Tuple1<String>> ps = session.select(user::name).where(user::id, "==", null).prepare();
PreparedStreamOperation<Tuple1<String>> ps = session.select(user::name).where(user::id, Operator.EQ, null).prepare();
long cnt = ps.bind(100L).sync().count();

View file

@ -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<V> {
private final CasserPropertyNode node;
private final Postulate<V> postulate;
private Filter(CasserPropertyNode node, Operator op, V value) {
this.node = node;
this.postulate = new Postulate<V>(op, value);
}
private Filter(CasserPropertyNode node, Operator op, V[] values) {
this.node = node;
this.postulate = new Postulate<V>(op, values);
}
private Filter(CasserPropertyNode node, Postulate<V> postulate) {
this.node = node;
this.postulate = postulate;
@ -55,7 +44,7 @@ public final class Filter<V> {
return create(getter, Operator.EQ, val);
}
public static <V> Filter<V> in(Getter<V> getter, V[] vals) {
public static <V> Filter<V> in(Getter<V> getter, V... vals) {
Objects.requireNonNull(getter, "empty getter");
Objects.requireNonNull(vals, "empty values");
@ -69,7 +58,9 @@ public final class Filter<V> {
CasserPropertyNode node = MappingUtil.resolveMappingProperty(getter);
return new Filter<V>(node, Operator.IN, vals);
Postulate<V> postulate = Postulate.of(Operator.IN, vals);
return new Filter<V>(node, postulate);
}
public static <V> Filter<V> greaterThan(Getter<V> getter, V val) {
@ -97,18 +88,6 @@ public final class Filter<V> {
return new Filter<V>(node, postulate);
}
public static <V> Filter<V> create(Getter<V> 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 <V> Filter<V> create(Getter<V> getter, Operator op, V val) {
Objects.requireNonNull(getter, "empty getter");
Objects.requireNonNull(op, "empty op");
@ -120,7 +99,9 @@ public final class Filter<V> {
CasserPropertyNode node = MappingUtil.resolveMappingProperty(getter);
return new Filter<V>(node, op, val);
Postulate<V> postulate = Postulate.of(op, val);
return new Filter<V>(node, postulate);
}
@Override

View file

@ -24,27 +24,15 @@ import com.noorq.casser.support.CasserMappingException;
public final class Postulate<V> {
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 <V> Postulate<V> of(Operator op, V... values) {
return new Postulate<V>(op, values);
}
public Clause getClause(CasserPropertyNode node, ColumnValuePreparer valuePreparer) {
@ -53,7 +41,7 @@ public final class Postulate<V> {
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<V> {
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<V> {
return "in(" + values + ")";
}
return operator.getName() + value;
return operator.getName() + values[0];
}

View file

@ -49,23 +49,23 @@ public final class Query {
}
public static <V> Postulate<V> eq(V val) {
return new Postulate<V>(Operator.EQ, val);
return Postulate.of(Operator.EQ, val);
}
public static <V> Postulate<V> lt(V val) {
return new Postulate<V>(Operator.LT, val);
return Postulate.of(Operator.LT, val);
}
public static <V> Postulate<V> lte(V val) {
return new Postulate<V>(Operator.LTE, val);
return Postulate.of(Operator.LTE, val);
}
public static <V> Postulate<V> gt(V val) {
return new Postulate<V>(Operator.GT, val);
return Postulate.of(Operator.GT, val);
}
public static <V> Postulate<V> gte(V val) {
return new Postulate<V>(Operator.GTE, val);
return Postulate.of(Operator.GTE, val);
}
public static <V> Postulate<V> in(Getter<V> getter, V[] vals) {

View file

@ -40,14 +40,6 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
return (O) this;
}
public <V> O where(Getter<V> getter, String operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O where(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -69,13 +61,6 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
return (O) this;
}
public <V> O and(Getter<V> getter, String operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O and(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -97,14 +82,6 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, String operator, V val) {
addIfFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, Operator operator, V val) {
addIfFilter(Filter.create(getter, operator, val));

View file

@ -40,13 +40,6 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
return (O) this;
}
public <V> O where(Getter<V> getter, String operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O where(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -68,13 +61,6 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
return (O) this;
}
public <V> O and(Getter<V> getter, String operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O and(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -96,14 +82,6 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, String operator, V val) {
addIfFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, Operator operator, V val) {
addIfFilter(Filter.create(getter, operator, val));

View file

@ -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<E> extends AbstractFilterStreamOperation<E, S
return new SelectTransformingOperation<R, E>(this, fn);
}
public SelectOperation<E> orderBy(Getter<?> getter, String direction) {
Objects.requireNonNull(direction, "direction is null");
return orderBy(getter, OrderingDirection.parseString(direction));
}
public SelectOperation<E> orderBy(Getter<?> getter, OrderingDirection direction) {
getOrCreateOrdering().add(new Ordered(getter, direction).getOrdering());
return this;

View file

@ -150,6 +150,10 @@ public final class CasserMappingProperty implements CasserProperty {
Class<Object> javaType = (Class<Object>) 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<Object> javaType = (Class<Object>) getJavaType();
UserType userType = repository.findUserType(columnType.getRight().getName());

View file

@ -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<Integer> c = new Mutable<Integer>(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 -> {

View file

@ -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();
}

View file

@ -29,6 +29,6 @@ public interface Account {
Address address();
@UserTypeName("address0")
UDTValue address2();
UDTValue addressNoMapping();
}

View file

@ -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() {