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
use std::str::FromStr;

use bigdecimal::BigDecimal;
use oracle::sql_type::ToSql;
use rbdc::Error;
use rbs::Value;

pub trait Encode {
    fn encode(self, vec: & mut Vec<Box<dyn ToSql>>,) -> Result<(), Error>;
}

impl Encode for Value {
    fn encode(self, vec: & mut Vec<Box<dyn ToSql>>) -> Result<(), Error> {
        match self{
            Value::Ext(t, v) => match t {
                "Date" => {
                    let s = v.as_str().unwrap_or_default();
                    let d = chrono::NaiveDate::parse_from_str(s,"%Y-%m-%d").unwrap();
                    vec.push(Box::new(d));
                },
                "DateTime" => {
                    let s = v.as_str().unwrap_or_default();
                    let d = chrono::NaiveDateTime::parse_from_str(s,"%Y-%m-%d %H:%M:%S").unwrap();
                    vec.push(Box::new(d));
                }
                "Time" => {
                    //TODO: need to fix this
                    let s = v.into_string().unwrap();
                    vec.push(Box::new(s));
                }
                "Decimal" => {
                    let d = BigDecimal::from_str(&v.into_string().unwrap_or_default()).unwrap().to_string();
                    vec.push(Box::new(d));
                }
                "Json" => {
                    return Err(Error::from("unimpl"));
                }
                "Timestamp" => {
                    let t = v.as_u64().unwrap_or_default() as i64;
                    vec.push(Box::new(t));
                }
                "Uuid" => {
                    let d = v.into_string().unwrap();
                    vec.push(Box::new(d));
                }
                _ => {
                    return Err(Error::from("unimpl"));
                },
            }
            Value::String(str)=>{
                vec.push(Box::new(str));
            }
            Value::I32(int)=>{
                vec.push(Box::new(int));
            }
            Value::I64(int)=>{
                vec.push(Box::new(int));
            }
            //TODO: more types!
            _=>{
                vec.push(Box::new(self.to_string()));
            }
        }
        Ok(())
    }
}