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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::str::from_utf8;
use geo_types::{Point};

use crate::decode::Decode;
use crate::error::UnexpectedNullError;
use crate::postgres::{PgTypeInfo, Postgres};
use crate::Result;
use crate::types::BigDecimal;
use crate::value::RawValue;

#[derive(Debug, Copy, Clone)]
pub enum PgData<'c> {
    Binary(&'c [u8]),
    Text(&'c str),
}

#[derive(Debug, Clone)]
pub struct PgValue<'c> {
    type_info: Option<PgTypeInfo>,
    data: Option<PgData<'c>>,
}

impl<'c> PgValue<'c> {
    /// Gets the binary or text data for this value; or, `UnexpectedNullError` if this

    /// is a `NULL` value.

    pub(crate) fn try_get(&self) -> crate::Result<PgData<'c>> {
        match self.data {
            Some(data) => Ok(data),
            None => Err(crate::Error::decode(UnexpectedNullError)),
        }
    }

    /// Gets the binary or text data for this value; or, `None` if this

    /// is a `NULL` value.

    #[inline]
    pub fn get(&self) -> Option<PgData<'c>> {
        self.data
    }

    pub(crate) fn null() -> Self {
        Self {
            type_info: None,
            data: None,
        }
    }

    pub(crate) fn bytes(type_info: PgTypeInfo, buf: &'c [u8]) -> Self {
        Self {
            type_info: Some(type_info),
            data: Some(PgData::Binary(buf)),
        }
    }

    pub(crate) fn utf8(type_info: PgTypeInfo, buf: &'c [u8]) -> crate::Result<Self> {
        Ok(Self {
            type_info: Some(type_info),
            data: Some(PgData::Text(from_utf8(&buf).map_err(crate::Error::decode)?)),
        })
    }

    #[cfg(test)]
    pub(crate) fn from_bytes(buf: &'c [u8]) -> Self {
        Self {
            type_info: None,
            data: Some(PgData::Binary(buf)),
        }
    }

    pub(crate) fn from_str(s: &'c str) -> Self {
        Self {
            type_info: None,
            data: Some(PgData::Text(s)),
        }
    }
}

impl<'c> RawValue<'c> for PgValue<'c> {
    type Database = Postgres;

    // The public type_info is used for type compatibility checks

    fn type_info(&self) -> Option<PgTypeInfo> {
        // For TEXT encoding the type defined on the value is unreliable

        if matches!(self.data, Some(PgData::Binary(_))) {
            self.type_info.clone()
        } else {
            None
        }
    }

    fn try_to_json(&self) -> Result<serde_json::Value> {
        if self.type_info.is_none() {
            return Ok(serde_json::Value::Null);
        }
        let type_string = format!("{}", self.type_info.as_ref().unwrap());
        match type_string.as_str() {
            "NUMERIC" => {
                //decimal

                let r: crate::Result<BigDecimal> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap().to_string()));
            }
            "BOOL" => {
                let r: crate::Result<bool> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "BYTEA" => {
                unimplemented!();
            }
            "FLOAT4" => {
                let r: crate::Result<f32> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "FLOAT8" => {
                let r: crate::Result<f64> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "INT2" => {
                let r: crate::Result<i16> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "INT4" => {
                let r: crate::Result<i32> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "INT8" => {
                let r: crate::Result<i64> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "TEXT" | "VARCHAR" | "BPCHAR" | "CHAR" => {
                let r: crate::Result<String> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }
            "UUID" => {
                let r: crate::Result<String> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                return Ok(serde_json::Value::from(r.unwrap()));
            }

            "TIME" => {
                let r: crate::Result<chrono::NaiveTime> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                let t = serde_json::to_value(&r.unwrap());
                return Ok(t.unwrap_or(serde_json::Value::Null));
            }
            "DATE" => {
                let r: crate::Result<chrono::NaiveDate> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                let t = serde_json::to_value(&r.unwrap());
                return Ok(t.unwrap_or(serde_json::Value::Null));
            }
            "TIMESTAMP" => {
                let r: crate::Result<chrono::NaiveDateTime> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                let t = serde_json::to_value(&r.unwrap());
                return Ok(t.unwrap_or(serde_json::Value::Null));
            }
            "TIMESTAMPTZ" => {
                let r: crate::Result<chrono::NaiveDateTime> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                let t = serde_json::to_value(&r.unwrap());
                return Ok(t.unwrap_or(serde_json::Value::Null));
            }
             "POINT" =>{
                let r: crate::Result<Point<f64>> = Decode::<'_, Postgres>::decode(self.clone());
                if r.is_err() {
                    return Err(r.err().unwrap());
                }
                let t = serde_json::to_value(&r.unwrap());
                return Ok(t.unwrap_or(serde_json::Value::Null));
            }

            _ => return Err(crate::Error::from(format!("un support database type for:{:?}!", type_string))),
        }
    }
}