Skip to content

Commit c33d55f

Browse files
jrandolfclaude
andauthored
feat: derive Copy and Clone on Queries (#11)
## Summary - Derive `Copy` and `Clone` on `Queries<E>` so it inherits those traits from `E` - Enables ergonomic use with cheap-to-clone executors like `&Pool` ## Test plan - [x] `cargo test` — 21 codegen snapshots + lib tests pass - [x] `cargo check` on all four examples passes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 77aadf1 commit c33d55f

25 files changed

Lines changed: 42 additions & 28 deletions

examples/advanced-types/src/queries.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// Code generated by sqlc-gen-sqlx v0.1.1. DO NOT EDIT.
1+
// Code generated by sqlc-gen-sqlx v0.1.4. DO NOT EDIT.
22
// sqlc version: v1.30.0
33

4-
#![allow(
5-
dead_code,
6-
reason = "generated queries may expose items a caller does not use"
7-
)]
4+
#![allow(dead_code, reason = "generated queries may expose items a caller does not use")]
85

96
const GET_EVENT: &str = "SELECT id, name, flags, event_window FROM events WHERE id = $1";
107
#[derive(Debug, Clone, sqlx::FromRow)]
@@ -55,6 +52,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
5552
(**self).as_executor()
5653
}
5754
}
55+
#[derive(Copy, Clone)]
5856
pub struct Queries<E> {
5957
db: E,
6058
}

examples/basic/src/queries.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// Code generated by sqlc-gen-sqlx v0.1.1. DO NOT EDIT.
1+
// Code generated by sqlc-gen-sqlx v0.1.4. DO NOT EDIT.
22
// sqlc version: v1.30.0
33

4-
#![allow(
5-
dead_code,
6-
reason = "generated queries may expose items a caller does not use"
7-
)]
4+
#![allow(dead_code, reason = "generated queries may expose items a caller does not use")]
85

96
const GET_AUTHOR: &str = "SELECT id, name, bio FROM authors WHERE id = $1";
107
#[derive(Debug, Clone, sqlx::FromRow)]
@@ -25,8 +22,7 @@ pub struct CreateAuthorParams {
2522
pub name: String,
2623
pub bio: Option<String>,
2724
}
28-
const CREATE_AUTHOR: &str =
29-
"INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id, name, bio";
25+
const CREATE_AUTHOR: &str = "INSERT INTO authors (name, bio) VALUES ($1, $2) RETURNING id, name, bio";
3026
#[derive(Debug, Clone, sqlx::FromRow)]
3127
pub struct CreateAuthorRow {
3228
pub id: i64,
@@ -68,6 +64,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
6864
(**self).as_executor()
6965
}
7066
}
67+
#[derive(Copy, Clone)]
7168
pub struct Queries<E> {
7269
db: E,
7370
}
@@ -102,10 +99,7 @@ impl<E: AsExecutor> Queries<E> {
10299
.await
103100
}
104101
pub async fn delete_author(&mut self, id: i64) -> Result<(), sqlx::Error> {
105-
sqlx::query(DELETE_AUTHOR)
106-
.bind(id)
107-
.execute(self.db.as_executor())
108-
.await?;
102+
sqlx::query(DELETE_AUTHOR).bind(id).execute(self.db.as_executor()).await?;
109103
Ok(())
110104
}
111105
pub async fn delete_author_rows(&mut self, id: i64) -> Result<u64, sqlx::Error> {

examples/batch/src/queries.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// Code generated by sqlc-gen-sqlx v0.1.1. DO NOT EDIT.
1+
// Code generated by sqlc-gen-sqlx v0.1.4. DO NOT EDIT.
22
// sqlc version: v1.30.0
33

4-
#![allow(
5-
dead_code,
6-
reason = "generated queries may expose items a caller does not use"
7-
)]
4+
#![allow(dead_code, reason = "generated queries may expose items a caller does not use")]
85

96
const BATCH_GET_AUTHOR: &str = "SELECT id, name, bio FROM authors WHERE id = $1";
107
#[derive(Debug, Clone, sqlx::FromRow)]
@@ -47,6 +44,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4744
(**self).as_executor()
4845
}
4946
}
47+
#[derive(Copy, Clone)]
5048
pub struct Queries<E> {
5149
db: E,
5250
}
@@ -62,7 +60,9 @@ impl<E: AsExecutor> Queries<E> {
6260
pub fn batch_get_author<'a, I>(
6361
&'a mut self,
6462
items: I,
65-
) -> impl futures_core::stream::Stream<Item = Result<BatchGetAuthorRow, sqlx::Error>> + 'a
63+
) -> impl futures_core::stream::Stream<
64+
Item = Result<BatchGetAuthorRow, sqlx::Error>,
65+
> + 'a
6666
where
6767
I: IntoIterator<Item = i64> + 'a,
6868
I::IntoIter: 'a,

examples/enums/src/queries.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// Code generated by sqlc-gen-sqlx v0.1.1. DO NOT EDIT.
1+
// Code generated by sqlc-gen-sqlx v0.1.4. DO NOT EDIT.
22
// sqlc version: v1.30.0
33

4-
#![allow(
5-
dead_code,
6-
reason = "generated queries may expose items a caller does not use"
7-
)]
4+
#![allow(dead_code, reason = "generated queries may expose items a caller does not use")]
85

96
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, sqlx::Type)]
107
#[sqlx(type_name = "status")]
@@ -69,6 +66,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
6966
(**self).as_executor()
7067
}
7168
}
69+
#[derive(Copy, Clone)]
7270
pub struct Queries<E> {
7371
db: E,
7472
}
@@ -96,7 +94,10 @@ impl<E: AsExecutor> Queries<E> {
9694
.fetch_all(self.db.as_executor())
9795
.await
9896
}
99-
pub async fn create_user(&mut self, arg: CreateUserParams) -> Result<(), sqlx::Error> {
97+
pub async fn create_user(
98+
&mut self,
99+
arg: CreateUserParams,
100+
) -> Result<(), sqlx::Error> {
100101
sqlx::query(CREATE_USER)
101102
.bind(arg.name)
102103
.bind(arg.status)

src/codegen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ pub fn generate(request: &GenerateRequestView<'_>, config: &Config) -> Result<St
9292
}
9393
}
9494

95+
#[derive(Copy, Clone)]
9596
pub struct Queries<E> {
9697
db: E,
9798
}

tests/snapshots/codegen__batch_dynamic_slice_param.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4747
(**self).as_executor()
4848
}
4949
}
50+
#[derive(Copy, Clone)]
5051
pub struct Queries<E> {
5152
db: E,
5253
}

tests/snapshots/codegen__batchexec.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4141
(**self).as_executor()
4242
}
4343
}
44+
#[derive(Copy, Clone)]
4445
pub struct Queries<E> {
4546
db: E,
4647
}

tests/snapshots/codegen__batchmany.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4747
(**self).as_executor()
4848
}
4949
}
50+
#[derive(Copy, Clone)]
5051
pub struct Queries<E> {
5152
db: E,
5253
}

tests/snapshots/codegen__batchone.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4747
(**self).as_executor()
4848
}
4949
}
50+
#[derive(Copy, Clone)]
5051
pub struct Queries<E> {
5152
db: E,
5253
}

tests/snapshots/codegen__composite_types.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: AsExecutor + ?Sized> AsExecutor for &mut T {
4747
(**self).as_executor()
4848
}
4949
}
50+
#[derive(Copy, Clone)]
5051
pub struct Queries<E> {
5152
db: E,
5253
}

0 commit comments

Comments
 (0)