| 1 | def build_filter(base, clauses, mode): |
| 2 | """Combine clauses with mode ("all" -> AND, "any" -> OR) and AND them onto base. |
| 3 | |
| 4 | More than one clause must be parenthesised so the combination binds before |
| 5 | the AND with base: build_filter("x=1", ["a=2", "b=3"], "any") |
| 6 | -> 'x=1 AND (a=2 OR b=3)'. A single clause needs no parentheses. |
| 7 | """ |
| 8 | joiner = " AND " if mode == "all" else " OR " |
| 9 | combined = joiner.join(clauses) |
| 10 | return base + " AND " + combined |
| 11 |