Fixed setting of compound parentheses, fixes #1168
I fixed this using slightly more compact code, hope it's more readable now.
This PR fixes the issue in #1168 by only wrapping non-compound queries in parentheses.

Changed the corresponding test as well.