From: Mike Bayer Date: Sun, 2 Apr 2006 03:22:17 +0000 (+0000) Subject: added "parenthesis" check on binary clauses referencing binary clauses, for [ticket... X-Git-Tag: rel_0_1_6~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3cd04eee8a7584a0bf75e504d3afa35d51d2f7d8;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git added "parenthesis" check on binary clauses referencing binary clauses, for [ticket:144] --- diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index b86c965c00..7f77acf177 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -850,6 +850,10 @@ class BinaryClause(ClauseElement): self.operator = operator self.type = type self.parens = False + if isinstance(self.left, BinaryClause): + self.left.parens = True + if isinstance(self.right, BinaryClause): + self.right.parens = True def copy_container(self): return BinaryClause(self.left.copy_container(), self.right.copy_container(), self.operator) def _get_from_objects(self): diff --git a/test/select.py b/test/select.py index c11cff5ea5..c61b332511 100644 --- a/test/select.py +++ b/test/select.py @@ -346,6 +346,30 @@ FROM mytable, myothertable WHERE foo.id = foofoo(lala) AND datetime(foo) = Today self.runtest(select([literal("foo") + literal("bar")], from_obj=[table1]), "SELECT :literal + :liter_1 FROM mytable") + def testcalculatedcolumns(self): + value_tbl = table('values', + Column('id', Integer), + Column('val1', Float), + Column('val2', Float), + ) + + self.runtest( + select([value_tbl.c.id, (value_tbl.c.val2 - + value_tbl.c.val1)/value_tbl.c.val1]), + "SELECT values.id, (values.val2 - values.val1) / values.val1 FROM values" + ) + + self.runtest( + select([value_tbl.c.id], (value_tbl.c.val2 - + value_tbl.c.val1)/value_tbl.c.val1 > 2.0), + "SELECT values.id FROM values WHERE ((values.val2 - values.val1) / values.val1) > :literal" + ) + + self.runtest( + select([value_tbl.c.id], value_tbl.c.val1 / (value_tbl.c.val2 - value_tbl.c.val1) /value_tbl.c.val1 > 2.0), + "SELECT values.id FROM values WHERE ((values.val1 / (values.val2 - values.val1)) / values.val1) > :literal" + ) + def testfunction(self): """tests the generation of functions using the func keyword""" # test an expression with a function @@ -554,7 +578,7 @@ class CRUDTest(SQLTest): values = { table1.c.name : table1.c.name + "lala", table1.c.myid : func.do_stuff(table1.c.myid, literal('hoho')) - }), "UPDATE mytable SET myid=do_stuff(mytable.myid, :liter_2), name=mytable.name + :mytable_name WHERE mytable.myid = hoho(:hoho) AND mytable.name = :literal + mytable.name + :liter_1") + }), "UPDATE mytable SET myid=do_stuff(mytable.myid, :liter_2), name=mytable.name + :mytable_name WHERE mytable.myid = hoho(:hoho) AND mytable.name = ((:literal + mytable.name) + :liter_1)") def testcorrelatedupdate(self): # test against a straight text subquery