-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julius de Bruijn
committed
Jul 27, 2021
1 parent
59248b4
commit 489adad
Showing
20 changed files
with
929 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use once_cell::sync::Lazy; | ||
use std::env; | ||
use tiberius::{BulkLoadRequest, Client, Config, IntoSql, TokenRow, TypeInfo}; | ||
use tokio::net::TcpStream; | ||
use tokio_util::compat::TokioAsyncWriteCompatExt; | ||
|
||
static CONN_STR: Lazy<String> = Lazy::new(|| { | ||
env::var("TIBERIUS_TEST_CONNECTION_STRING").unwrap_or_else(|_| { | ||
"server=tcp:localhost,1433;IntegratedSecurity=true;TrustServerCertificate=true".to_owned() | ||
}) | ||
}); | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
env_logger::init(); | ||
|
||
let config = Config::from_ado_string(&CONN_STR)?; | ||
|
||
let tcp = TcpStream::connect(config.get_addr()).await?; | ||
tcp.set_nodelay(true)?; | ||
|
||
let mut client = Client::connect(config, tcp.compat_write()).await?; | ||
|
||
let mut req = BulkLoadRequest::new(); | ||
req.add_column("val", TypeInfo::int()); | ||
|
||
for i in [0, 1, 2] { | ||
req.add_row({ | ||
let mut row = TokenRow::new(); | ||
row.push(i.into_sql()); | ||
row | ||
}); | ||
} | ||
|
||
let res = client.bulk_insert("bulk_test1", req).await?; | ||
|
||
dbg!(res); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use asynchronous_codec::BytesMut; | ||
use enumflags2::BitFlags; | ||
|
||
use super::{ | ||
BaseMetaDataColumn, Encode, MetaDataColumn, TokenColMetaData, TokenDone, TokenRow, TypeInfo, | ||
}; | ||
|
||
/// Column metadata for a bulk load request. | ||
#[derive(Debug, Default, Clone)] | ||
pub struct BulkLoadRequest<'a> { | ||
columns: Vec<MetaDataColumn>, | ||
rows: Vec<TokenRow<'a>>, | ||
} | ||
|
||
impl<'a> BulkLoadRequest<'a> { | ||
/// Creates a metadata with no columns specified. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Add a column to the request. Order should be same as the order of data | ||
/// in the rows. | ||
pub fn add_column(&mut self, name: &str, ty: TypeInfo) { | ||
self.columns.push(MetaDataColumn { | ||
base: BaseMetaDataColumn { | ||
flags: BitFlags::empty(), | ||
ty, | ||
}, | ||
col_name: name.into(), | ||
}); | ||
} | ||
|
||
/// Add a row of data to the request. | ||
pub fn add_row(&mut self, row: TokenRow<'a>) { | ||
self.rows.push(row); | ||
} | ||
|
||
pub(crate) fn column_descriptions(&'a self) -> impl Iterator<Item = String> + 'a { | ||
self.columns.iter().map(|c| format!("{}", c)) | ||
} | ||
} | ||
|
||
impl<'a> Encode<BytesMut> for BulkLoadRequest<'a> { | ||
fn encode(self, dst: &mut BytesMut) -> crate::Result<()> { | ||
let cmd = TokenColMetaData { | ||
columns: self.columns, | ||
}; | ||
|
||
cmd.encode(dst)?; | ||
|
||
for row in self.rows.into_iter() { | ||
row.encode(dst)?; | ||
} | ||
|
||
TokenDone::default().encode(dst)?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.