add single() method in SelectOperation

This commit is contained in:
Albert Shift 2015-06-03 13:29:33 -07:00
parent a4260436e8
commit 7f68e7a027
11 changed files with 437 additions and 53 deletions

View file

@ -0,0 +1,113 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.LinkedList;
import java.util.List;
import com.noorq.casser.core.AbstractSessionOperations;
import com.noorq.casser.core.Filter;
import com.noorq.casser.core.Getter;
import com.noorq.casser.core.Operator;
import com.noorq.casser.core.Postulate;
public abstract class AbstractFilterOptionalOperation<E, O extends AbstractFilterOptionalOperation<E, O>> extends AbstractOptionalOperation<E, O> {
protected List<Filter<?>> filters = null;
protected List<Filter<?>> ifFilters = null;
public AbstractFilterOptionalOperation(AbstractSessionOperations sessionOperations) {
super(sessionOperations);
}
public <V> O where(Getter<V> getter, Postulate<V> postulate) {
addFilter(Filter.create(getter, postulate));
return (O) this;
}
public <V> O where(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O where(Filter<V> filter) {
addFilter(filter);
return (O) this;
}
public <V> O and(Getter<V> getter, Postulate<V> postulate) {
addFilter(Filter.create(getter, postulate));
return (O) this;
}
public <V> O and(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O and(Filter<V> filter) {
addFilter(filter);
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, Postulate<V> postulate) {
addIfFilter(Filter.create(getter, postulate));
return (O) this;
}
public <V> O onlyIf(Getter<V> getter, Operator operator, V val) {
addIfFilter(Filter.create(getter, operator, val));
return (O) this;
}
public <V> O onlyIf(Filter<V> filter) {
addIfFilter(filter);
return (O) this;
}
private void addFilter(Filter<?> filter) {
if (filters == null) {
filters = new LinkedList<Filter<?>>();
}
filters.add(filter);
}
private void addIfFilter(Filter<?> filter) {
if (ifFilters == null) {
ifFilters = new LinkedList<Filter<?>>();
}
ifFilters.add(filter);
}
}

View file

@ -58,22 +58,6 @@ public abstract class AbstractOperation<E, O extends AbstractOperation<E, O>> ex
return Scala.asFuture(prepareAsync());
}
public <A> Future<Fun.Tuple2<PreparedOperation<E>, A>> prepareFuture(A a) {
return Scala.asFuture(prepareAsync(), a);
}
public <A, B> Future<Fun.Tuple3<PreparedOperation<E>, A, B>> prepareFuture(A a, B b) {
return Scala.asFuture(prepareAsync(), a, b);
}
public <A, B, C> Future<Fun.Tuple4<PreparedOperation<E>, A, B, C>> prepareFuture(A a, B b, C c) {
return Scala.asFuture(prepareAsync(), a, b, c);
}
public <A, B, C, D> Future<Fun.Tuple5<PreparedOperation<E>, A, B, C, D>> prepareFuture(A a, B b, C c, D d) {
return Scala.asFuture(prepareAsync(), a, b, c, d);
}
public E sync() {
ResultSet resultSet = sessionOps.executeAsync(options(buildStatement()), showValues).getUninterruptibly();

View file

@ -0,0 +1,106 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.Optional;
import scala.concurrent.Future;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.noorq.casser.core.AbstractSessionOperations;
import com.noorq.casser.support.Fun;
import com.noorq.casser.support.Scala;
public abstract class AbstractOptionalOperation<E, O extends AbstractOptionalOperation<E, O>> extends AbstractStatementOperation<E, O> {
public AbstractOptionalOperation(AbstractSessionOperations sessionOperations) {
super(sessionOperations);
}
public abstract Optional<E> transform(ResultSet resultSet);
public PreparedOptionalOperation<E> prepare() {
return new PreparedOptionalOperation<E>(prepareStatement(), this);
}
public ListenableFuture<PreparedOptionalOperation<E>> prepareAsync() {
final O _this = (O) this;
return Futures.transform(prepareStatementAsync(), new Function<PreparedStatement, PreparedOptionalOperation<E>>() {
@Override
public PreparedOptionalOperation<E> apply(PreparedStatement preparedStatement) {
return new PreparedOptionalOperation<E>(preparedStatement, _this);
}
});
}
public Future<PreparedOptionalOperation<E>> prepareFuture() {
return Scala.asFuture(prepareAsync());
}
public Optional<E> sync() {
ResultSet resultSet = sessionOps.executeAsync(options(buildStatement()), showValues).getUninterruptibly();
return transform(resultSet);
}
public ListenableFuture<Optional<E>> async() {
ResultSetFuture resultSetFuture = sessionOps.executeAsync(options(buildStatement()), showValues);
ListenableFuture<Optional<E>> future = Futures.transform(resultSetFuture, new Function<ResultSet, Optional<E>>() {
@Override
public Optional<E> apply(ResultSet resultSet) {
return transform(resultSet);
}
}, sessionOps.getExecutor());
return future;
}
public Future<Optional<E>> future() {
return Scala.asFuture(async());
}
public <A> Future<Fun.Tuple2<Optional<E>, A>> future(A a) {
return Scala.asFuture(async(), a);
}
public <A, B> Future<Fun.Tuple3<Optional<E>, A, B>> future(A a, B b) {
return Scala.asFuture(async(), a, b);
}
public <A, B, C> Future<Fun.Tuple4<Optional<E>, A, B, C>> future(A a, B b, C c) {
return Scala.asFuture(async(), a, b, c);
}
public <A, B, C, D> Future<Fun.Tuple5<Optional<E>, A, B, C, D>> future(A a, B b, C c, D d) {
return Scala.asFuture(async(), a, b, c, d);
}
}

View file

@ -32,7 +32,6 @@ import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.google.common.util.concurrent.ListenableFuture;
import com.noorq.casser.core.AbstractSessionOperations;
import com.noorq.casser.support.CasserException;
import com.noorq.casser.support.Fun;
import com.noorq.casser.support.Scala;
public abstract class AbstractStatementOperation<E, O extends AbstractStatementOperation<E, O>> {
@ -239,20 +238,4 @@ public abstract class AbstractStatementOperation<E, O extends AbstractStatementO
return Scala.asFuture(prepareStatementAsync());
}
public <A> Future<Fun.Tuple2<PreparedStatement, A>> prepareStatementFuture(A a) {
return Scala.asFuture(prepareStatementAsync(), a);
}
public <A, B> Future<Fun.Tuple3<PreparedStatement, A, B>> prepareStatementFuture(A a, B b) {
return Scala.asFuture(prepareStatementAsync(), a, b);
}
public <A, B, C> Future<Fun.Tuple4<PreparedStatement, A, B, C>> prepareStatementFuture(A a, B b, C c) {
return Scala.asFuture(prepareStatementAsync(), a, b, c);
}
public <A, B, C, D> Future<Fun.Tuple5<PreparedStatement, A, B, C, D>> prepareStatementFuture(A a, B b, C c, D d) {
return Scala.asFuture(prepareStatementAsync(), a, b, c, d);
}
}

View file

@ -59,22 +59,6 @@ public abstract class AbstractStreamOperation<E, O extends AbstractStreamOperati
public Future<PreparedStreamOperation<E>> prepareFuture() {
return Scala.asFuture(prepareAsync());
}
public <A> Future<Fun.Tuple2<PreparedStreamOperation<E>, A>> prepareFuture(A a) {
return Scala.asFuture(prepareAsync(), a);
}
public <A, B> Future<Fun.Tuple3<PreparedStreamOperation<E>, A, B>> prepareFuture(A a, B b) {
return Scala.asFuture(prepareAsync(), a, b);
}
public <A, B, C> Future<Fun.Tuple4<PreparedStreamOperation<E>, A, B, C>> prepareFuture(A a, B b, C c) {
return Scala.asFuture(prepareAsync(), a, b, c);
}
public <A, B, C, D> Future<Fun.Tuple5<PreparedStreamOperation<E>, A, B, C, D>> prepareFuture(A a, B b, C c, D d) {
return Scala.asFuture(prepareAsync(), a, b, c, d);
}
public Stream<E> sync() {

View file

@ -0,0 +1,45 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.Optional;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Statement;
public final class BoundOptionalOperation<E> extends AbstractOptionalOperation<E, BoundOptionalOperation<E>> {
private final BoundStatement boundStatement;
private final AbstractOptionalOperation<E, ?> delegate;
public BoundOptionalOperation(BoundStatement boundStatement, AbstractOptionalOperation<E, ?> operation) {
super(operation.sessionOps);
this.boundStatement = boundStatement;
this.delegate = operation;
}
@Override
public Optional<E> transform(ResultSet resultSet) {
return delegate.transform(resultSet);
}
@Override
public Statement buildStatement() {
return boundStatement;
}
}

View file

@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
public final class PreparedOptionalOperation<E> {
private final PreparedStatement preparedStatement;
private final AbstractOptionalOperation<E, ?> operation;
public PreparedOptionalOperation(PreparedStatement statement, AbstractOptionalOperation<E, ?> operation) {
this.preparedStatement = statement;
this.operation = operation;
}
public PreparedStatement getPreparedStatement() {
return preparedStatement;
}
public BoundOptionalOperation<E> bind(Object... params) {
BoundStatement boundStatement = preparedStatement.bind(params);
return new BoundOptionalOperation<E>(boundStatement, operation);
}
@Override
public String toString() {
return preparedStatement.getQueryString();
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.Optional;
import java.util.function.Function;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.BuiltStatement;
public final class SelectFirstOperation<E> extends AbstractFilterOptionalOperation<E, SelectFirstOperation<E>> {
private final SelectOperation<E> src;
public SelectFirstOperation(SelectOperation<E> src) {
super(src.sessionOps);
this.src = src;
this.filters = src.filters;
this.ifFilters = src.ifFilters;
}
public <R> SelectFirstTransformingOperation<R, E> map(Function<E, R> fn) {
return new SelectFirstTransformingOperation<R, E>(src, fn);
}
@Override
public BuiltStatement buildStatement() {
return src.buildStatement();
}
@Override
public Optional<E> transform(ResultSet resultSet) {
return src.transform(resultSet).findFirst();
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.Optional;
import java.util.function.Function;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.BuiltStatement;
public final class SelectFirstTransformingOperation<R, E> extends AbstractFilterOptionalOperation<R, SelectFirstTransformingOperation<R, E>> {
private final SelectOperation<E> src;
private final Function<E, R> fn;
public SelectFirstTransformingOperation(SelectOperation<E> src, Function<E, R> fn) {
super(src.sessionOps);
this.src = src;
this.fn = fn;
this.filters = src.filters;
this.ifFilters = src.ifFilters;
}
@Override
public BuiltStatement buildStatement() {
return src.buildStatement();
}
@Override
public Optional<R> transform(ResultSet resultSet) {
return src.transform(resultSet).findFirst().map(fn);
}
}

View file

@ -133,6 +133,11 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
return new CountOperation(sessionOps, entity);
}
public SelectFirstOperation<E> single() {
limit(1);
return new SelectFirstOperation<E>(this);
}
public <R> SelectTransformingOperation<R, E> mapTo(Class<R> entityClass) {
Objects.requireNonNull(entityClass, "entityClass is null");
@ -149,16 +154,16 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
});
}
public <R> SelectTransformingOperation<R, E> map(Function<E, R> fn) {
return new SelectTransformingOperation<R, E>(this, fn);
}
public SelectOperation<E> column(Getter<?> getter) {
CasserPropertyNode p = MappingUtil.resolveMappingProperty(getter);
this.props.add(p);
return this;
}
public <R> SelectTransformingOperation<R, E> map(Function<E, R> fn) {
return new SelectTransformingOperation<R, E>(this, fn);
}
public SelectOperation<E> orderBy(Getter<?> getter, OrderingDirection direction) {
getOrCreateOrdering().add(new Ordered(getter, direction).getOrdering());
return this;

View file

@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 Noorq, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.noorq.casser.core.operation;
import java.util.function.Function;