final List<HashMap<String, ByteIterator>> data = new ArrayList<HashMap<String, ByteIterator>>(recordcount); bucket.async() .query(N1qlQuery.parameterized( scanAllQuery, JsonArray.from(formatId(table, startkey), recordcount),
String scanSpecQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` WHERE meta().id >= '$1' LIMIT $2"; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( scanSpecQuery, JsonArray.from(formatId(table, startkey), recordcount),
throws Exception { String readQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` USE KEYS [$1]"; N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized( readQuery, JsonArray.from(docId),
/** * 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; }
/** * 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 #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; }
/** * 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; }
/** * Executes a N1QL kernelTransaction with positional parameters. * * @param statement the raw kernelTransaction string to execute (containing positional * placeholders: $1, $2, ...) * @param parameters the values for the positional placeholders in kernelTransaction * @return the list of {@link JsonObject}s retrieved by this query * @see N1qlQuery#parameterized(Statement, JsonArray) */ public List<JsonObject> executeParametrizedStatement(String statement, List<Object> parameters) { JsonArray positionalParams = JsonArray.from(parameters); ParameterizedN1qlQuery query = N1qlQuery.parameterized(statement, positionalParams); return executeQuery(query); }
/** * Executes a N1QL kernelTransaction with named parameters. * * @param statement the raw kernelTransaction string to execute (containing named * placeholders: $param1, $param2, ...) * @param parameterNames the placeholders' names in kernelTransaction * @param parameterValues the values for the named placeholders in kernelTransaction * @return the list of {@link JsonObject}s retrieved by this query * @see N1qlQuery#parameterized(Statement, JsonObject) */ public List<JsonObject> executeParametrizedStatement(String statement, List<String> parameterNames, List<Object> parameterValues) { JsonObject namedParams = JsonObject.create(); for (int param = 0; param < parameterNames.size(); param++) { namedParams.put(parameterNames.get(param), parameterValues.get(param)); } ParameterizedN1qlQuery query = N1qlQuery.parameterized(statement, namedParams); return executeQuery(query); }
@Override public List<DocumentEntity> n1qlQuery(Statement n1qlQuery, JsonObject params) throws NullPointerException { requireNonNull(n1qlQuery, "n1qlQuery is required"); requireNonNull(params, "params is required"); N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(n1qlQuery, params)); return convert(result, database); }
@Override public List<DocumentEntity> n1qlQuery(String n1qlQuery, JsonObject params) throws NullPointerException { requireNonNull(n1qlQuery, "n1qlQuery is required"); requireNonNull(params, "params is required"); N1qlQueryResult result = bucket.query(N1qlQuery.parameterized(n1qlQuery, params)); return convert(result, database); }
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; }
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; }
/** * 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; }
/** * 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 List<DocumentEntity> select(DocumentQuery query) throws NullPointerException { QueryConverter.QueryConverterResult select = QueryConverter.select(query, database); List<DocumentEntity> entities = new ArrayList<>(); if (nonNull(select.getStatement())) { ParameterizedN1qlQuery n1qlQuery = N1qlQuery.parameterized(select.getStatement(), select.getParams()); N1qlQueryResult result = bucket.query(n1qlQuery); entities.addAll(convert(result, database)); } if (!select.getKeys().isEmpty()) { entities.addAll(convert(select.getKeys(), bucket)); } return entities; }
@Override public void delete(DocumentDeleteQuery query) { QueryConverter.QueryConverterResult delete = QueryConverter.delete(query, database); if (nonNull(delete.getStatement())) { ParameterizedN1qlQuery n1qlQuery = N1qlQuery.parameterized(delete.getStatement(), delete.getParams()); bucket.query(n1qlQuery); } if (!delete.getKeys().isEmpty()) { delete.getKeys() .stream() .map(s -> getPrefix(query.getDocumentCollection(), s)) .forEach(bucket::remove); } }