|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * EDITING INSTRUCTIONS |
| 19 | + * This file is referenced in Query's javadoc. Any change to this file should be reflected in Query's |
| 20 | + * javadoc. |
| 21 | + */ |
| 22 | + |
| 23 | +package com.google.cloud.examples.datastore.snippets; |
| 24 | + |
| 25 | +import com.google.cloud.datastore.Datastore; |
| 26 | +import com.google.cloud.datastore.Entity; |
| 27 | +import com.google.cloud.datastore.Key; |
| 28 | +import com.google.cloud.datastore.ProjectionEntity; |
| 29 | +import com.google.cloud.datastore.Query; |
| 30 | +import com.google.cloud.datastore.QueryResults; |
| 31 | +import com.google.cloud.datastore.Transaction; |
| 32 | + |
| 33 | +/** This class contains a number of snippets for the {@link Transaction} interface. */ |
| 34 | +public class QuerySnippets { |
| 35 | + |
| 36 | + private final Transaction transaction; |
| 37 | + |
| 38 | + public QuerySnippets(Transaction transaction) { |
| 39 | + this.transaction = transaction; |
| 40 | + } |
| 41 | + |
| 42 | + // [TARGET gqlQueryBuilder(String)] |
| 43 | + public QueryResults<?> newQuery() { |
| 44 | + Datastore datastore = transaction.datastore(); |
| 45 | + // [START newQuery] |
| 46 | + Query<?> query = Query.gqlQueryBuilder("select * from kind").build(); |
| 47 | + QueryResults<?> results = datastore.run(query); |
| 48 | + // Use results |
| 49 | + // [END newQuery] |
| 50 | + return results; |
| 51 | + } |
| 52 | + |
| 53 | + // [TARGET gqlQueryBuilder(Query.ResultType, String)] |
| 54 | + public QueryResults<Entity> newTypedQuery() { |
| 55 | + Datastore datastore = transaction.datastore(); |
| 56 | + // [START newTypedQuery] |
| 57 | + Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY, "select * from kind").build(); |
| 58 | + QueryResults<Entity> results = datastore.run(query); |
| 59 | + // Use results |
| 60 | + // [END newTypedQuery] |
| 61 | + return results; |
| 62 | + } |
| 63 | + |
| 64 | + // [TARGET gqlQueryBuilder(Query.ResultType, String)] |
| 65 | + public QueryResults<Entity> newEntityQuery(String kind) { |
| 66 | + Datastore datastore = transaction.datastore(); |
| 67 | + // [START newEntityQuery] |
| 68 | + Query<Entity> query = Query.entityQueryBuilder().kind(kind).build(); |
| 69 | + QueryResults<Entity> results = datastore.run(query); |
| 70 | + // Use results |
| 71 | + // [END newEntityQuery] |
| 72 | + return results; |
| 73 | + } |
| 74 | + |
| 75 | + // [TARGET keyQueryBuilder(Query.ResultType, String)] |
| 76 | + public Query<Key> newKeyQuery(String kind) { |
| 77 | + Datastore datastore = transaction.datastore(); |
| 78 | + // [START newKeyQuery] |
| 79 | + Query<Key> query = Query.keyQueryBuilder().kind(kind).build(); |
| 80 | + QueryResults<Key> results = datastore.run(query); |
| 81 | + // Use results |
| 82 | + // [END newKeyQuery] |
| 83 | + return query; |
| 84 | + } |
| 85 | + |
| 86 | + // [TARGET projectionEntityQueryBuilder(Query.ResultType, String)] |
| 87 | + public Query<ProjectionEntity> newProjectionEntityQuery(String kind) { |
| 88 | + Datastore datastore = transaction.datastore(); |
| 89 | + // [START newProjectionEntityQuery] |
| 90 | + Query<ProjectionEntity> query = Query.projectionEntityQueryBuilder().kind(kind).build(); |
| 91 | + QueryResults<ProjectionEntity> results = datastore.run(query); |
| 92 | + // Use results |
| 93 | + // [END newProjectionEntityQuery] |
| 94 | + return query; |
| 95 | + } |
| 96 | +} |
0 commit comments