Databases Reference
In-Depth Information
property. Mixing of data types in a single property also creates a few side effects when it comes to
ordering. There is a hierarchy in ordering between data types. For example, integers are ordered
before fl oats. Therefore, ordering by a property that, for example, contains both integer and fl oat
values can be tricky because 5 < 4.5.
I mentioned earlier that every query needs an index. A query and its explicitly defi ned index could
be like so:
q = db.GqlQuery(“SELECT * FROM Task” +
“WHERE start_date >= :1” +
“tags IN :2” +
“ORDER BY start_date”,
datetime.datetime(2011, 1, 1, 12, 0, 0).date(), [“Important”, “Sample”])
Available for
download on
Wrox.com
taskmanager GAE project
indexes:
- kind: Task
properties:
- name: start_date
- name: tags
Available for
download on
Wrox.com
index.yaml in taskmanager GAE project
In the example so far, a query result has been obtained with the help of the fetch method. The
fetch method allows you to get a set of records in a single call. The number of result records
returned is defi ned by the limit. If you just want a single entity you can use the get method to
retrieve the entity at the top of the order. If all you want to know is the number of entities in the
result, then simply use the count method. The count method returns with a count of all entities in
the result unless it times out. The app engine is suited for fast responses that can scale easily. Any
response that takes more than 30 seconds times out.
If you need to traverse through the entire result set, you need use the Query interface as an iterable
object. An example could be as follows:
q = db.GqlQuery(“SELECT * FROM Task” +
“WHERE start_date = :1” +
“ORDER BY name”, datetime.datetime(2011, 1, 1, 12, 0, 0).date())
for task in q:
print “Task name: %s” % (task.name)
Available for
download on
Wrox.com
taskmanager GAE project
The iterable object allows you to access the result set in small chunks until you receive all the results.
Although an iterable object allows you to traverse the entire set it is doesn't let you go back later and
fetch incrementally since the last fetch. For such an incremental fetch the cursor is a suitable feature.
After a fetch, you can get a cursor using the query object's cursor method. A cursor is a base64-
encoded data pointer that allows you to fetch additional results incrementally. The second query
Search WWH ::




Custom Search