move sql to model