1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::io::MySqlBufMutExt;
use crate::protocol::text::{ColumnFlags, ColumnType};
use crate::types::Type;
use crate::{MySql, MySqlTypeInfo, MySqlValueRef};
use std::borrow::Cow;

const COLLATE_UTF8_GENERAL_CI: u16 = 33;
const COLLATE_UTF8_UNICODE_CI: u16 = 192;
const COLLATE_UTF8MB4_UNICODE_CI: u16 = 224;
const COLLATE_UTF8MB4_BIN: u16 = 46;
const COLLATE_UTF8MB4_GENERAL_CI: u16 = 45;
const COLLATE_UTF8MB4_0900_AI_CI: u16 = 255;

impl Type<MySql> for str {
    fn type_info() -> MySqlTypeInfo {
        MySqlTypeInfo {
            r#type: ColumnType::VarString,        // VARCHAR
            char_set: COLLATE_UTF8MB4_UNICODE_CI, // utf8mb4_unicode_ci
            flags: ColumnFlags::empty(),
            max_size: None,
        }
    }

    fn compatible(ty: &MySqlTypeInfo) -> bool {
        // TODO: Support more collations being returned from SQL?
        matches!(
            ty.r#type,
            ColumnType::VarChar
                | ColumnType::Blob
                | ColumnType::TinyBlob
                | ColumnType::MediumBlob
                | ColumnType::LongBlob
                | ColumnType::String
                | ColumnType::VarString
                | ColumnType::Enum
        ) && matches!(
            ty.char_set,
            COLLATE_UTF8MB4_UNICODE_CI
                | COLLATE_UTF8_UNICODE_CI
                | COLLATE_UTF8_GENERAL_CI
                | COLLATE_UTF8MB4_BIN
                | COLLATE_UTF8MB4_GENERAL_CI
                | COLLATE_UTF8MB4_0900_AI_CI
        )
    }
}

impl Encode<'_, MySql> for &'_ str {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        buf.put_str_lenenc(self);

        IsNull::No
    }
}

impl<'r> Decode<'r, MySql> for &'r str {
    fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
        value.as_str()
    }
}

impl Type<MySql> for Box<str> {
    fn type_info() -> MySqlTypeInfo {
        <&str as Type<MySql>>::type_info()
    }

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

impl Encode<'_, MySql> for Box<str> {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        <&str as Encode<MySql>>::encode(&**self, buf)
    }
}

impl<'r> Decode<'r, MySql> for Box<str> {
    fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
        <&str as Decode<MySql>>::decode(value).map(Box::from)
    }
}

impl Type<MySql> for String {
    fn type_info() -> MySqlTypeInfo {
        <str as Type<MySql>>::type_info()
    }

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

impl Encode<'_, MySql> for String {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        <&str as Encode<MySql>>::encode(&**self, buf)
    }
}

impl Decode<'_, MySql> for String {
    fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
        <&str as Decode<MySql>>::decode(value).map(ToOwned::to_owned)
    }
}

impl Type<MySql> for Cow<'_, str> {
    fn type_info() -> MySqlTypeInfo {
        <&str as Type<MySql>>::type_info()
    }

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

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

impl<'r> Decode<'r, MySql> for Cow<'r, str> {
    fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
        value.as_str().map(Cow::Borrowed)
    }
}