On Jun 11, 2006, at 10:31 PM, Robert Leftwich wrote:

> Next question - I have a few instances of very hairy queries that  
> are done as
> pure text (mainly for my sanity as they are hangovers from the bad  
> old days of
> using another ORM and took ages to get right). These are executed  
> in the helper
> functions using:
>
>    obj.mapper.table.engine.execute('''... lots of  
> sql ...''').fetchall()
>
> This is (obviously) not the cleanest approach, but it worked. In  
> 0.2x, I can use
> obj.mapper.mapped_table.metadata.engine.execute() but that makes me  
> unwell just
> looking at it :-)
>
> What is the preferred/recommended way to get hold of an engine for  
> these type of
> queries (or using text() for that matter), given that these helper  
> functions
> only have access to the class mapper?

having some function in your program somewhere that simply returns  
the Engine, which is basically the "Registry" pattern:

http://www.martinfowler.com/eaaCatalog/registry.html

if youre using multiple engines, then you still may have to rely upon  
the more lengthy lookup youre doing above...but you can abstract that  
away into a registry-like pattern, such as get_engine_by_object() or  
similar.


_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to