Skip to content

QueryDSL support. #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<couchbase.osgi>3.2.5</couchbase.osgi>
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
<java-module-name>spring.data.couchbase</java-module-name>
<apt>1.1.3</apt>
<querydsl>5.0.0</querydsl>
<mysema.querydsl>3.7.4</mysema.querydsl>
</properties>

<dependencyManagement>
Expand All @@ -37,6 +40,13 @@
</dependencyManagement>

<dependencies>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl}</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be <scope>provided</scope> to avoid mandatory dependencies on the annotation processor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added scope provided

</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
Expand Down Expand Up @@ -160,9 +170,10 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax-annotation-api}</version>
<scope>test</scope>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should actually remain in test scope. Probably a consequence of how the generated sources are mounted into the project.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restored scope test.

</dependency>

<!-- -addmods java.annotations.common -->

<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-se</artifactId>
Expand Down Expand Up @@ -298,6 +309,48 @@
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${apt}</version>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl}</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-test-sources</outputDirectory>
<processor>org.springframework.data.couchbase.repository.support.CouchbaseAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buildhelper should actually not be necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-test-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
* Copyright 2012-2022 the original author or authors
*
* 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
*
* https://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.querydsl.couchbase.document;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jetbrains.annotations.Nullable;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.query.QueryCriteriaDefinition;

import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.JoinExpression;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.QueryModifiers;
import com.querydsl.core.SimpleQuery;
import com.querydsl.core.support.QueryMixin;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.FactoryExpression;
import com.querydsl.core.types.Operation;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.ParamExpression;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;

/**
* renamed from AbstractCouchbaseQuery to AbstractCouchbaseQueryDSL to avoid confusion with the AbstractCouchbaseQuery
* that is in the package com.querydsl.couchbase
*
* @author Michael Reiche
*/

public abstract class AbstractCouchbaseQueryDSL<Q extends AbstractCouchbaseQueryDSL<Q>> implements SimpleQuery<Q> {
private final CouchbaseDocumentSerializer serializer;
private final QueryMixin<Q> queryMixin;// = new QueryMixin(this, new DefaultQueryMetadata(), false);
// TODO private ReadPreference readPreference;

public AbstractCouchbaseQueryDSL(CouchbaseDocumentSerializer serializer) {
this.serializer = serializer;
@SuppressWarnings("unchecked") // Q is this plus subclass
Q query = (Q) this;
this.queryMixin = new QueryMixin<Q>(query, new DefaultQueryMetadata(), false);
}

/**
* mongodb uses createQuery(Predicate filter) where the serializer creates the 'query' <br>
* and then uses the result to create a BasicQuery with queryObject = result <br>
* Couchbase Query has a 'criteria' which is a <br>
* List<QueryCriteriaDefinition> criteria <br>
* so we could create a List&lt;QueryCriteriaDefinition&gt; or an uber QueryCriteria that combines <br>
* all the sub QueryDefinitions in the filter.
*/
protected QueryCriteriaDefinition createCriteria(Predicate predicate) {
// mongodb uses createQuery(Predicate filter) where the serializer creates the 'queryObject' of the BasicQuery
return predicate != null ? (QueryCriteriaDefinition) this.serializer.handle(predicate) : null;
}

// TODO - need later
// public <T> JoinBuilder<Q, T> join(Path<T> ref, Path<T> target) {
// return new JoinBuilder(this.queryMixin, ref, target);
// }

// public <T> JoinBuilder<Q, T> join(CollectionPathBase<?, T, ?> ref, Path<T> target) {
// return new JoinBuilder(this.queryMixin, ref, target);
// }

// public <T> AnyEmbeddedBuilder<Q> anyEmbedded(Path<? extends Collection<T>> collection, Path<T> target) {
// return new AnyEmbeddedBuilder(this.queryMixin, collection);
// }

@Nullable
protected Predicate createFilter(QueryMetadata metadata) {
Predicate filter;
if (!metadata.getJoins().isEmpty()) {
filter = ExpressionUtils.allOf(new Predicate[] { metadata.getWhere(), this.createJoinFilter(metadata) });
} else {
filter = metadata.getWhere();
}
return filter;
}

@Nullable
protected Predicate createJoinFilter(QueryMetadata metadata) {
Map<Expression<?>, Predicate> predicates = new HashMap();
List<JoinExpression> joins = metadata.getJoins();

for (int i = joins.size() - 1; i >= 0; --i) {
JoinExpression join = (JoinExpression) joins.get(i);
Path<?> source = (Path) ((Operation) join.getTarget()).getArg(0);
Path<?> target = (Path) ((Operation) join.getTarget()).getArg(1);
Predicate extraFilters = (Predicate) predicates.get(target.getRoot());
Predicate filter = ExpressionUtils.allOf(new Predicate[] { join.getCondition(), extraFilters });
List<? extends Object> ids = this.getIds(target.getType(), filter);
if (ids.isEmpty()) {
throw new AbstractCouchbaseQueryDSL.NoResults();
}

Path<?> path = ExpressionUtils.path(String.class, source, "$id");
predicates.merge(source.getRoot(),
ExpressionUtils.in(path, (Collection) ids/* TODO was just ids without casting to Collection */),
ExpressionUtils::and);
}

Path<?> source = (Path) ((Operation) ((JoinExpression) joins.get(0)).getTarget()).getArg(0);
return predicates.get(source.getRoot());
}

private Predicate allOf(Collection<Predicate> predicates) {
return predicates != null ? ExpressionUtils.allOf(predicates) : null;
}

protected abstract List<Object> getIds(Class<?> var1, Predicate var2);

public Q distinct() {
return this.queryMixin.distinct();
}

public Q where(Predicate e) {
return this.queryMixin.where(e);
}

public Q where(Predicate... e) {
return this.queryMixin.where(e);
}

public Q limit(long limit) {
return this.queryMixin.limit(limit);
}

public Q offset(long offset) {
return this.queryMixin.offset(offset);
}

public Q restrict(QueryModifiers modifiers) {
return this.queryMixin.restrict(modifiers);
}

public Q orderBy(OrderSpecifier<?> o) {
return this.queryMixin.orderBy(o);
}

public Q orderBy(OrderSpecifier<?>... o) {
return this.queryMixin.orderBy(o);
}

public <T> Q set(ParamExpression<T> param, T value) {
return this.queryMixin.set(param, value);
}

protected Map<String, String> createProjection(Expression<?> projection) {
if (projection instanceof FactoryExpression) {
Map<String, String> obj = new HashMap();
Iterator var3 = ((FactoryExpression) projection).getArgs().iterator();

while (var3.hasNext()) {
Object expr = var3.next();
if (expr instanceof Expression) {
obj.put(expr.toString(), (String) this.serializer.handle((Expression) expr));
}
}
return obj;
} else {
return null;
}
}

protected CouchbaseDocument createQuery(@Nullable Predicate predicate) {
return predicate != null ? (CouchbaseDocument) this.serializer.handle(predicate) : new CouchbaseDocument();
}

// public void setReadPreference(ReadPreference readPreference) {
// this.readPreference = readPreference;
// }

protected QueryMixin<Q> getQueryMixin() {
return this.queryMixin;
}

protected CouchbaseDocumentSerializer getSerializer() {
return this.serializer;
}

// protected ReadPreference getReadPreference() {
// return this.readPreference;
// }

public CouchbaseDocument asDocument() {
return this.createQuery(this.queryMixin.getMetadata().getWhere());
}

public String toString() {
return this.asDocument().toString();
}

static class NoResults extends RuntimeException {
NoResults() {}
}
}
Loading