@After public void clear() { if (getCouchbaseContainer().isIndex() && getCouchbaseContainer().isQuery() && getCouchbaseContainer().isPrimaryIndex()) { getBucket().query( N1qlQuery.simple(String.format("DELETE FROM `%s`", getBucket().name()), N1qlParams.build().consistency(ScanConsistency.STATEMENT_PLUS))); } else { getBucket().bucketManager().flush(); } }
/** * Performs the {@link #delete(String, String)} (String, String)} operation via N1QL ("DELETE"). * * If this option should be used, the "-p couchbase.kv=false" property must be set. * * @param docId the document ID. * @return The result of the operation. */ private Status deleteN1ql(final String docId) throws Exception { String deleteQuery = "DELETE FROM `" + bucketName + "` USE KEYS [$1]"; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( deleteQuery, JsonArray.from(docId), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) )); if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) { throw new DBException("Error while parsing N1QL Result. Query: " + deleteQuery + ", Errors: " + queryResult.errors()); } return Status.OK; }
protected AbstractN1qlQuery(Statement statement, N1qlParams params) { this.statement = (statement instanceof SerializableStatement) ? (SerializableStatement) statement : new RawStatement(statement.toString()); this.queryParameters = params == null ? N1qlParams.build() : params; }
@Override public Observable<AsyncN1qlQueryResult> query(N1qlQuery query, long timeout, TimeUnit timeUnit) { if (!query.params().hasServerSideTimeout()) { query.params().serverSideTimeout(timeout, timeUnit); } if (query.params().clientContextId() == null || query.params().clientContextId().isEmpty()) { query.params().withContextId(UUID.randomUUID().toString()); } return n1qlQueryExecutor.execute(query, environment, timeout, timeUnit); }
/** * Start building a {@link N1qlParams}, allowing to customize an N1QL request. * * @return a new {@link N1qlParams} */ public static N1qlParams build() { return new N1qlParams(); }
/** * Creates the core query request and performs centralized string substitution. */ private GenericQueryRequest createN1qlRequest(final N1qlQuery query, String bucket, String username, String password, InetAddress targetNode) { String rawQuery = query.n1ql().toString(); rawQuery = rawQuery.replaceAll( CouchbaseAsyncBucket.CURRENT_BUCKET_IDENTIFIER, "`" + bucket + "`" ); if (targetNode != null) { return GenericQueryRequest.jsonQuery(rawQuery, bucket, username, password, targetNode, query.params().clientContextId()); } else { return GenericQueryRequest.jsonQuery(rawQuery, bucket, username, password, query.params().clientContextId()); } }
/** * Execute native query. * * @param n1qlQuery * the n1ql query * @param em * the entity manager * @return the list */ public List executeNativeQuery(String n1qlQuery, EntityMetadata em) { N1qlQueryResult result = bucket .query(N1qlQuery.simple(n1qlQuery, N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS))); LOGGER.debug("Executed query : " + n1qlQuery + " on the " + bucket.name() + " Bucket"); validateQueryResults(n1qlQuery, result); return result.allRows(); }
/** * Performs the {@link #insert(String, String, Map)} operation via N1QL ("INSERT"). * * If this option should be used, the "-p couchbase.kv=false" property must be set. * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status insertN1ql(final String docId, final Map<String, ByteIterator> values) throws Exception { String insertQuery = "INSERT INTO `" + bucketName + "`(KEY,VALUE) VALUES ($1,$2)"; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( insertQuery, JsonArray.from(docId, valuesToJsonObject(values)), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) )); if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) { throw new DBException("Error while parsing N1QL Result. Query: " + insertQuery + ", Errors: " + queryResult.errors()); } return Status.OK; }
/** * Execute query. * * @param stmt * the statement * @param em * the entity manager * @return the list */ public List executeQuery(Statement stmt, EntityMetadata em) { N1qlQuery query = N1qlQuery.simple(stmt, N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS)); N1qlQueryResult list = bucket.query(query); LOGGER.debug("Executed query : " + query.toString() + " on the " + bucket.name() + " Bucket"); validateQueryResults(stmt.toString(), list); List records = new ArrayList<>(); for (N1qlQueryRow row : list) { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(em.getPersistenceUnit()); EntityType entityType = metaModel.entity(em.getEntityClazz()); JsonObject jsonObj = row.value().containsKey(em.getSchema()) ? row.value().getObject(em.getSchema()) : row.value(); records.add(handler.getEntityFromDocument(em.getEntityClazz(), jsonObj, entityType)); } return records; }
/** * Performs the {@link #upsert(String, String, Map)} operation via N1QL ("UPSERT"). * * If this option should be used, the "-p couchbase.upsert=true -p couchbase.kv=false" properties must be set. * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status upsertN1ql(final String docId, final Map<String, ByteIterator> values) throws Exception { String upsertQuery = "UPSERT INTO `" + bucketName + "`(KEY,VALUE) VALUES ($1,$2)"; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( upsertQuery, JsonArray.from(docId, valuesToJsonObject(values)), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) )); if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) { throw new DBException("Error while parsing N1QL Result. Query: " + upsertQuery + ", Errors: " + queryResult.errors()); } return Status.OK; }
public static boolean fillDB(CouchbaseCluster cluster) { Bucket couchbaseBucket = cluster.openBucket(BUCKET_NAME); couchbaseBucket.insert(JsonDocument.create("artist:vincent_van_gogh", VINCENT_VAN_GOGH)); N1qlQueryResult queryResult = couchbaseBucket.query(N1qlQuery.simple(String.format(QUERY, BUCKET_NAME), N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS))); couchbaseBucket.close(); return queryResult.info().resultCount() == 1; }
scanAllQuery, JsonArray.from(formatId(table, startkey), recordcount), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) )) .doOnNext(new Action1<AsyncN1qlQueryResult>() {
protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) { N1qlParams n1qlParams = N1qlParams.build().consistency(scanConsistency); N1qlQuery query; if (queryPlaceholderValues instanceof JsonObject && !((JsonObject) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonObject) queryPlaceholderValues, n1qlParams); } else if (queryPlaceholderValues instanceof JsonArray && !((JsonArray) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonArray) queryPlaceholderValues, n1qlParams); } else { query = N1qlQuery.simple(statement, n1qlParams); } return query; }
/** * Performs the {@link #update(String, String, Map)} operation via N1QL ("UPDATE"). * * If this option should be used, the "-p couchbase.kv=false" property must be set. * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status updateN1ql(final String docId, final Map<String, ByteIterator> values) throws Exception { String fields = encodeN1qlFields(values); String updateQuery = "UPDATE `" + bucketName + "` USE KEYS [$1] SET " + fields; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( updateQuery, JsonArray.from(docId), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) )); if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) { throw new DBException("Error while parsing N1QL Result. Query: " + updateQuery + ", Errors: " + queryResult.errors()); } return Status.OK; }
protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) { N1qlParams n1qlParams = N1qlParams.build().consistency(scanConsistency); N1qlQuery query; if (queryPlaceholderValues instanceof JsonObject && !((JsonObject) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonObject) queryPlaceholderValues, n1qlParams); } else if (queryPlaceholderValues instanceof JsonArray && !((JsonArray) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonArray) queryPlaceholderValues, n1qlParams); } else { query = N1qlQuery.simple(statement, n1qlParams); } return query; }
scanSpecQuery, JsonArray.from(formatId(table, startkey), recordcount), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) ));
/** * Creates N1QLQuery object from the statement, query placeholder values and scan consistency * * @param statement * @param queryPlaceholderValues * @param scanConsistency * @return */ public static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) { N1qlParams n1qlParams = N1qlParams.build().consistency(scanConsistency); N1qlQuery query; if (queryPlaceholderValues instanceof JsonObject && !((JsonObject) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonObject) queryPlaceholderValues, n1qlParams); } else if (queryPlaceholderValues instanceof JsonArray && !((JsonArray) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonArray) queryPlaceholderValues, n1qlParams); } else { query = N1qlQuery.simple(statement, n1qlParams); } return query; }
readQuery, JsonArray.from(docId), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism) ));
/** * Creates N1QLQuery object from the statement, query placeholder values and scan consistency * * @param statement * @param queryPlaceholderValues * @param scanConsistency * @return */ public static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) { N1qlParams n1qlParams = N1qlParams.build().consistency(scanConsistency); N1qlQuery query; if (queryPlaceholderValues instanceof JsonObject && !((JsonObject) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonObject) queryPlaceholderValues, n1qlParams); } else if (queryPlaceholderValues instanceof JsonArray && !((JsonArray) queryPlaceholderValues).isEmpty()) { query = N1qlQuery.parameterized(statement, (JsonArray) queryPlaceholderValues, n1qlParams); } else { query = N1qlQuery.simple(statement, n1qlParams); } return query; }
@Override public N1qlQueryResult execute(String statement) throws ResourceException { N1qlQueryResult result = this.bucket.query(N1qlQuery.simple(statement, N1qlParams.build().consistency(scanConsistency))); if (!result.finalSuccess()) { throw new ResourceException(UTIL.gs("query_error", result.errors()), result.status()); //$NON-NLS-1$ } return result; }