1
votes

I'm a bit confused about how to properly pass a cursor in a GQL query. Here is what I have so far:

DatastoreV1.GqlQuery.Builder query = DatastoreV1.GqlQuery.newBuilder().setQueryString(q);
query.addNameArgBuilder().setName("cursor").setCursor(cursor);

My query string ends with OFFSET @cursor.

I can see how this should work correctly if I have a cursor to pass. However, the first time I run the query, I will not have a cursor, so I will be passing null.

Do I need to write a separate query (without OFFSET @cursor) to run initially and only pass a cursor once a retrieve one from the result set, or will the query still run correctly if a pass a null cursor?

1

1 Answers

1
votes

Every binding in the GQL query string does require a corresponding arg. But you can use an empty cursor to indicate that the query should start at the beginning of the result set:

import com.google.protobuf.ByteString;

...

GqlQuery.Builder query = GqlQuery.newBuilder().setQueryString(q);
query.addNameArgBuilder()
    .setName("cursor")
    .setCursor(ByteString.EMPTY);