/** * Adds a AS clause between the current and the given expression. Often used to alias an identifier. * * @param alias the right hand side expression. * @return a new expression with the clause applied. */ public Expression as(String alias) { return as(x(alias)); }
/** * 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()); }
/** * 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); }
/** * Creates a full N1QL query that counts total number of the given entity in the bucket. * * @param bucketName the name of the bucket where data is stored (will be escaped). * @param converter the {@link CouchbaseConverter} giving the attribute storing the type information can be extracted. * @param entityInformation the counted entity type. * @return the N1QL query that counts number of documents matching this entity type. */ public static <T> Statement createCountQueryForEntity(String bucketName, CouchbaseConverter converter, CouchbaseEntityInformation<T, String> entityInformation) { return select(count("*").as(CountFragment.COUNT_ALIAS)).from(escapedBucket(bucketName)).where(createWhereFilterForEntity(null, converter, entityInformation)); }
/** * Creates a full N1QL query that counts total number of the given entity in the bucket. * * @param bucketName the name of the bucket where data is stored (will be escaped). * @param converter the {@link CouchbaseConverter} giving the attribute storing the type information can be extracted. * @param entityInformation the counted entity type. * @return the N1QL query that counts number of documents matching this entity type. */ public static <T> Statement createCountQueryForEntity(String bucketName, CouchbaseConverter converter, CouchbaseEntityInformation<T, String> entityInformation) { return select(count("*").as(CountFragment.COUNT_ALIAS)).from(escapedBucket(bucketName)).where(createWhereFilterForEntity(null, converter, entityInformation)); }
@Override protected Statement getCount(ParameterAccessor accessor, Object[] runtimeParameters) { Expression bucket = i(getCouchbaseOperations().getCouchbaseBucket().name()); WherePath countFrom = select(count("*").as(CountFragment.COUNT_ALIAS)).from(bucket); N1qlCountQueryCreator queryCountCreator = new N1qlCountQueryCreator(partTree, accessor, countFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); Statement statement = queryCountCreator.createQuery(); this.placeHolderValues = queryCountCreator.getPlaceHolderValues(); return statement; }
@Override protected Statement getCount(ParameterAccessor accessor, Object[] runtimeParameters) { Expression bucket = i(getCouchbaseOperations().getCouchbaseBucket().name()); WherePath countFrom = select(count("*").as(CountFragment.COUNT_ALIAS)).from(bucket); N1qlCountQueryCreator queryCountCreator = new N1qlCountQueryCreator(partTree, accessor, countFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); Statement statement = queryCountCreator.createQuery(); this.placeHolderValues = queryCountCreator.getPlaceHolderValues(); return statement; }
@Override public Observable<IndexInfo> listN1qlIndexes() { Expression whereClause = x("keyspace_id").eq(s(bucket)) .and(i("using").eq(s("gsi"))); Statement listIndexes = select("idx.*").from(x("system:indexes").as("idx")).where(whereClause) .orderBy(Sort.desc("is_primary"), Sort.asc("name")); final Func1<List<JsonObject>, Observable<AsyncN1qlQueryRow>> errorHandler = errorsToThrowable( "Error while listing indexes: "); return queryExecutor.execute( N1qlQuery.simple(listIndexes, N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS)), env, env.managementTimeout(), TimeUnit.MILLISECONDS) .flatMap(new Func1<AsyncN1qlQueryResult, Observable<AsyncN1qlQueryRow>>() { @Override public Observable<AsyncN1qlQueryRow> call(final AsyncN1qlQueryResult aqr) { return aqr.finalSuccess() .flatMap(new Func1<Boolean, Observable<AsyncN1qlQueryRow>>() { @Override public Observable<AsyncN1qlQueryRow> call(Boolean success) { if (success) { return aqr.rows(); } else { return aqr.errors().toList().flatMap(errorHandler); } } }); } }).map(ROW_VALUE_TO_INDEXINFO); }