Skip to content

Commit 41aa37f

Browse files
committed
wip - IntoEncode
Signed-off-by: Joshua Potts <[email protected]>
1 parent 3358247 commit 41aa37f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+914
-119
lines changed

Cargo.lock

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ async-io = { version = "2.4.1", optional = true }
6666
base64 = { version = "0.22.0", default-features = false, features = ["std"] }
6767
bytes = "1.1.0"
6868
chrono = { version = "0.4.34", default-features = false, features = ["clock"], optional = true }
69+
combine = "4.6"
6970
crc = { version = "3", optional = true }
7071
crossbeam-queue = "0.3.2"
7172
either = "1.6.1"

sqlx-core/src/any/arguments.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::any::value::AnyValueKind;
22
use crate::any::{Any, AnyTypeInfoKind};
33
use crate::arguments::Arguments;
44
use crate::encode::{Encode, IsNull};
5+
use crate::encode_owned::IntoEncode;
56
use crate::error::BoxDynError;
67
use crate::types::Type;
78
use std::sync::Arc;
@@ -21,9 +22,9 @@ impl Arguments for AnyArguments {
2122

2223
fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError>
2324
where
24-
T: Encode<'t, Self::Database> + Type<Self::Database>,
25+
T: IntoEncode<Self::Database> + Type<Self::Database>,
2526
{
26-
let _: IsNull = value.encode(&mut self.values)?;
27+
let _: IsNull = value.into_encode().encode(&mut self.values)?;
2728
Ok(())
2829
}
2930

@@ -39,24 +40,26 @@ impl AnyArguments {
3940
#[doc(hidden)]
4041
pub fn convert_into<'a, A: Arguments>(self) -> Result<A, BoxDynError>
4142
where
42-
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
43-
Option<bool>: Type<A::Database> + Encode<'a, A::Database>,
44-
Option<i16>: Type<A::Database> + Encode<'a, A::Database>,
45-
Option<i32>: Type<A::Database> + Encode<'a, A::Database>,
46-
Option<i64>: Type<A::Database> + Encode<'a, A::Database>,
47-
Option<f32>: Type<A::Database> + Encode<'a, A::Database>,
48-
Option<f64>: Type<A::Database> + Encode<'a, A::Database>,
49-
Option<String>: Type<A::Database> + Encode<'a, A::Database>,
50-
Option<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
51-
bool: Type<A::Database> + Encode<'a, A::Database>,
52-
i16: Type<A::Database> + Encode<'a, A::Database>,
53-
i32: Type<A::Database> + Encode<'a, A::Database>,
54-
i64: Type<A::Database> + Encode<'a, A::Database>,
55-
f32: Type<A::Database> + Encode<'a, A::Database>,
56-
f64: Type<A::Database> + Encode<'a, A::Database>,
57-
Arc<String>: Type<A::Database> + Encode<'a, A::Database>,
58-
Arc<str>: Type<A::Database> + Encode<'a, A::Database>,
59-
Arc<Vec<u8>>: Type<A::Database> + Encode<'a, A::Database>,
43+
Option<i32>: IntoEncode<A::Database> + Type<A::Database>,
44+
Option<bool>: IntoEncode<A::Database> + Type<A::Database>,
45+
Option<i16>: IntoEncode<A::Database> + Type<A::Database>,
46+
Option<i32>: IntoEncode<A::Database> + Type<A::Database>,
47+
Option<i64>: IntoEncode<A::Database> + Type<A::Database>,
48+
Option<f32>: IntoEncode<A::Database> + Type<A::Database>,
49+
Option<f64>: IntoEncode<A::Database> + Type<A::Database>,
50+
Option<String>: IntoEncode<A::Database> + Type<A::Database>,
51+
Option<Vec<u8>>: IntoEncode<A::Database> + Type<A::Database>,
52+
bool: IntoEncode<A::Database> + Type<A::Database>,
53+
i16: IntoEncode<A::Database> + Type<A::Database>,
54+
i32: IntoEncode<A::Database> + Type<A::Database>,
55+
i64: IntoEncode<A::Database> + Type<A::Database>,
56+
f32: IntoEncode<A::Database> + Type<A::Database>,
57+
f64: IntoEncode<A::Database> + Type<A::Database>,
58+
String: IntoEncode<A::Database> + Type<A::Database>,
59+
Vec<u8>: IntoEncode<A::Database> + Type<A::Database>,
60+
Arc<String>: IntoEncode<A::Database> + Type<A::Database>,
61+
Arc<str>: IntoEncode<A::Database> + Type<A::Database>,
62+
Arc<Vec<u8>>: IntoEncode<A::Database> + Type<A::Database>,
6063
{
6164
let mut out = A::default();
6265

sqlx-core/src/arguments.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Types and traits for passing arguments to SQL queries.
22
33
use crate::database::Database;
4-
use crate::encode::Encode;
4+
use crate::encode_owned::IntoEncode;
55
use crate::error::BoxDynError;
66
use crate::types::Type;
77
use std::fmt::{self, Write};
@@ -17,9 +17,9 @@ pub trait Arguments: Send + Sized + Default {
1717
fn reserve(&mut self, additional: usize, size: usize);
1818

1919
/// Add the value to the end of the arguments.
20-
fn add<'t, T>(&mut self, value: T) -> Result<(), BoxDynError>
20+
fn add<T>(&mut self, value: T) -> Result<(), BoxDynError>
2121
where
22-
T: Encode<'t, Self::Database> + Type<Self::Database>;
22+
T: IntoEncode<Self::Database> + Type<Self::Database>;
2323

2424
/// The number of arguments that were already added.
2525
fn len(&self) -> usize;

sqlx-core/src/database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use crate::value::{Value, ValueRef};
6969
///
7070
/// This trait encapsulates a complete set of traits that implement a driver for a
7171
/// specific database (e.g., MySQL, PostgreSQL).
72-
pub trait Database: 'static + Sized + Send + Debug {
72+
pub trait Database: 'static + Sized + Send + Sync + Debug {
7373
/// The concrete `Connection` implementation for this database.
7474
type Connection: Connection<Database = Self>;
7575

sqlx-core/src/encode.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ impl_encode_for_smartpointer!(Arc<T>);
172172
impl_encode_for_smartpointer!(Box<T>);
173173
impl_encode_for_smartpointer!(Rc<T>);
174174

175-
impl<'q, T, DB: Database> Encode<'q, DB> for Cow<'q, T>
175+
impl<'q, T, DB: Database> Encode<'q, DB> for Cow<'_, T>
176176
where
177-
T: Encode<'q, DB>,
177+
T: for<'e> Encode<'e, DB>,
178178
T: ToOwned,
179179
{
180180
#[inline]
@@ -214,3 +214,12 @@ macro_rules! forward_encode_impl {
214214
}
215215
};
216216
}
217+
218+
impl<'q, DB: Database> Encode<'q, DB> for Box<dyn Encode<'q, DB>> {
219+
fn encode_by_ref(
220+
&self,
221+
buf: &mut <DB as Database>::ArgumentBuffer,
222+
) -> Result<IsNull, BoxDynError> {
223+
self.as_ref().encode_by_ref(buf)
224+
}
225+
}

0 commit comments

Comments
 (0)