-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
No mechanism for generic Connection instances #2384
Comments
Short answerDiesel is designed that way, such that Possible workarounds for client applications
Explanation why things are as they areDiesel tries to check and build most of the generated SQL at compile time using the type system. That implies that we need to know certain things, like the used backend, at compile time. Additionally those type information allowing us to provide certain functionality based in such a way that a specific backend is required as only those backend supports the underlying sql construct. Additionally we use all those type information to do some clever prepared connection statement caching for completely static queries, in such a way that we won't even need to build the SQL again if you executed the query once for a given connection. Those information are just not available if someone tries to use something like That all written: It does not mean that at least I personally would be completely opposed to support some kind of generic connection type with diesel. Likely I would like to see this as an extension crate, at least for an initial prototype version. So if someone is interested in working on that feel free to open a topic in our forum outlining at least a rough plan how such an API would look like and how this could possibly be implemented with the current infrastructure. |
Thanks for the thorough explanation! Your reasoning makes complete sense. I didn't realize that so much functionality hinged on that static type information. And thank you for the build-time config suggestion. That's a great option to get me moving forward again. I don't have the breadth of knowledge of diesel necessary to back this opinion up, but my intuition is that there's a path forward that could accomplish both objectives. Maybe if there was a generic connection enum that could be accepted by the various
where
There may be other hurdles I don't know about yet, but I think this is something I could do all on my application's side without built-in support within diesel (obviously the query objects wouldn't have the function I spec'd out, but I could do that with a free-function or macro of my own). And at least for my limited use-case this would be sufficient. I'm interested in hearing your opinion of this idea. Is this a fool's errand? Or is there some potential with this path? |
In theory something like this can work, but it requires a lot of work to make this working for the function case as you would need to proof at compile time that query implements all required traits for not only one backend, but all backends. Additionally for anything else than Using a macro will just remove the requirement of writing out those trait bounds initially. That works, but will lead to horrific compile time errors if something is wrong. (Additionally I think using a macro for this is really bad style as it's hard to document that in some meaningful way.) To write up some requirements for a future reference:
So again: Yes I think something can be possible, but I do not have the bandwidth to invest much time here. So anyone interested in this is welcome to try implementing this, but should not expect detailed mentoring here. |
I might be misunderstanding something, but as far as I can tell I've found an unnecessary limitation in the
Connection
API design. This limitation preventsConnection
from being used in a generic way, forcing the backend type to be known statically.Including
Backend
andTransactionManager
inConnection
statically (as types) means that you cannot create a genericConnection
(such as aBoxedConnection
, or something similar) that abstracts the specific implementation at runtime. If these properties were structs that could be accessed throughConnection
trait functions, this limitation would not be present.I would like to be able to do something like this (where I instantiate the pool from a runtime host url):
However, due to the
Backend
andTransactionManager
types I get the expected error:So first, I'd like to know if I've missed something fundamental in the API that would resolve this problem for me. High probability; it's a large API.
If not, then I'd like to know if this is intended or just an implementation detail. And what is the likelihood of changing that implementation detail?
The text was updated successfully, but these errors were encountered: