For example, filter for small pets with tails or wings (which is not the same as small pets with tails or pets with wings): >>> Group( >>> filter("type", "pet"), >>> filter("weight", (4,6), ":"), >>> Group( >>>
(self, **kwargs)
| 1110 | self.operator = kwargs.get("operator", AND) |
| 1111 | |
| 1112 | def SQL(self, **kwargs): |
| 1113 | """ For example, filter for small pets with tails or wings |
| 1114 | (which is not the same as small pets with tails or pets with wings): |
| 1115 | >>> Group( |
| 1116 | >>> filter("type", "pet"), |
| 1117 | >>> filter("weight", (4,6), ":"), |
| 1118 | >>> Group( |
| 1119 | >>> filter("tail", True), |
| 1120 | >>> filter("wing", True), operator=OR)) |
| 1121 | Yields: |
| 1122 | "type='pet' and weight between 4 and 6 and (tail=1 or wing=1)" |
| 1123 | """ |
| 1124 | # Remember to pass the right escape() function as optional parameter. |
| 1125 | a = [] |
| 1126 | for filter in self: |
| 1127 | # Traverse subgroups recursively. |
| 1128 | if isinstance(filter, Group): |
| 1129 | a.append("(%s)" % filter.SQL(**kwargs)) |
| 1130 | continue |
| 1131 | # Convert filter() to string with cmp() - see above. |
| 1132 | if isinstance(filter, (list, tuple)): |
| 1133 | a.append(cmp(*filter, **kwargs)) |
| 1134 | continue |
| 1135 | raise TypeError, "Group can contain other Group or filter(), not %s" % type(filter) |
| 1136 | return (" %s " % self.operator).join(a) |
| 1137 | |
| 1138 | sql = SQL |
| 1139 |