import re


def format_query(str_sql):
    """Strips all newlines, excess whitespace, and spaces around commas"""

    stage1 = str_sql.replace("\n", " ")
    stage2 = re.sub(r"\s+", " ", stage1).strip()
    stage3 = re.sub(r"(\s*,\s*)", ",", stage2)

    return stage3