[TransWarp] Trying out peak.storage

Geert-Jan Van den Bogaerde gvdbogaerde at pandora.be
Mon Jan 27 17:40:50 EST 2003


I sent this message quite a while back from a non-subscribed address and
resent it later from a subscribed one when it wasn't posted after a few
days. Please ignore it as it has been answered already.

Geert-Jan

On Sat, 2003-01-11 at 16:52, Geert-Jan Van den Bogaerde wrote:
> Hi,
> 
> 
> In trying to gain a better understanding of peak's API, I've been
> playing with the Contacts database example posted by Roché Compaan last
> month.
> 
> My code is this:
> 
> --------------------------------------------------
> from peak.api import storage
> from peak.api import binding, model
> from peak.model.datatypes import String
> 
> 
> class Contact(model.Element):
> 
>     class Name(model.Field):
>         referencedType = String
> 
>     class Surname(model.Field):
>         referencedType = String
> 
>     class Email(model.Field):
>         referencedType = String
>     
> 
> class ContactDM(storage.EntityDM):
> 
>     defaultClass = Contact
> 
>     attrs = ['Name', 'Surname', 'Email']
> 
>     DBConn = binding.bindTo("pgsql://user:pass@localhost/contacts")
> 
>     def defaultState(self, ob):
>         state = {}
>         for attr in self.attrs:
>             state[attr] = ''
>         return state
> 
>     def load(self, oid, ob):
>         print "load"
>         sql = 'SELECT %s FROM Contact WHERE id=%s' % 
> (','.join(self.attrs), oid)
>         print "Running SQL: %s" % sql
>         row = ~ self.DBConn(sql)
>         return dict(zip(self.attrs, row))
> 
>     def new(self, ob):
>         print "new"
>         sql = "SELECT NEXTVAL('ObjectIds')"
>         (oid,) = ~ self.DBConn(sql)
>         sql = "INSERT INTO Contact (id, %s) VALUES (" % 
> ','.join(self.attrs)
>         sql += "%d," % oid
>         values = []
>         for attr in self.attrs:
>             values.append('%r' % getattr(ob, attr))
>         sql += ','.join(values) + ")"
>         print "Running SQL: %s" % sql
>         self.DBConn(sql)
>         return oid
> 
>     def save(self, ob):
>         print "save"
>         sql = "UPDATE Contact SET "
>         sql_where = ' WHERE id = %s ' % ob._p_oid
>         values = []
>         for attr in self.attrs:
>             value = getattr(ob, attr)
>             values.append('%s = %r' % (attr, value))
>         sql += ','.join(values)
>         sql += sql_where
> 	print "Running SQL: %s" % sql
>         self.DBConn(sql)
> 
> 
> myDM = ContactDM()
> 
> # Create a new instance
> storage.begin()
> ni = myDM.newItem()
> ni.Name = 'John'
> ni.Surname = 'Smith'
> ni.Email = 'john at smith.com'
> storage.commit()
> 
> ------------------------------------------------------------
> 
> 
> 
> 
> Running this script gives no errors and the following output:
> 
> -------------------------------------------------------------
> new
> Running SQL: INSERT INTO Contact (id, Name,Surname,Email) VALUES
> (15,'John','Smith','john at smith.com')
> ---------------------------------------------------------------
> 
> which seems correct. However opening a psql console after the script has
> run shows no new rows added to the Contact table, so it seems the
> changes are not getting commited.
> 
> What am I missing here? Or is this a bug? (I checked out today's CVS,
> and all unit tests pass) 
> 
> Thanks,
> 
> Geert-Jan




More information about the PEAK mailing list