Adapted to make it work with other database engines.