Michael Chambliss wrote:
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.
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.
Michael - this may better illustrate the question.  Note the ??query??.

engine = create_engine('oracle://user:passw...@server:1521/database')
Session = sessionmaker(bind=engine)
s = Session()

metadata = MetaData()

vehicle_query = """
    select
        v.vehicle_id as vehicle_id,
        v.name as vehicle_name,
        v.description as vehicle_description,
        vt.name as vehicle_type,
        vs.name as vehicle_status,
        v.modify_dttm as vehicle_modify_dttm
    from
        vehicle v,
        vehicle_type vt,
        vehicle_status vs
    where
        v.vehicle_id = :vehicle_id
        and vs.vehicle_status_id = v.vehicle_status_id
        and vt.vehicle_type_id = v.vehicle_type_id
"""

class Vehicle(object):
    def __init__(self, vehicle_id, vehicle_name, vehicle_description,
                 vehicle_type, vehicle_status, vehicle_modify_dttm):
        self.vehicle_id = vehicle_id
        self.vehicle_name = vehicle_name
        self.vehicle_description = vehicle_description
        self.vehicle_type = vehicle_type
        self.vehicle_status = vehicle_status,
        self.vehicle_modify_dttm = vehicle_modify_dttm

    def __repr__(self):
        return "<Name('%s')>" % (self.vehicle_name)

# Here's where I get sideways...
# Obviously this won't work, but I'm not sure how to map the query to the class
mapper(Vehicle, ??query??)

metadata.bind = engine

q = s.query(Vehicle).from_statement(vehicle_query).params(vehicle_id=123)

for vehicle in q:
    print vehicle.vehicle_id, vehicle.vehicle_name

#...

Thanks,
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.

Reply via email to