from functools import reduce


def _concat_query_text(rows_items, key='id', text_col='query'):
    """
    Queries related tables tend to split the query text over several lines.
    This function concats the text.
    """
    queries = {}
    ret = []
    for row in rows_items:
        queries.setdefault(row[key], []).append(row)

    for query in queries.values():
        query_text = reduce(lambda text, query_data: "{}{}".format(text, query_data[text_col].strip()), query, "")
        query[0][text_col] = query_text.strip()
        ret.append(query[0])

    return ret