Fix a lot of bugs in SqlQueryBuilder. Remove dead code.