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);
}
}
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;
}
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();
}
}
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"));
}
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;
}
Aggregations