I generally avoid ORM's and DB to class mapping automagic as I've
found it a tad frustating when working with schema's not built by the
ORM package or when I need to do something truly bizarre or specific
only to a certian RDBMS.  So I hacked this together real quick and was
wondering if anyone could see any serious problems with using this for
a user base of maybe 4-5 people to be used as a control panel and
reports system.

   Using wire-shark to watch a single query transaction from a
controller, it looks like some of what SQLAlchemy does ( show
variables like ..., describe table, etc) stays resident in memory
between request cycles ( route, controller, render) so my only concern
is for SQLAlchemy handling things this way.

*ignore the credentials as I've got a proxy that handles that stuff
for me based upon which schema is requested on initial login.

#models/MultiSchema.py
import sqlalchemy as SA
from sqlalchemy.engine.url import URL

class MultiSchema(object):
    schemas = {}
    def addSchema(self,name,schemaURL):
        self.schemas[name] = schemaURL

    def __getattr__(self, attr):
        if attr in self.schemas:
            if type(self.schemas[attr]) == URL: #1st time calling this
schema, connect using provided URL
                tempMD = SA.MetaData(self.schemas[attr])
                self.schemas[attr] = MultiTable(attr,tempMD)
                del(tempMD)
            return self.schemas.get(attr, None)
        else:
            raise ValueError("There is no schema of %s" % attr)


class MultiTable(object):

    def __init__(self, parent, MD):
        self.parent = parent
        self.MD = MD
        self.tables = {}

    def __getattr__(self, attr):
        print "You're trying to get %s.%s!" % (self.parent,attr)
        if attr not in self.tables:
            self.tables[attr] = SA.Table(attr,self.MD,autoload=True)
        return self.tables[attr]


commonSchemaItems = dict(
    username = 'read_only',
    password = ' ',
    host     = 'dbproxy.local',
    port     = '12345'  #The kind of thing an idiot would have on his
luggage!
)

SchemaList = {
    'cron_reports':
URL('mysql',database='cron_reports',**commonSchemaItems),
    'pyrets_cove':
URL('mysql',database='pyrets_cove',**commonSchemaItems),
    'enterprise':
URL('mysql',database='enterprise',**commonSchemaItems),
    'idx':          URL('mysql',database='idx',**commonSchemaItems),
    'syslog':  URL('mysql',database='syslog',**commonSchemaItems)
}

DB = MultiSchema()
for name, uri in SchemaList.iteritems():
    DB.addSchema(name,uri)

######################
#Usage in controller


from multischema.models.MultiSchema import DB


#in an instancemethod

          quick way
           res = DB.idx.idx_master.select().limit(10).execute()
           c.result = res
           c.columns = res.c.keys()
           #have a template render data

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to