简体   繁体   English

了解数据存储区查询对象

[英]Understanding datastore Query object

When looking at debug view of a com.google.appengine.api.datastore.Query object, here is a sample of what I can see: 查看com.google.appengine.api.datastore.Query对象的调试视图时,以下是我所看到的示例:

SELECT * FROM Greetings WHERE greeting = good morning

Is this standard SQL or GQL ? 这是标准的SQL还是GQL Also, is there a way to build a Query object from this kind of query string? 另外,有没有一种方法可以从这种查询字符串构建Query对象?

According to GAE's documentation : 根据GAE的文档

" GQL is a SQL-like language for retrieving entities or keys from the App Engine scalable datastore. While GQL's features are different from those of a query language for a traditional relational database, the GQL syntax is similar to that of SQL . " 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> ...]])

So I think your code is indeed GQL... 所以我认为您的代码确实是GQL ...


As for your 2nd question, if you're using JDO API for the Datastore, you can create a query using a string, similar to a classic SQL query string. 关于第二个问题,如果您将JDO API用于数据存储区,则可以使用类似于经典SQL查询字符串的字符串创建查询。 This is the example in 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");

And looking at other examples, I think you could so this as well ( I've never tried this ): 再看其他示例,我想您也可以这样做( 我从未尝试过 ):

Query q = pm.newQuery("select from Person " +
                      "where lastName == 'Smith' " +
                      "order by height desc");

List<Person> results = (List<Person>) q.execute();

Here is an example of a datastore low level API query : (This is straight out of one of my own projects) 这是一个数据存储库低级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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM