processing the database function
it's too much text,
need carefully test