Parse input and return a list of SQL statements

def parse_statements(queries_input, engine):
    """ Parse input and return a list of SQL statements """

    queries = []
    query = ''
    sql_delimiter = ';'

    # Possible delimiters used in PostgreSQL functions
    postgres_func_delimiters = ['$$', '##']

    # Split input by lines
    lines = queries_input.strip().split('\n')

    for k, line in enumerate(lines):
        # Strip line
        line = line.strip()

        # Skip empty lines and comments
        if not line or line.startswith('--'):
            continue

        # Detect new SQL delimiter
        if engine == 'mysql' and line.upper().startswith('DELIMITER '):
            sql_delimiter = line.split()[1]
            continue
        elif engine == 'postgresql' and [delimiter for delimiter in postgres_func_delimiters if 'AS ' + delimiter in line.upper()]:
            sql_delimiter = line.split()[-1]

            # Ensure that we leave 'AS [DELIMITER]'
            query += line + '\n'

            continue

        # Statement is not finished
        if sql_delimiter not in line and k != len(lines) - 1:
            # Append line
            query += line + '\n'
        else:  # Statement is finished
            # Replace non default delimiter
            if sql_delimiter != ';' and engine == 'mysql' and line.endswith(sql_delimiter):
                line = line.replace(sql_delimiter, ';')

            queries.append(query + line)
            query = ''

    return queries