diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-08-03 21:19:32 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2008-08-03 21:19:32 +0000 |
commit | edf6b16fae38b4c103ed2827ee5448fec2fdcb1a (patch) | |
tree | 2d02c785663acab948e3ab8cf65e50ba738e60b6 /test/sql/select.py | |
parent | 8df49e7194f272573f043343e601c4ee4beb0f70 (diff) | |
download | sqlalchemy-edf6b16fae38b4c103ed2827ee5448fec2fdcb1a.tar.gz |
- compiler visit_label() checks a flag "within_order_by" and will render its own name
and not its contained expression, if the dialect reports true for supports_simple_order_by_label.
the flag is not propagated forwards, meant to closely mimic the syntax Postgres expects which is
that only a simple name can be in the ORDER BY, not a more complex expression or function call
with the label name embedded (mysql and sqlite support more complex expressions).
This further sets the standard for propigation of **kwargs within compiler, that we can't just send
**kwargs along blindly to each XXX.process() call; whenever a **kwarg needs to propagate through,
most methods will have to be aware of it and know when they should send it on forward and when not.
This was actually already the case with result_map as well.
The supports_simple_order_by dialect flag defaults to True but is conservatively explicitly set to
False on all dialects except SQLite/MySQL/Postgres to start.
[ticket:1068]
Diffstat (limited to 'test/sql/select.py')
-rw-r--r-- | test/sql/select.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/test/sql/select.py b/test/sql/select.py index 70d21798c..b4e47c3e0 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -3,6 +3,7 @@ import datetime, re, operator from sqlalchemy import * from sqlalchemy import exc, sql, util from sqlalchemy.sql import table, column, compiler +from sqlalchemy.engine import default from sqlalchemy.databases import sqlite, postgres, mysql, oracle, firebird, mssql from testlib import * @@ -326,6 +327,54 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A x = func.lala(table1.c.myid).label('foo') self.assert_compile(select([x], x==5), "SELECT lala(mytable.myid) AS foo FROM mytable WHERE lala(mytable.myid) = :param_1") + def test_labels_in_expressions(self): + """test that label() constructs in ORDER BY render as the labelname. + + Postgres' behavior was used as the guide for this, + so that only a simple label expression + and not a more complex expression involving the label + name would be rendered using the label name. + + """ + lab1 = (table1.c.myid + "12").label('foo') + lab2 = func.somefunc(table1.c.name).label('bar') + + dialect = default.DefaultDialect() + self.assert_compile(select([lab1, lab2]).order_by(lab1, desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY foo, bar DESC", + dialect=dialect + ) + + # the function embedded label renders as the function + self.assert_compile(select([lab1, lab2]).order_by(func.hoho(lab1), desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY hoho(mytable.myid + :myid_1), bar DESC", + dialect=dialect + ) + + # binary expressions render as the expression without labels + self.assert_compile(select([lab1, lab2]).order_by(lab1 + "test"), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY mytable.myid + :myid_1 + :param_1", + dialect=dialect + ) + + # labels within functions in the columns clause render with the expression + self.assert_compile( + select([lab1, func.foo(lab1)]), + "SELECT mytable.myid + :myid_1 AS foo, foo(mytable.myid + :myid_1) AS foo_1 FROM mytable", + dialect=dialect + ) + + dialect = default.DefaultDialect() + dialect.supports_simple_order_by_label = False + self.assert_compile(select([lab1, lab2]).order_by(lab1, desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY mytable.myid + :myid_1, somefunc(mytable.name) DESC", + dialect=dialect + ) + self.assert_compile(select([lab1, lab2]).order_by(func.hoho(lab1), desc(lab2)), + "SELECT mytable.myid + :myid_1 AS foo, somefunc(mytable.name) AS bar FROM mytable ORDER BY hoho(mytable.myid + :myid_1), somefunc(mytable.name) DESC", + dialect=dialect + ) + def test_conjunctions(self): self.assert_compile( and_(table1.c.myid == 12, table1.c.name=='asdf', table2.c.othername == 'foo', "sysdate() = today()"), |