Factorize some database code