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
use std::borrow::Cow;

use serde_json::Value as JsonValue;
use sqlx_core::{
    database::Database,
    value::{Value, ValueRef},
};

use crate::{database::Exasol, type_info::ExaTypeInfo};

#[derive(Clone, Debug)]
pub struct ExaValue {
    pub(crate) value: JsonValue,
    type_info: ExaTypeInfo,
}

#[derive(Clone, Debug)]
pub struct ExaValueRef<'r> {
    pub(crate) value: &'r JsonValue,
    pub(crate) type_info: &'r ExaTypeInfo,
}

impl Value for ExaValue {
    type Database = Exasol;

    fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_> {
        ExaValueRef {
            value: &self.value,
            type_info: &self.type_info,
        }
    }

    fn type_info(&self) -> Cow<'_, <Self::Database as Database>::TypeInfo> {
        Cow::Borrowed(&self.type_info)
    }

    fn is_null(&self) -> bool {
        self.value.is_null()
    }
}

impl<'r> ValueRef<'r> for ExaValueRef<'r> {
    type Database = Exasol;

    fn to_owned(&self) -> <Self::Database as Database>::Value {
        ExaValue {
            value: self.value.clone(),
            type_info: self.type_info.clone(),
        }
    }

    fn type_info(&self) -> Cow<'_, <Self::Database as Database>::TypeInfo> {
        Cow::Borrowed(self.type_info)
    }

    fn is_null(&self) -> bool {
        self.value.is_null()
    }
}