diff --git a/sqlx-mysql/src/types/bytes.rs b/sqlx-mysql/src/types/bytes.rs index ade079ad4e..d42e79d43c 100644 --- a/sqlx-mysql/src/types/bytes.rs +++ b/sqlx-mysql/src/types/bytes.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -83,3 +85,25 @@ impl Decode<'_, MySql> for Vec { <&[u8] as Decode>::decode(value).map(ToOwned::to_owned) } } + +impl Type for Arc<[u8]> { + fn type_info() -> MySqlTypeInfo { + <[u8] as Type>::type_info() + } + + fn compatible(ty: &MySqlTypeInfo) -> bool { + <&[u8] as Type>::compatible(ty) + } +} + +impl Encode<'_, MySql> for Arc<[u8]> { + fn encode_by_ref(&self, buf: &mut Vec) -> Result { + <&[u8] as Encode>::encode(&**self, buf) + } +} + +impl Decode<'_, MySql> for Arc<[u8]> { + fn decode(value: MySqlValueRef<'_>) -> Result { + <&[u8] as Decode>::decode(value).map(Into::into) + } +} diff --git a/sqlx-mysql/src/types/str.rs b/sqlx-mysql/src/types/str.rs index 8233e90893..cf2932f5b6 100644 --- a/sqlx-mysql/src/types/str.rs +++ b/sqlx-mysql/src/types/str.rs @@ -6,6 +6,7 @@ use crate::protocol::text::{ColumnFlags, ColumnType}; use crate::types::Type; use crate::{MySql, MySqlTypeInfo, MySqlValueRef}; use std::borrow::Cow; +use std::sync::Arc; impl Type for str { fn type_info() -> MySqlTypeInfo { @@ -114,3 +115,25 @@ impl<'r> Decode<'r, MySql> for Cow<'r, str> { value.as_str().map(Cow::Borrowed) } } + +impl Type for Arc { + fn type_info() -> MySqlTypeInfo { + >::type_info() + } + + fn compatible(ty: &MySqlTypeInfo) -> bool { + >::compatible(ty) + } +} + +impl Encode<'_, MySql> for Arc { + fn encode_by_ref(&self, buf: &mut Vec) -> Result { + <&str as Encode>::encode(&**self, buf) + } +} + +impl Decode<'_, MySql> for Arc { + fn decode(value: MySqlValueRef<'_>) -> Result { + <&str as Decode>::decode(value).map(Into::into) + } +} diff --git a/sqlx-postgres/src/types/array.rs b/sqlx-postgres/src/types/array.rs index 9b8be63412..d18efc39e9 100644 --- a/sqlx-postgres/src/types/array.rs +++ b/sqlx-postgres/src/types/array.rs @@ -1,6 +1,7 @@ use sqlx_core::bytes::Buf; use sqlx_core::types::Text; use std::borrow::Cow; +use std::sync::Arc; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; @@ -130,6 +131,19 @@ where } } +impl Type for Arc<[T]> +where + T: PgHasArrayType, +{ + fn type_info() -> PgTypeInfo { + T::array_type_info() + } + + fn compatible(ty: &PgTypeInfo) -> bool { + T::array_compatible(ty) + } +} + impl<'q, T> Encode<'q, Postgres> for Vec where for<'a> &'a [T]: Encode<'q, Postgres>, @@ -192,6 +206,17 @@ where } } +impl<'q, T> Encode<'q, Postgres> for Arc<[T]> +where + for<'a> &'a [T]: Encode<'q, Postgres>, + T: Encode<'q, Postgres>, +{ + #[inline] + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { + <&[T] as Encode>::encode_by_ref(&self.as_ref(), buf) + } +} + impl<'r, T, const N: usize> Decode<'r, Postgres> for [T; N] where T: for<'a> Decode<'a, Postgres> + Type, @@ -354,3 +379,12 @@ where } } } + +impl<'r, T> Decode<'r, Postgres> for Arc<[T]> +where + T: for<'a> Decode<'a, Postgres> + Type, +{ + fn decode(value: PgValueRef<'r>) -> Result { + as Decode>::decode(value).map(Into::into) + } +} diff --git a/sqlx-postgres/src/types/bytes.rs b/sqlx-postgres/src/types/bytes.rs index 45968837af..7ce0eb1692 100644 --- a/sqlx-postgres/src/types/bytes.rs +++ b/sqlx-postgres/src/types/bytes.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::decode::Decode; use crate::encode::{Encode, IsNull}; use crate::error::BoxDynError; @@ -28,6 +30,12 @@ impl PgHasArrayType for Vec { } } +impl PgHasArrayType for Arc<[u8]> { + fn array_type_info() -> PgTypeInfo { + <[&[u8]] as Type>::type_info() + } +} + impl PgHasArrayType for [u8; N] { fn array_type_info() -> PgTypeInfo { <[&[u8]] as Type>::type_info() @@ -60,6 +68,12 @@ impl Encode<'_, Postgres> for [u8; N] { } } +impl Encode<'_, Postgres> for Arc<[u8]> { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { + <&[u8] as Encode>::encode(self, buf) + } +} + impl<'r> Decode<'r, Postgres> for &'r [u8] { fn decode(value: PgValueRef<'r>) -> Result { match value.format() { @@ -110,3 +124,12 @@ impl Decode<'_, Postgres> for [u8; N] { Ok(bytes) } } + +impl Decode<'_, Postgres> for Arc<[u8]> { + fn decode(value: PgValueRef<'_>) -> Result { + Ok(match value.format() { + PgValueFormat::Binary => value.as_bytes()?.into(), + PgValueFormat::Text => hex::decode(text_hex_decode_input(value)?)?.into(), + }) + } +} diff --git a/sqlx-postgres/src/types/str.rs b/sqlx-postgres/src/types/str.rs index ca7e20a558..9b458cd961 100644 --- a/sqlx-postgres/src/types/str.rs +++ b/sqlx-postgres/src/types/str.rs @@ -5,6 +5,7 @@ use crate::types::array_compatible; use crate::types::Type; use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueRef, Postgres}; use std::borrow::Cow; +use std::sync::Arc; impl Type for str { fn type_info() -> PgTypeInfo { @@ -54,6 +55,16 @@ impl Type for String { } } +impl Type for Arc { + fn type_info() -> PgTypeInfo { + <&str as Type>::type_info() + } + + fn compatible(ty: &PgTypeInfo) -> bool { + <&str as Type>::compatible(ty) + } +} + impl PgHasArrayType for &'_ str { fn array_type_info() -> PgTypeInfo { PgTypeInfo::TEXT_ARRAY @@ -94,6 +105,16 @@ impl PgHasArrayType for String { } } +impl PgHasArrayType for Arc { + fn array_type_info() -> PgTypeInfo { + <&str as PgHasArrayType>::array_type_info() + } + + fn array_compatible(ty: &PgTypeInfo) -> bool { + <&str as PgHasArrayType>::array_compatible(ty) + } +} + impl Encode<'_, Postgres> for &'_ str { fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { buf.extend(self.as_bytes()); @@ -123,6 +144,12 @@ impl Encode<'_, Postgres> for String { } } +impl Encode<'_, Postgres> for Arc { + fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> Result { + <&str as Encode>::encode(&**self, buf) + } +} + impl<'r> Decode<'r, Postgres> for &'r str { fn decode(value: PgValueRef<'r>) -> Result { value.as_str() @@ -146,3 +173,9 @@ impl Decode<'_, Postgres> for String { Ok(value.as_str()?.to_owned()) } } + +impl Decode<'_, Postgres> for Arc { + fn decode(value: PgValueRef<'_>) -> Result { + Ok(value.as_str()?.into()) + } +} diff --git a/sqlx-sqlite/src/types/bytes.rs b/sqlx-sqlite/src/types/bytes.rs index f854b911c5..e053622b8b 100644 --- a/sqlx-sqlite/src/types/bytes.rs +++ b/sqlx-sqlite/src/types/bytes.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::sync::Arc; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; @@ -101,3 +102,36 @@ impl<'r> Decode<'r, Sqlite> for Vec { Ok(value.blob().to_owned()) } } + +impl Type for Arc<[u8]> { + fn type_info() -> SqliteTypeInfo { + <&[u8] as Type>::type_info() + } + + fn compatible(ty: &SqliteTypeInfo) -> bool { + <&[u8] as Type>::compatible(ty) + } +} + +impl<'q> Encode<'q, Sqlite> for Arc<[u8]> { + fn encode(self, args: &mut Vec>) -> Result { + args.push(SqliteArgumentValue::Blob(Cow::Owned(self.to_vec()))); + + Ok(IsNull::No) + } + + fn encode_by_ref( + &self, + args: &mut Vec>, + ) -> Result { + args.push(SqliteArgumentValue::Blob(Cow::Owned(self.to_vec()))); + + Ok(IsNull::No) + } +} + +impl<'r> Decode<'r, Sqlite> for Arc<[u8]> { + fn decode(value: SqliteValueRef<'r>) -> Result { + Ok(value.blob().into()) + } +} diff --git a/sqlx-sqlite/src/types/str.rs b/sqlx-sqlite/src/types/str.rs index bfaffae78e..06999d6a91 100644 --- a/sqlx-sqlite/src/types/str.rs +++ b/sqlx-sqlite/src/types/str.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::sync::Arc; use crate::decode::Decode; use crate::encode::{Encode, IsNull}; @@ -122,3 +123,32 @@ impl<'r> Decode<'r, Sqlite> for Cow<'r, str> { value.text().map(Cow::Borrowed) } } + +impl Type for Arc { + fn type_info() -> SqliteTypeInfo { + <&str as Type>::type_info() + } +} + +impl<'q> Encode<'q, Sqlite> for Arc { + fn encode(self, args: &mut Vec>) -> Result { + args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string()))); + + Ok(IsNull::No) + } + + fn encode_by_ref( + &self, + args: &mut Vec>, + ) -> Result { + args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string()))); + + Ok(IsNull::No) + } +} + +impl<'r> Decode<'r, Sqlite> for Arc { + fn decode(value: SqliteValueRef<'r>) -> Result { + value.text().map(Into::into) + } +} diff --git a/tests/mysql/types.rs b/tests/mysql/types.rs index e837a53f75..006237a301 100644 --- a/tests/mysql/types.rs +++ b/tests/mysql/types.rs @@ -3,6 +3,7 @@ extern crate time_ as time; use std::net::SocketAddr; #[cfg(feature = "rust_decimal")] use std::str::FromStr; +use std::sync::Arc; use sqlx::mysql::MySql; use sqlx::{Executor, Row}; @@ -384,3 +385,33 @@ CREATE TEMPORARY TABLE user_login ( Ok(()) } + +#[sqlx_macros::test] +async fn test_arc_str() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc = "Harold".into(); + + let username: Arc = sqlx::query_scalar("SELECT ? AS username") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +} + +#[sqlx_macros::test] +async fn test_arc_slice() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc<[u8]> = [5, 0].into(); + + let username: Arc<[u8]> = sqlx::query_scalar("SELECT ?") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +} diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index 3f6c362043..509a15ae1e 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -2,9 +2,11 @@ extern crate time_ as time; use std::net::SocketAddr; use std::ops::Bound; +use std::sync::Arc; use sqlx::postgres::types::{Oid, PgCiText, PgInterval, PgMoney, PgRange}; use sqlx::postgres::Postgres; +use sqlx_macros::FromRow; use sqlx_test::{new, test_decode_type, test_prepared_type, test_type}; use sqlx_core::executor::Executor; @@ -657,3 +659,50 @@ CREATE TEMPORARY TABLE user_login ( Ok(()) } + +#[sqlx_macros::test] +async fn test_arc_str() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc = "Harold".into(); + + let username: Arc = sqlx::query_scalar("SELECT $1 AS username") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +} + +#[sqlx_macros::test] +async fn test_arc_slice() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc<[u8]> = [5, 0].into(); + + let username: Arc<[u8]> = sqlx::query_scalar("SELECT $1") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +} + +#[sqlx_macros::test] +async fn test_arc_slice_2() -> anyhow::Result<()> { + let mut conn = new::().await?; + + #[derive(FromRow)] + struct Nested { + inner: Arc<[i32]>, + } + + let username: Nested = sqlx::query_as("SELECT ARRAY[1, 2, 3]::INT4[] as inner") + .fetch_one(&mut conn) + .await?; + + assert!(username.inner.as_ref() == &[1, 2, 3]); + Ok(()) +} diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index 2497e406cc..b99963b035 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -7,6 +7,7 @@ use sqlx_core::types::Text; use sqlx_test::new; use sqlx_test::test_type; use std::net::SocketAddr; +use std::sync::Arc; test_type!(null>(Sqlite, "NULL" == None:: @@ -250,3 +251,33 @@ CREATE TEMPORARY TABLE user_login ( Ok(()) } + +#[sqlx_macros::test] +async fn test_arc_str() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc = "Harold".into(); + + let username: Arc = sqlx::query_scalar("SELECT $1 AS username") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +} + +#[sqlx_macros::test] +async fn test_arc_slice() -> anyhow::Result<()> { + let mut conn = new::().await?; + + let name: Arc<[u8]> = [5, 0].into(); + + let username: Arc<[u8]> = sqlx::query_scalar("SELECT $1") + .bind(&name) + .fetch_one(&mut conn) + .await?; + + assert!(username == name); + Ok(()) +}