recursively construct conditions in where clause