sqlx_exasol_impl/types/
str.rs

1use std::{borrow::Cow, rc::Rc, sync::Arc};
2
3use serde::Deserialize;
4use sqlx_core::{
5    decode::Decode,
6    encode::{Encode, IsNull},
7    error::BoxDynError,
8    forward_encode_impl,
9    types::Type,
10};
11
12use crate::{
13    arguments::ExaBuffer,
14    database::Exasol,
15    type_info::{Charset, ExaDataType, ExaTypeInfo},
16    types::ExaHasArrayType,
17    value::ExaValueRef,
18};
19
20impl Type<Exasol> for str {
21    fn type_info() -> ExaTypeInfo {
22        ExaDataType::Varchar {
23            size: ExaDataType::VARCHAR_MAX_LEN,
24            character_set: Charset::Utf8,
25        }
26        .into()
27    }
28}
29
30impl Type<Exasol> for String {
31    fn type_info() -> ExaTypeInfo {
32        <str as Type<Exasol>>::type_info()
33    }
34}
35
36impl ExaHasArrayType for &str {}
37impl ExaHasArrayType for String {}
38impl ExaHasArrayType for Box<str> {}
39impl ExaHasArrayType for Rc<str> {}
40impl ExaHasArrayType for Arc<str> {}
41
42impl Encode<'_, Exasol> for &'_ str {
43    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
44        // Exasol treats empty strings as NULL
45        if self.is_empty() {
46            buf.append(())?;
47            return Ok(IsNull::Yes);
48        }
49
50        buf.append(self)?;
51        Ok(IsNull::No)
52    }
53
54    fn size_hint(&self) -> usize {
55        // 2 quotes + length
56        2 + self.len()
57    }
58}
59
60impl<'r> Decode<'r, Exasol> for &'r str {
61    fn decode(value: ExaValueRef<'r>) -> Result<Self, BoxDynError> {
62        <&str>::deserialize(value.value).map_err(From::from)
63    }
64}
65
66impl Decode<'_, Exasol> for String {
67    fn decode(value: ExaValueRef<'_>) -> Result<Self, BoxDynError> {
68        <&str as Decode<Exasol>>::decode(value).map(ToOwned::to_owned)
69    }
70}
71
72forward_encode_impl!(String, &str, Exasol);
73forward_encode_impl!(Cow<'_, str>, &str, Exasol);
74forward_encode_impl!(Box<str>, &str, Exasol);
75forward_encode_impl!(Rc<str>, &str, Exasol);
76forward_encode_impl!(Arc<str>, &str, Exasol);