Search in sources :

Example 1 with N1QLExpression

use of org.springframework.data.couchbase.core.query.N1QLExpression in project spring-data-couchbase by spring-projects.

the class N1qlUtils method createSort.

/**
 * Create a N1QL {@link N1QLExpression} out of a Spring Data {@link Sort}. Note that the later must use alternative
 * field names as declared by the {@link Field} annotation on the entity, if any.
 */
public static N1QLExpression[] createSort(Sort sort) {
    List<N1QLExpression> cbSortList = new ArrayList<>();
    for (Sort.Order order : sort) {
        String orderProperty = order.getProperty();
        // FIXME the order property should be converted to its corresponding fieldName
        String[] orderPropertyParts = orderProperty.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (String part : orderPropertyParts) {
            if (sb.length() != 0) {
                sb.append(".");
            }
            sb.append(i(part).toString());
        }
        N1QLExpression orderFieldName = x(sb.toString());
        if (order.isIgnoreCase()) {
            orderFieldName = orderFieldName.convertToString().lower();
        }
        if (order.isAscending()) {
            cbSortList.add(orderFieldName.asc());
        } else {
            cbSortList.add(orderFieldName.desc());
        }
    }
    return cbSortList.toArray(new N1QLExpression[cbSortList.size()]);
}
Also used : ArrayList(java.util.ArrayList) N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression) Sort(org.springframework.data.domain.Sort)

Example 2 with N1QLExpression

use of org.springframework.data.couchbase.core.query.N1QLExpression in project spring-data-couchbase by spring-projects.

the class N1qlUtils method createReturningExpressionForDelete.

/**
 * 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 N1QLExpression createReturningExpressionForDelete(String bucketName) {
    N1QLExpression fullEntity = path(i(bucketName), "*");
    N1QLExpression metaId = path(meta(i(bucketName)), "id").as(x(SELECT_ID));
    N1QLExpression metaCas = path(meta(i(bucketName)), "cas").as(x(SELECT_CAS));
    List<N1QLExpression> expList = new ArrayList<>();
    expList.add(fullEntity);
    expList.add(metaId);
    expList.add(metaCas);
    StringBuilder sb = new StringBuilder();
    for (N1QLExpression exp : expList) {
        if (sb.length() != 0) {
            sb.append(", ");
        }
        sb.append(exp.toString());
    }
    return x(sb.toString());
}
Also used : ArrayList(java.util.ArrayList) N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

Example 3 with N1QLExpression

use of org.springframework.data.couchbase.core.query.N1QLExpression in project spring-data-couchbase by spring-projects.

the class N1qlUtils method createWhereFilterForEntity.

/**
 * Produces an {@link 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 {@link EntityMetadata} parameter).
 *
 * @param baseWhereCriteria the other criteria of the WHERE clause, or null if none.
 * @param converter the {@link CouchbaseConverter} giving the attribute storing the type information can be extracted.
 * @param entityInformation the expected type information.
 * @return an {@link N1QLExpression} to be used as a WHERE clause, that additionally restricts on the given type.
 */
public static N1QLExpression createWhereFilterForEntity(N1QLExpression baseWhereCriteria, CouchbaseConverter converter, EntityMetadata<?> entityInformation) {
    // add part that filters on type key
    String typeKey = converter.getTypeKey();
    String typeValue = entityInformation.getJavaType().getName();
    N1QLExpression typeSelector = i(typeKey).eq(s(typeValue));
    if (baseWhereCriteria == null) {
        baseWhereCriteria = typeSelector;
    } else {
        baseWhereCriteria = x("(" + baseWhereCriteria.toString() + ")").and(typeSelector);
    }
    return baseWhereCriteria;
}
Also used : N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

Example 4 with N1QLExpression

use of org.springframework.data.couchbase.core.query.N1QLExpression in project spring-data-couchbase by spring-projects.

the class PartTreeN1qlBasedQuery method getExpression.

