Skip to content

Commit df2d5c7

Browse files
authored
Encode/Decode impl for Cow<'_, str> (#1343)
* Encode/Decode impl for Cow<'_, str> resolves #1214 * --wip-- [skip ci] * Add Cow decode/encode to other databases and fix build
1 parent 626dd0d commit df2d5c7

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

sqlx-core/src/mssql/types/str.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::mssql::io::MssqlBufMutExt;
55
use crate::mssql::protocol::type_info::{Collation, CollationFlags, DataType, TypeInfo};
66
use crate::mssql::{Mssql, MssqlTypeInfo, MssqlValueRef};
77
use crate::types::Type;
8+
use std::borrow::Cow;
89

910
impl Type<Mssql> for str {
1011
fn type_info() -> MssqlTypeInfo {
@@ -81,3 +82,33 @@ impl Decode<'_, Mssql> for String {
8182
.into_owned())
8283
}
8384
}
85+
86+
impl Encode<'_, Mssql> for Cow<'_, str> {
87+
fn produces(&self) -> Option<MssqlTypeInfo> {
88+
match self {
89+
Cow::Borrowed(str) => <&str as Encode<Mssql>>::produces(str),
90+
Cow::Owned(str) => <&str as Encode<Mssql>>::produces(&(str.as_ref())),
91+
}
92+
}
93+
94+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
95+
match self {
96+
Cow::Borrowed(str) => <&str as Encode<Mssql>>::encode_by_ref(str, buf),
97+
Cow::Owned(str) => <&str as Encode<Mssql>>::encode_by_ref(&(str.as_ref()), buf),
98+
}
99+
}
100+
}
101+
102+
impl<'r> Decode<'r, Mssql> for Cow<'r, str> {
103+
fn decode(value: MssqlValueRef<'r>) -> Result<Self, BoxDynError> {
104+
Ok(Cow::Owned(
105+
value
106+
.type_info
107+
.0
108+
.encoding()?
109+
.decode_without_bom_handling(value.as_bytes()?)
110+
.0
111+
.into_owned(),
112+
))
113+
}
114+
}

sqlx-core/src/mysql/types/str.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::mysql::io::MySqlBufMutExt;
55
use crate::mysql::protocol::text::{ColumnFlags, ColumnType};
66
use crate::mysql::{MySql, MySqlTypeInfo, MySqlValueRef};
77
use crate::types::Type;
8+
use std::borrow::Cow;
89

910
const COLLATE_UTF8_GENERAL_CI: u16 = 33;
1011
const COLLATE_UTF8_UNICODE_CI: u16 = 192;
@@ -80,3 +81,18 @@ impl Decode<'_, MySql> for String {
8081
<&str as Decode<MySql>>::decode(value).map(ToOwned::to_owned)
8182
}
8283
}
84+
85+
impl Encode<'_, MySql> for Cow<'_, str> {
86+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
87+
match self {
88+
Cow::Borrowed(str) => <&str as Encode<MySql>>::encode(*str, buf),
89+
Cow::Owned(str) => <&str as Encode<MySql>>::encode(&**str, buf),
90+
}
91+
}
92+
}
93+
94+
impl<'r> Decode<'r, MySql> for Cow<'r, str> {
95+
fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
96+
value.as_str().map(Cow::Borrowed)
97+
}
98+
}

sqlx-core/src/postgres/types/str.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::error::BoxDynError;
44
use crate::postgres::types::array_compatible;
55
use crate::postgres::{PgArgumentBuffer, PgTypeInfo, PgValueRef, Postgres};
66
use crate::types::Type;
7+
use std::borrow::Cow;
78

89
impl Type<Postgres> for str {
910
fn type_info() -> PgTypeInfo {
@@ -22,6 +23,16 @@ impl Type<Postgres> for str {
2223
}
2324
}
2425

26+
impl Type<Postgres> for Cow<'_, str> {
27+
fn type_info() -> PgTypeInfo {
28+
<&str as Type<Postgres>>::type_info()
29+
}
30+
31+
fn compatible(ty: &PgTypeInfo) -> bool {
32+
<&str as Type<Postgres>>::compatible(ty)
33+
}
34+
}
35+
2536
impl Type<Postgres> for [&'_ str] {
2637
fn type_info() -> PgTypeInfo {
2738
PgTypeInfo::TEXT_ARRAY
@@ -50,6 +61,15 @@ impl Encode<'_, Postgres> for &'_ str {
5061
}
5162
}
5263

64+
impl Encode<'_, Postgres> for Cow<'_, str> {
65+
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
66+
match self {
67+
Cow::Borrowed(str) => <&str as Encode<Postgres>>::encode(*str, buf),
68+
Cow::Owned(str) => <&str as Encode<Postgres>>::encode(&**str, buf),
69+
}
70+
}
71+
}
72+
5373
impl Encode<'_, Postgres> for String {
5474
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
5575
<&str as Encode<Postgres>>::encode(&**self, buf)
@@ -62,6 +82,12 @@ impl<'r> Decode<'r, Postgres> for &'r str {
6282
}
6383
}
6484

85+
impl<'r> Decode<'r, Postgres> for Cow<'r, str> {
86+
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
87+
Ok(Cow::Borrowed(value.as_str()?))
88+
}
89+
}
90+
6591
impl Type<Postgres> for String {
6692
fn type_info() -> PgTypeInfo {
6793
<&str as Type<Postgres>>::type_info()

sqlx-core/src/sqlite/types/str.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,23 @@ impl<'r> Decode<'r, Sqlite> for String {
5252
value.text().map(ToOwned::to_owned)
5353
}
5454
}
55+
56+
impl<'q> Encode<'q, Sqlite> for Cow<'q, str> {
57+
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
58+
args.push(SqliteArgumentValue::Text(self));
59+
60+
IsNull::No
61+
}
62+
63+
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
64+
args.push(SqliteArgumentValue::Text(self.clone()));
65+
66+
IsNull::No
67+
}
68+
}
69+
70+
impl<'r> Decode<'r, Sqlite> for Cow<'r, str> {
71+
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
72+
value.text().map(Cow::Borrowed)
73+
}
74+
}

0 commit comments

Comments
 (0)