move sql into model