Hi all,

I'm preparing to start using EntityGroups for transactions on GAE and this 
has led me to desire to have access to a GAE entity's key and parent 
information from selects and inserts.

Along the way i noticed that the fix 
for http://code.google.com/p/web2py/issues/detail?id=694 breaks the 
automatic rendering of SQLTable, and also stores a Null valued field in the 
database (which takes up space and requires an extra 2 writes for the value 
and automatic index).

my proposal:
 - since the GoogleDatastoreAdaptor allows you to create fields with native 
GAE types, in sqlhtml.py check that the field type is a string before using 
.startswith()
 - for GAE only, the Reference() object returned from insert() will include 
._gaekey which is the GAE Key object return from the gae put().
 - for GAE only, each row of a select will have a gae_item property which 
is the native GAE model item returned from the query.  you can then call 
key() or parent() on it for example to get the full key or to get the 
entity's parent.

a patch against trunk is attached.  This is getting into the nitty-gritty 
of web2py DAL, so please let me know if there are better ways to do this, 
or if there are things that i should be doing differently.

here's a sample of it in use:

def test2():
    db=DAL('google:datastore')
    
    db.define_table('testContact', 
                    db.Field('name', 'string'),
                    )
    
    db.define_table('testPhoneNumber',
                    db.Field('contact', 
gae.ReferenceProperty(db.testContact._tableobj, required=False, 
collection_name='PhoneNumberCollection')),
                    db.Field('phone_type', 'string'), 
                    db.Field('number', 'string')) 
                    
    myContactrid = db.testContact.insert(name='Scotty')

    key = myContactrid._gaekey
    
    db.testPhoneNumber.insert(contact=key,     
                phone_type='home', 
                number='(650) 555 - fred') 
    db.testPhoneNumber.insert(contact=key, 
                phone_type='mobile', 
                number='(650) 555 - free') 

    myContacts = db(db.testContact.id>0).select(db.testContact.ALL)
       
    myContact = myContacts[-1]
    
    myGAENativeContact = myContact.gae_item
    
    formdata = ""
    for pn in myGAENativeContact.PhoneNumberCollection:
        formdata += '%s: %s ' % (pn.phone_type, pn.number) 

    # Use generic HTML views
    response.generic_patterns = ['html']
    return dict(form=formdata,
        myContacts=myContacts,
        myPhoneNums = db(db.testPhoneNumber.id>0).select())


thanks all!

christian

Attachment: gaerefpatch.out
Description: Binary data

Reply via email to