use serde::Deserialize;
use sqlx_core::{
decode::Decode,
encode::{Encode, IsNull},
error::BoxDynError,
types::Type,
};
use crate::{
arguments::ExaBuffer,
database::Exasol,
type_info::{ExaDataType, ExaTypeInfo},
types::ExaHasArrayType,
value::ExaValueRef,
};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HashType(pub String);
impl Type<Exasol> for HashType {
fn type_info() -> ExaTypeInfo {
ExaDataType::HashType { size: None }.into()
}
}
impl ExaHasArrayType for HashType {}
impl Encode<'_, Exasol> for HashType {
fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
<&str as Encode<Exasol>>::encode_by_ref(&self.0.as_str(), buf)
}
fn size_hint(&self) -> usize {
<&str as Encode<Exasol>>::size_hint(&self.0.as_str())
}
}
impl<'r> Decode<'r, Exasol> for HashType {
fn decode(value: ExaValueRef<'r>) -> Result<Self, BoxDynError> {
String::deserialize(value.value)
.map(HashType)
.map_err(From::from)
}
}