[TransWarp] First attempt to use the storage package in PEAK

Roché Compaan roche at upfrontsystems.co.za
Tue Dec 24 04:19:58 EST 2002


Hi Philip

Here is my first quick attempt to use the storage package in PEAK.
Creating new instances work fine but loading and saving doesn't work
yet.  My use of the binding package is still a shot in the dark. Can you
maybe comment on the way I used the various parts of PEAK below:

from peak.storage.SQL import MySQLConnection
from peak.api import storage
from peak.api import binding, model
from peak.model.datatypes import String


class Address:

    server = binding.requireBinding('server')
    db = binding.requireBinding('db')
    user = binding.requireBinding('user')
    passwd = binding.requireBinding('passwd')


class ContactsDB(MySQLConnection):

    __implements__ = storage.ISQLConnection

    address = Address()
    address.server = 'localhost'
    address.db = 'Contacts'
    address.user = 'roche'
    address.passwd = 'mypasswd'


class Contact(model.Element):

    Name = binding.New(String)
    Surname = binding.New(String)
    HomePhone = binding.New(String)
    WorkPhone = binding.New(String)
    Email = binding.New(String)


class ContactDM(storage.EntityDM):

    defaultClass = Contact

    attrs = ['Name', 'Surname', 'HomePhone', 'WorkPhone', 'Email']

    DBConn = binding.bindTo(storage.ISQLConnection)

    def defaultState(self, ob):
        state = {}
        for attr in self.attrs:
            state[attr] = ''
        return state

    def load(self, oid, ob):
        print "load"
        result = self.DBConn('SELECT * FROM Contact WHERE oid=%s'%oid)
        state = {}
        for r in result:
            for attr in self.attrs:
                value = getattr(r, attr)
                state[attr] = value
        return state


    def new(self, ob):
        print "new"
        sql = "INSERT INTO Contact (%s) VALUES (" % ','.join(self.attrs)
        values = []
        for attr in self.attrs:
            values.append('"%s"' % getattr(ob, attr))
        sql += ','.join(values) + ")"
        self.DBConn(sql)

        # get new oid
        sql = """SELECT MAX(oid) FROM Contact"""
        for r in self.DBConn(sql):
            return r[0]

    def save(self, ob):
        sql = "UPDATE Contact SET "
        sql_where = "oid = %s" % ob._p_oid
        values = []
        for attr in self.attrs:
            value = getattr(ob, attr)
            values.append('%s = "%s"' % (attr, value))
        sql += ','.join(values)
        sql += sql_where
        self.DBConn(sql)


db = ContactsDB()
db._open()

myDM = ContactDM()
myDM.DBConn = db

# Create a new instance
storage.begin(ContactDM)
ni = myDM.newItem()
ni.Name = 'John'
ni.Surname = 'Smith'
ni.HomePhone = '888 0000'
ni.WorkPhone = '999 0000'
ni.Email = 'john at smith.com'
storage.commit(ContactDM)

# Retrieve and modify existing instance with oid=1
storage.begin(ContactDM)
ni = myDM.preloadState(1)

# I would have thought that the object will get loaded here but it
# doesn't.  If I call print ni.HomePhone before assigning to it the load
# method on the DM does get called.
ni.HomePhone = '777 0000'
storage.commit(ContactDM)

-- 
Roché Compaan
Upfront Systems                 http://www.upfrontsystems.co.za



More information about the PEAK mailing list