Here is some sample code using these features.
I have tested it, although the following is slightly edited from what
I tested:
try:
from gluon.contrib.gql import * # if running on Google App
Engine
except:
db=SQLDB('sqlite://storage.db')
datastoretype = 'SQL'
else:
db=GQLDB() # connect to GAE
datastoretype = 'GAE'
from gluon.contrib.gql import gae
def TestNativeGAE(request, response, session):
if not 'ParentTable' in db.tables:
db.define_table('ParentTable',
# NOTE: web2py provides the 'id' column automatically
db.Field('nKey','integer'),
db.Field('name','text')
)
if not 'ChildTable' in db.tables:
db.define_table('ChildTable',
# NOTE: web2py provides the 'id' column automatically
db.Field('idParentFolder','reference ParentTable'),
db.Field('age','integer'),
db.Field('name','text'),
db.Field('parentLink',
gae.ReferenceProperty(db.ParentTable._tableobj, required=True,
collection_name='ChildCollection'))
)
# Here is the code to insert a parent and a child
myParent = db.ParentTable.insert(nKey=1, name='Bill')
myParentNativeRef = myParent._table._last_reference
myParentid = myParent.id
myname = myParentNativeRef._name
myname2 = myParentNativeRef.name
myParentnKey = myParentNativeRef.nKey
# NOTE: if myParentNativeRef is not available, then instead you
can
# use the technique in my previous forum msg to query for
the native ref and id of the Parent
myChild = db.ChildTable.insert(age = 9, name = 'Johnny',
idParent = myParentid,
parentLink = myParentNativeRef)
# -------- End of code to insert data
------------------------------------------------------------
#
-------------------------------------------------------------------------------------------------
# Here is the code to retrieve the parent and children using the
native references
# Performance is very fast because GAE does this in a single call
to the Datastore:
#rows = db(db.ParentTable.name ==
'Bill' ).select(db.ParentTable.ALL) # finds nothing for some reason
rows = db(db.ParentTable.id==myParentid).select()
myParent2 = rows[0]
#myNativeParent2 = myParent2._table._last_reference # Would be
nice to have this work as well but at the moment it does not work....
theNativeParentClass = db.ParentTable._tableobj
nativeParentTableQuery = theNativeParentClass.all()
#nativeParentTableQuery.filter('name =', 'Bill') # This filter
finds nothing!! Some problem with text keys ???? < ======
#nativeParentTableQuery.filter('id =', myParentid) # This filter
finds nothing because native GAE Entity has no auto generated id
nativeParentTableQuery.filter('nKey =', myParentnKey)
nativeresults = nativeParentTableQuery.fetch(limit=1)
myGAENativeParent = nativeresults[0]
for child in myGAENativeParent.ChildCollection:
myChildName = child.name
myChildAge = child.age
return dict(form=child.name)
# ---------------------- End of Sample code
-------------------------------------------------------------------