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

use bigdecimal::BigDecimal;
use oracle::Statement;
use rbdc::Error;
use rbs::Value;

pub trait Encode {
    fn encode(self, idx: usize, statement: &mut Statement) -> Result<(), Error>;
}

impl Encode for Value {
    fn encode(self, idx: usize, statement: &mut Statement) -> Result<(), Error> {
        let idx = idx + 1;//oracle is one-based
        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();
                    statement.bind(idx, &d).map_err(|e| e.to_string())?
                }
                "DateTime" => {
                    let s = v.as_str().unwrap_or_default();
                    let d = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S").unwrap();
                    statement.bind(idx, &d).map_err(|e| e.to_string())?
                }
                "Time" => {
                    //TODO: need to fix this
                    let s = v.into_string().unwrap();
                    statement.bind(idx, &s).map_err(|e| e.to_string())?
                }
                "Decimal" => {
                    let d = BigDecimal::from_str(&v.into_string().unwrap_or_default()).unwrap().to_string();
                    statement.bind(idx, &d).map_err(|e| e.to_string())?
                }
                "Json" => {
                    return Err(Error::from("unimpl"));
                }
                "Timestamp" => {
                    let t = v.as_u64().unwrap_or_default() as i64;
                    statement.bind(idx, &t).map_err(|e| e.to_string())?
                }
                "Uuid" => {
                    let d = v.into_string().unwrap();
                    statement.bind(idx, &d).map_err(|e| e.to_string())?
                }
                _ => {
                    return Err(Error::from("unimpl"));
                }
            }
            Value::String(str) => {
                statement.bind(idx, &str).map_err(|e| e.to_string())?
            }
            Value::U32(u) => {
                statement.bind(idx, &u).map_err(|e| e.to_string())?
            }
            Value::U64(u) => {
                statement.bind(idx, &u).map_err(|e| e.to_string())?
            }
            Value::I32(int) => {
                statement.bind(idx, &int).map_err(|e| e.to_string())?
            }
            Value::I64(long) => {
                statement.bind(idx, &long).map_err(|e| e.to_string())?
            }
            Value::F32(float) => {
                statement.bind(idx, &float).map_err(|e| e.to_string())?
            }
            Value::F64(double) => {
                statement.bind(idx, &double).map_err(|e| e.to_string())?
            }
            Value::Binary(bin) => {
                statement.bind(idx, &bin).map_err(|e| e.to_string())?
            }
            Value::Array(bin) => {
                //add suupport for Vec<u8>
                let x1:Vec<u8> = bin.into_iter().map(|x|(x.as_u64().unwrap())as u8).collect();
                statement.bind(idx, &x1).map_err(|e| e.to_string())?
            }
            Value::Null => {
                statement.bind(idx, &Option::<String>::None).unwrap();
            }
            //TODO: more types!
            _ => {
                statement.bind(idx, &self.to_string()).map_err(|e| e.to_string())?
            }
        }
        Ok(())
    }
}