tokio-rs / rdbc

Rust DataBase Connectivity (RDBC) :: Common Rust API for database drivers
Apache License 2.0
569 stars 25 forks source link

Use Stream<Row> in API #61

Open andygrove opened 4 years ago

sd2k commented 4 years ago

@andygrove I tried to use a bunch of associated types on the various traits and managed to get the trait part to compile, although I haven't actually tried implementing the trait on the various database structs yet. I'm not sure if it's what you decided on in the end (following the chat in Discord) but maybe it's useful! https://github.com/sd2k/rdbc/commit/2adb34ab1009d66eecb0da3a9b4b482f5808df40

andygrove commented 4 years ago

@sd2k This is awesome :-) I was hoping someone would jump in and show me the way ... I think I chose the wrong database drivers to integrate with so we should perhaps look at tokio-postgres as the first one to implement with these new traits?

sd2k commented 4 years ago

Just taking a look at tokio-postgres. It immediately requires a change to the traits to make Driver::connect async, since the tokio_postgres::connect is async. Alternatively we could make the driver block on the creation of the connection. I've not got much experience with this sort of thing, but my hunch is that Drivers aren't created and destroyed regularly, so that may be acceptable? Never mind, we need to spawn the connection into an ongoing runtime so the method will still need to be async!

sd2k commented 4 years ago

This appears to be close to compiling now: https://github.com/sd2k/rdbc/commit/a483a82f69a7e97e19961adb5c27f2e44a1595fb. I can't figure out how to make the params in execute_query Send which is blocking at the minute. I had to resort to using an Arc<tokio_postgres::Client> rather than tying the lifetime of TokioPostgresStatement to that of the client, but it probably won't matter realistically!

sd2k commented 4 years ago

By some absolute miracle I got it to compile. Check the tests for rdbc-tokio-postgres for examples! There are an awful lot of unwraps in the tests due to the amount of optional/result types returned by the methods, but I'm not sure it's avoidable - they're all valid from what I can tell.

I haven't even touched the other databases and I'm not completely sure how they'll work with the async traits.

Latest commit is here: https://github.com/sd2k/rdbc/commit/0993910185ed89b0ff14a11b4d5528f41618ae1a