Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

Mapped Columns

Sometimes you just don't have enough control over your database, but you still want those pretty Python names for your properties, instead of the ugly ones your database is saddled with. With a custom StorageManager?, you can map from one to the other easily; just override the column_name and table_name methods (with revision 123 or later):

from dejavu.storage.storepypgsql import StorageManagerPgSQL

class MyMappingSM(StorageManagerPgSQL):
    
    tables = {"Address": "dbo.CONSTIT_ADDRESS"}
    
    cols = {"Address.ID": "ID",
            "Address.DirectoryID": "CONSTIT_ID",
            "Address.AddressID": "ADDRESS_ID",
            "Address.AddressType": ("(SELECT LONGDESCRIPTION FROM "
                "dbo.TABLEENTRIES WHERE TABLEENTRIES.TABLEENTRIESID = "
                "CONSTIT_ADDRESS.TYPE) AS ADDRTYPE"),
            "Address.Preferred": "PREFERRED",
            }
    
    def column_name(self, classname, name, full=False, quoted=True):
        """The column name, escaped for SQL. If full, include tablename."""
        name = self.cols.get(classname + "." + name, name)
        return StorageManagerPgSQL.column_name(self, classname, name, full, quoted)
    
    def table_name(self, name, quoted=True):
        """The table name, escaped for SQL."""
        name = self.tables.get(name, name)
        return StorageManagerPgSQL.table_name(self, name, quoted)

Note in this example that, if a name is not found in the cols dict, the name that was supplied will be used unchanged. The same goes for self.tables. This makes it easy to map just a couple of names if you need to.