On Jan 26, 2010, at 7:48 PM, Michael Chambliss wrote:

> Hello,
> 
> I'm new to SQLAlchemy (and really Python in general) and admittedly I'm 
> probably not following the best process for learning it.  Ultimately, I'd 
> prefer to deal with raw SQL as opposed to working through the expression 
> building methods despite the benefits of the framework I leave on the table.  
> The down side, of course, is that the tutorials aren't written for this 
> wanton approach.
> 
> Presently, I'm trying to determine the best way to map a class against an 
> arbitrary select where the select is constructed from raw SQL.  Based on 
> this, it's possible using the expression builders:
> 
> http://www.sqlalchemy.org/docs/mappers.html#mapping-a-class-against-arbitrary-selects
> 
> so I assume it's possible using SQL.  I've researched the text() and Query 
> from_statement() methods, but these don't appear to be applicable in this 
> case.  Is there another method to short-cut the mapping of a rowset 
> (generated by raw SQL) to an object?

from_statement() is the primary means of doing this, assuming you're mapped to 
some kind of construct already and just need to select the rows from some 
particular statement you happen to have as a string.   This means, the 
configuration of your application would consist of mapping your classes to 
table metadata as per the documentation, and then at query time you can load 
and persist objects, using all hand-written SQL to load rows.

But the literal request to "map to an arbitrary select with raw SQL" is 
strange, but this may be semantic - the word "map" in SQLA parlance means to 
construct a mapper(), which is a configuration-time, not a query-time, concern. 
    Your mapper would be against the fixed SQL statement, and would be invoked 
when, for example, you said query.all().   However, that would be all you can 
do with it - SQLA doesn't parse SQL strings, so its impossible for it to, by 
itself, alter your string SQL statement to add filtering criterion, ordering, 
or do anything else with it.   Your mapper also wouldn't be able to persist 
anything - since the requirement that you "map to raw SQL" means you don't want 
to tell it which individual tables are referenced in your select.

But its all absolutely possible I think we just need more specifics as to what 
patterns you're looking to achieve.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to