rename asc/desc functions to orderBy in SelectOperation

This commit is contained in:
Albert Shift 2015-03-23 00:39:18 -07:00
parent 1924b72031
commit d97e67b420
7 changed files with 51 additions and 39 deletions

View file

@ -23,7 +23,7 @@ import java.util.Set;
import casser.mapping.CasserMappingEntity;
import casser.mapping.CasserMappingProperty;
import casser.mapping.Ordering;
import casser.mapping.OrderingDirection;
import casser.support.CasserMappingException;
import com.datastax.driver.core.ColumnMetadata;
@ -161,11 +161,11 @@ public final class SchemaUtil {
}
private static SchemaBuilder.Direction mapDirection(Ordering o) {
private static SchemaBuilder.Direction mapDirection(OrderingDirection o) {
switch(o) {
case ASCENDING:
case ASC:
return SchemaBuilder.Direction.ASC;
case DESCENDING:
case DESC:
return SchemaBuilder.Direction.DESC;
}
throw new CasserMappingException("unknown ordering " + o);

View file

@ -31,6 +31,7 @@ import casser.mapping.CasserMappingEntity;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import casser.mapping.MappingUtil;
import casser.mapping.OrderingDirection;
import casser.mapping.RowColumnValueProvider;
import casser.support.CasserMappingException;
@ -81,40 +82,33 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
return new SelectTransformingOperation<R, E>(this, fn);
}
public SelectOperation<E> asc(Getter<?> getter) {
Objects.requireNonNull(getter, "property is null");
CasserMappingProperty<?> prop = MappingUtil.resolveMappingProperty(getter);
return asc(prop);
public SelectOperation<E> orderBy(Getter<?> getter, String direction) {
Objects.requireNonNull(direction, "direction is null");
return orderBy(getter, OrderingDirection.parseString(direction));
}
private SelectOperation<E> asc(CasserMappingProperty<?> prop) {
public SelectOperation<E> orderBy(Getter<?> getter, OrderingDirection direction) {
Objects.requireNonNull(getter, "property is null");
Objects.requireNonNull(direction, "direction is null");
CasserMappingProperty<?> prop = MappingUtil.resolveMappingProperty(getter);
if (!prop.isClusteringColumn()) {
throw new CasserMappingException("property must be a clustering column " + prop.getPropertyName());
}
getOrCreateOrdering().add(QueryBuilder.asc(prop.getColumnName()));
return this;
}
public SelectOperation<E> desc(Getter<?> getter) {
Objects.requireNonNull(getter, "property is null");
CasserMappingProperty<?> prop = MappingUtil.resolveMappingProperty(getter);
return desc(prop);
}
private SelectOperation<E> desc(CasserMappingProperty<?> prop) {
if (!prop.isClusteringColumn()) {
throw new CasserMappingException("property must be a clustering column " + prop.getPropertyName());
switch(direction) {
case ASC:
getOrCreateOrdering().add(QueryBuilder.asc(prop.getColumnName()));
return this;
case DESC:
getOrCreateOrdering().add(QueryBuilder.desc(prop.getColumnName()));
return this;
}
getOrCreateOrdering().add(QueryBuilder.desc(prop.getColumnName()));
return this;
throw new CasserMappingException("unknown ordering direction " + direction);
}
public SelectOperation<E> limit(Integer limit) {
this.limit = limit;
return this;

View file

@ -50,7 +50,7 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
private boolean isPartitionKey = false;
private boolean isClusteringColumn = false;
private int ordinal = 0;
private Ordering ordering = Ordering.ASCENDING;
private OrderingDirection ordering = OrderingDirection.ASC;
private Class<?> javaType = null;
@ -140,7 +140,7 @@ public class CasserMappingProperty<E> implements CasserProperty<E> {
}
@Override
public Ordering getOrdering() {
public OrderingDirection getOrdering() {
ensureKeyInfo();
return ordering;
}

View file

@ -36,7 +36,7 @@ public interface CasserProperty<E> {
int getOrdinal();
Ordering getOrdering();
OrderingDirection getOrdering();
boolean isStatic();

View file

@ -28,7 +28,7 @@ public @interface ClusteringColumn {
int ordinal() default 0;
Ordering ordering() default Ordering.ASCENDING;
OrderingDirection ordering() default OrderingDirection.ASC;
boolean forceQuote() default false;

View file

@ -15,15 +15,17 @@
*/
package casser.mapping;
public enum Ordering {
import casser.support.CasserMappingException;
ASCENDING("ASC"),
public enum OrderingDirection {
DESCENDING("DESC");
ASC("ASC"),
DESC("DESC");
private final String cql;
private Ordering(String cql) {
private OrderingDirection(String cql) {
this.cql = cql;
}
@ -31,4 +33,18 @@ public enum Ordering {
return cql;
}
public static OrderingDirection parseString(String name) {
if (ASC.cql.equalsIgnoreCase(name)) {
return ASC;
}
else if (DESC.cql.equalsIgnoreCase(name)) {
return DESC;
}
throw new CasserMappingException("invalid ordering direction name " + name);
}
}

View file

@ -23,6 +23,7 @@ import org.junit.Test;
import casser.core.Casser;
import casser.core.CasserSession;
import casser.mapping.OrderingDirection;
import casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
@ -61,7 +62,8 @@ public class CompondKeyTest extends AbstractEmbeddedCassandraTest {
session.showCql(true);
session.select(timeline::getUserId, timeline::getTimestamp, timeline::getText)
.where(timeline::getUserId, "==", userId).desc(timeline::getTimestamp).limit(5).sync()
.where(timeline::getUserId, "==", userId)
.orderBy(timeline::getTimestamp, "desc").limit(5).sync()
.forEach(t -> System.out.println(t));
}