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

Impossible to create multiple prepared statements at once #46

Open
weiznich opened this issue Dec 31, 2019 · 1 comment
Open

Impossible to create multiple prepared statements at once #46

weiznich opened this issue Dec 31, 2019 · 1 comment

Comments

@weiznich
Copy link

I would expect that it's possible to create multiple prepared statements for a single connection at once like following:

fn check<'a>(
    conn: &'a mut (dyn Connection + 'static),
) -> (
    Rc<RefCell<dyn Statement + 'a>>,
    Rc<RefCell<dyn Statement + 'a>>,
) {
    let a = conn.prepare("SELECT name, id FROM users").unwrap();
    let b = conn.prepare("SELECT name, id FROM posts").unwrap();

    (a, b)
}

This fails because to create a prepared statement it is required to borrow the connection mutably, which implies that you can only create one prepared statement per connection at one point in time.
This disallows common patterns like a prepared statement cache.

Error message:

error[E0499]: cannot borrow `*conn` as mutable more than once at a time
  --> src/main.rs:16:13
   |
9  | fn check<'a>(
   |          -- lifetime `'a` defined here
...
15 |     let a = conn.prepare("SELECT name, id FROM users").unwrap();
   |             ---- first mutable borrow occurs here
16 |     let b = conn.prepare("SELECT name, id FROM posts").unwrap();
   |             ^^^^ second mutable borrow occurs here
17 | 
18 |     (a, b)
   |     ------ returning this value requires that `*conn` is borrowed for `'a`
@andygrove
Copy link
Contributor

Thanks for raising this and also for filing blackbeam/rust-mysql-simple#199

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