Class N1qlUtils
java.lang.Object
org.springframework.data.couchbase.repository.query.support.N1qlUtils
Utility class to deal with constructing well formed N1QL queries around Spring Data entities, so that the framework
can use N1QL to find such entities (eg. restrict the bucket search to a particular type).
- Author:
- Simon Baslé, Subhashni Balakrishnan, Mark Paluch, Michael Reiche
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Converter<? super CouchbasePersistentProperty,
String> A converter that can be used to extract thefieldName
, eg. when one wants a path fromPersistentPropertyPath.toDotPath(Converter)
made of escaped field names. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic N1QLQuery
buildQuery
(N1QLExpression expression, com.couchbase.client.java.json.JsonValue queryPlaceholderValues, com.couchbase.client.java.query.QueryScanConsistency scanConsistency) Creates N1QLQuery object from the statement, query placeholder values and scan consistencystatic <T> N1QLExpression
createCountQueryForEntity
(String bucketName, CouchbaseConverter converter, CouchbaseEntityInformation<T, String> entityInformation) Creates a full N1QL query that counts total number of the given entity in the bucket.static N1QLExpression
createReturningExpressionForDelete
(String bucketName) Creates the returning clause for N1ql deletes with all attributes of the entity and meta informationstatic N1QLExpression
createSelectClauseForEntity
(String bucketName) Produce aN1QLExpression
that corresponds to the SELECT clause for looking for Spring Data entities stored in Couchbase.static N1QLExpression
createSelectClauseForEntity
(String bucketName, ReturnedType returnedType, CouchbaseConverter converter) Produce aN1QLExpression
that corresponds to the SELECT clause for looking for Spring Data entities stored in Couchbase.static N1QLExpression
createSelectFromForEntity
(String bucketName) Produce aN1QLExpression
that corresponds to the SELECT...FROM clauses for looking for Spring Data entities stored in Couchbase.static N1QLExpression[]
createSort
(Sort sort) Create a N1QLN1QLExpression
out of a Spring DataSort
.static N1QLExpression
createWhereFilterForEntity
(N1QLExpression baseWhereCriteria, CouchbaseConverter converter, EntityMetadata<?> entityInformation) Produces anN1QLExpression
that can serve as a WHERE clause criteria to only select documents in a bucket that matches a particular Spring Data entity (as given by theEntityMetadata
parameter).static N1QLExpression
escapedBucket
(String bucketName) Escape the given bucketName and produce anN1QLExpression
.static String
Given aPersistentPropertyPath
ofCouchbasePersistentProperty
(seegetPathWithAlternativeFieldNames(CouchbaseConverter, PropertyPath)
), obtain a String representation of the path, separated with dots and using alternative field names.getPathWithAlternativeFieldNames
(CouchbaseConverter converter, PropertyPath property) Given a commonPropertyPath
, returns the correspondingPersistentPropertyPath
ofCouchbasePersistentProperty
which will allow to discover alternative naming for fields.
-
Field Details
-
FIELD_NAME_ESCAPED
A converter that can be used to extract thefieldName
, eg. when one wants a path fromPersistentPropertyPath.toDotPath(Converter)
made of escaped field names.
-
-
Constructor Details
-
N1qlUtils
public N1qlUtils()
-
-
Method Details
-
escapedBucket
Escape the given bucketName and produce anN1QLExpression
. -
createSelectClauseForEntity
public static N1QLExpression createSelectClauseForEntity(String bucketName, ReturnedType returnedType, CouchbaseConverter converter) Produce aN1QLExpression
that corresponds to the SELECT clause for looking for Spring Data entities stored in Couchbase. Notably it will select the content of the document AND its id and cas and use custom construction of query if required.- Parameters:
bucketName
- the bucket that stores the entity documents (will be escaped).returnedType
- Returned type projection information from result processor.converter
- couchbase converter- Returns:
- the needed SELECT clause of the statement.
-
createReturningExpressionForDelete
Creates the returning clause for N1ql deletes with all attributes of the entity and meta information- Parameters:
bucketName
- the bucket that stores the entity documents (will be escaped).- Returns:
- the needed returning clause of the statement.
-
createSelectClauseForEntity
Produce aN1QLExpression
that corresponds to the SELECT clause for looking for Spring Data entities stored in Couchbase. Notably it will select the content of the document AND its id and cas.- Parameters:
bucketName
- the bucket that stores the entity documents (will be escaped).- Returns:
- the needed SELECT clause of the statement.
-
createSelectFromForEntity
Produce aN1QLExpression
that corresponds to the SELECT...FROM clauses for looking for Spring Data entities stored in Couchbase. Notably it will select the content of the document AND its id and cas FROM the given bucket.- Parameters:
bucketName
- the bucket that stores the entity documents (will be escaped).- Returns:
- the needed SELECT...FROM clauses of the statement.
-
createWhereFilterForEntity
public static N1QLExpression createWhereFilterForEntity(N1QLExpression baseWhereCriteria, CouchbaseConverter converter, EntityMetadata<?> entityInformation) Produces anN1QLExpression
that can serve as a WHERE clause criteria to only select documents in a bucket that matches a particular Spring Data entity (as given by theEntityMetadata
parameter).- Parameters:
baseWhereCriteria
- the other criteria of the WHERE clause, or null if none.converter
- theCouchbaseConverter
giving the attribute storing the type information can be extracted.entityInformation
- the expected type information.- Returns:
- an
N1QLExpression
to be used as a WHERE clause, that additionally restricts on the given type.
-
getPathWithAlternativeFieldNames
public static PersistentPropertyPath<CouchbasePersistentProperty> getPathWithAlternativeFieldNames(CouchbaseConverter converter, PropertyPath property) Given a commonPropertyPath
, returns the correspondingPersistentPropertyPath
ofCouchbasePersistentProperty
which will allow to discover alternative naming for fields. -
getDottedPathWithAlternativeFieldNames
public static String getDottedPathWithAlternativeFieldNames(PersistentPropertyPath<CouchbasePersistentProperty> path) Given aPersistentPropertyPath
ofCouchbasePersistentProperty
(seegetPathWithAlternativeFieldNames(CouchbaseConverter, PropertyPath)
), obtain a String representation of the path, separated with dots and using alternative field names. -
createSort
Create a N1QLN1QLExpression
out of a Spring DataSort
. Note that the later must use alternative field names as declared by theField
annotation on the entity, if any. -
createCountQueryForEntity
public static <T> N1QLExpression createCountQueryForEntity(String bucketName, CouchbaseConverter converter, CouchbaseEntityInformation<T, String> entityInformation) Creates a full N1QL query that counts total number of the given entity in the bucket.- Parameters:
bucketName
- the name of the bucket where data is stored (will be escaped).converter
- theCouchbaseConverter
giving the attribute storing the type information can be extracted.entityInformation
- the counted entity type.- Returns:
- the N1QL query that counts number of documents matching this entity type.
-
buildQuery
public static N1QLQuery buildQuery(N1QLExpression expression, com.couchbase.client.java.json.JsonValue queryPlaceholderValues, com.couchbase.client.java.query.QueryScanConsistency scanConsistency) Creates N1QLQuery object from the statement, query placeholder values and scan consistency- Parameters:
expression
- AN1QLExpression
representing the query to executequeryPlaceholderValues
- The positional or named parameters needed for the queryscanConsistency
- TheQueryScanConsistency
to be used.- Returns:
- A
N1QLQuery
to be executed.
-