Skip to content

Commit a7f0aa3

Browse files
committed
refactor(sqlite): Introduce SqliteArgumentsBuffer type
Signed-off-by: Joshua Potts <[email protected]>
1 parent 4336d88 commit a7f0aa3

File tree

16 files changed

+103
-93
lines changed

16 files changed

+103
-93
lines changed

sqlx-sqlite/src/any.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use sqlx_core::any::{
1212
};
1313
use sqlx_core::sql_str::SqlStr;
1414

15+
use crate::arguments::SqliteArgumentsBuffer;
1516
use crate::type_info::DataType;
1617
use sqlx_core::connection::{ConnectOptions, Connection};
1718
use sqlx_core::database::Database;
@@ -204,25 +205,27 @@ impl<'a> TryFrom<&'a AnyConnectOptions> for SqliteConnectOptions {
204205
}
205206

206207
fn map_arguments(args: AnyArguments<'_>) -> SqliteArguments {
208+
let values = args
209+
.values
210+
.0
211+
.into_iter()
212+
.map(|val| match val {
213+
AnyValueKind::Null(_) => SqliteArgumentValue::Null,
214+
AnyValueKind::Bool(b) => SqliteArgumentValue::Int(b as i32),
215+
AnyValueKind::SmallInt(i) => SqliteArgumentValue::Int(i as i32),
216+
AnyValueKind::Integer(i) => SqliteArgumentValue::Int(i),
217+
AnyValueKind::BigInt(i) => SqliteArgumentValue::Int64(i),
218+
AnyValueKind::Real(r) => SqliteArgumentValue::Double(r as f64),
219+
AnyValueKind::Double(d) => SqliteArgumentValue::Double(d),
220+
AnyValueKind::Text(t) => SqliteArgumentValue::Text(t.to_string()),
221+
AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(b.to_vec()),
222+
// AnyValueKind is `#[non_exhaustive]` but we should have covered everything
223+
_ => unreachable!("BUG: missing mapping for {val:?}"),
224+
})
225+
.collect();
226+
207227
SqliteArguments {
208-
values: args
209-
.values
210-
.0
211-
.into_iter()
212-
.map(|val| match val {
213-
AnyValueKind::Null(_) => SqliteArgumentValue::Null,
214-
AnyValueKind::Bool(b) => SqliteArgumentValue::Int(b as i32),
215-
AnyValueKind::SmallInt(i) => SqliteArgumentValue::Int(i as i32),
216-
AnyValueKind::Integer(i) => SqliteArgumentValue::Int(i),
217-
AnyValueKind::BigInt(i) => SqliteArgumentValue::Int64(i),
218-
AnyValueKind::Real(r) => SqliteArgumentValue::Double(r as f64),
219-
AnyValueKind::Double(d) => SqliteArgumentValue::Double(d),
220-
AnyValueKind::Text(t) => SqliteArgumentValue::Text(t.to_string()),
221-
AnyValueKind::Blob(b) => SqliteArgumentValue::Blob(b.to_vec()),
222-
// AnyValueKind is `#[non_exhaustive]` but we should have covered everything
223-
_ => unreachable!("BUG: missing mapping for {val:?}"),
224-
})
225-
.collect(),
228+
values: SqliteArgumentsBuffer::new(values),
226229
}
227230
}
228231

