Surely by the time you're programmatically generating SQL queries you should be using either an ORM or some other kind of SQL expression language embedded in a more expressive language (such as SQLAlchemy or the myriad lisp DSPs for relational databases)?