Skip to content

Commit c58e2b1

Browse files
committed
refactor(sqlite): Improve support for references as query macro bind arguments by removing lifetime parameter from SqliteArguments
Signed-off-by: Joshua Potts <[email protected]> Signed-off-by: Joshua Potts <[email protected]>
1 parent 6d79bd0 commit c58e2b1

File tree

18 files changed

+108
-215
lines changed

18 files changed

+108
-215
lines changed

sqlx-sqlite/src/any.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ impl<'a> TryFrom<&'a AnyConnectOptions> for SqliteConnectOptions {
203203
}
204204
}
205205

206-
/// Instead of `AnyArguments::convert_into()`, we can do a direct mapping and preserve the lifetime.
207-
fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments<'_> {
206+
fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments {
208207
SqliteArguments {
209208
values: args
210209
.values
@@ -218,8 +217,8 @@ fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments<'_> {
218217
AnyValueKind::BigInt(i) => SqliteArgumentValue::Int64(i),
219218
AnyValueKind::Real(r) => SqliteArgumentValue::Double(r as f64),
220219
AnyValueKind::Double(d) => SqliteArgumentValue::Double(d),
221-
AnyValueKind::Text(t) => SqliteArgumentValue::Text(t),
222-
AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(b),
220+
AnyValueKind::Text(t) => SqliteArgumentValue::Text(t.to_string()),
221+
AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(b.to_vec()),
223222
// AnyValueKind is `#[non_exhaustive]` but we should have covered everything
224223
_ => unreachable!("BUG: missing mapping for {val:?}"),
225224
})

sqlx-sqlite/src/arguments.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@ use crate::statement::StatementHandle;
44
use crate::Sqlite;
55
use atoi::atoi;
66
use libsqlite3_sys::SQLITE_OK;
7-
use std::borrow::Cow;
87

98
pub(crate) use sqlx_core::arguments::*;
109
use sqlx_core::error::BoxDynError;
1110

1211
#[derive(Debug, Clone)]
13-
pub enum SqliteArgumentValue<'q> {
12+
pub enum SqliteArgumentValue {
1413
Null,
15-
Text(Cow<'q, str>),
16-
Blob(Cow<'q, [u8]>),
14+
Text(String),
15+
Blob(Vec<u8>),
1716
Double(f64),
1817
Int(i32),
1918
Int64(i64),
2019
}
2120

2221
#[derive(Default, Debug, Clone)]
23-
pub struct SqliteArguments<'q> {
24-
pub(crate) values: Vec<SqliteArgumentValue<'q>>,
22+
pub struct SqliteArguments {
23+
pub(crate) values: Vec<SqliteArgumentValue>,
2524
}
2625

27-
impl<'q> SqliteArguments<'q> {
26+
impl<'q> SqliteArguments {
2827
pub(crate) fn add<T>(&mut self, value: T) -> Result<(), BoxDynError>
2928
where
3029
T: Encode<'q, Sqlite>,
@@ -43,19 +42,9 @@ impl<'q> SqliteArguments<'q> {
4342

4443
Ok(())
4544
}
46-
47-
pub(crate) fn into_static(self) -> SqliteArguments<'static> {
48-
SqliteArguments {
49-
values: self
50-
.values
51-
.into_iter()
52-
.map(SqliteArgumentValue::into_static)
53-
.collect(),
54-
}
55-
}
5645
}
5746

58-
impl<'q> Arguments<'q> for SqliteArguments<'q> {
47+
impl<'q> Arguments<'q> for SqliteArguments {
5948
type Database = Sqlite;
6049

6150
fn reserve(&mut self, len: usize, _size_hint: usize) {
@@ -74,7 +63,7 @@ impl<'q> Arguments<'q> for SqliteArguments<'q> {
7463
}
7564
}
7665

77-
impl SqliteArguments<'_> {
66+
impl SqliteArguments {
7867
pub(super) fn bind(&self, handle: &mut StatementHandle, offset: usize) -> Result<usize, Error> {
7968
let mut arg_i = offset;
8069
// for handle in &statement.handles {
@@ -120,20 +109,7 @@ impl SqliteArguments<'_> {
120109
}
121110
}
122111

123-
impl SqliteArgumentValue<'_> {
124-
fn into_static(self) -> SqliteArgumentValue<'static> {
125-
use SqliteArgumentValue::*;
126-
127-
match self {
128-
Null => Null,
129-
Text(text) => Text(text.into_owned().into()),
130-
Blob(blob) => Blob(blob.into_owned().into()),
131-
Int(v) => Int(v),
132-
Int64(v) => Int64(v),
133-
Double(v) => Double(v),
134-
}
135-
}
136-
112+
impl SqliteArgumentValue {
137113
fn bind(&self, handle: &mut StatementHandle, i: usize) -> Result<(), Error> {
138114
use SqliteArgumentValue::*;
139115

sqlx-sqlite/src/connection/execute.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct ExecuteIter<'a> {
1010
handle: &'a mut ConnectionHandle,
1111
statement: &'a mut VirtualStatement,
1212
logger: QueryLogger,
13-
args: Option<SqliteArguments<'a>>,
13+
args: Option<SqliteArguments>,
1414

1515
/// since a `VirtualStatement` can encompass multiple actual statements,
1616
/// this keeps track of the number of arguments so far
@@ -19,12 +19,12 @@ pub struct ExecuteIter<'a> {
1919
goto_next: bool,
2020
}
2121

22-
pub(crate) fn iter<'a>(
23-
conn: &'a mut ConnectionState,
22+
pub(crate) fn iter(
23+
conn: &mut ConnectionState,
2424
query: impl SqlSafeStr,
25-
args: Option<SqliteArguments<'a>>,
25+
args: Option<SqliteArguments>,
2626
persistent: bool,
27-
) -> Result<ExecuteIter<'a>, Error> {
27+
) -> Result<ExecuteIter<'_>, Error> {
2828
let query = query.into_sql_str();
2929
// fetch the cached statement or allocate a new one
3030
let statement = conn.statements.get(query.as_str(), persistent)?;
@@ -43,7 +43,7 @@ pub(crate) fn iter<'a>(
4343

