Skip to content
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

How to store and use values made during on_acquire? #110

Open
mu-arch opened this issue Sep 1, 2021 · 2 comments
Open

How to store and use values made during on_acquire? #110

mu-arch opened this issue Sep 1, 2021 · 2 comments

Comments

@mu-arch
Copy link

mu-arch commented Sep 1, 2021

I have the following code:

#[derive(Debug)]
struct ConnectionCustomizer {}

#[async_trait]
impl bb8_postgres::bb8::CustomizeConnection<Client, bb8_postgres::tokio_postgres::Error> for ConnectionCustomizer {
    async fn on_acquire(
        &self,
        connection: & mut bb8_postgres::tokio_postgres::Client,
    ) -> Result<(), bb8_postgres::tokio_postgres::Error> {
        println!("test");
        Ok(())
    }
}

I want to prepare a number of queries using this for every connection. However, I'm struggling to think of a way to actually get the reference to the prepared statements for a specific connection. Is there a way to bind some data to the connection object, or at least a way to get some unique identifier for a connection that I could use to lookup their prepared statements in a hash table?

@djc
Copy link
Owner

djc commented Sep 1, 2021

Maybe you should write your own ManageConnection instead of using bb8-postgres, which can deal with a type that is wrapped around tokio_postgres::Client (and potentially Derefs to it)?

@djc
Copy link
Owner

djc commented Sep 1, 2021

(If it's generic enough I'd be happy to accept it into bb8-postgres and maintain it.)

film42 added a commit to film42/bb8 that referenced this issue Oct 24, 2021
With this example we show how easy it is to wrap the postgres connection
pool to prepare several queries when a connection is created. Then, once
a connection is checked out, this examples shows how easy it is to use
the custom state to pull a prepared statement.

Inspired by djc#110 which requests this kind of behavior in bb8 directly,
but for now it's easy to extend any connection manager with your custom
needs.
film42 added a commit to film42/bb8 that referenced this issue Oct 27, 2021
With this example we show how easy it is to wrap the postgres connection
pool to prepare several queries when a connection is created. Then, once
a connection is checked out, this examples shows how easy it is to use
the custom state to pull a prepared statement.

Inspired by djc#110 which requests this kind of behavior in bb8 directly,
but for now it's easy to extend any connection manager with your custom
needs.
djc pushed a commit that referenced this issue Oct 28, 2021
With this example we show how easy it is to wrap the postgres connection
pool to prepare several queries when a connection is created. Then, once
a connection is checked out, this examples shows how easy it is to use
the custom state to pull a prepared statement.

Inspired by #110 which requests this kind of behavior in bb8 directly,
but for now it's easy to extend any connection manager with your custom
needs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants