import io
import json


def build_insert_rows(schema, table, payload):
    """Return the INSERT INTO statement as a string."""
    out = io.StringIO()

    out.write('INSERT INTO {}.{}\n'.format(schema, table))
    out.write('SELECT column1, column2, parse_json(column3)\n')
    out.write('FROM VALUES\n')

    title = payload['title']
    data = payload['data']
    count = len(data)
    for i, obj in enumerate(data):
        out.write("('{}', current_timestamp, '{}')".format(
            title, json.dumps(obj)
        ))
        if i != count - 1:
            out.write(',')
        out.write('\n')

    return out.getvalue()