Re-factor into a sql module