You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0db4bde
Browse filesBrowse the repository at this point in the historyBrowse files
feat: support PoolConnection via AsExecutor trait (#4)
## Summary
- Replaces the `for<'c> &'c mut E: sqlx::Executor<'c, Database =
Postgres>` bound on `Queries<E>` with a generated `AsExecutor` trait.
- `AsExecutor` has impls for `PgPool`, `&PgPool`, `PgConnection`,
`Transaction<'_, Postgres>`, `PoolConnection<Postgres>`, and `&mut T` —
covering all eight call shapes (owned + `&mut`).
- Fixes a latent bug: the README previously claimed
`Queries::new(&pool)` and `Queries::new(&mut tx)` worked, but neither
actually type-checked against the old bound. Only `Queries::new(conn)`
with owned `PgConnection` did.
- Also fixes broken `enums` and `batch` example mains that didn't match
the current codegen (struct params / stream returns).
## Why the previous bound failed
sqlx has a commented-out `impl Executor for &mut PoolConnection<DB>` in
`sqlx-core/src/pool/executor.rs` citing trait-resolver overflow in the
generic blanket form. `AsExecutor` sidesteps that by providing concrete
per-type impls that yield `&mut PgConnection` directly.
## Test plan
- [x] `cargo fmt --all --check`
- [x] `cargo clippy --workspace --all-targets -- -D warnings`
- [x] `cargo test --workspace` (snapshots; integration tests require
DATABASE_URL)
- [x] `cargo build --target wasm32-wasip1`
- [x] e2e test (testcontainers Postgres) — compiles and runs generated
code for all annotation types
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: README.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,12 +11,18 @@ For each SQL query annotated with a sqlc command, the plugin emits:
11
11
- An optional params struct (`QueryNameParams`) when a query has 2+ parameters.
12
12
- A `&mut self` method on `pub struct Queries<E>` that executes the query.
13
13
14
-
`Queries<E>` wraps an executor once at construction. Pass `&pool` for pool usage or `&mut tx` for transactions:
14
+
`Queries<E>` wraps anything implementing the generated `AsExecutor` trait. `AsExecutor` is implemented for `PgPool`, `&PgPool`, `PgConnection`, `Transaction<'_, Postgres>`, `PoolConnection<Postgres>`, and `&mut T` of each:
15
15
16
16
```rust
17
+
// From a pool:
17
18
letmutq=Queries::new(&pool);
18
19
letauthor=q.get_author(1).await?;
19
20
21
+
// Borrowed or owned pool connection:
22
+
letmutconn=pool.acquire().await?;
23
+
letmutq=Queries::new(&mutconn);
24
+
// ...or Queries::new(conn) to take ownership.
25
+
20
26
// Transactions:
21
27
letmuttx=pool.begin().await?;
22
28
letmutq=Queries::new(&muttx);
@@ -126,7 +132,7 @@ Array types (`type[]`) become `Vec<T>`. Nullable columns become `Option<T>`.
126
132
| `:batchmany` | `impl Stream<Item = Result<Vec<QueryRow>, sqlx::Error>>` | Lazily fetch all rows per item |
127
133
| `:copyfrom` | `Result<u64, sqlx::Error>` | Chunked bulk insert from any `IntoIterator` |
128
134
129
-
All functions are `&mut self` methods on `Queries<E>`. The bound `for<'c> &'c mut E: sqlx::Executor<'c, Database = sqlx::Postgres>` is satisfied by `&PgPool`, `&mut PgConnection`, and `&mut Transaction<'_, Postgres>`.
135
+
All functions are `&mut self` methods on `Queries<E>`. The bound is `E: AsExecutor`, where `AsExecutor` is the trait emitted in each generated file. Impls cover `PgPool`, `&PgPool`, `PgConnection`, `Transaction<'_, Postgres>`, `PoolConnection<Postgres>`, and `&mut T` of each.
130
136
131
137
Batch methods generate `Stream`-returning APIs and reference `futures_core` and `futures_util` directly. Consumer crates should include those dependencies alongside `sqlx`.
0 commit comments