Search in sources :

Example 76 with JsonObject

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

the class JavaIntegrationTests method waitForQueryIndexerToHaveBucket.

protected static void waitForQueryIndexerToHaveBucket(final Cluster cluster, final String bucketName) {
    boolean ready = false;
    int guard = 100;
    while (!ready && guard != 0) {
        guard -= 1;
        String statement = "SELECT COUNT(*) > 0 as present FROM system:keyspaces where name = '" + bucketName + "';";
        QueryResult queryResult = cluster.query(statement);
        List<JsonObject> rows = queryResult.rowsAsObject();
        if (rows.size() == 1 && rows.get(0).getBoolean("present")) {
            ready = true;
        }
        if (!ready) {
            // need to do this because of https://issues.couchbase.com/browse/MB-50132
            createAndDeleteBucket();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
            }
        }
    }
    if (guard == 0) {
        throw new IllegalStateException("Query indexer is still not aware of bucket " + bucketName);
    }
}
Also used : QueryResult(com.couchbase.client.java.query.QueryResult) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 77 with JsonObject

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

the class OptionsBuilder method getQueryOpts.

private static JsonObject getQueryOpts(QueryOptions.Built optsBuilt) {
    JsonObject jo = JsonObject.create();
    optsBuilt.injectParams(jo);
    return jo;
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject)

Example 78 with JsonObject

use of com.couchbase.client.java.json.JsonObject 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 79 with JsonObject

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

the class N1qlQueryCreatorTests method queryParametersList.

@Test
void queryParametersList() throws Exception {
    String input = "findByFirstnameIn";
    PartTree tree = new PartTree(input, User.class);
    Method method = UserRepository.class.getMethod(input, String[].class);
    QueryMethod queryMethod = new QueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory());
    List<String> list = new LinkedList<>();
    list.add("Oliver");
    list.add("Charles");
    N1qlQueryCreator creator = new N1qlQueryCreator(tree, getAccessor(getParameters(method), new Object[] { list }), queryMethod, converter, bucketName);
    Query query = creator.createQuery();
    Query expected = (new Query()).addCriteria(where(i("firstname")).in("Oliver", "Charles"));
    assertEquals(" WHERE `firstname` in $1", query.export(new int[1]));
    JsonObject expectedOptions = JsonObject.create();
    expected.buildQueryOptions(null, null).build().injectParams(expectedOptions);
    JsonObject actualOptions = JsonObject.create();
    expected.buildQueryOptions(null, null).build().injectParams(actualOptions);
    assertEquals(expectedOptions.removeKey("client_context_id"), actualOptions.removeKey("client_context_id"));
}
Also used : Query(org.springframework.data.couchbase.core.query.Query) QueryMethod(org.springframework.data.repository.query.QueryMethod) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) JsonObject(com.couchbase.client.java.json.JsonObject) QueryMethod(org.springframework.data.repository.query.QueryMethod) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) UserRepository(org.springframework.data.couchbase.domain.UserRepository) JsonObject(com.couchbase.client.java.json.JsonObject) PartTree(org.springframework.data.repository.query.parser.PartTree) Test(org.junit.jupiter.api.Test)

Example 80 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class TestData method createJson.

public JsonObject createJson(String id) {
    JsonObject js = JsonObject.create();
    js.put("t_string", id);
    js.put("t_int_min", getColIntMin());
    js.put("t_int_max", getColIntMax());
    js.put("t_long_min", getColLongMin());
    js.put("t_long_max", getColLongMax());
    js.put("t_float_min", getColFloatMin());
    js.put("t_float_max", getColFloatMax());
    js.put("t_double_min", getColDoubleMin());
    js.put("t_double_max", getColDoubleMax());
    js.put("t_boolean", isColBoolean());
    js.put("t_datetime", getColDateTime().toString());
    js.put("t_array", getColList());
    return js;
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject)

Aggregations

JsonObject (com.couchbase.client.java.json.JsonObject)189 Test (org.junit.jupiter.api.Test)145 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)119 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)39 JsonArray (com.couchbase.client.java.json.JsonArray)18 QueryResult (com.couchbase.client.java.query.QueryResult)16 GetResult (com.couchbase.client.java.kv.GetResult)15 MutationResult (com.couchbase.client.java.kv.MutationResult)15 QueryOptions (com.couchbase.client.java.query.QueryOptions)15 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)15 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)6 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)6 Collection (com.couchbase.client.java.Collection)6 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)6 Duration (java.time.Duration)6 DisplayName (org.junit.jupiter.api.DisplayName)6 CouchbaseUtilTest (org.talend.components.couchbase.CouchbaseUtilTest)6 ArrayList (java.util.ArrayList)5 TestData (org.talend.components.couchbase.TestData)5 MutateInResult (com.couchbase.client.java.kv.MutateInResult)4