workers-rs icon indicating copy to clipboard operation
workers-rs copied to clipboard

[Feature] Add `[must_use]` attribute and implement `IntoFuture` for `D1PreparedStatement`

Open lexoliu opened this issue 7 months ago • 1 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Description

D1PreparedStatement is just a statement, which has not been executed actually. However, user could make mistake by writing code like this:

query!(&self.db, "DELETE FROM users WHERE id = ?", id)?; // correct version: query!(&self.db, "DELETE FROM users WHERE id = ?", id)?.all().await?

This code does not work, D1PreparedStatement just like a Future in Rust. It wouldn't been executed automatically, it must been called explicitly .

I suggest that we could implement IntoFuture for D1PreparedStatement, so that we can execute a statement by await it directly. What's more, D1PreparedStatement need to been attributed as #[must_use], to avoid misuse statically.

lexoliu avatar Jul 14 '25 15:07 lexoliu

I'm confused about the functionality of D1PreparedStatement::run, I looked up the documents of Cloudflare. It says the run method

Runs the prepared query (or queries) and returns results. The returned results includes metadata.

While D1PreparedStatement::run

Executes a query against the database but only return metadata.

Why the behaviour between Rust and Javascript is different?

Image

lexoliu avatar Jul 14 '25 17:07 lexoliu