-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add find and delete on documentQuery, #144. Signed-off-by: Pan Li <panli@microsoft.com>
- Loading branch information
1 parent
513537e
commit 83e1e54
Showing
9 changed files
with
254 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
.../java/com/microsoft/azure/spring/data/cosmosdb/core/generator/AbstractQueryGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for | ||
* license information. | ||
*/ | ||
package com.microsoft.azure.spring.data.cosmosdb.core.generator; | ||
|
||
import com.microsoft.azure.spring.data.cosmosdb.core.criteria.Criteria; | ||
import com.microsoft.azure.spring.data.cosmosdb.core.criteria.CriteriaType; | ||
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery; | ||
import com.microsoft.azure.spring.data.cosmosdb.repository.support.DocumentDbEntityInformation; | ||
import org.javatuples.Pair; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.microsoft.azure.spring.data.cosmosdb.Constants.ID_PROPERTY_NAME; | ||
|
||
public abstract class AbstractQueryGenerator { | ||
|
||
protected final DocumentDbEntityInformation information; | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> AbstractQueryGenerator(@NonNull Class<T> domainClass) { | ||
this.information = new DocumentDbEntityInformation(domainClass); | ||
} | ||
|
||
private String getCriteriaSubject(@NonNull Criteria criteria) { | ||
String subject = criteria.getSubject(); | ||
|
||
if (subject.equals(information.getIdField().getName())) { | ||
subject = ID_PROPERTY_NAME; | ||
} | ||
|
||
return subject; | ||
} | ||
|
||
private String generateIsEqual(@NonNull Criteria criteria, @NonNull List<Pair<String, Object>> parameters) { | ||
final String subject = this.getCriteriaSubject(criteria); | ||
|
||
Assert.isTrue(criteria.getSubValues().size() == 1, "IS_EQUAL should have only one subject value"); | ||
|
||
parameters.add(Pair.with(subject, criteria.getSubValues().get(0))); | ||
|
||
return String.format("r.%s=@%s", subject, subject); | ||
} | ||
|
||
private String generateBinaryQuery(@NonNull String left, @NonNull String right, CriteriaType type) { | ||
Assert.isTrue(Criteria.isBinaryOperation(type), "Criteria type should be binary operation"); | ||
|
||
final String keyword = CriteriaType.toSqlKeyword(type); | ||
|
||
return String.join(" ", left, keyword, right); | ||
} | ||
|
||
private String generateQueryBodyDfs(@NonNull Criteria criteria, @NonNull List<Pair<String, Object>> parameters) { | ||
final CriteriaType type = criteria.getType(); | ||
|
||
switch (type) { | ||
case IS_EQUAL: | ||
return this.generateIsEqual(criteria, parameters); | ||
case AND: | ||
case OR: | ||
Assert.isTrue(criteria.getSubCriteria().size() == 2, "criteria should have two SubCriteria"); | ||
|
||
final String left = generateQueryBodyDfs(criteria.getSubCriteria().get(0), parameters); | ||
final String right = generateQueryBodyDfs(criteria.getSubCriteria().get(1), parameters); | ||
|
||
return generateBinaryQuery(left, right, type); | ||
default: | ||
throw new UnsupportedOperationException("unsupported Criteria type" + type); | ||
} | ||
} | ||
|
||
/** | ||
* Generate a query body for interface QuerySpecGenerator. | ||
* The query body compose of Sql query String and its' parameters. | ||
* The parameters organized as a list of Pair, for each pair compose parameter name and value. | ||
* | ||
* @param query the representation for query method. | ||
* @return A pair tuple compose of Sql query. | ||
*/ | ||
@NonNull | ||
protected Pair<String, List<Pair<String, Object>>> generateQueryBody(@NonNull DocumentQuery query) { | ||
final List<Pair<String, Object>> parameters = new ArrayList<>(); | ||
final String queryString = this.generateQueryBodyDfs(query.getCriteria(), parameters); | ||
|
||
return Pair.with(queryString, parameters); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../java/com/microsoft/azure/spring/data/cosmosdb/core/generator/FindQuerySpecGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for | ||
* license information. | ||
*/ | ||
package com.microsoft.azure.spring.data.cosmosdb.core.generator; | ||
|
||
import com.microsoft.azure.documentdb.SqlParameter; | ||
import com.microsoft.azure.documentdb.SqlParameterCollection; | ||
import com.microsoft.azure.documentdb.SqlQuerySpec; | ||
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentQuery; | ||
import org.javatuples.Pair; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.microsoft.azure.spring.data.cosmosdb.core.convert.MappingDocumentDbConverter.toDocumentDBValue; | ||
|
||
public class FindQuerySpecGenerator extends AbstractQueryGenerator implements QuerySpecGenerator { | ||
|
||
public <T> FindQuerySpecGenerator(@NonNull Class<T> domainClass) { | ||
super(domainClass); | ||
} | ||
|
||
@Override | ||
public SqlQuerySpec generate(@NonNull DocumentQuery query) { | ||
final Pair<String, List<Pair<String, Object>>> queryBody = super.generateQueryBody(query); | ||
final String queryHeader = "SELECT * FROM ROOT r WHERE"; | ||
final String queryString = queryHeader + " " + queryBody.getValue0(); | ||
final List<Pair<String, Object>> parameters = queryBody.getValue1(); | ||
final SqlParameterCollection sqlParameters = new SqlParameterCollection(); | ||
|
||
sqlParameters.addAll(parameters.stream() | ||
.map(p -> new SqlParameter("@" + p.getValue0(), toDocumentDBValue(p.getValue1()))) | ||
.collect(Collectors.toList())); | ||
|
||
return new SqlQuerySpec(queryString, sqlParameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.