4444
fn bind(
4545
statement: &mut StatementHandle,
46-
arguments: &Option<SqliteArguments<'_>>,
46+
arguments: &Option<SqliteArguments>,
4747
offset: usize,
4848
) -> Result<usize, Error> {
4949
let mut n = 0;
@@ -56,7 +56,7 @@ fn bind(
5656
}
5757

5858
impl ExecuteIter<'_> {
59-
pub fn finish(&mut self) -> Result<(), Error> {
59+
pub fn finish(self) -> Result<(), Error> {
6060
for res in self {
6161
let _ = res?;
6262
}

sqlx-sqlite/src/connection/worker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ enum Command {
6363
},
6464
Execute {
6565
query: SqlStr,
66-
arguments: Option<SqliteArguments<'static>>,
66+
arguments: Option<SqliteArguments>,
6767
persistent: bool,
6868
tx: flume::Sender<Result<Either<SqliteQueryResult, SqliteRow>, Error>>,
6969
limit: Option<usize>,
@@ -353,7 +353,7 @@ impl ConnectionWorker {
353353
pub(crate) async fn execute(
354354
&mut self,
355355
query: SqlStr,
356-
args: Option<SqliteArguments<'_>>,
356+
args: Option<SqliteArguments>,
357357
chan_size: usize,
358358
persistent: bool,
359359
limit: Option<usize>,
@@ -364,7 +364,7 @@ impl ConnectionWorker {
364364
.send_async((
365365
Command::Execute {
366366
query,
367-
arguments: args.map(SqliteArguments::into_static),
367+
arguments: args,
368368
persistent,
369369
tx,
370370
limit,

sqlx-sqlite/src/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ impl Database for Sqlite {
2626
type Value = SqliteValue;
2727
type ValueRef<'r> = SqliteValueRef<'r>;
2828

29-
type Arguments<'q> = SqliteArguments<'q>;
30-
type ArgumentBuffer<'q> = Vec<SqliteArgumentValue<'q>>;
29+
type Arguments<'q> = SqliteArguments;
30+
type ArgumentBuffer<'q> = Vec<SqliteArgumentValue>;
3131

3232
type Statement = SqliteStatement;
3333

sqlx-sqlite/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'c, T: Executor<'c, Database = Sqlite>> SqliteExecutor<'c> for T {}
147147
pub type SqliteTransaction<'c> = sqlx_core::transaction::Transaction<'c, Sqlite>;
148148

149149
// NOTE: required due to the lack of lazy normalization
150-
impl_into_arguments_for_arguments!(SqliteArguments<'q>);
150+
impl_into_arguments_for_arguments!(SqliteArguments);
151151
impl_column_index_for_row!(SqliteRow);
152152
impl_column_index_for_statement!(SqliteStatement);
153153
impl_acquire!(Sqlite, SqliteConnection);

sqlx-sqlite/src/statement/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Statement for SqliteStatement {
4545
&self.columns
4646
}
4747

48-
impl_statement_query!(SqliteArguments<'_>);
48+
impl_statement_query!(SqliteArguments);
4949
}
5050

5151
impl ColumnIndex<SqliteStatement> for &'_ str {

sqlx-sqlite/src/types/bool.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ impl Type<Sqlite> for bool {
1515
}
1616
}
1717

18-
impl<'q> Encode<'q, Sqlite> for bool {
19-
fn encode_by_ref(
20-
&self,
21-
args: &mut Vec<SqliteArgumentValue<'q>>,
22-
) -> Result<IsNull, BoxDynError> {
18+
impl Encode<'_, Sqlite> for bool {
19+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
2320
args.push(SqliteArgumentValue::Int((*self).into()));
2421

2522
Ok(IsNull::No)

sqlx-sqlite/src/types/bytes.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ impl Type<Sqlite> for [u8] {
1919
}
2020
}
2121

22-
impl<'q> Encode<'q, Sqlite> for &'q [u8] {
23-
fn encode_by_ref(
24-
&self,
25-
args: &mut Vec<SqliteArgumentValue<'q>>,
26-
) -> Result<IsNull, BoxDynError> {
27-
args.push(SqliteArgumentValue::Blob(Cow::Borrowed(self)));
22+
impl Encode<'_, Sqlite> for &'_ [u8] {
23+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
24+
args.push(SqliteArgumentValue::Blob(self.to_vec()));
2825

2926
Ok(IsNull::No)
3027
}
@@ -37,19 +34,14 @@ impl<'r> Decode<'r, Sqlite> for &'r [u8] {
3734
}
3835

3936
impl Encode<'_, Sqlite> for Box<[u8]> {
40-
fn encode(self, args: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
41-
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.into_vec())));
37+
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
38+
args.push(SqliteArgumentValue::Blob(self.into_vec()));
4239

4340
Ok(IsNull::No)
4441
}
4542

46-
fn encode_by_ref(
47-
&self,
48-
args: &mut Vec<SqliteArgumentValue<'_>>,
49-
) -> Result<IsNull, BoxDynError> {
50-
args.push(SqliteArgumentValue::Blob(Cow::Owned(
51-
self.clone().into_vec(),
52-
)));
43+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
44+
args.push(SqliteArgumentValue::Blob(self.clone().into_vec()));
5345

5446
Ok(IsNull::No)
5547
}
@@ -65,18 +57,15 @@ impl Type<Sqlite> for Vec<u8> {
6557
}
6658
}
6759

68-
impl<'q> Encode<'q, Sqlite> for Vec<u8> {
69-
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
70-
args.push(SqliteArgumentValue::Blob(Cow::Owned(self)));
60+
impl Encode<'_, Sqlite> for Vec<u8> {
61+
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
62+
args.push(SqliteArgumentValue::Blob(self));
7163

7264
Ok(IsNull::No)
7365
}
7466

75-
fn encode_by_ref(
76-
&self,
77-
args: &mut Vec<SqliteArgumentValue<'q>>,
78-
) -> Result<IsNull, BoxDynError> {
79-
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.clone())));
67+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
68+
args.push(SqliteArgumentValue::Blob(self.clone()));
8069

8170
Ok(IsNull::No)
8271
}
@@ -88,37 +77,28 @@ impl<'r> Decode<'r, Sqlite> for Vec<u8> {
8877
}
8978
}
9079

91-
impl<'q> Encode<'q, Sqlite> for Cow<'q, [u8]> {
92-
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
93-
args.push(SqliteArgumentValue::Blob(self));
80+
impl Encode<'_, Sqlite> for Cow<'_, [u8]> {
81+
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
82+
args.push(SqliteArgumentValue::Blob(self.to_vec()));
9483

9584
Ok(IsNull::No)
9685
}
9786

98-
fn encode_by_ref(
99-
&self,
100-
args: &mut Vec<SqliteArgumentValue<'q>>,
101-
) -> Result<IsNull, BoxDynError> {
102-
args.push(SqliteArgumentValue::Blob(self.clone()));
87+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
88+
args.push(SqliteArgumentValue::Blob(self.to_vec()));
10389

10490
Ok(IsNull::No)
10591
}
10692
}
10793

108-
impl<'q> Encode<'q, Sqlite> for Arc<[u8]> {
109-
fn encode_by_ref(
110-
&self,
111-
args: &mut Vec<SqliteArgumentValue<'q>>,
112-
) -> Result<IsNull, BoxDynError> {
94+
impl Encode<'_, Sqlite> for Arc<[u8]> {
95+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
11396
<Vec<u8> as Encode<'_, Sqlite>>::encode(self.to_vec(), args)
11497
}
11598
}
11699

117-
impl<'q> Encode<'q, Sqlite> for Rc<[u8]> {
118-
fn encode_by_ref(
119-
&self,
120-
args: &mut Vec<SqliteArgumentValue<'q>>,
121-
) -> Result<IsNull, BoxDynError> {
100+
impl Encode<'_, Sqlite> for Rc<[u8]> {
101+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
122102
<Vec<u8> as Encode<'_, Sqlite>>::encode(self.to_vec(), args)
123103
}
124104
}

sqlx-sqlite/src/types/chrono.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,25 @@ impl<Tz: TimeZone> Encode<'_, Sqlite> for DateTime<Tz>
6565
where
6666
Tz::Offset: Display,
6767
{
68-
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
68+
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
6969
Encode::<Sqlite>::encode(self.to_rfc3339_opts(SecondsFormat::AutoSi, false), buf)
7070
}
7171
}
7272

7373
impl Encode<'_, Sqlite> for NaiveDateTime {
74-
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
74+
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
7575
Encode::<Sqlite>::encode(self.format("%F %T%.f").to_string(), buf)
7676
}
7777
}
7878

7979
impl Encode<'_, Sqlite> for NaiveDate {
80-
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
80+
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
8181
Encode::<Sqlite>::encode(self.format("%F").to_string(), buf)
8282
}
8383
}
8484

8585
impl Encode<'_, Sqlite> for NaiveTime {
86-
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
86+
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
8787
Encode::<Sqlite>::encode(self.format("%T%.f").to_string(), buf)
8888
}
8989
}

0 commit comments

Comments
 (0)