ps = MyModel.objects.filter(foo__lt=Param('a').prepare()The result is now a callable that accepts one parameter - "a". To invoke the query:results = ps(a=1000)Clearly it's early days yet - I've written no code. And akaariai has pointed out already there's some corners cases which won't work well with existing behaviours (e.g. foo=None being silently translated to foo__isnull=True), but it's best to get this idea under wider public scrutiny earlier, rather than later.
--To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3123fda6-d7b3-46d3-82ec-28ed3003e837%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/201403252136.50615.shai%40platonix.com.
Firstly -- can we assume anyone using this feature is not a complete novice, and so will take the caveats mentioned into consideration?
Yes, prepared statements are local to their connection/session. And would be expected to "go away" should the connection drop. However, in most cases connection drop-out is quite rare [at least, in my experience], and would be even more rare in the case of people using certain connection pooling tools.
Assuming it's not fatal to a transaction, would it be feasible for a prepared statement to work on the assumption is has been prepared, and if it hasn't, prepare itself and continue? I'd prefer this to, for instance, having PS listen for connection closed signals.
Further, as an "expert" feature, would it be unreasonable to limit its use to cases where you know it will benefit, but also only be used in a single connection? True, this limits its use cases somewhat, but it's still available when of benefit.
As to Jeremy's idea of multiple shapes of a single prepared query based on _potential_ arguments, I disagree. Much safer [and easier to code] to detail in the documentation that "because we can't guess at values, you can't rely on these ORM shortcuts".
On Tuesday, March 25, 2014 11:57:51 PM UTC+1, Curtis Maloney wrote:Firstly -- can we assume anyone using this feature is not a complete novice, and so will take the caveats mentioned into consideration?
Yes
Yes, prepared statements are local to their connection/session. And would be expected to "go away" should the connection drop. However, in most cases connection drop-out is quite rare [at least, in my experience], and would be even more rare in the case of people using certain connection pooling tools.
Assume connection drops don't exist for now. How can/will Django know if a query is prepared already and when does it have to prepare it, or do you expect me to issue a PREPARE statement everywhere I use it and handle the error if it already exists?! I can easily see this working for management scripts, but not for web requests which are possible routed through pgpool etc… [That said, I have no real experience with those things, but I'd like to know how this can work]
Assuming it's not fatal to a transaction, would it be feasible for a prepared statement to work on the assumption is has been prepared, and if it hasn't, prepare itself and continue? I'd prefer this to, for instance, having PS listen for connection closed signals.
See above, how can you reliable determine if this assumption holds up?
Further, as an "expert" feature, would it be unreasonable to limit its use to cases where you know it will benefit, but also only be used in a single connection? True, this limits its use cases somewhat, but it's still available when of benefit.
What do you mean by "single connection", persistent connections are single connections and as you said above prepared statements are local to the connection/session anyways…
Further, as an "expert" feature, would it be unreasonable to limit its use to cases where you know it will benefit, but also only be used in a single connection? True, this limits its use cases somewhat, but it's still available when of benefit.
What do you mean by "single connection", persistent connections are single connections and as you said above prepared statements are local to the connection/session anyways…
And I would expect the prepared statement to persist between requests in that case.
If I thought we could rely on DB dis/connect signals [maybe we can, I don't know yet] we could teach prepared statements to track that and re-prepare themselves on first use.
Just throwing ideas out there... seeing which ones excite :)
We can do this by having a map of SQL for the statement -> name of the prepared statement in the connection. On connection close the known statements are cleaned.
There was a recent query about prepared statements on the db-sig mailing list, too. Apparently thought is being given to adding such functionality to Psycopg. If such functionality is added, it could be useful to support, I suppose. Some SQL engines apparently benefit from the techinque. MS SQL Server is not one of them, so I have not bothered to add support for them to adodbapi. If I do so, it will use the same api as mxodbc uses now (a copy of the SQL statement is kept with the cursor). [note: my reading of Microsoft's recommendation is not "don't do that", it is "why bother?".]
Pep-0249 is silent on the subject of how to support prepared statements, so any existing systems are likely to do so differently. In particular, there can be no expectation that there is any support whatsoever for the concept, so it will have to be emulated where not present (-- i.e. almost everywhere).