[Sqlalchemy-tickets] [sqlalchemy] #2824: Add ability to query column sets as one entity
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-09-16 10:52:40
|
#2824: Add ability to query column sets as one entity
-------------------------+-----------------------------------------
Reporter: vmagamedov | Owner: zzzeek
Type: enhancement | Status: new
Priority: medium | Milestone:
Component: orm | Severity: no triage selected yet
Keywords: | Progress State: awaiting triage
-------------------------+-----------------------------------------
In our company we have a tool called {{{construct}}}, which is doing next
things:
1. You declare what you want to show in your template (we use Mako, but we
want to make templates as dumb as possible):
{{{#!python
product_struct = Construct(dict(
name=Product.name,
url=apply_(
get_product_url,
args=[Product.id, Product.name],
),
image_url=if_(
Product.main_image_id,
then_=apply_(get_image_url,
args=[Image.id, Image.file_name, Image.store_type,
100, 100]),
else_=None,
),
))
}}}
2. You query this structure using {{{session.query}}}
{{{#!python
products = (
db.session.query(product_struct)
.outerjoin(Product.main_image)
.limit(10)
.all()
)
}}}
3. And you get these results:
{{{#!python
[
Object(name=u'Foo',
url=u'/p1-foo.html',
image_url=u'http://images.example.st/123-foo-100x100.jpg'),
Object(name=u'Bar',
url=u'/p2-bar.html',
image_url=None),
# ...
Object(name=u'Baz',
url=u'/p10-baz.html',
image_url=u'http://images.example.st/789-baz-100x100.jpg'),
]
}}}
{{{Object}}} is a namedtuple-like data structure.
At this time, construct requires custom {{{Query}}} subclass, which
overrides {{{_set_entities}}} and {{{instances}}} methods (plus some other
methods). I find this hacky and I can't find any other solution in how to
make construct and {{{Query}}} work better together.
I've tried to subclass {{{ClauseList}}} and {{{ColumnElement}}} classes,
to provide necessary columns to query (this works), but I can't figure out
how to gather these columns as one entity/structure in the resulting row.
Looks like {{{_ColumnEntity}}} wrapper doesn't support this functionality
or implementation would be also very hacky, {{{_MapperEntity}}} is tied to
mapped classes and {{{Query}}} doesn't support any other option.
Is it possible to support so weird option?
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2824>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|