Google Datastore issue requesting * User type *

In this question, I solved the Google Datastore query problem for user data retrieval (com.google.appengine.api.users.User) as follows:

User user = userService.getCurrentUser(); String select_query = "select from " + Greeting.class.getName(); Query query = pm.newQuery(select_query); query.setFilter("author == paramAuthor"); query.declareParameters("java.lang.String paramAuthor"); greetings = (List<Greeting>) query.execute(user); 

This works well, but after a bit of clutter, I realized that this syntax is not very practical, since there is a need to create more complex queries, so I decided to manually create my filters, and now I got, for example, something like (where is the filter usually passed as a string variable, but now built into the string for its simplicity):

 User user = userService.getCurrentUser(); String select_query = "select from " + Greeting.class.getName(); Query query = pm.newQuery(select_query); query.setFilter("author == '"+ user.getEmail() +"'"); greetings = (List<Greeting>) query.execute(); 

Obviously, this will not work even if this syntax with field = 'value' supported by JDOQL and it works fine in other fields (String types and enumerations). Another strange thing is that, looking at the data viewer in the application dashboard, the "author" field is saved as the "User" type, but the value is " user@gmail.com ", and then again when I set it as a parameter ( the case above, which works fine) I declare the parameter as a string, and then pass the instance of the user (user), which is serialized with a simple toString() (I think).

Any idea?

+1
source share
1 answer

Using string substitution in query languages ​​is always a bad idea. It is too easy for the user to break out and mess with his environment, and also presents a whole set of encoding problems, etc.

What happened to your early wildcard approach? As far as I know, it supports everything, and this wraps around any parsing issues. Regarding the problem of understanding how many arguments to pass, you can use Query.executeWithMap or Query.executeWithArray to execute a query with an unknown number of arguments.

+1
source

All Articles