@Override
protected N1QLExpression getExpression(ParameterAccessor accessor, Object[] runtimeParameters, ReturnedType returnedType) {
    String bucketName = getCouchbaseOperations().getBucketName();
    N1QLExpression bucket = N1qlUtils.escapedBucket(bucketName);
    if (partTree.isDelete()) {
        N1QLExpression deleteUsePath = delete().from(bucket);
        N1qlMutateQueryCreator mutateQueryCreator = new N1qlMutateQueryCreator(partTree, accessor, deleteUsePath, getCouchbaseOperations().getConverter(), getQueryMethod());
        N1QLExpression mutateFromWhereOrderBy = mutateQueryCreator.createQuery();
        this.placeHolderValues = mutateQueryCreator.getPlaceHolderValues();
        if (partTree.isLimiting()) {
            return mutateFromWhereOrderBy.limit(partTree.getMaxResults());
        } else {
            return mutateFromWhereOrderBy.returning(createReturningExpressionForDelete(bucketName));
        }
    } else {
        N1QLExpression select;
        if (partTree.isCountProjection()) {
            select = select(count(x("*")));
        } else {
            select = N1qlUtils.createSelectClauseForEntity(bucketName, returnedType, this.getCouchbaseOperations().getConverter());
        }
        N1QLExpression selectFrom = select.from(bucket);
        OldN1qlQueryCreator queryCreator = new OldN1qlQueryCreator(partTree, accessor, selectFrom, getCouchbaseOperations().getConverter(), getQueryMethod());
        N1QLExpression selectFromWhereOrderBy = queryCreator.createQuery();
        this.placeHolderValues = queryCreator.getPlaceHolderValues();
        if (queryMethod.isPageQuery()) {
            Pageable pageable = accessor.getPageable();
            Assert.notNull(pageable, "Pageable must not be null!");
            return selectFromWhereOrderBy.limit(pageable.getPageSize()).offset(Math.toIntExact(pageable.getOffset()));
        } else if (queryMethod.isSliceQuery() && accessor.getPageable().isPaged()) {
            Pageable pageable = accessor.getPageable();
            Assert.notNull(pageable, "Pageable must not be null!");
            return selectFromWhereOrderBy.limit(pageable.getPageSize() + 1).offset(Math.toIntExact(pageable.getOffset()));
        } else if (partTree.isLimiting()) {
            return selectFromWhereOrderBy.limit(partTree.getMaxResults());
        } else {
            return selectFromWhereOrderBy;
        }
    }
}
Also used : Pageable(org.springframework.data.domain.Pageable) N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

Example 5 with N1QLExpression

use of org.springframework.data.couchbase.core.query.N1QLExpression in project spring-data-couchbase by spring-projects.

the class AbstractN1qlBasedQuery method execute.

@Override
public Object execute(Object[] parameters) {
    ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
    ResultProcessor processor = this.queryMethod.getResultProcessor().withDynamicProjection(accessor);
    ReturnedType returnedType = processor.getReturnedType();
    // TODO: review this - I just hacked it to work, basically...
    // This was what was here in sdk2, but seem to end up being always Object. Forcing
    // it to be the same as the object type for the repo.
    // Class<?> typeToRead = returnedType.getTypeToRead();
    // typeToRead = typeToRead == null ? returnedType.getDomainType() : typeToRead;
    Class<?> typeToRead = queryMethod.getEntityInformation().getJavaType();
    N1QLExpression statement = getExpression(accessor, parameters, returnedType);
    JsonValue queryPlaceholderValues = getPlaceholderValues(accessor);
    // prepare the final query
    N1QLQuery query = buildQuery(statement, queryPlaceholderValues, getScanConsistency());
    // prepare a count query
    N1QLExpression countStatement = getCount(accessor, parameters);
    // the place holder values are the same for the count query as well
    N1QLQuery countQuery = buildQuery(countStatement, queryPlaceholderValues, getScanConsistency());
    return processor.processResult(executeDependingOnType(query, countQuery, queryMethod, accessor.getPageable(), typeToRead));
}
Also used : N1QLQuery(org.springframework.data.couchbase.core.query.N1QLQuery) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) JsonValue(com.couchbase.client.java.json.JsonValue) N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression) ResultProcessor(org.springframework.data.repository.query.ResultProcessor) ReturnedType(org.springframework.data.repository.query.ReturnedType)

Aggregations

N1QLExpression (org.springframework.data.couchbase.core.query.N1QLExpression)13 ArrayList (java.util.ArrayList)3 JsonValue (com.couchbase.client.java.json.JsonValue)2 N1QLQuery (org.springframework.data.couchbase.core.query.N1QLQuery)2 Pageable (org.springframework.data.domain.Pageable)2 ResultProcessor (org.springframework.data.repository.query.ResultProcessor)2 ReturnedType (org.springframework.data.repository.query.ReturnedType)2 JsonObject (com.couchbase.client.java.json.JsonObject)1 Sort (org.springframework.data.domain.Sort)1 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)1 EvaluationContext (org.springframework.expression.EvaluationContext)1