I just posted this in trunk following your suggestion.
>>> db=SQLDB()
>>> db.define_table('person',SQLField('name'))
>>> db.person['name']
<gluon.sql.SQLField object at 0x1327510>
>>> db.person.insert(name='Robin')
1
>>> db.person.insert(name='Robin')
2
>>> db.person[1]
<SQLStorage {'update_record': <function <lambda> at 0x13ca930>, 'id':
1, 'name': 'Robin'}>
>>> db.person[2]=dict(name='Tim')
>>> db.person[1]=db.person[2]
>>> db.person[1]
<SQLStorage {'update_record': <function <lambda> at 0x13ca930>, 'id':
1, 'name': 'Tim'}>
>>> del db.person[1]
Beautiful, thank you!
The lambda stuff requires more thought and work.
Massimo
On Jan 22, 10:39 pm, Robin B <[email protected]> wrote:
> I was just reading UserDict and thought how getitem, setitem, delitem
> could be added to db.Table for these:
>
> person = db.person[person.id]
> -> shortcut syntax for db(db.person.id==person.id).select()[0]
> db.person[person.id] = db.person(age=lambda attrs: attrs.age+=1)
> -> update a record without having to fetch it first
> del db.person[person.id]
> -> delete a record without having to fetch it first
>
> There are more to be had:
>
> len(db.posts) -> count()
>
> for post in db.posts:
> post.update_record(...)
>
> Robin
>
> On Jan 22, 10:13 pm, mdipierro <[email protected]> wrote:
>
> > Could you elaborate on dictmixin?
>
> > SQLStorage already extends a dict. Which methods do you think are
> > missing?
>
> > Massimo
>
> > On Jan 22, 9:03 pm, Robin B <[email protected]> wrote:
>
> > > Simple queries could be generated from lists of filters, also simple
> > > queries require fewer ()'s:
>
> > > db(*programmatic_filters).select()
> > > db(programmatic_filters).select()
> > > db(db.person.last_name=='Obama',db.person.first_name=='Barack').select
> > > ()
>
> > > vs
>
> > > db((db.person.last_name=='Obama')&
> > > (db.person.first_name=='Barack')).select()
>
> > > and
>
> > > db(db.person.last_name=='Obama')(db.person.first_name=='Barack').select
> > > ()
>
> > > You have to iteratively build the query from the list, it would be
> > > easier to just pass in the list with or without a * (splat).
>
> > > The real benefit is that drivers that do not allow "|" (GAE, MEMDB)
> > > could use trivial query parsers, which would make those drivers simple
> > > to write and maintain.
>
> > > > > person = db.person(party='Republican')
> > > > > -> returns a person with default= values populated and **attrs set
> > > > > person.first_name = 'John'
> > > > > response.render(person=person)
> > > > > -> the record can be printed on a page without inserting it in db
> > > > > person.insert()
> > > > > -> actually inserts the record, returns 1 for success, 0 for failure,
> > > > > updates person attrs
>
> > > > Not convinced about this one.
>
> > > The benefit here is that you have easy access to all the default
> > > values without doing an insert, this become especially useful if
> > > db.Field(default=lamda:foo(123)) becomes possible.
>
> > > > > person = db.person[person.id]
> > > > > -> shortcut syntax for db(db.person.id==person.id).select()[0]
>
> > > > love this one
>
> > > > > db.person[person.id] = db.person(age=lambda attrs: attrs.age+=1)
> > > > > -> update a record without having to fetch it first
>
> > > > this depends on the one above I am not convinced about. One could do
> > > > db.person[person.id].update_record(age=...)
>
> > > db.person[person.id].update_record(age=...)
> > > -> would require 2 db requests (select,update)
>
> > > db.person[person.id] = db.person(age=...)
> > > -> would require 1 db request (update)
>
> > > Since __setitem__(self,key,value) maps to self[key] = value,
> > > if implemented properly in the driver,
> > > this eliminates the redundant db request.
>
> > > > > del db.person[person.id]
> > > > > -> delete a record without having to fetch it first
>
> > > > love this one too
>
> > > DictMixin could be used to implement a lot of functionality in the
> > > driver:http://svn.python.org/view/python/trunk/Lib/UserDict.py?view=markup
>
> > > Robin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---