Refactored from/to sql code to method