Search in sources :

Example 1 with SimpleN1qlQuery

use of com.couchbase.client.java.query.SimpleN1qlQuery 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)

Aggregations

JsonObject (com.couchbase.client.java.document.json.JsonObject)1 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)1 SimpleN1qlQuery (com.couchbase.client.java.query.SimpleN1qlQuery)1 Statement (com.couchbase.client.java.query.Statement)1 GeneralSecurityException (java.security.GeneralSecurityException)1