GQL - Conditional Or?

447 views
Skip to first unread message

HansKlopek

unread,
Jun 25, 2008, 12:36:40 PM6/25/08
to Google App Engine
I haven't been able to find anything on this topic, so I'm hoping
someone can help me. I want to use a conditional OR in a GQL clause.
I'm trying to prevent a user from signing up with a username or email
that has already been used. Here's what I WANT to do:

q = db.GqlQuery("SELECT * FROM UserAccount WHERE userName = :1 OR
email = :2", userName, email)

Unfortunately, GAE seems to barf on that line as there is no OR
operator for GQL's WHERE clause. Currently, I have this broken into
two SELECT statements and then just check for each condition
separately, but that's far less than ideal.

Does anyone have any ideas? Am I just missing something in the syntax?

And yes, I've already looked here: http://code.google.com/appengine/docs/datastore/gqlreference.html

Thanks!

Barry Hunter

unread,
Jun 25, 2008, 1:44:32 PM6/25/08
to google-a...@googlegroups.com
From the page you reference
"GQL does not have an OR operator."

And if you look further
"Note: The IN and != operators use multiple queries behind the scenes.
For example, the IN operator executes a separate underlying datastore
query for every item in the list. The entities returned are a result
of the cross-product of all the underlying datastore queries and are
de-duplicated. A maximum of 30 datastore queries are allowed for any
single GQL query."

So thats how its implemented for IN, so the method you already use
(multiple queries) sounds the best there is at the moment.

(although at write time you could put both the username and email into
one Kind - simply marking used names - or you could even create a
single string String List property on your current UserAccount, and
put both email and user name on that, and just run the query on that.
But its not really going to save you much as will still need two final
queries even if you can express it in one gql query)

--
Barry

- www.nearby.org.uk - www.geograph.org.uk -

KennyCarlile

unread,
Jun 25, 2008, 2:29:38 PM6/25/08
to Google App Engine
Thanks for the help. I knew it didn't have an OR. I was simply trying
to explain what I was trying to do in hopes of someone seeing a better
solution. Maybe with BigTable 2 queries instead of one doesn't
matter....

Thanks again.


On Jun 25, 10:44 am, "Barry Hunter" <barrybhun...@googlemail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages