DATAMONGO-2327 - Add toJson method to Querydsl query support.
This allows to obtain the raw Json representation of the query for eg. debug usage. We also updated the toString method to return a full Mongo Shell compatible representation of the query including projections, order, skip and limit. Original pull request: #774.
This commit is contained in:
committed by
Mark Paluch
parent
69a4217c4b
commit
7d9c08409b
@@ -18,6 +18,8 @@ package org.springframework.data.mongodb.repository.support;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.json.JsonMode;
|
||||
import org.bson.json.JsonWriterSettings;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import com.querydsl.core.DefaultQueryMetadata;
|
||||
@@ -48,6 +50,9 @@ import com.querydsl.core.types.Predicate;
|
||||
public abstract class QuerydslAbstractMongodbQuery<K, Q extends QuerydslAbstractMongodbQuery<K, Q>>
|
||||
implements SimpleQuery<Q> {
|
||||
|
||||
private static final JsonWriterSettings JSON_WRITER_SETTINGS = JsonWriterSettings.builder().outputMode(JsonMode.SHELL)
|
||||
.build();
|
||||
|
||||
private final MongodbDocumentSerializer serializer;
|
||||
private final QueryMixin<Q> queryMixin;
|
||||
|
||||
@@ -195,8 +200,65 @@ public abstract class QuerydslAbstractMongodbQuery<K, Q extends QuerydslAbstract
|
||||
return createQuery(queryMixin.getMetadata().getWhere());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@literal Mongo Shell} representation of the query. <br />
|
||||
* The following query
|
||||
*
|
||||
* <pre class="code">
|
||||
*
|
||||
* where(p.lastname.eq("Matthews")).orderBy(p.firstname.asc()).offset(1).limit(5);
|
||||
* </pre>
|
||||
*
|
||||
* results in
|
||||
*
|
||||
* <pre class="code">
|
||||
*
|
||||
* find({"lastname" : "Matthews"}).sort({"firstname" : 1}).skip(1).limit(5)
|
||||
* </pre>
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return asDocument().toString();
|
||||
|
||||
Document projection = createProjection(queryMixin.getMetadata().getProjection());
|
||||
Document sort = createSort(queryMixin.getMetadata().getOrderBy());
|
||||
|
||||
StringBuilder sb = new StringBuilder("find(" + asDocument().toJson(JSON_WRITER_SETTINGS));
|
||||
if (!projection.isEmpty()) {
|
||||
sb.append(", " + projection.toJson(JSON_WRITER_SETTINGS));
|
||||
}
|
||||
sb.append(")");
|
||||
if (!sort.isEmpty()) {
|
||||
sb.append(".sort(" + sort.toJson(JSON_WRITER_SETTINGS) + ")");
|
||||
}
|
||||
if (queryMixin.getMetadata().getModifiers().getOffset() != null) {
|
||||
sb.append(".skip(" + queryMixin.getMetadata().getModifiers().getOffset() + ")");
|
||||
}
|
||||
if (queryMixin.getMetadata().getModifiers().getLimit() != null) {
|
||||
sb.append(".limit(" + queryMixin.getMetadata().getModifiers().getLimit() + ")");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain the {@literal Mongo Shell} json query representation.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 2.2
|
||||
*/
|
||||
public String toJson() {
|
||||
return toJson(JSON_WRITER_SETTINGS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain the json query representation applying given {@link JsonWriterSettings settings}.
|
||||
*
|
||||
* @param settings must not be {@literal null}.
|
||||
* @return never {@literal null}.
|
||||
* @since 2.2
|
||||
*/
|
||||
public String toJson(JsonWriterSettings settings) {
|
||||
return asDocument().toJson(settings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.bson.types.ObjectId;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.annotation.Id;
|
||||
@@ -39,6 +38,7 @@ import org.springframework.data.mongodb.repository.QPerson;
|
||||
import org.springframework.data.mongodb.repository.User;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QuerydslRepositorySupport}.
|
||||
@@ -248,6 +248,33 @@ public class QuerydslRepositorySupportTests {
|
||||
assertThat(inQuery.fetchOne()).isEqualTo(document);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2327
|
||||
public void toJsonShouldRenderQuery() {
|
||||
|
||||
QPerson p = QPerson.person;
|
||||
SpringDataMongodbQuery<Person> query = repoSupport.from(p).where(p.lastname.eq("Matthews"))
|
||||
.orderBy(p.firstname.asc()).offset(1).limit(5);
|
||||
|
||||
assertThat(StringUtils.trimAllWhitespace(query.toJson())).isEqualTo("{\"lastname\":\"Matthews\"}");
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2327
|
||||
public void toStringShouldRenderQuery() {
|
||||
|
||||
QPerson p = QPerson.person;
|
||||
SpringDataMongodbQuery<Person> query = repoSupport.from(p).where(p.lastname.eq("Matthews"));
|
||||
|
||||
assertThat(StringUtils.trimAllWhitespace(query.toString())).isEqualTo("find({\"lastname\":\"Matthews\"})");
|
||||
|
||||
query = query.orderBy(p.firstname.asc());
|
||||
assertThat(StringUtils.trimAllWhitespace(query.toString()))
|
||||
.isEqualTo("find({\"lastname\":\"Matthews\"}).sort({\"firstname\":1})");
|
||||
|
||||
query = query.offset(1).limit(5);
|
||||
assertThat(StringUtils.trimAllWhitespace(query.toString()))
|
||||
.isEqualTo("find({\"lastname\":\"Matthews\"}).sort({\"firstname\":1}).skip(1).limit(5)");
|
||||
}
|
||||
|
||||
@Data
|
||||
@Document
|
||||
public static class Outer {
|
||||
|
||||
Reference in New Issue
Block a user