/** * 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()))); }
/** * Produce a {@link Statement} 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. * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed SELECT...FROM clauses of the statement. */ public static WherePath createSelectFromForEntity(String bucketName) { return createSelectClauseForEntity(bucketName).from(escapedBucket(bucketName)); }
/** * Produce a {@link Statement} 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. * * @param bucketName the bucket that stores the entity documents (will be escaped). * @return the needed SELECT...FROM clauses of the statement. */ public static WherePath createSelectFromForEntity(String bucketName) { return createSelectClauseForEntity(bucketName).from(escapedBucket(bucketName)); }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents) .from(i(bucket)) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)).offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).limit(maxResult); } return select(documents).from(i(bucket)); }
/** * Query and get a result by username. * * @param usernameAttribute the username attribute * @param usernameValue the username value * @return the n1ql query result * @throws GeneralSecurityException the general security exception */ public N1qlQueryResult query(final String usernameAttribute, final String usernameValue) throws GeneralSecurityException { val theBucket = getBucket(); val statement = Select.select("*") .from(Expression.i(theBucket.name())) .where(Expression.x(usernameAttribute).eq('\'' + usernameValue + '\'')); LOGGER.debug("Running query [{}] on bucket [{}]", statement.toString(), theBucket.name()); val query = N1qlQuery.simple(statement); val result = theBucket.query(query, timeout, TimeUnit.MILLISECONDS); if (!result.finalSuccess()) { LOGGER.error("Couchbase query failed with [{}]", result.errors() .stream() .map(JsonObject::toString) .collect(Collectors.joining(","))); throw new GeneralSecurityException("Could not locate account for user " + usernameValue); } return result; }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult, Sort[] sorts) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents) .from(i(bucket)) .orderBy(sorts) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)).orderBy(sorts).offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).orderBy(sorts).limit(maxResult); } return select(documents).from(i(bucket)).orderBy(sorts); }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult, Expression condition) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents).from(i(bucket)) .where(condition) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)) .where(condition) .offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).where(condition) .limit(maxResult); } return select(documents).from(i(bucket)).where(condition); }
/** * 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)); }
public List<User> findAll() { if (useCouchbaseForFindAll && couchbaseBucket != null) { try { Statement statement = Select.select("avatarId", "bio", "displayName", "id", "nickname").from(x("default")) .where(x("type").eq(s("user"))).groupBy(x("displayName")); N1qlQueryResult queryResult = couchbaseBucket.query(statement); List<User> users = new ArrayList<User>(); for (N1qlQueryRow qr : queryResult) { users.add(User.fromJsonObject(qr.value())); } return users; } catch (Exception e) { throw new DogePoolException("Error while getting list of users from database", Error.DATABASE, HttpStatus.INTERNAL_SERVER_ERROR, e); } } else { return Arrays.asList(User.USER, User.OTHERUSER); } } }
private static Statement get(String bucket, String[] documents, int firstResult, int maxResult, Sort[] sorts, Expression condition) { boolean hasFistResult = firstResult > 0; boolean hasMaxResult = maxResult > 0; if (hasFistResult && hasMaxResult) { return select(documents).from(i(bucket)) .where(condition) .orderBy(sorts) .limit(maxResult) .offset(firstResult); } else if (hasFistResult) { return select(documents).from(i(bucket)) .where(condition) .orderBy(sorts) .offset(firstResult); } else if (hasMaxResult) { return select(documents).from(i(bucket)).where(condition) .orderBy(sorts) .limit(maxResult); } return select(documents).from(i(bucket)).where(condition).orderBy(sorts); }
@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); }
@Override protected Statement getStatement(ParameterAccessor accessor, Object[] runtimeParameters, ReturnedType returnedType) { String bucketName = getCouchbaseOperations().getCouchbaseBucket().name(); Expression bucket = N1qlUtils.escapedBucket(bucketName); FromPath select; if (partTree.isCountProjection()) { select = select(count("*")); } else { select = N1qlUtils.createSelectClauseForEntity(bucketName, returnedType, this.getCouchbaseOperations().getConverter()); } WherePath selectFrom = select.from(bucket); N1qlQueryCreator queryCreator = new N1qlQueryCreator(partTree, accessor, selectFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); LimitPath selectFromWhereOrderBy = queryCreator.createQuery(); this.placeHolderValues = queryCreator.getPlaceHolderValues(); if (partTree.isLimiting()) { return selectFromWhereOrderBy.limit(partTree.getMaxResults()); } else { return selectFromWhereOrderBy; } } }
@Override protected Statement getStatement(ParameterAccessor accessor, Object[] runtimeParameters, ReturnedType returnedType) { String bucketName = getCouchbaseOperations().getCouchbaseBucket().name(); Expression bucket = N1qlUtils.escapedBucket(bucketName); FromPath select; if (partTree.isCountProjection()) { select = select(count("*")); } else { select = N1qlUtils.createSelectClauseForEntity(bucketName, returnedType, this.getCouchbaseOperations().getConverter()); } WherePath selectFrom = select.from(bucket); N1qlQueryCreator queryCreator = new N1qlQueryCreator(partTree, accessor, selectFrom, getCouchbaseOperations().getConverter(), getQueryMethod()); LimitPath selectFromWhereOrderBy = queryCreator.createQuery(); this.placeHolderValues = queryCreator.getPlaceHolderValues(); if (partTree.isLimiting()) { return selectFromWhereOrderBy.limit(partTree.getMaxResults()); } else { return selectFromWhereOrderBy; } } }
select = N1qlUtils.createSelectClauseForEntity(bucketName, returnedType, this.getCouchbaseOperations().getConverter()); WherePath selectFrom = select.from(bucket); N1qlQueryCreator queryCreator = new N1qlQueryCreator(partTree, accessor, selectFrom, getCouchbaseOperations().getConverter(), getQueryMethod());
select = N1qlUtils.createSelectClauseForEntity(bucketName, returnedType, this.getCouchbaseOperations().getConverter()); WherePath selectFrom = select.from(bucket); N1qlQueryCreator queryCreator = new N1qlQueryCreator(partTree, accessor, selectFrom, getCouchbaseOperations().getConverter(), getQueryMethod());