refactor common insert and update queries