rbdc_oracle/
encode.rs

1use std::str::FromStr;
2
3use bigdecimal::BigDecimal;
4use oracle::Statement;
5use rbdc::Error;
6use rbs::Value;
7
8pub trait Encode {
9    fn encode(self, idx: usize, statement: &mut Statement) -> Result<(), Error>;
10}
11
12impl Encode for Value {
13    fn encode(self, idx: usize, statement: &mut Statement) -> Result<(), Error> {
14        let idx = idx + 1;//oracle is one-based
15        match self {
16            Value::Ext(t, v) => match t {
17                "Date" => {
18                    let s = v.as_str().unwrap_or_default();
19                    let d = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap();
20                    statement.bind(idx, &d).map_err(|e| e.to_string())?
21                }
22                "DateTime" => {
23                    let s = v.as_str().unwrap_or_default();
24                    let d = chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%dT%H:%M:%S%z").unwrap();
25                    statement.bind(idx, &d).map_err(|e| e.to_string())?
26                }
27                "Time" => {
28                    //TODO: need to fix this
29                    let s = v.into_string().unwrap();
30                    statement.bind(idx, &s).map_err(|e| e.to_string())?
31                }
32                "Decimal" => {
33                    let d = BigDecimal::from_str(&v.into_string().unwrap_or_default()).unwrap().to_string();
34                    statement.bind(idx, &d).map_err(|e| e.to_string())?
35                }
36                "Json" => {
37                    return Err(Error::from("unimpl"));
38                }
39                "Timestamp" => {
40                    let t = v.as_u64().unwrap_or_default() as i64;
41                    statement.bind(idx, &t).map_err(|e| e.to_string())?
42                }
43                "Uuid" => {
44                    let d = v.into_string().unwrap();
45                    statement.bind(idx, &d).map_err(|e| e.to_string())?
46                }
47                _ => {
48                    return Err(Error::from("unimpl"));
49                }
50            }
51            Value::String(str) => {
52                statement.bind(idx, &str).map_err(|e| e.to_string())?
53            }
54            Value::U32(u) => {
55                statement.bind(idx, &u).map_err(|e| e.to_string())?
56            }
57            Value::U64(u) => {
58                statement.bind(idx, &u).map_err(|e| e.to_string())?
59            }
60            Value::I32(int) => {
61                statement.bind(idx, &int).map_err(|e| e.to_string())?
62            }
63            Value::I64(long) => {
64                statement.bind(idx, &long).map_err(|e| e.to_string())?
65            }
66            Value::F32(float) => {
67                statement.bind(idx, &float).map_err(|e| e.to_string())?
68            }
69            Value::F64(double) => {
70                statement.bind(idx, &double).map_err(|e| e.to_string())?
71            }
72            Value::Binary(bin) => {
73                statement.bind(idx, &bin).map_err(|e| e.to_string())?
74            }
75            Value::Null => {
76                statement.bind(idx, &Option::<String>::None).unwrap();
77            }
78            //TODO: more types!
79            _ => {
80                statement.bind(idx, &self.to_string()).map_err(|e| e.to_string())?
81            }
82        }
83        Ok(())
84    }
85}