Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: launchbadge/sqlx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0480f292afa5b19a2c41bc50b6c71b3906b3afd6
Choose a base ref
..
head repository: launchbadge/sqlx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ff041d65e2b436cee28352e25327a11399ffde9f
Choose a head ref
Showing with 66 additions and 1 deletion.
  1. +0 −1 sqlx-core/src/encode.rs
  2. +17 −0 sqlx-mysql/src/types/str.rs
  3. +15 −0 sqlx-postgres/src/types/str.rs
  4. +34 −0 sqlx-sqlite/src/types/str.rs
1 change: 0 additions & 1 deletion sqlx-core/src/encode.rs
Original file line number Diff line number Diff line change
@@ -176,7 +176,6 @@ impl<'q, T, DB: Database> Encode<'q, DB> for Cow<'_, T>
where
for<'a> &'a T: Encode<'q, DB>,
T: ToOwned,
T: ?Sized,
{
#[inline]
fn encode(self, buf: &mut <DB as Database>::ArgumentBuffer<'q>) -> Result<IsNull, BoxDynError> {
17 changes: 17 additions & 0 deletions sqlx-mysql/src/types/str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
@@ -72,3 +74,18 @@ impl Decode<'_, MySql> for String {
<&str as Decode<MySql>>::decode(value).map(ToOwned::to_owned)
}
}

impl Encode<'_, MySql> for Cow<'_, str> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
match self {
Cow::Borrowed(str) => <&str as Encode<MySql>>::encode(*str, buf),
Cow::Owned(str) => <&str as Encode<MySql>>::encode(&**str, buf),
}
}
}

impl Encode<'_, MySql> for Cow<'_, [u8]> {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<MySql>>::encode(self.as_ref(), buf)
}
}
15 changes: 15 additions & 0 deletions sqlx-postgres/src/types/str.rs
Original file line number Diff line number Diff line change
@@ -105,3 +105,18 @@ impl Decode<'_, Postgres> for String {
Ok(value.as_str()?.to_owned())
}
}

impl Encode<'_, Postgres> for Cow<'_, str> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
match self {
Cow::Borrowed(str) => <&str as Encode<Postgres>>::encode(*str, buf),
Cow::Owned(str) => <&str as Encode<Postgres>>::encode(&**str, buf),
}
}
}

impl Encode<'_, Postgres> for Cow<'_, [u8]> {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<Postgres>>::encode(self.as_ref(), buf)
}
}
34 changes: 34 additions & 0 deletions sqlx-sqlite/src/types/str.rs
Original file line number Diff line number Diff line change
@@ -77,3 +77,37 @@ impl<'r> Decode<'r, Sqlite> for String {
value.text().map(ToOwned::to_owned)
}
}

impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Text(self));

Ok(IsNull::No)
}

fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Text(self.clone()));

Ok(IsNull::No)
}
}

impl<'q> Encode<'q, Sqlite> for Cow<'q, [u8]> {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(self));

Ok(IsNull::No)
}

fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(self.clone()));

Ok(IsNull::No)
}
}