在查看com.google.appengine.api.datastore.Query
对象的调试视图时,以下是我可以看到的示例:
SELECT * FROM Greetings WHERE greeting = good morning
这是标准SQL
还是GQL
?另外,有没有办法从这种查询字符串构建Query对象?
答案 0 :(得分:0)
“ GQL 是一种类似SQL的语言,用于从中检索实体或密钥 App Engine可扩展数据存储区。虽然GQL的功能不同于 那些传统关系数据库的查询语言, GQL语法类似于SQL 。“
The GQL syntax can be summarized as follows:
SELECT [DISTINCT] [* | <property list> | __key__]
[FROM <kind>]]
[WHERE <condition> [AND <condition> ...]]
[ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
[LIMIT [<offset>,]<count>]
[OFFSET <offset>]
<property list> := <property> [, <property> ...]
<condition> := <property> {< | <= | > | >= | = | != } <value>
<condition> := <property> IN <list>
<condition> := ANCESTOR IS <entity or key>
<list> := (<value> [, <value> ...]])
所以我认为你的代码确实是GQL ......
关于第二个问题,如果您对数据存储区使用 JDO API ,则可以使用字符串创建查询,类似于传统的SQL查询字符串。这是the documentation中的示例:
Query q = pm.newQuery("select from Person " +
"where lastName == lastNameParam " +
"parameters String lastNameParam " +
"order by height desc");
List<Person> results = (List<Person>) q.execute("Smith");
在查看其他示例时,我认为您也可以这样做(我从未尝试过这个):
Query q = pm.newQuery("select from Person " +
"where lastName == 'Smith' " +
"order by height desc");
List<Person> results = (List<Person>) q.execute();
答案 1 :(得分:0)
以下是数据存储区低级API查询的示例: (这是我自己的一个项目)
public List<Game> getGames(Date gameDateMin, Date gameDateMax, boolean includePrivateGames) {
// Create a query for the Entity Kind you are searching for
Query query = new Query(Game.class.getName());
// Create filters
List<Filter> filters = new ArrayList<Query.Filter>();
if (gameDateMin != null) {
Filter gameDateMinFilter = new Query.FilterPredicate("gameDate", FilterOperator.GREATER_THAN_OR_EQUAL, gameDateMin);
filters.add(gameDateMinFilter);
}
if (gameDateMax != null) {
Filter gameDateMaxFilter = new Query.FilterPredicate("gameDate", FilterOperator.LESS_THAN_OR_EQUAL, gameDateMax);
filters.add(gameDateMaxFilter);
}
if (!includePrivateGames) {
Filter privateGameFilter = new Query.FilterPredicate("privateGame", FilterOperator.EQUAL, false);
filters.add(privateGameFilter);
}
if (!filters.isEmpty()) {
query.setFilter(CompositeFilterOperator.and(filters));
}
// ordering
query.addSort("gameDate", SortDirection.ASCENDING);
query.addSort("status", SortDirection.ASCENDING);
PreparedQuery preparedQuery = dataStoreService.prepare(query);
return this.toObjects(preparedQuery.asIterable());
}