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