2

查看com.google.appengine.api.datastore.Query对象的调试视图时,以下是我可以看到的示例:

SELECT * FROM Greetings WHERE greeting = good morning

这是标准SQL还是GQL?另外,有没有办法从这种查询字符串构建一个查询对象?

4

2 回答 2

0

根据GAE 的文档

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 查询字符串。这是文档中的示例:

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();
于 2013-06-10T21:02:29.033 回答
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());
}
于 2013-06-12T09:41:30.637 回答