Skip to content
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2327-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2327-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2327-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2327-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
import java.util.List;

import org.bson.Document;
import org.bson.codecs.DocumentCodec;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;

import org.springframework.lang.Nullable;

import com.mongodb.MongoClientSettings;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.QueryModifiers;
import com.querydsl.core.SimpleQuery;
Expand All @@ -38,7 +43,7 @@
* 2.0.
* </p>
* Modified for usage with {@link MongodbDocumentSerializer}.
*
*
* @param <Q> concrete subtype
* @author laimw
* @author Mark Paluch
Expand All @@ -48,6 +53,9 @@
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;

Expand Down Expand Up @@ -179,7 +187,7 @@ protected Document createSort(List<OrderSpecifier<?>> orderSpecifiers) {

/**
* Get the actual {@link QueryMixin} delegate.
*
*
* @return
*/
QueryMixin<Q> getQueryMixin() {
Expand All @@ -195,8 +203,69 @@ Document asDocument() {
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>
*
* Note that encoding to {@link String} may fail when using data types that cannot be encoded or DBRef's without an
* identifier.
*
* @return never {@literal null}.
*/
@Override
public String toString() {
return asDocument().toString();

Document projection = createProjection(queryMixin.getMetadata().getProjection());
Document sort = createSort(queryMixin.getMetadata().getOrderBy());
DocumentCodec codec = new DocumentCodec(MongoClientSettings.getDefaultCodecRegistry());

StringBuilder sb = new StringBuilder("find(" + asDocument().toJson(JSON_WRITER_SETTINGS, codec));
if (!projection.isEmpty()) {
sb.append(", ").append(projection.toJson(JSON_WRITER_SETTINGS, codec));
}
sb.append(")");
if (!sort.isEmpty()) {
sb.append(".sort(").append(sort.toJson(JSON_WRITER_SETTINGS, codec)).append(")");
}
if (queryMixin.getMetadata().getModifiers().getOffset() != null) {
sb.append(".skip(").append(queryMixin.getMetadata().getModifiers().getOffset()).append(")");
}
if (queryMixin.getMetadata().getModifiers().getLimit() != null) {
sb.append(".limit(").append(queryMixin.getMetadata().getModifiers().getLimit()).append(")");
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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}.
Expand Down Expand Up @@ -248,6 +249,39 @@ public void shouldUseStringForValidObjectIdHexStrings() {
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;
User user = new User();
user.setId("id");
SpringDataMongodbQuery<Person> query = repoSupport.from(p)
.where(p.lastname.eq("Matthews").and(p.coworker.eq(user)));

assertThat(StringUtils.trimAllWhitespace(query.toString()))
.isEqualTo("find({\"lastname\":\"Matthews\",\"coworker\":{\"$ref\":\"user\",\"$id\":\"id\"}})");

query = query.orderBy(p.firstname.asc());
assertThat(StringUtils.trimAllWhitespace(query.toString()))
.isEqualTo(
"find({\"lastname\":\"Matthews\",\"coworker\":{\"$ref\":\"user\",\"$id\":\"id\"}}).sort({\"firstname\":1})");

query = query.offset(1).limit(5);
assertThat(StringUtils.trimAllWhitespace(query.toString()))
.isEqualTo(
"find({\"lastname\":\"Matthews\",\"coworker\":{\"$ref\":\"user\",\"$id\":\"id\"}}).sort({\"firstname\":1}).skip(1).limit(5)");
}

@Data
@Document
public static class Outer {
Expand Down