pypika icon indicating copy to clipboard operation
pypika copied to clipboard

add API for just converting Python input values to SQL

Open jannikmi opened this issue 3 years ago • 0 comments

I have the use case of preparing and then executing an sql command (cf. https://www.postgresql.org/docs/current/sql-prepare.html) During execution I need to convert the values to pass to the prepared command:

EXECUTE insertion_cmd(NULL, 'test')

For that it would be nice if pypika supported conversions of input values without creating a query first. I have created following workaround:


def get_terms(query):
    terms = []
    for row in query._values:
        terms += [term for term in row]
    return terms


def values2sql(*values):
    query = Query.into(nodes).insert(*values)
    terms = get_terms(query)
    vals_in_sql = [
        term.get_sql(
            with_alias=True,
            subquery=True,
        )
        for term in terms
    ]
    sql_val_str = ", ".join(vals_in_sql)
    return sql_val_str

jannikmi avatar May 31 '21 12:05 jannikmi