Replies: 1 comment
-
Thanks for posting your solution. As already stated I would be interested in having this feature in diesel itself, but at least for me personally that's something I do not want to spend time on working myself. (This is something that could be added after the 2.0 release, so it's not important for finishing the release). That written I've put together a small experimental implementation to checkout what would be required to support this in tree and to see if we should in cooperate changes into the 2.0 release to make this possible. The corresponding implementation is here. If someone is interested in doing the required work to turn this into a actual PR these are the things that are left to do:
For any interested person: It's not required to do this all on your own without any help from me (or someone else). I'm willing to help with some advice and pointing to the right locations here, I just cannot afford the time to finish this by myself currently. |
Beta Was this translation helpful? Give feedback.
-
Previously discussed in #2032.
There is currently no support for updating many structs annotated with
AsChangeset
. The current implementation denotes the only current way to update rows is to iterate over the changesets and update them synchronously, which can lead to bad performance. Batch updates are possible in many SQL engines, but through non-intuitive and more complicated queries, such as a VALUES clause or utilizing a CTE. For the purposes of this discussion, I'm going to be focusing on Postgres, given my use-case, but this should apply to other SQLs.After a while of working, I have been able to achieve my desired results of batch-updating by generating a query in the format of:
Note: I have forked Diesel at the 1.4.x branch in order to adjust the visibility of some struct fields in order to implement batch updates in user-land, though this would not be necessary if implemented in Diesel proper.
My implementation is as follows:
In order to iterate over column names for the purposes of generating the
AS
clause, and theSET
clause, as well as the variousVALUES
statements, I've implemented the following macro, utilizing the pre-existing__diesel_for_each_tuple
:The usage of
Assign
and accessing of the struct valueexpr
is specifically where I needed to change the visibility in my forked version.I was then able to utilize these tuple implementations for the main QueryFragment generation:
Due to time constraints, I elided some more generalized parts of this implementation and noted the assumptions made in-line. Also note that, for all intents and purposes, I have very little idea of what I'm doing, and this was a "best-effort" attempt to get something workable due to, again, time constraints. However I thought it pertinent, due to the conversations I've had both on Twitter and in Gitter regarding this problem, to share my solution in hopes of being able to contribute it back in a more polish, less-janky manner.
Beta Was this translation helpful? Give feedback.
All reactions