Search in sources :

Example 11 with N1QLExpression

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

the class N1qlQueryCreatorUtils method createExpression.

public static N1QLExpression createExpression(Part.Type partType, String fieldNamePath, boolean ignoreCase, Iterator<Object> parameterValues, AtomicInteger position, JsonArray placeHolderValues) {
    // create the left hand side of the expression, taking ignoreCase into account
    N1QLExpression left = ignoreCase ? (x(fieldNamePath).lower()) : x(fieldNamePath);
    N1QLExpression exp;
    switch(partType) {
        case BETWEEN:
            exp = left.between(getPlaceHolder(position, ignoreCase).and(getPlaceHolder(position, ignoreCase)));
            placeHolderValues.add(getValue(parameterValues));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case IS_NOT_NULL:
            exp = left.isNotNull();
            break;
        case IS_NULL:
            exp = left.isNull();
            break;
        case NEGATING_SIMPLE_PROPERTY:
            exp = left.ne(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case SIMPLE_PROPERTY:
            exp = left.eq(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case BEFORE:
        case LESS_THAN:
            exp = left.lt(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case LESS_THAN_EQUAL:
            exp = left.lte(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case GREATER_THAN_EQUAL:
            exp = left.gte(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case AFTER:
        case GREATER_THAN:
            exp = left.gt(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case NOT_LIKE:
            exp = left.notLike(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case LIKE:
            exp = left.like(getPlaceHolder(position, ignoreCase));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case STARTING_WITH:
            exp = left.like(x(getPlaceHolder(position, ignoreCase) + " || '%'"));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case ENDING_WITH:
            exp = left.like(x("'%' || " + getPlaceHolder(position, ignoreCase)));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case NOT_CONTAINING:
            exp = left.notLike(x("'%' || " + getPlaceHolder(position, ignoreCase) + " || '%'"));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case CONTAINING:
            exp = left.like(x("'%' || " + getPlaceHolder(position, ignoreCase) + " || '%'"));
            placeHolderValues.add(getValue(parameterValues));
            break;
        case NOT_IN:
            exp = left.notIn(getPlaceHolder(position, false));
            placeHolderValues.add(getArray(parameterValues));
            break;
        case IN:
            exp = left.in(getPlaceHolder(position, false));
            placeHolderValues.add(getArray(parameterValues));
            break;
        case TRUE:
            exp = left.eq(true);
            break;
        case FALSE:
            exp = left.eq(false);
            break;
        case REGEX:
            exp = x("REGEXP_LIKE(" + left.toString() + ", " + getPlaceHolder(position, false) + ")");
            placeHolderValues.add(getValueAsString(parameterValues));
            break;
        case EXISTS:
            exp = left.isNotMissing();
            break;
        case WITHIN:
        case NEAR:
        default:
            throw new IllegalArgumentException("Unsupported keyword in N1QL query derivation");
    }
    return exp;
}
Also used : N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

Example 12 with N1QLExpression

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

the class N1qlUtils method createSelectClauseForEntity.

/**
 * Produce a {@link 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.
 *
 * @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 N1QLExpression createSelectClauseForEntity(String bucketName, ReturnedType returnedType, CouchbaseConverter converter) {
    N1QLExpression bucket = escapedBucket(bucketName);
    N1QLExpression metaId = path(meta(bucket), "id").as(x(SELECT_ID));
    N1QLExpression metaCas = path(meta(bucket), "cas").as(x(SELECT_CAS));
    List<N1QLExpression> expList = new ArrayList<>();
    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, "*"));
    }
    N1QLExpression[] propertiesExp = new N1QLExpression[expList.size()];
    propertiesExp = expList.toArray(propertiesExp);
    return select(propertiesExp);
}
Also used : ArrayList(java.util.ArrayList) N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

Example 13 with N1QLExpression

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

the class ReactivePartTreeN1qlBasedQuery method getExpression.

@Override
protected N1QLExpression getExpression(ParameterAccessor accessor, Object[] runtimeParameters, ReturnedType returnedType) {
    String bucketName = getCouchbaseOperations().getBucketName();
    N1QLExpression bucket = N1qlUtils.escapedBucket(bucketName);
    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 (partTree.isLimiting()) {
        return selectFromWhereOrderBy.limit(partTree.getMaxResults());
    } else {
        return selectFromWhereOrderBy;
    }
}
Also used : N1QLExpression(org.springframework.data.couchbase.core.query.N1QLExpression)

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