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
use {
super::{base::convert_table_name, ColumnSet},
crate::{DBBase, DBMut, ODBCDatabase, Result, Row, Value},
async_trait::async_trait,
odbc_api::buffers::TextRowSet,
};
const BATCH_SIZE: usize = 4096;
#[async_trait(?Send)]
impl DBMut for ODBCDatabase {
async fn insert_data(&mut self, table_name: &str, rows: Vec<Row>) -> Result<()> {
self.insert(table_name, rows.to_vec()).await?;
Ok(())
}
}
impl ODBCDatabase {
async fn insert(&mut self, table_name: &str, rows: Vec<Row>) -> Result<()> {
let connection = self
.environment
.connect_with_connection_string(&self.connection_string)?;
let schema = self.fetch_schema(&table_name).await?.unwrap();
let table_name = convert_table_name(table_name);
let columns = schema
.column_defs
.iter()
.map(|col_def| col_def.name.as_str())
.collect::<Vec<&str>>();
let rows: Vec<Vec<Value>> = rows.into_iter().map(|Row(row)| row).collect();
connection.set_autocommit(false)?;
for rows in rows.chunks(BATCH_SIZE) {
let column_set = ColumnSet::new(rows.to_vec(), BATCH_SIZE);
let query = column_set.query(&table_name, &columns);
let mut statement = connection.prepare(&query)?;
let buffer: TextRowSet = column_set.try_into()?;
statement.execute(&buffer)?;
}
connection.commit()?;
Ok(())
}
}