Format and escape queries