use of com.couchbase.client.java.query.Statement in project cas by apereo.
the class CouchbaseClientFactory method query.
/**
* Query and get a result by username.
*
* @param usernameAttribute the username attribute
* @param usernameValue the username value
* @return the n1ql query result
* @throws GeneralSecurityException the general security exception
*/
public N1qlQueryResult query(final String usernameAttribute, final String usernameValue) throws GeneralSecurityException {
final Statement statement = Select.select("*").from(Expression.i(getBucket().name())).where(Expression.x(usernameAttribute).eq('\'' + usernameValue + '\''));
LOGGER.debug("Running query [{}] on bucket [{}]", statement.toString(), getBucket().name());
final SimpleN1qlQuery query = N1qlQuery.simple(statement);
final N1qlQueryResult result = getBucket().query(query, timeout, TimeUnit.MILLISECONDS);
if (!result.finalSuccess()) {
LOGGER.error("Couchbase query failed with [{}]", result.errors().stream().map(JsonObject::toString).collect(Collectors.joining(",")));
throw new GeneralSecurityException("Could not locate account for user " + usernameValue);
}
return result;
}
use of com.couchbase.client.java.query.Statement in project tutorials by eugenp.
the class N1QLIntegrationTest method givenSelectDSL_whenQuery_thenResult.
@Test
public void givenSelectDSL_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
Statement statement = select("*").from(i("travel-sample")).where(x("type").eq(s("airport"))).limit(100);
N1qlQueryResult r3 = bucket.query(N1qlQuery.simple(statement));
List<JsonNode> list2 = extractJsonResult(r3);
System.out.println("First Airport Name: " + list2.get(0).get("travel-sample").get("airportname").asText());
}
use of com.couchbase.client.java.query.Statement in project tutorials by eugenp.
the class N1QLIntegrationTest method givenSelectStatementWithDSL2_whenQuery_thenResult.
@Test
public void givenSelectStatementWithDSL2_whenQuery_thenResult() {
Bucket bucket = bucketFactory.getTravelSampleBucket();
Statement st2 = select(x("t.city, t.airportname").concat(s(" (")).concat(x("t.faa")).concat(s(")")).as("portname_faa")).from(i("travel-sample").as("t")).where(x("t.type").eq(s("airport")).and(x("t.country").like(s("%States"))).and(x("t.geo.lat").gte(70))).limit(2);
N1qlQueryResult r5 = bucket.query(N1qlQuery.simple(st2));
List<JsonNode> list5 = extractJsonResult(r5);
System.out.println("First Doc : " + list5.get(0));
System.out.println("Query from Statement2: " + st2.toString());
}
use of com.couchbase.client.java.query.Statement in project jnosql-diana-driver by eclipse.
the class QueryConverter method select.
static QueryConverterResult select(DocumentQuery query, String bucket) {
JsonObject params = JsonObject.create();
List<String> keys = new ArrayList<>();
String[] documents = query.getDocuments().stream().toArray(String[]::new);
if (documents.length == 0) {
documents = ALL_SELECT;
}
Statement statement = null;
int firstResult = (int) query.getFirstResult();
int maxResult = (int) query.getMaxResults();
com.couchbase.client.java.query.dsl.Sort[] sorts = query.getSorts().stream().map(SORT_MAP).toArray(com.couchbase.client.java.query.dsl.Sort[]::new);
if (query.getCondition().isPresent()) {
Expression condition = getCondition(query.getCondition().get(), params, keys, query.getDocumentCollection());
if (nonNull(condition)) {
statement = create(bucket, documents, firstResult, maxResult, sorts, condition);
} else {
statement = null;
}
} else {
statement = create(bucket, documents, firstResult, maxResult, sorts);
}
return new QueryConverterResult(params, statement, keys);
}
use of com.couchbase.client.java.query.Statement in project jnosql-diana-driver by eclipse.
the class CouchbaseDocumentCollectionManagerAsyncTest method shouldRunN1QlStatement.
@Test
public void shouldRunN1QlStatement() {
AtomicReference<List<DocumentEntity>> references = new AtomicReference<>();
DocumentEntity entity = getEntity();
entityManager.insert(entity);
Statement statement = Select.select("*").from("jnosql").where(x("name").eq("\"Poliana\""));
entityManagerAsync.n1qlQuery(statement, references::set);
assertFalse(references.get().isEmpty());
assertEquals(1, references.get().size());
}
Aggregations