Skip to content

Commit 9310b1e

Browse files
committed
Add snippets for datastore.Query
1 parent 9a0e9de commit 9310b1e

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
package com.google.cloud.examples.datastore.snippets;
18+
19+
import com.google.cloud.datastore.Datastore;
20+
import com.google.cloud.datastore.DatastoreOptions;
21+
import com.google.cloud.datastore.Entity;
22+
import com.google.cloud.datastore.Key;
23+
import com.google.cloud.datastore.QueryResults;
24+
import com.google.cloud.datastore.Transaction;
25+
26+
import org.junit.BeforeClass;
27+
import org.junit.Test;
28+
29+
public class ITQuerySnippets {
30+
31+
private static Datastore datastore;
32+
33+
@BeforeClass
34+
public static void beforeClass() {
35+
datastore = DatastoreOptions.defaultInstance().service();
36+
}
37+
38+
@Test
39+
public void testNewQuery() {
40+
Transaction transaction = datastore.newTransaction();
41+
QuerySnippets transactionSnippets = new QuerySnippets(transaction);
42+
QueryResults<?> results = transactionSnippets.newQuery();
43+
}
44+
45+
@Test
46+
public void testNewTypedQuery() {
47+
Transaction transaction = datastore.newTransaction();
48+
QuerySnippets transactionSnippets = new QuerySnippets(transaction);
49+
QueryResults<Entity> results = transactionSnippets.newTypedQuery();
50+
}
51+
52+
@Test
53+
public void testNewEntityQuery() {
54+
Transaction transaction = datastore.newTransaction();
55+
QuerySnippets transactionSnippets = new QuerySnippets(transaction);
56+
QueryResults<Entity> results = transactionSnippets.newEntityQuery("*");
57+
}
58+
}

0 commit comments

Comments
 (0)