Michael Bayer wrote:
On Jan 26, 2010, at 7:48 PM, Michael Chambliss wrote:
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
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.
Hey Michael - thanks for the patient and helpful response. I played
around with the from_statement() approach earlier today, but what I was
able to derive seemed to follow the standard model of define table,
define class, map table to class, execute query. That approach would be
great assuming I can map to some composite result (IE,
multi-table/function). Perhaps I need to dive further into this to
determine how joins are handled and how the mapping should be defined
for them. The original example I linked seemed to imply some mapping
magic in that the "Customer" class wasn't defined but was mapped to the
complex "Selectable". However, my research and attempts to do this
mapping with from_statement() proved fruitless.
In a theoretical example, say I have a CAR table that refers to both a
CAR_TYPE table and CAR_ATTRIBUTES table. CAR_TYPE is simply an
enumeration for a static list of types, and CAR_ATTRIBUTES is an
arbitrarily long list of key,value attributes (color, weight, top speed,
etc). So, ultimately, a "Car" is made up of these three.
I'd want to bake all of these together, passing in a CAR.ID (primary
key) to map to a Car instance. I prefer to live in SQL because I'm
pretty good at it, and I need to reference, specifically, Oracle Spatial
and Workspace functions. I do not, however, need to chain additional
filters off of this, handle updates/inserts (at least at this point),
etc. I'm literally just looking for a "cheap" way to map a row to an
object and scoop up connection pooling, type handling, and other great
things I'll probably learn about as I go.
Thanks again for your help!
Mike
--
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.