at Mike Orr's prodding, ive worked up a SQLAlchemy context object  
based on his proposal for a facade that deals with the various session 
(context)/engine/metadata mixtures.  I think this would be a good  
idea for the very reason that it encapsulates all the details of how  
engines/metadata/session/etc get created and used.  right now we have  
tutorials and such all throwing out BoundMetaDatas and Sessions with  
bind_to's and a lot of mixing up of paradigms.  by putting it all  
inside a single facade, nobody has to make those decisions anymore if  
they dont want to.  the facade would be configurable to support a few  
different operational paradigms, including engines bound to tables,  
or engines bound to sessions...each of which has its pros and cons  
depending on the type of app youre writing (i.e. one app with one  
database, one app with multiple databases for different objects, one  
app with multiple databases for different use cases (e.g. separate  
readonly/write db's), multiple apps on multiple databases, multiple  
apps sharing common tables across multiple databases).  by default it  
would internally bind engines to metadatas which is the most flexible  
configuration for a single application that may persist different  
model classes to different dbs.

this object would just give you everything you need:  metadata,  
engine, session.  how they got constructed, which one connects to  
what, you dont care.  because really, most people dont.  qualified  
method accessors engine/metadata for alternate connection names would  
be provided.   multiple applications that talk to different  
tablesets, i.e. for middleware that has its own deal going on, just  
makes its own SAContext.   this probably would obviate the need for a  
"global engine registry" at all (though im not sure).

from pylons.database import SAContext

sac = SAContext()   # connects to engine in the .ini file by default

sac = SAContext(url='sqlite://', engine_args={})   # or send a url,  
args in

# create a table.  metadata is a BoundMetaData to the default engine.
users = Table('users', sac.metadata, Column(...)...)

# alternatively, we could have a table creation function on the  
object w/o the metadata argument (opinions?)
users = sac.define_table('users', Column(...)...)

# add extra engine
sac.add_engine('pgconn', url='postgres://', engine_args={})

# table on alt engine.  metadata is a BoundMetaData to the 'pgconn'  
engine.
remote_users = Table('remote_users', sac.get_metadata('pgconn'),  
Column(...)...)

# mappers can bind to the sessioncontext via the SAContext directly
# (its got a SessionContext inside, sends out SessionContext.ext out
# via "ext")
mapper(User, users, extension=sac.ext)
mapper(RemoteUser, remote_users, extension=sac.ext)

# query accessor
sac.query(Users).filter_by(...).list()

# session.  gets pulled from the SessionContext.  note you never deal
# with the context itself.
# session doesnt even have a bind_to, its just using the engines for
# each table it gets.
sac.session.save(User())
sac.session.save(RemoteUser())

# that way this statement saves each user to its correct DB without
# any issues.
sac.session.flush()

if we get everyone on a simple object like this, then we can plug in  
strategies for other scenarios, build up clustering strategies, etc.






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
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