finished refactoring for sql statements