Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions sqlx-core/src/any/types/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ impl<'r> Decode<'r, Any> for &'r [u8] {
}
}

impl<const N: usize> Type<Any> for [u8; N] {
fn type_info() -> AnyTypeInfo {
<[u8] as Type<Any>>::type_info()
}
}

impl<const N: usize> Encode<'_, Any> for [u8; N] {
fn encode_by_ref(
&self,
buf: &mut <Any as Database>::ArgumentBuffer,
) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<'_, Any>>::encode(self.as_slice(), buf)
}
}

impl<const N: usize> Decode<'_, Any> for [u8; N] {
fn decode(value: <Any as Database>::ValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(<&[u8] as Decode<'_, Any>>::decode(value)?.try_into()?)
}
}

impl Type<Any> for Vec<u8> {
fn type_info() -> AnyTypeInfo {
<[u8] as Type<Any>>::type_info()
Expand Down
22 changes: 22 additions & 0 deletions sqlx-mysql/src/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ impl<'r> Decode<'r, MySql> for &'r [u8] {
}
}

impl<const N: usize> Type<MySql> for [u8; N] {
fn type_info() -> MySqlTypeInfo {
<[u8] as Type<MySql>>::type_info()
}

fn compatible(ty: &MySqlTypeInfo) -> bool {
<[u8] as Type<MySql>>::compatible(ty)
}
}

impl<const N: usize> Encode<'_, MySql> for [u8; N] {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<MySql>>::encode(self.as_slice(), buf)
}
}

impl<const N: usize> Decode<'_, MySql> for [u8; N] {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(<&[u8] as Decode<'_, MySql>>::decode(value)?.try_into()?)
}
}

impl Type<MySql> for Vec<u8> {
fn type_info() -> MySqlTypeInfo {
<[u8] as Type<MySql>>::type_info()
Expand Down
22 changes: 22 additions & 0 deletions sqlx-sqlite/src/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ impl<'r> Decode<'r, Sqlite> for &'r [u8] {
}
}

impl<const N: usize> Type<Sqlite> for [u8; N] {
fn type_info() -> SqliteTypeInfo {
<[u8] as Type<Sqlite>>::type_info()
}

fn compatible(ty: &SqliteTypeInfo) -> bool {
<[u8] as Type<Sqlite>>::compatible(ty)
}
}

impl<const N: usize> Encode<'_, Sqlite> for [u8; N] {
fn encode_by_ref(&self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
<&[u8] as Encode<'_, Sqlite>>::encode(self.as_slice(), args)
}
}

impl<const N: usize> Decode<'_, Sqlite> for [u8; N] {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(<&[u8] as Decode<'_, Sqlite>>::decode(value)?.try_into()?)
}
}

impl Encode<'_, Sqlite> for Box<[u8]> {
fn encode(self, args: &mut SqliteArgumentsBuffer) -> Result<IsNull, BoxDynError> {
args.push(SqliteArgumentValue::Blob(Arc::new(self.into_vec())));
Expand Down
8 changes: 8 additions & 0 deletions tests/mysql/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ test_type!(bytes<Vec<u8>>(MySql,
== vec![0_u8, 0, 0, 0, 0x52]
));

test_type!(byte_array_empty<[u8; 0]>(MySql,
"X''" == [0_u8; 0],
));

test_type!(byte_array<[u8; 4]>(MySql,
"X'DEADBEEF'" == [0xDE_u8, 0xAD, 0xBE, 0xEF],
));

#[cfg(feature = "uuid")]
test_type!(uuid<sqlx::types::Uuid>(MySql,
"x'b731678f636f4135bc6f19440c13bd19'"
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ test_type!(bytes<Vec<u8>>(Sqlite,
== vec![0_u8, 0, 0, 0, 0x52]
));

test_type!(byte_array_empty<[u8; 0]>(Sqlite,
"X''" == [0_u8; 0],
));

test_type!(byte_array<[u8; 4]>(Sqlite,
"X'DEADBEEF'" == [0xDE_u8, 0xAD, 0xBE, 0xEF],
));

#[cfg(feature = "json")]
mod json_tests {
use super::*;
Expand Down
Loading