move more SQL code to ::DB module