From 7f68e7a027cd9440de71abe88ab89cbaf08929c2 Mon Sep 17 00:00:00 2001 From: Albert Shift Date: Wed, 3 Jun 2015 13:29:33 -0700 Subject: [PATCH] add single() method in SelectOperation --- .../AbstractFilterOptionalOperation.java | 113 ++++++++++++++++++ .../core/operation/AbstractOperation.java | 16 --- .../operation/AbstractOptionalOperation.java | 106 ++++++++++++++++ .../operation/AbstractStatementOperation.java | 17 --- .../operation/AbstractStreamOperation.java | 16 --- .../operation/BoundOptionalOperation.java | 45 +++++++ .../operation/PreparedOptionalOperation.java | 48 ++++++++ .../core/operation/SelectFirstOperation.java | 52 ++++++++ .../SelectFirstTransformingOperation.java | 49 ++++++++ .../core/operation/SelectOperation.java | 13 +- .../SelectTransformingOperation.java | 15 +++ 11 files changed, 437 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/noorq/casser/core/operation/AbstractFilterOptionalOperation.java create mode 100644 src/main/java/com/noorq/casser/core/operation/AbstractOptionalOperation.java create mode 100644 src/main/java/com/noorq/casser/core/operation/BoundOptionalOperation.java create mode 100644 src/main/java/com/noorq/casser/core/operation/PreparedOptionalOperation.java create mode 100644 src/main/java/com/noorq/casser/core/operation/SelectFirstOperation.java create mode 100644 src/main/java/com/noorq/casser/core/operation/SelectFirstTransformingOperation.java diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractFilterOptionalOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractFilterOptionalOperation.java new file mode 100644 index 0000000..7b52ec2 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/AbstractFilterOptionalOperation.java @@ -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> extends AbstractOptionalOperation { + + protected List> filters = null; + protected List> ifFilters = null; + + public AbstractFilterOptionalOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } + + public O where(Getter getter, Postulate postulate) { + + addFilter(Filter.create(getter, postulate)); + + return (O) this; + } + + public O where(Getter getter, Operator operator, V val) { + + addFilter(Filter.create(getter, operator, val)); + + return (O) this; + } + + public O where(Filter filter) { + + addFilter(filter); + + return (O) this; + } + + public O and(Getter getter, Postulate postulate) { + + addFilter(Filter.create(getter, postulate)); + + return (O) this; + } + + public O and(Getter getter, Operator operator, V val) { + + addFilter(Filter.create(getter, operator, val)); + + return (O) this; + } + + public O and(Filter filter) { + + addFilter(filter); + + return (O) this; + } + + public O onlyIf(Getter getter, Postulate postulate) { + + addIfFilter(Filter.create(getter, postulate)); + + return (O) this; + } + + public O onlyIf(Getter getter, Operator operator, V val) { + + addIfFilter(Filter.create(getter, operator, val)); + + return (O) this; + } + + public O onlyIf(Filter filter) { + + addIfFilter(filter); + + return (O) this; + } + + private void addFilter(Filter filter) { + if (filters == null) { + filters = new LinkedList>(); + } + filters.add(filter); + } + + private void addIfFilter(Filter filter) { + if (ifFilters == null) { + ifFilters = new LinkedList>(); + } + ifFilters.add(filter); + } + +} diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractOperation.java index f832c89..742b810 100644 --- a/src/main/java/com/noorq/casser/core/operation/AbstractOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/AbstractOperation.java @@ -58,22 +58,6 @@ public abstract class AbstractOperation> ex return Scala.asFuture(prepareAsync()); } - public Future, A>> prepareFuture(A a) { - return Scala.asFuture(prepareAsync(), a); - } - - public Future, A, B>> prepareFuture(A a, B b) { - return Scala.asFuture(prepareAsync(), a, b); - } - - public Future, A, B, C>> prepareFuture(A a, B b, C c) { - return Scala.asFuture(prepareAsync(), a, b, c); - } - - public Future, 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(); diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractOptionalOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractOptionalOperation.java new file mode 100644 index 0000000..103a126 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/AbstractOptionalOperation.java @@ -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> extends AbstractStatementOperation { + + public AbstractOptionalOperation(AbstractSessionOperations sessionOperations) { + super(sessionOperations); + } + + public abstract Optional transform(ResultSet resultSet); + + public PreparedOptionalOperation prepare() { + return new PreparedOptionalOperation(prepareStatement(), this); + } + + public ListenableFuture> prepareAsync() { + + final O _this = (O) this; + + return Futures.transform(prepareStatementAsync(), new Function>() { + + @Override + public PreparedOptionalOperation apply(PreparedStatement preparedStatement) { + return new PreparedOptionalOperation(preparedStatement, _this); + } + + }); + + } + + public Future> prepareFuture() { + return Scala.asFuture(prepareAsync()); + } + + public Optional sync() { + + ResultSet resultSet = sessionOps.executeAsync(options(buildStatement()), showValues).getUninterruptibly(); + + return transform(resultSet); + } + + public ListenableFuture> async() { + + ResultSetFuture resultSetFuture = sessionOps.executeAsync(options(buildStatement()), showValues); + + ListenableFuture> future = Futures.transform(resultSetFuture, new Function>() { + + @Override + public Optional apply(ResultSet resultSet) { + return transform(resultSet); + } + + }, sessionOps.getExecutor()); + + return future; + } + + public Future> future() { + return Scala.asFuture(async()); + } + + public Future, A>> future(A a) { + return Scala.asFuture(async(), a); + } + + public Future, A, B>> future(A a, B b) { + return Scala.asFuture(async(), a, b); + } + + public Future, A, B, C>> future(A a, B b, C c) { + return Scala.asFuture(async(), a, b, c); + } + + public Future, A, B, C, D>> future(A a, B b, C c, D d) { + return Scala.asFuture(async(), a, b, c, d); + } + +} diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractStatementOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractStatementOperation.java index ca9790e..9fd1cb3 100644 --- a/src/main/java/com/noorq/casser/core/operation/AbstractStatementOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/AbstractStatementOperation.java @@ -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> { @@ -239,20 +238,4 @@ public abstract class AbstractStatementOperation Future> prepareStatementFuture(A a) { - return Scala.asFuture(prepareStatementAsync(), a); - } - - public Future> prepareStatementFuture(A a, B b) { - return Scala.asFuture(prepareStatementAsync(), a, b); - } - - public Future> prepareStatementFuture(A a, B b, C c) { - return Scala.asFuture(prepareStatementAsync(), a, b, c); - } - - public Future> prepareStatementFuture(A a, B b, C c, D d) { - return Scala.asFuture(prepareStatementAsync(), a, b, c, d); - } - } diff --git a/src/main/java/com/noorq/casser/core/operation/AbstractStreamOperation.java b/src/main/java/com/noorq/casser/core/operation/AbstractStreamOperation.java index e7ae02e..5516c57 100644 --- a/src/main/java/com/noorq/casser/core/operation/AbstractStreamOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/AbstractStreamOperation.java @@ -59,22 +59,6 @@ public abstract class AbstractStreamOperation> prepareFuture() { return Scala.asFuture(prepareAsync()); } - - public Future, A>> prepareFuture(A a) { - return Scala.asFuture(prepareAsync(), a); - } - - public Future, A, B>> prepareFuture(A a, B b) { - return Scala.asFuture(prepareAsync(), a, b); - } - - public Future, A, B, C>> prepareFuture(A a, B b, C c) { - return Scala.asFuture(prepareAsync(), a, b, c); - } - - public Future, A, B, C, D>> prepareFuture(A a, B b, C c, D d) { - return Scala.asFuture(prepareAsync(), a, b, c, d); - } public Stream sync() { diff --git a/src/main/java/com/noorq/casser/core/operation/BoundOptionalOperation.java b/src/main/java/com/noorq/casser/core/operation/BoundOptionalOperation.java new file mode 100644 index 0000000..32a5899 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/BoundOptionalOperation.java @@ -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 extends AbstractOptionalOperation> { + + private final BoundStatement boundStatement; + private final AbstractOptionalOperation delegate; + + public BoundOptionalOperation(BoundStatement boundStatement, AbstractOptionalOperation operation) { + super(operation.sessionOps); + this.boundStatement = boundStatement; + this.delegate = operation; + } + + @Override + public Optional transform(ResultSet resultSet) { + return delegate.transform(resultSet); + } + + @Override + public Statement buildStatement() { + return boundStatement; + } + +} diff --git a/src/main/java/com/noorq/casser/core/operation/PreparedOptionalOperation.java b/src/main/java/com/noorq/casser/core/operation/PreparedOptionalOperation.java new file mode 100644 index 0000000..e0d0691 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/PreparedOptionalOperation.java @@ -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 { + + private final PreparedStatement preparedStatement; + private final AbstractOptionalOperation operation; + + public PreparedOptionalOperation(PreparedStatement statement, AbstractOptionalOperation operation) { + this.preparedStatement = statement; + this.operation = operation; + } + + public PreparedStatement getPreparedStatement() { + return preparedStatement; + } + + public BoundOptionalOperation bind(Object... params) { + + BoundStatement boundStatement = preparedStatement.bind(params); + + return new BoundOptionalOperation(boundStatement, operation); + } + + @Override + public String toString() { + return preparedStatement.getQueryString(); + } + + +} diff --git a/src/main/java/com/noorq/casser/core/operation/SelectFirstOperation.java b/src/main/java/com/noorq/casser/core/operation/SelectFirstOperation.java new file mode 100644 index 0000000..2201e84 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/SelectFirstOperation.java @@ -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 extends AbstractFilterOptionalOperation> { + + private final SelectOperation src; + + public SelectFirstOperation(SelectOperation src) { + super(src.sessionOps); + + this.src = src; + this.filters = src.filters; + this.ifFilters = src.ifFilters; + } + + public SelectFirstTransformingOperation map(Function fn) { + return new SelectFirstTransformingOperation(src, fn); + } + + @Override + public BuiltStatement buildStatement() { + return src.buildStatement(); + } + + @Override + public Optional transform(ResultSet resultSet) { + return src.transform(resultSet).findFirst(); + } + + +} diff --git a/src/main/java/com/noorq/casser/core/operation/SelectFirstTransformingOperation.java b/src/main/java/com/noorq/casser/core/operation/SelectFirstTransformingOperation.java new file mode 100644 index 0000000..25878d1 --- /dev/null +++ b/src/main/java/com/noorq/casser/core/operation/SelectFirstTransformingOperation.java @@ -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 extends AbstractFilterOptionalOperation> { + + private final SelectOperation src; + private final Function fn; + + public SelectFirstTransformingOperation(SelectOperation src, Function 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 transform(ResultSet resultSet) { + return src.transform(resultSet).findFirst().map(fn); + } + +} 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 af2260c..8754811 100644 --- a/src/main/java/com/noorq/casser/core/operation/SelectOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/SelectOperation.java @@ -133,6 +133,11 @@ public final class SelectOperation extends AbstractFilterStreamOperation single() { + limit(1); + return new SelectFirstOperation(this); + } + public SelectTransformingOperation mapTo(Class entityClass) { Objects.requireNonNull(entityClass, "entityClass is null"); @@ -149,16 +154,16 @@ public final class SelectOperation extends AbstractFilterStreamOperation SelectTransformingOperation map(Function fn) { + return new SelectTransformingOperation(this, fn); + } + public SelectOperation column(Getter getter) { CasserPropertyNode p = MappingUtil.resolveMappingProperty(getter); this.props.add(p); return this; } - public SelectTransformingOperation map(Function fn) { - return new SelectTransformingOperation(this, fn); - } - 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/core/operation/SelectTransformingOperation.java b/src/main/java/com/noorq/casser/core/operation/SelectTransformingOperation.java index f394f7e..02d06ec 100644 --- a/src/main/java/com/noorq/casser/core/operation/SelectTransformingOperation.java +++ b/src/main/java/com/noorq/casser/core/operation/SelectTransformingOperation.java @@ -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;