On Aug 28, 2019, at 12:45, stpa...@gmail.com wrote:
> 
> In the thread from
> 2013 where this issue was discussed, many people wanted `sql"..."`
> literal to be available as literal and nothing else.

Since this specific use has come up a few times—and a similar feature in other 
languages—can you summarize exactly what people want from this one?

IIRC, DB-API 2.0 doesn’t have any notion of compiled statements, or bound 
statements, just this:

    Connection.execute(statement: str, *args) -> Cursor

So the only thing I can think of is that sql"…" is a shortcut for that. Maybe:

    curs = sql"SELECT lastname FROM person WHERE firstname={firstname}"

… which would do the equivalent of:

    curs = conn.execute("SELECT lastname FROM person WHERE firstname=?", 
firstname)

… except that it knows whether your particular database library uses ? or %s or 
whatever for SQL params.

I can see how that could be useful, but I’m not sure how it could be easily 
implemented.

First, it has to know where to find your connection object. Maybe the library 
that exposes the prefix requires you to put the connection in a global (or 
threadlocal or contextvar) with a specific name, or manages a pool of 
connections that it stores in its own module or something? But that seems 
simultaneously too magical and too restrictive. 

And then it has to do f-string-style evaluation of the brace contents, in your 
scope, to get the args to pass along. Which I’d assume means that prefix 
handlers need to get passed locals and globals, so the sql prefix handler can 
eval each braced expression? (Even that wouldn’t be as good as f-strings, but 
it might be good enough here?)

Even with all that, I‘m pretty sure I’d never use it. I’m often willing to 
bring magic into my database API, but only if I get a lot more magic (an 
expression-builder library, a full-blown ORM, that thing that I forget the name 
of that translates generators into SQL queries quasi-LINQ-style, etc.). But 
maybe there are lots of people who do want just this much magic and no more. Is 
this roughly what people are asking for?

If so, is that eval magic needed for any other examples you’ve seen besides 
sql? It’s definitely not needed for regexes, paths, really-raw strings, or any 
of the numeric examples, but if it is needed for more than one good example, 
it’s probably still worth looking at whether it’s feasible.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TIDUG5ZWIX2ATV7QZMYULQCFPERF3LMI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to