java.lang.Object
org.springframework.data.couchbase.repository.query.support.N1qlUtils

public class N1qlUtils extends Object
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 Details

  • Constructor Details

    • N1qlUtils

      public N1qlUtils()
  • Method Details

    • escapedBucket

      public static N1QLExpression escapedBucket(String bucketName)
      Escape the given bucketName and produce an N1QLExpression.
    • createSelectClauseForEntity

      public static N1QLExpression createSelectClauseForEntity(String bucketName, ReturnedType returnedType, CouchbaseConverter converter)
      Produce a N1QLExpression 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

      public static N1QLExpression createReturningExpressionForDelete(String bucketName)
      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

      public static N1QLExpression createSelectClauseForEntity(String bucketName)
      Produce a N1QLExpression 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

      public static N1QLExpression createSelectFromForEntity(String bucketName)
      Produce a N1QLExpression 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 an N1QLExpression 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 the EntityMetadata parameter).
      Parameters:
      baseWhereCriteria - the other criteria of the WHERE clause, or null if none.
      converter - the CouchbaseConverter 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 common PropertyPath, returns the corresponding PersistentPropertyPath of CouchbasePersistentProperty which will allow to discover alternative naming for fields.
    • getDottedPathWithAlternativeFieldNames

      public static String getDottedPathWithAlternativeFieldNames(PersistentPropertyPath<CouchbasePersistentProperty> path)
      Given a PersistentPropertyPath of CouchbasePersistentProperty (see getPathWithAlternativeFieldNames(CouchbaseConverter, PropertyPath)), obtain a String representation of the path, separated with dots and using alternative field names.
    • createSort

      public static N1QLExpression[] createSort(Sort sort)
      Create a N1QL N1QLExpression out of a Spring Data Sort. Note that the later must use alternative field names as declared by the Field 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 - the CouchbaseConverter 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 - A N1QLExpression representing the query to execute
      queryPlaceholderValues - The positional or named parameters needed for the query
      scanConsistency - The QueryScanConsistency to be used.
      Returns:
      A N1QLQuery to be executed.