+ ", Errors: " + queryResult.errors());
/** * 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; }
+ ", Errors: " + queryResult.errors());
/** * 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; }
/** * 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; }
/** * 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; }
/** * Validate query results. * * @param query * the query * @param result * the result */ private void validateQueryResults(String query, N1qlQueryResult result) { LOGGER.debug("Query output status: " + result.finalSuccess()); if (!result.finalSuccess()) { StringBuilder errorBuilder = new StringBuilder(); for (JsonObject obj : result.errors()) { errorBuilder.append(obj.toString()); errorBuilder.append("\n"); } errorBuilder.deleteCharAt(errorBuilder.length() - 1); String errors = errorBuilder.toString(); LOGGER.error(errors); throw new KunderaException("Not able to execute query/statement:" + query + ". More details : " + errors); } }
@Override public <T> List<T> findByN1QLProjection(N1qlQuery n1ql, Class<T> entityClass) { checkN1ql(); try { N1qlQueryResult queryResult = queryN1QL(n1ql); if (queryResult.finalSuccess()) { List<N1qlQueryRow> allRows = queryResult.allRows(); List<T> result = new ArrayList<T>(allRows.size()); for (N1qlQueryRow row : allRows) { JsonObject json = row.value(); T decoded = translationService.decodeFragment(json.toString(), entityClass); result.add(decoded); } return result; } else { StringBuilder message = new StringBuilder("Unable to execute query due to the following n1ql errors: "); for (JsonObject error : queryResult.errors()) { message.append('\n').append(error); } throw new CouchbaseQueryExecutionException(message.toString()); } } catch (TranscodingException e) { throw new CouchbaseQueryExecutionException("Unable to execute query", e); } }
private void createPrimaryIndex(Bucket bucket, CouchbaseBuildDefinition buildDefinition) { LOG.debug("Creating primary index in bucket '{}'", buildDefinition.getBucketName()); N1qlQueryResult result = bucket.query(N1qlQuery.simple( Index.createPrimaryIndex().on(bucket.name()) )); if (!result.finalSuccess()) { LOG.error("Failed to create primary index: {}", result.errors()); throw new EnvironmentBuilderException("Failed to create primary index for " + buildDefinition.getBucketName()); } LOG.debug("Primary index created in bucket '{}'", buildDefinition.getBucketName()); }
@Override public <T> List<T> findByN1QLProjection(N1qlQuery n1ql, Class<T> entityClass) { checkN1ql(); try { N1qlQueryResult queryResult = queryN1QL(n1ql); if (queryResult.finalSuccess()) { List<N1qlQueryRow> allRows = queryResult.allRows(); List<T> result = new ArrayList<T>(allRows.size()); for (N1qlQueryRow row : allRows) { JsonObject json = row.value(); T decoded = translationService.decodeFragment(json.toString(), entityClass); result.add(decoded); } return result; } else { StringBuilder message = new StringBuilder("Unable to execute query due to the following n1ql errors: "); for (JsonObject error : queryResult.errors()) { message.append('\n').append(error); } throw new CouchbaseQueryExecutionException(message.toString()); } } catch (TranscodingException e) { throw new CouchbaseQueryExecutionException("Unable to execute query", e); } }
/** * 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 { val theBucket = getBucket(); val statement = Select.select("*") .from(Expression.i(theBucket.name())) .where(Expression.x(usernameAttribute).eq('\'' + usernameValue + '\'')); LOGGER.debug("Running query [{}] on bucket [{}]", statement.toString(), theBucket.name()); val query = N1qlQuery.simple(statement); val result = theBucket.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; }
for (JsonObject error : queryResult.errors()) { message.append('\n').append(error);
for (JsonObject error : queryResult.errors()) { message.append('\n').append(error);
@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; }
@Override public N1qlQueryResult execute(String statement) throws TranslatorException { N1qlQueryResult result = this.bucket.query(N1qlQuery.simple(statement, N1qlParams.build().consistency(scanConsistency))); if (!result.finalSuccess()) { TranslatorException te = new TranslatorException(UTIL.gs("query_error", result.errors())); //$NON-NLS-1$ te.setCode(result.status()); throw te; } return result; }