sqlx-sqlite/src/arguments.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ pub enum SqliteArgumentValue {
2020

2121
#[derive(Default, Debug, Clone)]
2222
pub struct SqliteArguments {
23-
pub(crate) values: Vec<SqliteArgumentValue>,
23+
pub(crate) values: SqliteArgumentsBuffer,
2424
}
2525

26+
#[derive(Default, Debug, Clone)]
27+
pub struct SqliteArgumentsBuffer(Vec<SqliteArgumentValue>);
28+
2629
impl<'q> SqliteArguments {
2730
pub(crate) fn add<T>(&mut self, value: T) -> Result<(), BoxDynError>
2831
where
2932
T: Encode<'q, Sqlite>,
3033
{
31-
let value_length_before_encoding = self.values.len();
34+
let value_length_before_encoding = self.values.0.len();
3235

3336
match value.encode(&mut self.values) {
34-
Ok(IsNull::Yes) => self.values.push(SqliteArgumentValue::Null),
37+
Ok(IsNull::Yes) => self.values.0.push(SqliteArgumentValue::Null),
3538
Ok(IsNull::No) => {}
3639
Err(error) => {
3740
// reset the value buffer to its previous value if encoding failed so we don't leave a half-encoded value behind
38-
self.values.truncate(value_length_before_encoding);
41+
self.values.0.truncate(value_length_before_encoding);
3942
return Err(error);
4043
}
4144
};
@@ -48,7 +51,7 @@ impl<'q> Arguments<'q> for SqliteArguments {
4851
type Database = Sqlite;
4952

5053
fn reserve(&mut self, len: usize, _size_hint: usize) {
51-
self.values.reserve(len);
54+
self.values.0.reserve(len);
5255
}
5356

5457
fn add<T>(&mut self, value: T) -> Result<(), BoxDynError>
@@ -59,7 +62,7 @@ impl<'q> Arguments<'q> for SqliteArguments {
5962
}
6063

6164
fn len(&self) -> usize {
62-
self.values.len()
65+
self.values.0.len()
6366
}
6467
}
6568

@@ -92,7 +95,7 @@ impl SqliteArguments {
9295
arg_i
9396
};
9497

95-
if n > self.values.len() {
98+
if n > self.values.0.len() {
9699
// SQLite treats unbound variables as NULL
97100
// we reproduce this here
98101
// If you are reading this and think this should be an error, open an issue and we can
@@ -102,13 +105,24 @@ impl SqliteArguments {
102105
break;
103106
}
104107

105-
self.values[n - 1].bind(handle, param_i)?;
108+
self.values.0[n - 1].bind(handle, param_i)?;
106109
}
107110

108111
Ok(arg_i - offset)
109112
}
110113
}
111114

115+
impl SqliteArgumentsBuffer {
116+
#[allow(dead_code)] // clippy incorrectly reports this as unused
117+
pub(crate) fn new(values: Vec<SqliteArgumentValue>) -> SqliteArgumentsBuffer {
118+
Self(values)
119+
}
120+
121+
pub(crate) fn push(&mut self, value: SqliteArgumentValue) {
122+
self.0.push(value);
123+
}
124+
}
125+
112126
impl SqliteArgumentValue {
113127
fn bind(&self, handle: &mut StatementHandle, i: usize) -> Result<(), Error> {
114128
use SqliteArgumentValue::*;

sqlx-sqlite/src/database.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub(crate) use sqlx_core::database::{Database, HasStatementCache};
22

3+
use crate::arguments::SqliteArgumentsBuffer;
34
use crate::{
4-
SqliteArgumentValue, SqliteArguments, SqliteColumn, SqliteConnection, SqliteQueryResult,
5-
SqliteRow, SqliteStatement, SqliteTransactionManager, SqliteTypeInfo, SqliteValue,
6-
SqliteValueRef,
5+
SqliteArguments, SqliteColumn, SqliteConnection, SqliteQueryResult, SqliteRow, SqliteStatement,
6+
SqliteTransactionManager, SqliteTypeInfo, SqliteValue, SqliteValueRef,
77
};
88

99
/// Sqlite database driver.
@@ -27,7 +27,7 @@ impl Database for Sqlite {
2727
type ValueRef<'r> = SqliteValueRef<'r>;
2828

2929
type Arguments<'q> = SqliteArguments;
30-
type ArgumentBuffer<'q> = Vec<SqliteArgumentValue>;
30+
type ArgumentBuffer<'q> = SqliteArgumentsBuffer;
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
@@ -74,7 +74,7 @@ extern crate sqlx_core;
7474

7575
use std::sync::atomic::AtomicBool;
7676

77-
pub use arguments::{SqliteArgumentValue, SqliteArguments};
77+
pub use arguments::{SqliteArgumentValue, SqliteArguments, SqliteArgumentsBuffer};
7878
pub use column::SqliteColumn;
7979
#[cfg(feature = "deserialize")]
8080
#[cfg_attr(docsrs, doc(cfg(feature = "deserialize")))]

sqlx-sqlite/src/statement/mod.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,3 @@ impl ColumnIndex<SqliteStatement> for &'_ str {
5757
.copied()
5858
}
5959
}
60-
61-
// #[cfg(feature = "any")]
62-
// impl<'q> From<SqliteStatement<'q>> for crate::any::AnyStatement<'q> {
63-
// #[inline]
64-
// fn from(statement: SqliteStatement<'q>) -> Self {
65-
// crate::any::AnyStatement::<'q> {
66-
// columns: statement
67-
// .columns
68-
// .iter()
69-
// .map(|col| col.clone().into())
70-
// .collect(),
71-
// column_names: statement.column_names,
72-
// parameters: Some(Either::Right(statement.parameters)),
73-
// sql: statement.sql,
74-
// }
75-
// }
76-
// }

sqlx-sqlite/src/types/bool.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::arguments::SqliteArgumentsBuffer;
12
use crate::decode::Decode;
23
use crate::encode::{Encode, IsNull};
34
use crate::error::BoxDynError;
@@ -16,7 +17,7 @@ impl Type<Sqlite> for bool {
1617
}
1718

1819
impl Encode<'_, Sqlite> for bool {
19-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
20+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
2021
args.push(SqliteArgumentValue::Int((*self).into()));
2122

2223
Ok(IsNull::No)

sqlx-sqlite/src/types/bytes.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22
use std::rc::Rc;
33
use std::sync::Arc;
44

5+
use crate::arguments::SqliteArgumentsBuffer;
56
use crate::decode::Decode;
67
use crate::encode::{Encode, IsNull};
78
use crate::error::BoxDynError;
@@ -20,7 +21,7 @@ impl Type<Sqlite> for [u8] {
2021
}
2122

2223
impl Encode<'_, Sqlite> for &'_ [u8] {
23-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
24+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
2425
args.push(SqliteArgumentValue::Blob(self.to_vec()));
2526

2627
Ok(IsNull::No)
@@ -34,13 +35,13 @@ impl<'r> Decode<'r, Sqlite> for &'r [u8] {
3435
}
3536

3637
impl Encode<'_, Sqlite> for Box<[u8]> {
37-
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
38+
fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
3839
args.push(SqliteArgumentValue::Blob(self.into_vec()));
3940

4041
Ok(IsNull::No)
4142
}
4243

43-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
44+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
4445
args.push(SqliteArgumentValue::Blob(self.clone().into_vec()));
4546

4647
Ok(IsNull::No)
@@ -58,13 +59,13 @@ impl Type<Sqlite> for Vec<u8> {
5859
}
5960

6061
impl Encode<'_, Sqlite> for Vec<u8> {
61-
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
62+
fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
6263
args.push(SqliteArgumentValue::Blob(self));
6364

6465
Ok(IsNull::No)
6566
}
6667

