feedResponse = AzureCosmosClient.client.queryDocuments(getDocumentCollectionLink(this.databaseName, table), new SqlQuerySpec(queryText, new SqlParameterCollection(new SqlParameter("@recordcount", recordcount), new SqlParameter("@startkey", startkey))), getFeedOptions(startkey));
private <T> SqlQuerySpec createSqlQuerySpec(Query query, Class<T> entityClass) { String queryStr = "SELECT * FROM ROOT r WHERE "; final SqlParameterCollection parameterCollection = new SqlParameterCollection(); for (final Map.Entry<String, Object> entry : query.getCriteria().entrySet()) { if (queryStr.contains("=@")) { queryStr += " AND "; } String fieldName = entry.getKey(); if (isIdField(fieldName, entityClass)) { fieldName = "id"; } queryStr += "r." + fieldName + "=@" + entry.getKey(); parameterCollection.add(new SqlParameter("@" + entry.getKey(), mappingDocumentDbConverter.mapToDocumentDBValue(entry.getValue()))); } return new SqlQuerySpec(queryStr, parameterCollection); }
/** * Generate SqlQuerySpec with given DocumentQuery and query head. * * @param query DocumentQuery represent one query method. * @param queryHead * @return The SqlQuerySpec for DocumentClient. */ protected SqlQuerySpec generateQuery(@NonNull DocumentQuery query, @NonNull String queryHead) { Assert.hasText(queryHead, "query head should have text."); final Pair<String, List<Pair<String, Object>>> queryBody = generateQueryBody(query); final String queryString = String.join(" ", queryHead, queryBody.getValue0(), generateQueryTail(query)); final List<Pair<String, Object>> parameters = queryBody.getValue1(); final SqlParameterCollection sqlParameters = new SqlParameterCollection(); sqlParameters.addAll( parameters.stream() .map(p -> new SqlParameter("@" + p.getValue0(), toDocumentDBValue(p.getValue1()))) .collect(Collectors.toList()) ); return new SqlQuerySpec(queryString, sqlParameters); } }
@Override void populatePropertyBag() { boolean defaultParameters = (this.parameters != null && this.parameters.size() != 0); if (defaultParameters) { super.set("parameters", this.parameters); } else { super.remove("parameters"); } } }
switch (queryCompatibilityMode) { case SqlQuery: if (querySpec.getParameters() != null && querySpec.getParameters().size() > 0) { throw new IllegalArgumentException( String.format("Unsupported argument in query compatibility mode '{%s}'",
/** * Gets the collection of query parameters. * * @return the query parameters. */ public SqlParameterCollection getParameters() { if (this.parameters == null) { Collection<SqlParameter> sqlParameters = super.getCollection("parameters", SqlParameter.class); if (sqlParameters == null) { sqlParameters = new ArrayList<SqlParameter>(); } this.parameters = new SqlParameterCollection(sqlParameters); } return this.parameters; }
private DocumentCollection getDocCollection(String collectionName) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new IllegalCollectionException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } return collections.get(0); }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new RuntimeException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), "SELECT * FROM c", null) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
public DocumentCollection createCollectionIfNotExists(String collectionName, String partitionKeyFieldName, Integer requestUnit, IndexingPolicy policy) { if (this.databaseCache == null) { this.databaseCache = createDatabaseIfNotExists(this.databaseName); } final List<DocumentCollection> collectionList = documentDbFactory.getDocumentClient() .queryCollections(getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (!collectionList.isEmpty()) { return collectionList.get(0); } else { final RequestOptions requestOptions = getRequestOptions(null, requestUnit); return createCollection(this.databaseName, collectionName, requestOptions, partitionKeyFieldName, policy); } }
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = documentDbFactory.getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDatabase {}", dbName); } final Resource resource = documentDbFactory.getDocumentClient() .createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { LOGGER.error("create database {} get unexpected result: {}" + resource.getSelfLink()); throw new RuntimeException("create database {} get unexpected result: " + resource.getSelfLink()); } } } catch (DocumentClientException ex) { throw new RuntimeException("createOrGetDatabase exception", ex); } }
private DocumentCollection createCollectionIfNotExists(String dbName, String collectionName) { if (this.databaseCache == null) { this.databaseCache = createDatabaseIfNotExists(dbName); } final List<DocumentCollection> collectionList = documentDbFactory.getDocumentClient() .queryCollections(getDatabaseLink(dbName), new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (!collectionList.isEmpty()) { return collectionList.get(0); } else { final RequestOptions requestOptions = new RequestOptions(); requestOptions.setOfferThroughput(1000); return createCollection(dbName, collectionName, requestOptions); } }
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = documentDbFactory.getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); if (LOGGER.isDebugEnabled()) { LOGGER.debug("execute createDatabase {}", dbName); } final Resource resource = documentDbFactory.getDocumentClient() .createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { final String errorMessage = MessageFormat.format( "create database {0} and get unexpected result: {1}", dbName, resource.getSelfLink()); LOGGER.error(errorMessage); throw new DatabaseCreationException(errorMessage); } } } catch (DocumentClientException ex) { throw new DocumentDBAccessException("createOrGetDatabase exception", ex); } }
public <T> List<T> findAll(String collectionName, final Class<T> entityClass) { final List<DocumentCollection> collections = documentDbFactory.getDocumentClient(). queryCollections( getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM ROOT r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (collections.size() != 1) { throw new IllegalCollectionException("expect only one collection: " + collectionName + " in database: " + this.databaseName + ", but found " + collections.size()); } final FeedOptions feedOptions = new FeedOptions(); feedOptions.setEnableCrossPartitionQuery(true); final SqlQuerySpec sqlQuerySpec = new SqlQuerySpec("SELECT * FROM root c"); final List<Document> results = documentDbFactory.getDocumentClient() .queryDocuments(collections.get(0).getSelfLink(), sqlQuerySpec, feedOptions) .getQueryIterable().toList(); final List<T> entities = new ArrayList<>(); for (int i = 0; i < results.size(); i++) { final T entity = mappingDocumentDbConverter.read(entityClass, results.get(i)); entities.add(entity); } return entities; }
private Database createDatabaseIfNotExists(String dbName) { try { final List<Database> dbList = getDocumentClient() .queryDatabases(new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", dbName))), null) .getQueryIterable().toList(); if (!dbList.isEmpty()) { return dbList.get(0); } else { // create new database final Database db = new Database(); db.setId(dbName); log.debug("execute createDatabase {}", dbName); final Resource resource = getDocumentClient().createDatabase(db, null).getResource(); if (resource instanceof Database) { return (Database) resource; } else { final String errorMessage = MessageFormat.format( "create database {0} and get unexpected result: {1}", dbName, resource.getSelfLink()); log.error(errorMessage); throw new DatabaseCreationException(errorMessage); } } } catch (DocumentClientException ex) { throw new DocumentDBAccessException("createOrGetDatabase exception", ex); } }
@Override public DocumentCollection createCollectionIfNotExists(@NonNull DocumentDbEntityInformation information) { if (this.databaseCache == null) { this.databaseCache = createDatabaseIfNotExists(this.databaseName); } final String collectionName = information.getCollectionName(); final String partitionKeyFieldName = information.getPartitionKeyFieldName(); final List<DocumentCollection> collectionList = getDocumentClient() .queryCollections(getDatabaseLink(this.databaseName), new SqlQuerySpec("SELECT * FROM root r WHERE r.id=@id", new SqlParameterCollection(new SqlParameter("@id", collectionName))), null) .getQueryIterable().toList(); if (!collectionList.isEmpty()) { return collectionList.get(0); } else { return createCollection(this.databaseName, partitionKeyFieldName, information); } }
path = "/" + path; SqlQuerySpec querySpec = new SqlQuerySpec(query, new SqlParameterCollection()); FeedOptions options = new FeedOptions(); options.setEnableCrossPartitionQuery(true);