google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

What is the purpose of varargs in GqlQuery.Builder's addBinding and setBinding methods?

Open sai-pullabhotla opened this issue 9 years ago • 2 comments

I'm trying to figure out the purpose of vararg parameter in the above methods and can't figure out why.

For example, take the below method that allows binding zero or more String parameters:

public GqlQuery.Builder<V> addBinding(String... value)

Since there is no JavaDoc on these methods yet - one way to interpret this method is that it allows binding of a ListValue to do search on Array property. Other way is to bind each String value to @1, @2, etc. Looking at the code tells me that multiple values are converted to a ListValue, so...

However, when I try this with more than one value,

Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, "select * from MyEntity where MyProperty= @1").addBinding("value1", "value2").build();

This errors out with the below exception:

Exception in thread "main" com.google.cloud.datastore.DatastoreException: A list value is not allowed
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:110)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:96)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.runQuery(DefaultDatastoreRpc.java:164)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:93)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:89)
	at com.google.cloud.RetryHelper.doRetry(RetryHelper.java:179)
	at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
	at com.google.cloud.datastore.DatastoreImpl.runQuery(DatastoreImpl.java:88)
	at com.google.cloud.datastore.QueryResultsImpl.sendRequest(QueryResultsImpl.java:73)
	at com.google.cloud.datastore.QueryResultsImpl.<init>(QueryResultsImpl.java:57)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:82)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:73)

So, why do these methods have varargs?

sai-pullabhotla avatar Jan 24 '17 19:01 sai-pullabhotla

For GA we should fix the JavaDocs.

As I read it, it should mean: Query<Entity> query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, "select * from MyEntity where MyProperty= @1 and MySecondProperty=@2").addBinding("value1", "value2").build();

lesv avatar Feb 23 '17 22:02 lesv

I have added some javadocs to emphasize that setBinding() and addBinding() methods only add one new binding per call regardless of how many arguments are passed in. If multiple arguments are passed to these methods, a ListValue will be used for the binding. Based on the gql reference, it seems the "where clause" doesn't support list value binding yet.

neozwu avatar Apr 25 '17 19:04 neozwu