67-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
68+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
6869
args.push(SqliteArgumentValue::Blob(self.clone()));
6970

7071
Ok(IsNull::No)
@@ -78,27 +79,27 @@ impl<'r> Decode<'r, Sqlite> for Vec<u8> {
7879
}
7980

8081
impl Encode<'_, Sqlite> for Cow<'_, [u8]> {
81-
fn encode(self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
82+
fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
8283
args.push(SqliteArgumentValue::Blob(self.to_vec()));
8384

8485
Ok(IsNull::No)
8586
}
8687

87-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
88+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
8889
args.push(SqliteArgumentValue::Blob(self.to_vec()));
8990

9091
Ok(IsNull::No)
9192
}
9293
}
9394

9495
impl Encode<'_, Sqlite> for Arc<[u8]> {
95-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
96+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
9697
<Vec<u8> as Encode<'_, Sqlite>>::encode(self.to_vec(), args)
9798
}
9899
}
99100

100101
impl Encode<'_, Sqlite> for Rc<[u8]> {
101-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
102+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
102103
<Vec<u8> as Encode<'_, Sqlite>>::encode(self.to_vec(), args)
103104
}
104105
}

sqlx-sqlite/src/types/chrono.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
error::BoxDynError,
88
type_info::DataType,
99
types::Type,
10-
Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef,
10+
Sqlite, SqliteArgumentsBuffer, SqliteTypeInfo, SqliteValueRef,
1111
};
1212
use chrono::FixedOffset;
1313
use chrono::{
@@ -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 SqliteArgumentsBuffer) -> 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 SqliteArgumentsBuffer) -> 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 SqliteArgumentsBuffer) -> 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 SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
8787
Encode::<Sqlite>::encode(self.format("%T%.f").to_string(), buf)
8888
}
8989
}

sqlx-sqlite/src/types/float.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::arguments::SqliteArgumentsBuffer;
12
use crate::decode::Decode;
23
use crate::encode::{Encode, IsNull};
34
use crate::error::BoxDynError;
@@ -12,7 +13,7 @@ impl Type<Sqlite> for f32 {
1213
}
1314

1415
impl Encode<'_, Sqlite> for f32 {
15-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
16+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
1617
args.push(SqliteArgumentValue::Double((*self).into()));
1718

1819
Ok(IsNull::No)
@@ -34,7 +35,7 @@ impl Type<Sqlite> for f64 {
3435
}
3536

3637
impl Encode<'_, Sqlite> for f64 {
37-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
38+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
3839
args.push(SqliteArgumentValue::Double(*self));
3940

4041
Ok(IsNull::No)

sqlx-sqlite/src/types/int.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::arguments::SqliteArgumentsBuffer;
12
use crate::decode::Decode;
23
use crate::encode::{Encode, IsNull};
34
use crate::error::BoxDynError;
@@ -16,7 +17,7 @@ impl Type<Sqlite> for i8 {
1617
}
1718

1819
impl Encode<'_, Sqlite> for i8 {
19-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
20+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
2021
args.push(SqliteArgumentValue::Int(*self as i32));
2122

2223
Ok(IsNull::No)
@@ -44,7 +45,7 @@ impl Type<Sqlite> for i16 {
4445
}
4546

4647
impl Encode<'_, Sqlite> for i16 {
47-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
48+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
4849
args.push(SqliteArgumentValue::Int(*self as i32));
4950

5051
Ok(IsNull::No)
@@ -68,7 +69,7 @@ impl Type<Sqlite> for i32 {
6869
}
6970

7071
impl Encode<'_, Sqlite> for i32 {
71-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
72+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
7273
args.push(SqliteArgumentValue::Int(*self));
7374

7475
Ok(IsNull::No)
@@ -92,7 +93,7 @@ impl Type<Sqlite> for i64 {
9293
}
9394

9495
impl Encode<'_, Sqlite> for i64 {
95-
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue>) -> Result<IsNull, BoxDynError> {
96+
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
9697
args.push(SqliteArgumentValue::Int64(*self));
9798

9899
Ok(IsNull::No)

0 commit comments

Comments
 (0)