Factorize some db common code.