1919import static com .google .common .base .Preconditions .checkNotNull ;
2020import static com .google .gcloud .datastore .Validator .validateNamespace ;
2121
22- import com .google .api .services .datastore .DatastoreV1 ;
2322import com .google .common .base .MoreObjects ;
2423import com .google .common .collect .ImmutableList ;
2524import com .google .common .collect .ImmutableMap ;
@@ -75,25 +74,22 @@ public final class GqlQuery<V> extends Query<V> {
7574
7675 private final transient String queryString ;
7776 private final transient boolean allowLiteral ;
78- private final transient ImmutableList < Binding > namedBindings ;
77+ private final transient ImmutableMap < String , Binding > namedBindings ;
7978 private final transient ImmutableList <Binding > positionalBindings ;
8079
81- static final class Binding extends Serializable <DatastoreV1 . GqlQueryArg > {
80+ static final class Binding extends Serializable <com . google . datastore . v1beta3 . GqlQueryParameter > {
8281
8382 private static final long serialVersionUID = 1976895435257636275L ;
8483
85- private final transient String name ;
8684 private final transient Cursor cursor ;
8785 private final transient Value <?> value ;
8886
89- Binding (String name , Cursor cursor ) {
90- this .name = name ;
87+ Binding (Cursor cursor ) {
9188 this .cursor = checkNotNull (cursor );
9289 value = null ;
9390 }
9491
95- Binding (String name , Value <?> value ) {
96- this .name = name ;
92+ Binding (Value <?> value ) {
9793 this .value = checkNotNull (value );
9894 cursor = null ;
9995 }
@@ -102,13 +98,9 @@ Object cursorOrValue() {
10298 return MoreObjects .firstNonNull (cursor , value );
10399 }
104100
105- String name () {
106- return name ;
107- }
108-
109101 @ Override
110102 public int hashCode () {
111- return Objects .hash (name , cursor , value );
103+ return Objects .hash (cursor , value );
112104 }
113105
114106 @ Override
@@ -120,40 +112,36 @@ public boolean equals(Object obj) {
120112 return false ;
121113 }
122114 Binding other = (Binding ) obj ;
123- return Objects .equals (name , other .name )
124- && Objects .equals (cursor , other .cursor )
125- && Objects .equals (value , other .value );
115+ return Objects .equals (cursor , other .cursor ) && Objects .equals (value , other .value );
126116 }
127117
128118 @ Override
129- protected DatastoreV1 .GqlQueryArg toPb () {
130- DatastoreV1 .GqlQueryArg .Builder argPb = DatastoreV1 .GqlQueryArg .newBuilder ();
131- if (name != null ) {
132- argPb .setName (name );
133- }
119+ protected com .google .datastore .v1beta3 .GqlQueryParameter toPb () {
120+ com .google .datastore .v1beta3 .GqlQueryParameter .Builder argPb =
121+ com .google .datastore .v1beta3 .GqlQueryParameter .newBuilder ();
134122 if (cursor != null ) {
135123 argPb .setCursor (cursor .byteString ());
136124 }
137125 if (value != null ) {
138- // TODO(ajaykannan): fix me!
139- //argPb.setValue(value.toPb());
126+ argPb .setValue (value .toPb ());
140127 }
141128 return argPb .build ();
142129 }
143130
144131 @ Override
145132 protected Object fromPb (byte [] bytesPb ) throws InvalidProtocolBufferException {
146- return fromPb (DatastoreV1 . GqlQueryArg .parseFrom (bytesPb ));
133+ return fromPb (com . google . datastore . v1beta3 . GqlQueryParameter .parseFrom (bytesPb ));
147134 }
148135
149- static Binding fromPb (DatastoreV1 .GqlQueryArg argPb ) {
150- String name = argPb .hasName () ? argPb .getName () : null ;
151- if (argPb .hasCursor ()) {
152- return new Binding (name , new Cursor (argPb .getCursor ()));
136+ static Binding fromPb (com .google .datastore .v1beta3 .GqlQueryParameter argPb ) {
137+ switch (argPb .getParameterTypeCase ()) {
138+ case CURSOR :
139+ return new Binding (new Cursor (argPb .getCursor ()));
140+ case VALUE :
141+ return new Binding (Value .fromPb (argPb .getValue ()));
142+ default :
143+ throw new AssertionError ("Unexpected enum value " + argPb .getParameterTypeCase ());
153144 }
154- // TODO(ajaykannan): fix me!
155- //return new Binding(name, Value.fromPb(argPb.getValue()));
156- return new Binding (name , new Cursor (null )); // TODO(ajaykannan): fix me!
157145 }
158146 }
159147
@@ -196,52 +184,52 @@ public Builder<V> clearBindings() {
196184 }
197185
198186 public Builder <V > setBinding (String name , Cursor cursor ) {
199- namedBindings .put (name , new Binding (name , cursor ));
187+ namedBindings .put (name , new Binding (cursor ));
200188 return this ;
201189 }
202190
203191 public Builder <V > setBinding (String name , String ... value ) {
204- namedBindings .put (name , toBinding (name , StringValue .MARSHALLER , Arrays .asList (value )));
192+ namedBindings .put (name , toBinding (StringValue .MARSHALLER , Arrays .asList (value )));
205193 return this ;
206194 }
207195
208196 public Builder <V > setBinding (String name , long ... value ) {
209- namedBindings .put (name , toBinding (name , LongValue .MARSHALLER , Longs .asList (value )));
197+ namedBindings .put (name , toBinding (LongValue .MARSHALLER , Longs .asList (value )));
210198 return this ;
211199 }
212200
213201 public Builder <V > setBinding (String name , double ... value ) {
214- namedBindings .put (name , toBinding (name , DoubleValue .MARSHALLER , Doubles .asList (value )));
202+ namedBindings .put (name , toBinding (DoubleValue .MARSHALLER , Doubles .asList (value )));
215203 return this ;
216204 }
217205
218206 public Builder <V > setBinding (String name , boolean ... value ) {
219- namedBindings .put (name , toBinding (name , BooleanValue .MARSHALLER , Booleans .asList (value )));
207+ namedBindings .put (name , toBinding (BooleanValue .MARSHALLER , Booleans .asList (value )));
220208 return this ;
221209 }
222210
223211 public Builder <V > setBinding (String name , DateTime ... value ) {
224- namedBindings .put (name , toBinding (name , DateTimeValue .MARSHALLER , Arrays .asList (value )));
212+ namedBindings .put (name , toBinding (DateTimeValue .MARSHALLER , Arrays .asList (value )));
225213 return this ;
226214 }
227215
228216 public Builder <V > setBinding (String name , Key ... value ) {
229- namedBindings .put (name , toBinding (name , KeyValue .MARSHALLER , Arrays .asList (value )));
217+ namedBindings .put (name , toBinding (KeyValue .MARSHALLER , Arrays .asList (value )));
230218 return this ;
231219 }
232220
233221 public Builder <V > setBinding (String name , FullEntity <?>... value ) {
234- namedBindings .put (name , toBinding (name , EntityValue .MARSHALLER , Arrays .asList (value )));
222+ namedBindings .put (name , toBinding (EntityValue .MARSHALLER , Arrays .asList (value )));
235223 return this ;
236224 }
237225
238226 public Builder <V > setBinding (String name , Blob ... value ) {
239- namedBindings .put (name , toBinding (name , BlobValue .MARSHALLER , Arrays .asList (value )));
227+ namedBindings .put (name , toBinding (BlobValue .MARSHALLER , Arrays .asList (value )));
240228 return this ;
241229 }
242230
243231 public Builder <V > addBinding (Cursor cursor ) {
244- positionalBindings .add (new Binding (null , cursor ));
232+ positionalBindings .add (new Binding (cursor ));
245233 return this ;
246234 }
247235
@@ -289,11 +277,7 @@ public GqlQuery<V> build() {
289277 return new GqlQuery <>(this );
290278 }
291279
292- private static Binding toBinding (Value .BuilderFactory <?, ?, ?> builderFactory , List <?> values ) {
293- return toBinding (null , builderFactory , values );
294- }
295-
296- private static <V > Binding toBinding (String name , Value .BuilderFactory <V , ?, ?> builderFactory ,
280+ private static <V > Binding toBinding (Value .BuilderFactory <V , ?, ?> builderFactory ,
297281 List <?> values ) {
298282 List <Value <V >> list = new ArrayList <>(values .size ());
299283 for (Object object : values ) {
@@ -309,15 +293,15 @@ private static <V> Binding toBinding(String name, Value.BuilderFactory<V, ?, ?>
309293 } else {
310294 value = new ListValue (list );
311295 }
312- return new Binding (name , value );
296+ return new Binding (value );
313297 }
314298 }
315299
316300 private GqlQuery (Builder <V > builder ) {
317301 super (builder .resultType , builder .namespace );
318302 queryString = builder .queryString ;
319303 allowLiteral = builder .allowLiteral ;
320- namedBindings = ImmutableList .copyOf (builder .namedBindings . values () );
304+ namedBindings = ImmutableMap .copyOf (builder .namedBindings );
321305 positionalBindings = ImmutableList .copyOf (builder .positionalBindings );
322306 }
323307
@@ -334,8 +318,8 @@ public boolean allowLiteral() {
334318 */
335319 public Map <String , Object > namedBindings () {
336320 ImmutableMap .Builder <String , Object > builder = ImmutableSortedMap .naturalOrder ();
337- for (Binding binding : namedBindings ) {
338- builder .put (binding .name (), binding .cursorOrValue ());
321+ for (Map . Entry < String , Binding > binding : namedBindings . entrySet () ) {
322+ builder .put (binding .getKey (), binding . getValue () .cursorOrValue ());
339323 }
340324 return builder .build ();
341325 }
@@ -373,50 +357,53 @@ public boolean equals(Object obj) {
373357 }
374358
375359 @ Override
376- protected DatastoreV1 .GqlQuery toPb () {
377- DatastoreV1 .GqlQuery .Builder queryPb = DatastoreV1 .GqlQuery .newBuilder ();
360+ protected com .google .datastore .v1beta3 .GqlQuery toPb () {
361+ com .google .datastore .v1beta3 .GqlQuery .Builder queryPb =
362+ com .google .datastore .v1beta3 .GqlQuery .newBuilder ();
378363 queryPb .setQueryString (queryString );
379- queryPb .setAllowLiteral (allowLiteral );
380- for (Binding argument : namedBindings ) {
381- queryPb .addNameArg (argument .toPb ());
364+ queryPb .setAllowLiterals (allowLiteral );
365+ Map <String , com .google .datastore .v1beta3 .GqlQueryParameter > namedBindingsPb =
366+ queryPb .getMutableNamedBindings ();
367+ for (Map .Entry <String , Binding > entry : namedBindings .entrySet ()) {
368+ namedBindingsPb .put (entry .getKey (), entry .getValue ().toPb ());
382369 }
383370 for (Binding argument : positionalBindings ) {
384- queryPb .addNumberArg (argument .toPb ());
371+ queryPb .addPositionalBindings (argument .toPb ());
385372 }
386373 return queryPb .build ();
387374 }
388375
389376 @ Override
390- protected void populatePb (DatastoreV1 .RunQueryRequest .Builder requestPb ) {
377+ protected void populatePb (com . google . datastore . v1beta3 .RunQueryRequest .Builder requestPb ) {
391378 requestPb .setGqlQuery (toPb ());
392379 }
393380
394381 @ Override
395- protected GqlQuery <V > nextQuery (DatastoreV1 .QueryResultBatch responsePb ) {
382+ protected GqlQuery <V > nextQuery (com . google . datastore . v1beta3 .QueryResultBatch responsePb ) {
396383 // See issue #17
397384 throw new UnsupportedOperationException ("paging for this query is not implemented yet" );
398385 }
399386
400387 @ Override
401388 protected Object fromPb (ResultType <V > resultType , String namespace , byte [] bytesPb )
402389 throws InvalidProtocolBufferException {
403- return fromPb (resultType , namespace , DatastoreV1 .GqlQuery .parseFrom (bytesPb ));
390+ return fromPb (resultType , namespace , com . google . datastore . v1beta3 .GqlQuery .parseFrom (bytesPb ));
404391 }
405392
406393 private static <V > GqlQuery <V > fromPb (
407- ResultType <V > resultType , String ns , DatastoreV1 .GqlQuery queryPb ) {
394+ ResultType <V > resultType , String ns , com . google . datastore . v1beta3 .GqlQuery queryPb ) {
408395 Builder <V > builder = new Builder <>(resultType , queryPb .getQueryString ());
409396 builder .namespace (ns );
410- if ( queryPb .hasAllowLiteral ()) {
411- builder . allowLiteral = queryPb . getAllowLiteral ();
412- }
413- for ( DatastoreV1 . GqlQueryArg nameArg : queryPb . getNameArgList ()) {
414- Binding argument = Binding . fromPb (nameArg );
415- builder . namedBindings . put ( argument . name (), argument );
416- }
417- for ( DatastoreV1 . GqlQueryArg numberArg : queryPb .getNumberArgList ()) {
418- Binding argument = Binding .fromPb (numberArg );
419- builder .positionalBindings .add (argument );
397+ builder . allowLiteral = queryPb .getAllowLiterals ();
398+ for ( Map . Entry < String , com . google . datastore . v1beta3 . GqlQueryParameter > nameArg
399+ : queryPb . getNamedBindings (). entrySet ()) {
400+ Binding currBinding = Binding . fromPb ( nameArg . getValue ());
401+ builder . namedBindings . put (nameArg . getKey (), currBinding );
402+ }
403+ for ( com . google . datastore . v1beta3 . GqlQueryParameter numberArg
404+ : queryPb .getPositionalBindingsList ()) {
405+ Binding currBinding = Binding .fromPb (numberArg );
406+ builder .positionalBindings .add (currBinding );
420407 }
421408 return builder .build ();
422409 }
0 commit comments