Search in sources :

Example 16 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class GetProjectionIntegrationTest method animals_0.

@Test
void animals_0() {
    JsonObject decoded = collection.get(DOC_ID, getOptions().project("animals[0]")).contentAsObject();
    JsonArray arr = decoded.getArray("animals");
    assertEquals(1, arr.size());
    assertEquals("cat", arr.get(0));
    assertEquals(1, decoded.size());
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 17 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class GetProjectionIntegrationTest method animals.

@Test
void animals() {
    JsonObject decoded = collection.get(DOC_ID, getOptions().project("animals")).contentAsObject();
    JsonArray arr = decoded.getArray("animals");
    assertEquals(3, arr.size());
    assertEquals(1, decoded.size());
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) JsonObject(com.couchbase.client.java.json.JsonObject) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 18 with JsonArray

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

the class StringBasedN1qlQueryParser method getPositionalPlaceholderValues.

/**
 * Get Postional argument placeholders to use for parameters. $1, $2 etc.
 *
 * @param accessor
 * @return - JsonValue holding parameters.
 */
private JsonValue getPositionalPlaceholderValues(ParameterAccessor accessor) {
    JsonArray posValues = JsonArray.create();
    for (Parameter parameter : this.queryMethod.getParameters().getBindableParameters()) {
        Object rawValue = accessor.getBindableValue(parameter.getIndex());
        Object value = couchbaseConverter.convertForWriteIfNeeded(rawValue);
        putPositionalValue(posValues, value);
    }
    return posValues;
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Parameter(org.springframework.data.repository.query.Parameter) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 19 with JsonArray

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

the class QueryCriteria method maybeWrapValue.

/**
 * Possibly convert an operand to a positional or named parameter
 *
 * @param paramIndexPtr - this is a reference to the parameter index to be used for positional parameters There may
 *          already be positional parameters in the beginning of the statement, so it may not always start at 1. If it
 *          has the value -1, the query is using named parameters. If the pointer is null, the query is not using
 *          parameters.
 * @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
 * @return string containing part of N1QL query
 */
private String maybeWrapValue(N1QLExpression key, Object value, int[] paramIndexPtr, JsonValue parameters, CouchbaseConverter converter) {
    if (paramIndexPtr != null) {
        if (paramIndexPtr[0] >= 0) {
            JsonArray params = (JsonArray) parameters;
            if (value instanceof Object[] || value instanceof Collection) {
                addAsCollection(params, asCollection(value), converter);
            } else {
                params.add(convert(converter, value));
            }
            // these are generated in order
            return "$" + (++paramIndexPtr[0]);
        } else {
            JsonObject params = (JsonObject) parameters;
            // from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
            try {
                params.put(key.toString(), convert(converter, value));
            } catch (InvalidArgumentException iae) {
                if (value instanceof Object[]) {
                    params.put(key.toString(), JsonArray.from((Object[]) value));
                } else {
                    throw iae;
                }
            }
            return "$" + key;
        }
    }
    if (value instanceof String) {
        return "\"" + value + "\"";
    } else if (value == null) {
        return "null";
    } else if (value instanceof Object[]) {
        // convert array into sequence of comma-separated values
        StringBuilder l = new StringBuilder();
        l.append("[");
        Object[] array = (Object[]) value;
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                l.append(",");
            }
            l.append(maybeWrapValue(null, array[i], null, null, converter));
        }
        l.append("]");
        return l.toString();
    } else {
        return value.toString();
    }
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Collection(java.util.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 20 with JsonArray

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

Aggregations

JsonArray (com.couchbase.client.java.json.JsonArray)48 Test (org.junit.jupiter.api.Test)26 JsonObject (com.couchbase.client.java.json.JsonObject)24 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)3 List (java.util.List)3 Airport (org.springframework.data.couchbase.domain.Airport)3 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)2 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)2 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)2 JsonValue (com.couchbase.client.java.json.JsonValue)2 GetResult (com.couchbase.client.java.kv.GetResult)2 QueryOptions (com.couchbase.client.java.query.QueryOptions)2 SearchQuery (com.couchbase.client.java.search.SearchQuery)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Query (org.springframework.data.couchbase.core.query.Query)2 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)2