On Jul 31, 2013, at 10:53 PM, Matt Murphy <matthew.john.mur...@gmail.com> wrote:

> 
> When I look at the SQL that is created it looks like this:
> 
> FROM components
> WHERE NOT (components.major < %(major_1)s OR components.major = %(major_2)s 
> AND components.minor < %(minor_1)s OR components.major = %(major_3)s AND 
> components.minor = %(minor_2)s AND components.tiny < %(tiny_1)s OR 
> components.major = %(major_4)s AND components.minor = %(minor_3)s AND 
> components.tiny = %(tiny_2)s AND components.phase < %(phase_1)s OR 
> components.major = %(major_5)s AND components.minor = %(minor_4)s AND 
> components.tiny = %(tiny_3)s AND components.phase = %(phase_2)s AND 
> components.build < %(build_1)s)
> 
> My first assumption was that each argument in an and_ or an or_ would be 
> wrapped in parenthesis, but they are not.  Is there any way to wrap my 
> statements in parenthesis in the SQL?  This query does not work because all 
> the statements are executed together.

the parenthesis are rendered using operator precedence.    AND takes higher 
precedence than OR, so parenthesis don't render for that.   "a AND b OR c AND d 
OR e AND f ...." is equivalent to "(a AND b) OR (c AND d) OR (e AND f) ....".

here's a demonstration:

import itertools

for a, b, c, d, e, f in itertools.product(*[(True, False) for i in range(6)]):
    print(a, b, c, d, e, f)

    assert (a and b) or (c and d) or (e and f) == (a and b or c and d or e and 
f)
    assert (a and b and c) or (d and e and f) == (a and b and c or d and e and 
f)
    assert (a and b and c and d) or (e and f) == (a and b and c and d or e and 
f)

there's a call self_group() that will force the parenthesis,  and_(a, b, 
c).self_group() but it shouldn't be needed.    


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to