Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templating Identifers into SQL #21

Closed
nackjicholson opened this issue Jul 8, 2020 · 6 comments
Closed

Templating Identifers into SQL #21

nackjicholson opened this issue Jul 8, 2020 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@nackjicholson
Copy link
Owner

nackjicholson commented Jul 8, 2020

honza/anosql#45 and honza/anosql#47

Are both asking for support for Identifiers in order to template other values into the SQL, like table names. I've never had need for this, and actually wasn't aware of it as a feature in psycopg2, but it's worth some thought to see if it's something we can do in this project.

One potential problem in the way of doing this is that the other driver libraries like sqlite or asyncpg may not have safe features for doing this. I just really don't know since I've never had to do this kind of table name formatting.

@danmurphy1217
Copy link
Contributor

danmurphy1217 commented Oct 25, 2020

Hmmm, neat idea. Let me think about how we could approach this. Any initial thoughts @nackjicholson?

Thoughts on requiring another identifier, such as --type: <> in the .sql file that defines whether a query is static or dynamic? Then, if --type is 'static' or not specified, the path continues as things currently operate. If the --type is 'dynamic', we can branch off and handle the query differently (still don't have a clear roadmap for this, but maybe we could add another parameter to the QueryDatum class that accepts the dynamic values?)

@jkr78
Copy link

jkr78 commented May 27, 2021

It would be neat to have a possibility to dynamically change the behavior of the query depending on supplied columns.
The simplest example would be a PATCH command for REST API, e.g., update only some columns.

The way I would do this is to use Jinja2 to process SQL and provide access to parameters. If dependencies or performance is an issue, use an operator or smth like this to specify normal or Jinja2 style of SQL.

@frafra
Copy link

frafra commented May 28, 2021

Using string templates or jinja to do this could be dangerous. psycopg2 has sql identities to handle this case correctly.
It would be nice just to use ::colname to specify that the variable should be escaped as identity instead of a value.

@jkr78
Copy link

jkr78 commented Jun 8, 2021

@frafra I didn't mean to use Jinja2 for variable substitution in SQL. Contrary, I think variable substitution must be handled by the driver.

But let's define what problem I would solve with Jinja2. Imagine you have to implement a query to update users table. Table contains id, full_name, email and is_admin. All or some fields may be updated (except id).
Currently, I cannot see how to write it using aiosql without defining every possible update as a separate query.

With help of Jinja2 (or another template) it would look like this:

update "users"
   set {% if 'full_name' in aiosql.fields -%}
       "full_name" = :full_name
       {% endif -%}
       {% if 'email' in aiosql.fields -%}
       "email" = :email
       {% endif -%}
       {% if 'is_admin' in aiosql.fields -%}
       "is_admin" = :is_admin
       {% endif -%}
 where "id" = :id

Here I used aiosql as a variable containing a dict of fields/values passed. SQL arguments/fields left as is. Information about fields passed is used to select what fields should be updated.

@midir99
Copy link

midir99 commented Apr 1, 2022

There is a project called JinjaSQL that allows you to write SQL queries dynamically depending on the values you pass, maybe we could rely on this dependency, but make it optional, only for those users who want templated SQL queries.

Or maybe we can dive into psycopg, extract all the functions that enable one to generate SQL queries and then copy those on aiosql modules: https://github.com/psycopg/psycopg/blob/master/psycopg/psycopg/sql.py

I think that using JinjaSQL would fit better the things aiosql users want to achieve.

@zx80
Copy link
Collaborator

zx80 commented Jun 25, 2022

Hmmm. I agree that it could be helpful in some cases, but ISTM that aiosqlshould not depend (too much) on driver features, but stay as much as possible at the DB API level.

@zx80 zx80 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants