added more options to staements

This commit is contained in:
Albert Shift 2015-04-04 19:49:55 -07:00
parent a32d5609ba
commit b70e8a348e
5 changed files with 131 additions and 3 deletions

View file

@ -35,13 +35,22 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
public abstract Statement buildStatement();
private ConsistencyLevel consistencyLevel;
private ConsistencyLevel serialConsistencyLevel;
private RetryPolicy retryPolicy;
private boolean enableTracing = false;
private long[] defaultTimestamp = null;
private int[] fetchSize = null;
public AbstractStatementOperation(AbstractSessionOperations sessionOperations) {
this.sessionOps = sessionOperations;
}
public O defaultTimestamp(long timestamp) {
this.defaultTimestamp = new long[1];
this.defaultTimestamp[0] = timestamp;
return (O) this;
}
public O retryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
return (O) this;
@ -87,6 +96,31 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
return (O) this;
}
public O serialConsistency(ConsistencyLevel level) {
this.serialConsistencyLevel = level;
return (O) this;
}
public O serialConsistencyAny() {
this.serialConsistencyLevel = ConsistencyLevel.ANY;
return (O) this;
}
public O serialConsistencyOne() {
this.serialConsistencyLevel = ConsistencyLevel.ONE;
return (O) this;
}
public O serialConsistencyQuorum() {
this.serialConsistencyLevel = ConsistencyLevel.QUORUM;
return (O) this;
}
public O serialConsistencyAll() {
this.serialConsistencyLevel = ConsistencyLevel.ALL;
return (O) this;
}
public O disableTracing() {
this.enableTracing = false;
return (O) this;
@ -102,12 +136,26 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
return (O) this;
}
public O fetchSize(int fetchSize) {
this.fetchSize = new int[1];
this.fetchSize[0] = fetchSize;
return (O) this;
}
protected Statement options(Statement statement) {
if (defaultTimestamp != null) {
statement.setDefaultTimestamp(defaultTimestamp[0]);
}
if (consistencyLevel != null) {
statement.setConsistencyLevel(consistencyLevel);
}
if (serialConsistencyLevel != null) {
statement.setSerialConsistencyLevel(serialConsistencyLevel);
}
if (retryPolicy != null) {
statement.setRetryPolicy(retryPolicy);
}
@ -119,6 +167,10 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
statement.disableTracing();
}
if (fetchSize != null) {
statement.setFetchSize(fetchSize[0]);
}
return statement;
}

View file

@ -29,6 +29,11 @@ public final class DeleteOperation extends AbstractFilterOperation<ResultSet, De
private final CasserMappingEntity entity;
private boolean ifExists = false;
private int[] ttl;
private long[] timestamp;
public DeleteOperation(AbstractSessionOperations sessionOperations, CasserMappingEntity entity) {
super(sessionOperations);
@ -42,12 +47,23 @@ public final class DeleteOperation extends AbstractFilterOperation<ResultSet, De
Delete delete = QueryBuilder.delete().from(entity.getName().toCql());
if (this.ifExists) {
delete.ifExists();
}
Where where = delete.where();
for (Filter<?> filter : filters) {
where.and(filter.getClause(sessionOps.getValuePreparer()));
}
if (this.ttl != null) {
delete.using(QueryBuilder.ttl(this.ttl[0]));
}
if (this.timestamp != null) {
delete.using(QueryBuilder.timestamp(this.timestamp[0]));
}
return delete;
}
@ -61,4 +77,20 @@ public final class DeleteOperation extends AbstractFilterOperation<ResultSet, De
return resultSet;
}
public DeleteOperation ifExists() {
this.ifExists = true;
return this;
}
public DeleteOperation usingTtl(int ttl) {
this.ttl = new int[1];
this.ttl[0] = ttl;
return this;
}
public DeleteOperation usingTimestamp(long timestamp) {
this.timestamp = new long[1];
this.timestamp[0] = timestamp;
return this;
}
}

View file

@ -39,6 +39,9 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
private final List<Tuple2<CasserPropertyNode, Object>> values = new ArrayList<Tuple2<CasserPropertyNode, Object>>();
private final boolean ifNotExists;
private int[] ttl;
private long[] timestamp;
public InsertOperation(AbstractSessionOperations sessionOperations, boolean ifNotExists) {
super(sessionOperations);
@ -112,6 +115,13 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
insert.value(t._1.getColumnName(), t._2);
});
if (this.ttl != null) {
insert.using(QueryBuilder.ttl(this.ttl[0]));
}
if (this.timestamp != null) {
insert.using(QueryBuilder.timestamp(this.timestamp[0]));
}
return insert;
}
@ -119,7 +129,17 @@ public final class InsertOperation extends AbstractOperation<ResultSet, InsertOp
public ResultSet transform(ResultSet resultSet) {
return resultSet;
}
public InsertOperation usingTtl(int ttl) {
this.ttl = new int[1];
this.ttl[0] = ttl;
return this;
}
public InsertOperation usingTimestamp(long timestamp) {
this.timestamp = new long[1];
this.timestamp[0] = timestamp;
return this;
}
}

View file

@ -130,7 +130,7 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
if (limit != null) {
select.limit(limit.intValue());
}
if (filters != null && !filters.isEmpty()) {
Where where = select.where();

View file

@ -36,6 +36,9 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
private final CasserPropertyNode[] props;
private final Object[] vals;
private int[] ttl;
private long[] timestamp;
public UpdateOperation(AbstractSessionOperations sessionOperations) {
super(sessionOperations);
@ -93,6 +96,8 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
Update update = QueryBuilder.update(entity.getName().toCql());
for (int i = 0; i != props.length; ++i) {
Object value = sessionOps.getValuePreparer().prepareColumnValue(vals[i], props[i].getProperty());
@ -108,6 +113,13 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
}
}
if (this.ttl != null) {
update.using(QueryBuilder.ttl(this.ttl[0]));
}
if (this.timestamp != null) {
update.using(QueryBuilder.timestamp(this.timestamp[0]));
}
return update;
}
@ -116,5 +128,17 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
return resultSet;
}
public UpdateOperation usingTtl(int ttl) {
this.ttl = new int[1];
this.ttl[0] = ttl;
return this;
}
public UpdateOperation usingTimestamp(long timestamp) {
this.timestamp = new long[1];
this.timestamp[0] = timestamp;
return this;
}
}