refactor SQL report to allow more modular use of code

I needed this in to make it easier to render multiple reports in a single request.