sqlx_clickhouse_ext/
value.rs

1pub use sqlx_core::{
2    database::{Database, HasValueRef},
3    decode::Decode,
4    error::Error,
5    type_info::TypeInfo as _,
6    types::Type,
7    value::ValueRef as _,
8};
9
10use crate::error::mismatched_types;
11
12pub trait ValueRefTryGet<'r, DB, T>
13where
14    DB: Database,
15    T: Decode<'r, DB> + Type<DB>,
16{
17    fn try_get(self) -> Result<T, Error>;
18}
19
20impl<'r, DB, T> ValueRefTryGet<'r, DB, T> for (<DB as HasValueRef<'r>>::ValueRef, usize)
21where
22    DB: Database,
23    T: Decode<'r, DB> + Type<DB>,
24{
25    fn try_get(self) -> Result<T, Error> {
26        let (value, index) = self;
27
28        // https://github.com/launchbadge/sqlx/blob/v0.5.1/sqlx-core/src/row.rs#L111
29        if !value.is_null() {
30            let ty = value.type_info();
31
32            if !ty.is_null() && !T::compatible(&ty) {
33                return Err(Error::ColumnDecode {
34                    index: format!("{index:?}"),
35                    source: mismatched_types::<DB, T>(&ty),
36                });
37            }
38        }
39
40        T::decode(value).map_err(|source| Error::ColumnDecode {
41            index: format!("{index:?}"),
42            source,
43        })
44    }
45}