- def commit (self):
- self.session().commit()
-
- def insert (self, stuff, commit=False):
- if isinstance (stuff,list):
- self.session().add_all(stuff)
- else:
- self.session().add(obj)
-
- # for compat with the previous PostgreSQL stuff
- def update (self, record):
- self.commit()
-
- def remove (self, record):
- del record
- self.commit()
-
-####################
-# dicts vs objects
-####################
-# historically the front end to the db dealt with dicts, so the code was only dealing with dicts
-# sqlalchemy however offers an object interface, meaning that you write obj.id instead of obj['id']
-# which is admittedly much nicer
-# however we still need to deal with dictionaries if only for the xmlrpc layer
-#
-# here are a few utilities for this
-#
-# (*) first off, when an old pieve of code needs to be used as-is, if only temporarily, the simplest trick
-# is to use obj.__dict__
-# this behaves exactly like required, i.e. obj.__dict__['field']='new value' does change obj.field
-# however this depends on sqlalchemy's implementation so it should be avoided
-#
-# (*) second, when an object needs to be exposed to the xmlrpc layer, we need to convert it into a dict
-# remember though that writing the resulting dictionary won't change the object
-# essentially obj.__dict__ would be fine too, except that we want to discard alchemy private keys starting with '_'
-# 2 ways are provided for that:
-# . dict(obj)
-# . obj.todict()
-# the former dict(obj) relies on __iter__() and next() below, and does not rely on the fields names
-# although it seems to work fine, I've found cases where it issues a weird python error that I could not get right
-# so the latter obj.todict() seems more reliable but more hacky as is relies on the form of fields, so this can probably be improved
-#
-# (*) finally for converting a dictionary into an sqlalchemy object, we provide
-# obj.set_from_dict(dict)