kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.49k stars 294 forks source link

Building prepared statements #163

Closed grigi closed 5 years ago

grigi commented 5 years ago

Hi, in Tortoise-ORM we would like to build prepared statements, specifically for inserts. This would allow us to use the DB-provided query cache, and DB-specific bulk insert operations, or update operations.

Right now we have a statement like: (cleaned up)

Query.into(Table(table_name)).columns(*columns).insert(*values)

Where columns and values are lists of equal length.

This generates SQL like so:

INSERT INTO table_name (col1,col2,col3) VALUES ('value1', 'value2', 'value3)

But to use the execute_many() statement, I need to have SQL like this:

INSERT INTO table_name (col1,col2,col3) VALUES (?,?,?)

(or %, depending on the DB driver)

It also raises the concern of escaping, where I would normally want to delegate escaping the values to the DB driver itself. Is escaping happening correctly?

Now, I can manually generate the SQL (I'd rather not bypass this excellent library unless needed). What would be the best way for me to get there?

twheys commented 5 years ago

A couple have requested this already. I don't think I'll have time to do it in the immediate future, but I could help if you would be interested in trying to add the feature yourself? Let me know if you'd be interested.

twheys commented 5 years ago

Going to close this as a duplicate to #113

grigi commented 5 years ago

I am thinking of doing something like this: Query.into(Table(table_name)).columns(*columns).prepared()

This is easy for inserts, less so for everything else. Will have to think on it some more.

MySQL wants %s, SQLite ? and PostgreSQL &1,&2 etc... Would not be surprised if it also varies by driver...