Here is my (very small) contribution to sqlobject, that I find very cool
to program with.
There are 3 parts:
- pickle support: I store sqlobject instances directly in my web session
object which is picked.
- getNone is just a helper routine ...
- a very interesting one is updateSchema:
I was trying to find a simple way to change the sqlobject schema, and
propagate it into the database automaticaly.
This is now possible with updateSchema which drops or add columns that
are not in the database.
The routine won't change anything if you don't set the doIt arg to True

I know this should have been written in the SqlMeta class, but as I have
multiple environment (prod+multiple dev) I need my project to be
portable, and I don't want to change anything in sqlobject.
I found some changes that could be done un sqlobject's col.py:
the mysqlCreateSql ( or more precisely _extraSQL()) does not use the
default parameter. So when adding a new column, I can't give it a
default value for already existing rows.

When I have some time (not now), I will try to make a clean patch+tests.
(SQLObject 0.7.1dev)

class MySqlObject(SQLObject):
        class sqlmeta:
                style = MixedCaseStyle()
        def __getstate__(self):
                return self.id
        def __setstate__(self,id):
            pass
        def __new__(cls, *args, **kw):
                if len(args) == 1:
                        return cls.get(args[0])
                return SQLObject.__new__(cls, *args, **kw)

        def __getnewargs__(self):
                return (self.id,)

        def getNone(cls,id):
                try:
                        return cls.get(id)
                except SQLObjectNotFound:
                        return None
                except:
                        raise
        getNone=classmethod(getNone)

        def updateSchema(self, doIt=False, connection=None):
                conn = connection or self._connection
                schema={}
                cols=self.sqlmeta.columns
                for a in conn.columnsFromSchema(self.sqlmeta.table, self):
                        schema[a.name]=a
                        #print a.name
                        if a.name not in cols.keys():
                                print a.name ,'not in class'
                                if doIt:
                                        conn.query('ALTER TABLE %s DROP
COLUMN %s'%(self.sqlmeta.table,str(a.name)))
                for name,col in cols.iteritems():
                        if name not in schema.keys():
                                print name ,'not in schema'
                                if doIt:
                                       
conn.addColumn(self.sqlmeta.table,col)

        updateSchema=classmethod(updateSchema)


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to