Abstract some of the query logic for insert/select