Move database functionality out of Sporran and into the database