Search in sources :

Example 1 with JsonValue

use of com.couchbase.client.java.json.JsonValue in project spring-data-couchbase by spring-projects.

the class StringQuery method toN1qlSelectString.

@Override
public String toN1qlSelectString(ReactiveCouchbaseTemplate template, String collection, Class domainClass, Class resultClass, boolean isCount, String[] distinctFields, String[] fields) {
    final StringBuilder statement = new StringBuilder();
    boolean makeCount = isCount && inlineN1qlQuery != null && !inlineN1qlQuery.toLowerCase(Locale.ROOT).contains("count(");
    if (makeCount) {
        statement.append("SELECT COUNT(*) AS " + TemplateUtils.SELECT_COUNT + " FROM (");
    }
    // apply the string statement
    appendInlineN1qlStatement(statement);
    // To use generated parameters for literals
    // we need to figure out if we must use positional or named parameters
    // If we are using positional parameters, we need to start where
    // the inlineN1ql left off.
    int[] paramIndexPtr = null;
    JsonValue params = this.getParameters();
    if (params instanceof JsonArray) {
        // positional parameters
        paramIndexPtr = new int[] { ((JsonArray) params).size() };
    } else {
        // named parameters or no parameters, no index required
        paramIndexPtr = new int[] { -1 };
    }
    // criteria on this Query - should be empty for
    appendWhere(statement, paramIndexPtr, template.getConverter());
    if (!isCount) {
        appendSort(statement);
        appendSkipAndLimit(statement);
    }
    if (makeCount) {
        statement.append(") predicate_query");
    }
    return statement.toString();
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonValue(com.couchbase.client.java.json.JsonValue)

Example 2 with JsonValue

use of com.couchbase.client.java.json.JsonValue 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)

Example 3 with JsonValue

use of com.couchbase.client.java.json.JsonValue in project spring-data-couchbase by spring-projects.

the class ReactiveAbstractN1qlBasedQuery method execute.

@Override
public Object execute(Object[] parameters) {
    ReactiveCouchbaseParameterAccessor accessor = new ReactiveCouchbaseParameterAccessor(queryMethod, parameters);
    ResultProcessor processor = this.queryMethod.getResultProcessor().withDynamicProjection(accessor);
    ReturnedType returnedType = processor.getReturnedType();
    Class<?> typeToRead = returnedType.getTypeToRead();
    typeToRead = typeToRead == null ? returnedType.getDomainType() : typeToRead;
    N1QLExpression expression = getExpression(accessor, parameters, returnedType);
    JsonValue queryPlaceholderValues = getPlaceholderValues(accessor);
    // prepare the final query
    N1QLQuery query = N1qlUtils.buildQuery(expression, queryPlaceholderValues, getScanConsistency());
    return ReactiveWrapperConverters.toWrapper(processor.processResult(executeDependingOnType(query, queryMethod, typeToRead)), Flux.class);
}
Also used : N1QLQuery(org.springframework.data.couchbase.core.query.N1QLQuery) 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)

Example 4 with JsonValue

use of com.couchbase.client.java.json.JsonValue in project spring-data-couchbase by spring-projects.

the class StringN1qlQueryCreator method complete.

@Override
protected Query complete(QueryCriteria criteria, Sort sort) {
    Query q = new StringQuery(parsedExpression.toString()).with(sort);
    JsonValue params = queryParser.getPlaceholderValues(accessor);
    if (params instanceof JsonArray) {
        q.setPositionalParameters((JsonArray) params);
    } else {
        q.setNamedParameters((JsonObject) params);
    }
    return q;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) StringQuery(org.springframework.data.couchbase.core.query.StringQuery) Query(org.springframework.data.couchbase.core.query.Query) JsonValue(com.couchbase.client.java.json.JsonValue) StringQuery(org.springframework.data.couchbase.core.query.StringQuery)

Aggregations

JsonValue (com.couchbase.client.java.json.JsonValue)4 JsonArray (com.couchbase.client.java.json.JsonArray)2 N1QLExpression (org.springframework.data.couchbase.core.query.N1QLExpression)2 N1QLQuery (org.springframework.data.couchbase.core.query.N1QLQuery)2 ResultProcessor (org.springframework.data.repository.query.ResultProcessor)2 ReturnedType (org.springframework.data.repository.query.ReturnedType)2 Query (org.springframework.data.couchbase.core.query.Query)1 StringQuery (org.springframework.data.couchbase.core.query.StringQuery)1 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)1