def escape(identifier, ansi_quotes, should_quote):
    """
    Escape identifiers.

    ANSI uses single quotes, but many databases use back quotes.

    """
    if not should_quote(identifier):
        return identifier
    quote = '"' if ansi_quotes else '`'
    identifier = identifier.replace(quote, 2 * quote)
    return '{0}{1}{2}'.format(quote, identifier, quote)
