Skip to content

Commit 48cb27f

Browse files
committed
Merge pull request #585 from ajkannan/fix-query-to-builder
Fix query toBuilder()
2 parents 25692fc + f287462 commit 48cb27f

File tree

6 files changed

+483
-144
lines changed

6 files changed

+483
-144
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2015 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.gcloud.datastore;
18+
19+
import com.google.api.services.datastore.DatastoreV1;
20+
21+
/**
22+
* An implementation of a Google Cloud Datastore entity query that can be constructed by providing
23+
* all the specific query elements.
24+
*
25+
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
26+
* queries</a>
27+
*/
28+
public final class EntityQuery extends StructuredQuery<Entity> {
29+
30+
private static final long serialVersionUID = 2990565454831019471L;
31+
32+
/**
33+
* A {@code EntityQuery} builder for queries that return {@link Entity} results.
34+
*/
35+
public static final class Builder extends StructuredQuery.BuilderImpl<Entity, Builder> {
36+
37+
Builder(EntityQuery query) {
38+
super(query);
39+
}
40+
41+
Builder() {
42+
super(ResultType.ENTITY);
43+
}
44+
45+
@Override
46+
Builder mergeFrom(DatastoreV1.Query queryPb) {
47+
super.mergeFrom(queryPb);
48+
clearProjection();
49+
clearGroupBy();
50+
return this;
51+
}
52+
53+
@Override
54+
public EntityQuery build() {
55+
return new EntityQuery(this);
56+
}
57+
}
58+
59+
EntityQuery(Builder builder) {
60+
super(builder);
61+
}
62+
63+
@Override
64+
public Builder toBuilder() {
65+
return new Builder(this);
66+
}
67+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2015 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.gcloud.datastore;
18+
19+
import com.google.api.services.datastore.DatastoreV1;
20+
21+
/**
22+
* An implementation of a Google Cloud Datastore key-only query that can be constructed by providing
23+
* all the specific query elements.
24+
*
25+
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
26+
* queries</a>
27+
*/
28+
public final class KeyQuery extends StructuredQuery<Key> {
29+
30+
private static final long serialVersionUID = -746768461459070045L;
31+
32+
/**
33+
* A {@code KeyQuery} builder for queries that return {@link Key} results.
34+
*/
35+
public static final class Builder extends StructuredQuery.BuilderImpl<Key, Builder> {
36+
37+
Builder(KeyQuery query) {
38+
super(query);
39+
}
40+
41+
Builder() {
42+
super(ResultType.KEY);
43+
projection(Projection.property(KEY_PROPERTY_NAME));
44+
}
45+
46+
@Override
47+
Builder mergeFrom(DatastoreV1.Query queryPb) {
48+
super.mergeFrom(queryPb);
49+
projection(Projection.property(KEY_PROPERTY_NAME));
50+
clearGroupBy();
51+
return this;
52+
}
53+
54+
@Override
55+
public KeyQuery build() {
56+
return new KeyQuery(this);
57+
}
58+
}
59+
60+
KeyQuery(Builder builder) {
61+
super(builder);
62+
}
63+
64+
@Override
65+
public Builder toBuilder() {
66+
return new Builder(this);
67+
}
68+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2015 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.gcloud.datastore;
18+
19+
/**
20+
* An implementation of a Google Cloud Datastore projection entity query that can be constructed by
21+
* providing all the specific query elements.
22+
*
23+
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
24+
* queries</a>
25+
*/
26+
public final class ProjectionEntityQuery extends StructuredQuery<ProjectionEntity> {
27+
28+
private static final long serialVersionUID = 5488451194542425391L;
29+
30+
/**
31+
* A {@code ProjectionEntityQuery} builder for queries that return {@link ProjectionEntity}
32+
* results.
33+
*/
34+
public static final class Builder extends StructuredQuery.BuilderImpl<ProjectionEntity, Builder> {
35+
36+
Builder(ProjectionEntityQuery query) {
37+
super(query);
38+
}
39+
40+
Builder() {
41+
super(ResultType.PROJECTION_ENTITY);
42+
}
43+
44+
/**
45+
* Clears the projection clause.
46+
*/
47+
@Override
48+
public Builder clearProjection() {
49+
super.clearProjection();
50+
return this;
51+
}
52+
53+
/**
54+
* Sets the query's projection clause (clearing any previously specified Projection settings).
55+
*/
56+
@Override
57+
public Builder projection(Projection projection, Projection... others) {
58+
super.projection(projection, others);
59+
return this;
60+
}
61+
62+
/**
63+
* Adds one or more projections to the existing projection clause.
64+
*/
65+
@Override
66+
public Builder addProjection(Projection projection, Projection... others) {
67+
super.addProjection(projection, others);
68+
return this;
69+
}
70+
71+
/**
72+
* Clears the group by clause.
73+
*/
74+
@Override
75+
public Builder clearGroupBy() {
76+
super.clearGroupBy();
77+
return this;
78+
}
79+
80+
/**
81+
* Sets the query's group by clause (clearing any previously specified GroupBy settings).
82+
*/
83+
@Override
84+
public Builder groupBy(String property, String... others) {
85+
super.groupBy(property, others);
86+
return this;
87+
}
88+
89+
/**
90+
* Adds one or more properties to the existing group by clause.
91+
*/
92+
@Override
93+
public Builder addGroupBy(String property, String... others) {
94+
super.addGroupBy(property, others);
95+
return this;
96+
}
97+
98+
@Override
99+
public ProjectionEntityQuery build() {
100+
return new ProjectionEntityQuery(this);
101+
}
102+
}
103+
104+
ProjectionEntityQuery(Builder builder) {
105+
super(builder);
106+
}
107+
108+
@Override
109+
public Builder toBuilder() {
110+
return new Builder(this);
111+
}
112+
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Query.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import com.google.common.base.MoreObjects;
2323
import com.google.common.base.MoreObjects.ToStringHelper;
2424
import com.google.common.collect.Maps;
25-
import com.google.gcloud.datastore.StructuredQuery.EntityQueryBuilder;
26-
import com.google.gcloud.datastore.StructuredQuery.KeyQueryBuilder;
27-
import com.google.gcloud.datastore.StructuredQuery.ProjectionEntityQueryBuilder;
2825
import com.google.protobuf.GeneratedMessage;
2926
import com.google.protobuf.InvalidProtocolBufferException;
3027

@@ -217,21 +214,21 @@ public static <V> GqlQuery.Builder<V> gqlQueryBuilder(ResultType<V> resultType,
217214
/**
218215
* Returns a new {@link StructuredQuery} builder for full (complete entities) queries.
219216
*/
220-
public static EntityQueryBuilder entityQueryBuilder() {
221-
return new EntityQueryBuilder();
217+
public static EntityQuery.Builder entityQueryBuilder() {
218+
return new EntityQuery.Builder();
222219
}
223220

224221
/**
225222
* Returns a new {@link StructuredQuery} builder for key only queries.
226223
*/
227-
public static KeyQueryBuilder keyQueryBuilder() {
228-
return new KeyQueryBuilder();
224+
public static KeyQuery.Builder keyQueryBuilder() {
225+
return new KeyQuery.Builder();
229226
}
230227

231228
/**
232229
* Returns a new {@link StructuredQuery} builder for projection queries.
233230
*/
234-
public static ProjectionEntityQueryBuilder projectionEntityQueryBuilder() {
235-
return new ProjectionEntityQueryBuilder();
231+
public static ProjectionEntityQuery.Builder projectionEntityQueryBuilder() {
232+
return new ProjectionEntityQuery.Builder();
236233
}
237234
}

0 commit comments

Comments
 (0)