[TransWarp] Trying out peak.storage

Geert-Jan Van den Bogaerde gvdbogaerde at pandora.be
Tue Jan 14 16:04:39 EST 2003


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

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 (" % 
        sql += "%d," % oid
        values = []
        for attr in self.attrs:
            values.append('%r' % getattr(ob, attr))
        sql += ','.join(values) + ")"
        print "Running SQL: %s" % 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

myDM = ContactDM()

# Create a new instance
ni = myDM.newItem()
ni.Name = 'John'
ni.Surname = 'Smith'
ni.Email = 'john at smith.com'


Running this script gives no errors and the following output:

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 or simply not yet implemented? 
(I checked out today's CVS, and all unit tests pass) 



More information about the PEAK mailing list