A possible way to refactor database code to remove code duplication and, I hope, provide a simpler, more intuitive model to be copied by others