refactoring of sql controller