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
use std::sync::Arc;

use futures_core::future::BoxFuture;
use oracle::Connection as OraConnect;
use oracle::sql_type::OracleType;
use rbdc::db::{Connection, ExecResult, Row};
use rbdc::Error;
use rbs::Value;

use crate::{OracleColumn, OracleData, OracleRow};
use crate::driver::OracleDriver;
use crate::encode::Encode;
use crate::options::OracleConnectOptions;

#[derive(Clone)]
pub struct OracleConnection {
    pub conn: Arc<OraConnect>,
    pub is_trans: bool
}

impl Connection for OracleConnection {
    fn get_rows(
        &mut self,
        sql: &str,
        params: Vec<Value>,
    ) -> BoxFuture<Result<Vec<Box<dyn Row>>, rbdc::Error>> {
        let sql: String = OracleDriver {}.pub_exchange(sql);
        Box::pin(async move {
            let builder = self.conn.statement(&sql);
            let mut stmt = builder.build().map_err(|e| Error::from(e.to_string()))?;

            for (idx,x) in params.into_iter().enumerate() {
                x.encode(idx,&mut stmt).map_err(|e| Error::from(e.to_string()))?
            }

            let rows = stmt.query(&[]).map_err(|e| Error::from(e.to_string()))?;
            let col_infos = rows.column_info();
            let col_count = col_infos.len();
            let mut results = Vec::with_capacity(col_count);
            let mut columns = Vec::with_capacity(col_count);
            for info in col_infos.iter() {
                columns.push(OracleColumn {
                    name: info.name().to_string().to_lowercase(),
                    column_type: info.oracle_type().clone(),
                })
            }
            for row_result in rows {
                let row = row_result.map_err(|e| Error::from(e.to_string()))?;
                let mut datas = Vec::with_capacity(col_count);
                for col in row.sql_values().iter() {
                    let t = col.oracle_type().map_err(|e| Error::from(e.to_string()))?;
                    let t = t.clone();
                    if let Ok(true) = col.is_null() {
                        datas.push(OracleData {
                            str: None,
                            bin: None,
                            column_type: t.clone(),
                            is_sql_null: true
                        })
                    } else {
                        if t == OracleType::BLOB {
                            match col.get::<Vec<u8>>() {
                                Ok(bin) => datas.push(OracleData {
                                    str: None,
                                    bin: Some(bin),
                                    column_type: t.clone(),
                                    is_sql_null: false
                                }),
                                Err(_) => datas.push(OracleData {
                                    str: None,
                                    bin: None,
                                    column_type: t.clone(),
                                    is_sql_null: false
                                }),
                            }
                        }else{
                            match col.get::<String>() {
                                Ok(str) => datas.push(OracleData {
                                    str: Some(str),
                                    bin: None,
                                    column_type: t.clone(),
                                    is_sql_null: false
                                }),
                                Err(_) => datas.push(OracleData {
                                    str: None,
                                    bin: None,
                                    column_type: t.clone(),
                                    is_sql_null: false
                                }),
                            }
                        }
                    }
                }
                let row = OracleRow {
                    columns: Arc::new(columns.clone()),
                    datas: datas,
                };
                results.push(Box::new(row) as Box<dyn Row>);
            }
            Ok(results)
        })
    }

    fn exec(
        &mut self,
        sql: &str,
        params: Vec<Value>,
    ) -> BoxFuture<Result<ExecResult, rbdc::Error>> {
        if sql == "begin" {
            self.is_trans = true;
            Box::pin(async move {
                Ok(ExecResult {
                    rows_affected: 0,
                    last_insert_id: Value::Null,
                })
            })
        } else if sql == "commit" {
            self.is_trans = false;
            Box::pin(async move {
                self.conn.commit().unwrap();
                Ok(ExecResult {
                    rows_affected: 0,
                    last_insert_id: Value::Null,
                })
            })
        } else if sql == "rollback" {
            self.is_trans = false;
            Box::pin(async move {
                self.conn.rollback().unwrap();
                Ok(ExecResult {
                    rows_affected: 0,
                    last_insert_id: Value::Null,
                })
            })
        } else {
            let sql: String = OracleDriver {}.pub_exchange(sql);
            Box::pin(async move {
                let builder = self.conn.statement(&sql);
                let mut stmt = builder.build().map_err(|e| Error::from(e.to_string()))?;
                for (idx,x) in params.into_iter().enumerate() {
                    x.encode(idx,&mut stmt).map_err(|e| Error::from(e.to_string()))?
                }
                stmt
                    .execute(&[])
                    .map_err(|e| Error::from(e.to_string()))?;
                let rows_affected = stmt.row_count().map_err(|e| Error::from(e.to_string()))?;
                if !(self.is_trans) {
                    self.conn.commit().map_err(|e| Error::from(e.to_string()))?;
                }
                Ok(ExecResult {
                    rows_affected,
                    last_insert_id: Value::Null,
                })
            })
        }
    }

    fn close(&mut self) -> BoxFuture<Result<(), rbdc::Error>> {
        Box::pin(async move {
            self.conn.commit().map_err(|e| Error::from(e.to_string()))?;
            self.conn.close().map_err(|e| Error::from(e.to_string()))?;
            Ok(())
        })
    }

    fn ping(&mut self) -> BoxFuture<Result<(), rbdc::Error>> {
        Box::pin(async move {
            self.conn.ping()
                .map_err(|e| Error::from(e.to_string()))?;
            Ok(())
        })
    }
}

impl OracleConnection {
    pub async fn establish(opt: &OracleConnectOptions) -> Result<Self, Error> {
        let conn = OraConnect::connect(opt.username.clone(), opt.password.clone(), opt.connect_string.clone())
            .map_err(|e| Error::from(e.to_string()))?;
        Ok(Self {
            conn: Arc::new(conn),
            is_trans: false
        })
    }
}