/** * Creates a new {@link N1qlQuery} for the given class, this method build a query like: * * <pre> * select b.* from bucket b where b._class = 'model.Beer' * </pre> * * @param bucket Bucket name. * @param entityClass Entity class. * @return A query. */ public static GroupByPath from(final String bucket, final Class<?> entityClass) { String alias = String.valueOf(entityClass.getSimpleName().charAt(0)); return Select.select(alias + ".*") .from(i(bucket) + " " + alias) .where(path(alias, CLASS).eq(s(entityClass.getName()))); }
/** * Creates the returning clause for N1ql deletes with all attributes of the entity and meta information * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed returning clause of the statement. */ public static Expression createReturningExpressionForDelete(String bucketName) { Expression fullEntity = path(i(bucketName), "*"); Expression metaId = path(meta(i(bucketName)), "id").as(SELECT_ID); Expression metaCas = path(meta(i(bucketName)), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(fullEntity); expList.add(metaId); expList.add(metaCas); StringBuilder sb = new StringBuilder(); for (Expression exp: expList) { if (sb.length() != 0) { sb.append(", "); } sb.append(exp.toString()); } return x(sb.toString()); }
/** * Creates the returning clause for N1ql deletes with all attributes of the entity and meta information * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed returning clause of the statement. */ public static Expression createReturningExpressionForDelete(String bucketName) { Expression fullEntity = path(i(bucketName), "*"); Expression metaId = path(meta(i(bucketName)), "id").as(SELECT_ID); Expression metaCas = path(meta(i(bucketName)), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(fullEntity); expList.add(metaId); expList.add(metaCas); StringBuilder sb = new StringBuilder(); for (Expression exp: expList) { if (sb.length() != 0) { sb.append(", "); } sb.append(exp.toString()); } return x(sb.toString()); }
/** * Get attribute of an object using the given string as attribute name. * * @param expression The attribute name * @return the getter expression */ public Expression get(String expression) { return new Expression(path(toString(), x(expression))); }
/** * Produce a {@link Statement} 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. * * @param bucketName the bucket that stores the entity documents (will be escaped). * @param returnedType Returned type projection information from result processor. * @param converter couchbase converter * @return the needed SELECT clause of the statement. */ public static FromPath createSelectClauseForEntity(String bucketName, ReturnedType returnedType, CouchbaseConverter converter) { Expression bucket = escapedBucket(bucketName); Expression metaId = path(meta(bucket), "id").as(SELECT_ID); Expression metaCas = path(meta(bucket), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(metaId); expList.add(metaCas); if (returnedType != null && returnedType.needsCustomConstruction()) { List<String> properties = returnedType.getInputProperties(); CouchbasePersistentEntity<?> entity = converter.getMappingContext().getRequiredPersistentEntity(returnedType.getDomainType()); for (String property : properties) { expList.add(path(bucket, i(entity.getRequiredPersistentProperty(property).getFieldName()))); } } else { expList.add(path(bucket, "*")); } Expression[] propertiesExp = new Expression[expList.size()]; propertiesExp = expList.toArray(propertiesExp); return select(propertiesExp); }
/** * Produce a {@link Statement} 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. * * @param bucketName the bucket that stores the entity documents (will be escaped). * @param returnedType Returned type projection information from result processor. * @param converter couchbase converter * @return the needed SELECT clause of the statement. */ public static FromPath createSelectClauseForEntity(String bucketName, ReturnedType returnedType, CouchbaseConverter converter) { Expression bucket = escapedBucket(bucketName); Expression metaId = path(meta(bucket), "id").as(SELECT_ID); Expression metaCas = path(meta(bucket), "cas").as(SELECT_CAS); List<Expression> expList = new ArrayList<Expression>(); expList.add(metaId); expList.add(metaCas); if (returnedType != null && returnedType.needsCustomConstruction()) { List<String> properties = returnedType.getInputProperties(); CouchbasePersistentEntity<?> entity = converter.getMappingContext().getRequiredPersistentEntity(returnedType.getDomainType()); for (String property : properties) { expList.add(path(bucket, i(entity.getRequiredPersistentProperty(property).getFieldName()))); } } else { expList.add(path(bucket, "*")); } Expression[] propertiesExp = new Expression[expList.size()]; propertiesExp = expList.toArray(propertiesExp); return select(propertiesExp); }