I am defining a Comparator as follows:
class VersionComparator(CompositeProperty.Comparator):
def __eq__(self, other):
return and_(*[a == b for a, b in
zip(self.__clause_element__().clauses, other.__composite_values__())])
def __lt__(self, other):
lhs = self.__clause_element__().clauses
lhs_major = lhs[0]
lhs_minor = lhs[1]
lhs_tiny = lhs[2]
lhs_phase = lhs[3]
lhs_build = lhs[4]
rhs = other.__composite_values__()
rhs_major = rhs[0]
rhs_minor = rhs[1]
rhs_tiny = rhs[2]
rhs_phase = rhs[3]
rhs_build = rhs[4]
return and_(
or_(
(lhs_major < rhs_major),
and_(lhs_major == rhs_major, lhs_minor < rhs_minor),
and_(lhs_major == rhs_major, lhs_minor == rhs_minor,
lhs_tiny < rhs_tiny),
and_(lhs_major == rhs_major, lhs_minor == rhs_minor,
lhs_tiny == rhs_tiny, lhs_phase < rhs_phase),
and_(lhs_major == rhs_major, lhs_minor == rhs_minor,
lhs_tiny == rhs_tiny, lhs_phase == rhs_phase,
lhs_build < rhs_build)
)
)
def __gt__(self, other):
return not_(self.__lt__(other))
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.
Also, I am not too familiar with how to properly define comparators for a
composite. Am I doing this in an acceptable way? Or is there a better way
to get the two sets of columns and compare them? I copied the base code
from SQLAlchemy Docs.
Thanks,
Matt
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.