From 9310b1e824f35e37d46a3423fe33e3dd10811f26 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Fri, 16 Sep 2016 12:27:03 -0700 Subject: [PATCH] Add snippets for datastore.Query --- .../datastore/snippets/QuerySnippets.java | 96 +++++++++++++++++++ .../datastore/snippets/ITQuerySnippets.java | 58 +++++++++++ 2 files changed, 154 insertions(+) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/QuerySnippets.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITQuerySnippets.java diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/QuerySnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/QuerySnippets.java new file mode 100644 index 000000000000..aa1abb6b95ab --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/datastore/snippets/QuerySnippets.java @@ -0,0 +1,96 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * EDITING INSTRUCTIONS + * This file is referenced in Query's javadoc. Any change to this file should be reflected in Query's + * javadoc. + */ + +package com.google.cloud.examples.datastore.snippets; + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.ProjectionEntity; +import com.google.cloud.datastore.Query; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.Transaction; + +/** This class contains a number of snippets for the {@link Transaction} interface. */ +public class QuerySnippets { + + private final Transaction transaction; + + public QuerySnippets(Transaction transaction) { + this.transaction = transaction; + } + + // [TARGET gqlQueryBuilder(String)] + public QueryResults newQuery() { + Datastore datastore = transaction.datastore(); + // [START newQuery] + Query query = Query.gqlQueryBuilder("select * from kind").build(); + QueryResults results = datastore.run(query); + // Use results + // [END newQuery] + return results; + } + + // [TARGET gqlQueryBuilder(Query.ResultType, String)] + public QueryResults newTypedQuery() { + Datastore datastore = transaction.datastore(); + // [START newTypedQuery] + Query query = Query.gqlQueryBuilder(Query.ResultType.ENTITY, "select * from kind").build(); + QueryResults results = datastore.run(query); + // Use results + // [END newTypedQuery] + return results; + } + + // [TARGET gqlQueryBuilder(Query.ResultType, String)] + public QueryResults newEntityQuery(String kind) { + Datastore datastore = transaction.datastore(); + // [START newEntityQuery] + Query query = Query.entityQueryBuilder().kind(kind).build(); + QueryResults results = datastore.run(query); + // Use results + // [END newEntityQuery] + return results; + } + + // [TARGET keyQueryBuilder(Query.ResultType, String)] + public Query newKeyQuery(String kind) { + Datastore datastore = transaction.datastore(); + // [START newKeyQuery] + Query query = Query.keyQueryBuilder().kind(kind).build(); + QueryResults results = datastore.run(query); + // Use results + // [END newKeyQuery] + return query; + } + + // [TARGET projectionEntityQueryBuilder(Query.ResultType, String)] + public Query newProjectionEntityQuery(String kind) { + Datastore datastore = transaction.datastore(); + // [START newProjectionEntityQuery] + Query query = Query.projectionEntityQueryBuilder().kind(kind).build(); + QueryResults results = datastore.run(query); + // Use results + // [END newProjectionEntityQuery] + return query; + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITQuerySnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITQuerySnippets.java new file mode 100644 index 000000000000..b78738199af2 --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/datastore/snippets/ITQuerySnippets.java @@ -0,0 +1,58 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.examples.datastore.snippets; + +import com.google.cloud.datastore.Datastore; +import com.google.cloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.Entity; +import com.google.cloud.datastore.Key; +import com.google.cloud.datastore.QueryResults; +import com.google.cloud.datastore.Transaction; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class ITQuerySnippets { + + private static Datastore datastore; + + @BeforeClass + public static void beforeClass() { + datastore = DatastoreOptions.defaultInstance().service(); + } + + @Test + public void testNewQuery() { + Transaction transaction = datastore.newTransaction(); + QuerySnippets transactionSnippets = new QuerySnippets(transaction); + QueryResults results = transactionSnippets.newQuery(); + } + + @Test + public void testNewTypedQuery() { + Transaction transaction = datastore.newTransaction(); + QuerySnippets transactionSnippets = new QuerySnippets(transaction); + QueryResults results = transactionSnippets.newTypedQuery(); + } + + @Test + public void testNewEntityQuery() { + Transaction transaction = datastore.newTransaction(); + QuerySnippets transactionSnippets = new QuerySnippets(transaction); + QueryResults results = transactionSnippets.newEntityQuery("*"); + } +}