Search in sources :

Example 1 with Statement

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;
}
Also used : SimpleN1qlQuery(com.couchbase.client.java.query.SimpleN1qlQuery) Statement(com.couchbase.client.java.query.Statement) GeneralSecurityException(java.security.GeneralSecurityException) JsonObject(com.couchbase.client.java.document.json.JsonObject) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult)

Example 2 with Statement

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());
}
Also used : Bucket(com.couchbase.client.java.Bucket) Statement(com.couchbase.client.java.query.Statement) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 3 with Statement

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());
}
Also used : Bucket(com.couchbase.client.java.Bucket) Statement(com.couchbase.client.java.query.Statement) N1qlQueryResult(com.couchbase.client.java.query.N1qlQueryResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 4 with Statement

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);
}
Also used : Statement(com.couchbase.client.java.query.Statement) ArrayList(java.util.ArrayList) JsonObject(com.couchbase.client.java.document.json.JsonObject) Expression(com.couchbase.client.java.query.dsl.Expression) Sort(org.jnosql.diana.api.Sort)

Example 5 with Statement

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());
}
Also used : DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Statement(com.couchbase.client.java.query.Statement) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Statement (com.couchbase.client.java.query.Statement)7 JsonObject (com.couchbase.client.java.document.json.JsonObject)3 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)3 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)3 Test (org.junit.jupiter.api.Test)3 Bucket (com.couchbase.client.java.Bucket)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Test (org.junit.Test)2 SimpleN1qlQuery (com.couchbase.client.java.query.SimpleN1qlQuery)1 Expression (com.couchbase.client.java.query.dsl.Expression)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Sort (org.jnosql.diana.api.Sort)1