Refactored sql logic into separate file for use in an upcoming PostreSQL adapter.