Skip to content

Commit

Permalink
clippy stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
aersam committed May 1, 2024
1 parent 2d38bb6 commit be864ac
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 50 deletions.
76 changes: 38 additions & 38 deletions src/bulk_options.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
use bitflags::bitflags;

bitflags! {
/// Options for MS Sql Bulk Insert
/// see also: https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopyoptions?view=dotnet-plat-ext-7.0#fields
pub struct SqlBulkCopyOptions: u32 {
/// Default options
const Default = 0b00000000;
/// Preserve source identity values. When not specified, identity values are assigned by the destination.
const KeepIdentity = 0b00000001;
/// Check constraints while data is being inserted. By default, constraints are not checked.
const CheckConstraints = 0b00000010;
/// Obtain a bulk update lock for the duration of the bulk copy operation. When not specified, row locks are used.
const TableLock = 0b00000100;
/// Preserve null values in the destination table regardless of the settings for default values. When not specified, null values are replaced by default values where applicable.
const KeepNulls = 0b00001000;
/// When specified, cause the server to fire the insert triggers for the rows being inserted into the database.
const FireTriggers = 0b00010000;
}
}

impl Default for SqlBulkCopyOptions {
fn default() -> Self {
SqlBulkCopyOptions::Default
}
}

/// The sort order of a column, used for bulk insert
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SortOrder {
/// Ascending order
Ascending,
/// Descending order
Descending
}

/// An order hint for bulk insert
pub type ColumOrderHint<'a> = (&'a str, SortOrder);
use bitflags::bitflags;

bitflags! {
/// Options for MS Sql Bulk Insert
/// see also: https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlbulkcopyoptions?view=dotnet-plat-ext-7.0#fields
pub struct SqlBulkCopyOptions: u32 {
/// Default options
const Default = 0b00000000;
/// Preserve source identity values. When not specified, identity values are assigned by the destination.
const KeepIdentity = 0b00000001;
/// Check constraints while data is being inserted. By default, constraints are not checked.
const CheckConstraints = 0b00000010;
/// Obtain a bulk update lock for the duration of the bulk copy operation. When not specified, row locks are used.
const TableLock = 0b00000100;
/// Preserve null values in the destination table regardless of the settings for default values. When not specified, null values are replaced by default values where applicable.
const KeepNulls = 0b00001000;
/// When specified, cause the server to fire the insert triggers for the rows being inserted into the database.
const FireTriggers = 0b00010000;
}
}

impl Default for SqlBulkCopyOptions {
fn default() -> Self {
SqlBulkCopyOptions::Default
}
}

/// The sort order of a column, used for bulk insert
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SortOrder {
/// Ascending order
Ascending,
/// Descending order
Descending,
}

/// An order hint for bulk insert
pub type ColumOrderHint<'a> = (&'a str, SortOrder);
22 changes: 15 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub use auth::*;
pub use config::*;
pub(crate) use connection::*;

use crate::bulk_options::{SqlBulkCopyOptions, ColumOrderHint};
use crate::bulk_options::{ColumOrderHint, SqlBulkCopyOptions};
pub use crate::client::config::ado_net::AdoNetConfig;
use crate::tds::stream::ReceivedToken;
use crate::{
result::ExecuteResult,
Expand All @@ -29,7 +30,6 @@ use enumflags2::BitFlags;
use futures_util::io::{AsyncRead, AsyncWrite};
use futures_util::stream::TryStreamExt;
use std::{borrow::Cow, fmt::Debug};
pub use crate::client::config::ado_net::AdoNetConfig;

/// `Client` is the main entry point to the SQL Server, providing query
/// execution capabilities.
Expand Down Expand Up @@ -258,7 +258,9 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> Client<S> {
&'a mut self,
table: &'a str,
) -> crate::Result<BulkLoadRequest<'a, S>> {
return self.bulk_insert_with_options(table, &[], Default::default(), &[]).await;
return self
.bulk_insert_with_options(table, &[], Default::default(), &[])
.await;
}

/// Execute a `BULK INSERT` statement, efficiantly storing a large number of
Expand Down Expand Up @@ -390,10 +392,16 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> Client<S> {
query.push_str(
&order_hints
.iter()
.map(|(col, order)| format!("{} {}", col, match order {
crate::bulk_options::SortOrder::Ascending => "ASC",
crate::bulk_options::SortOrder::Descending => "DESC",
}))
.map(|(col, order)| {
format!(
"{} {}",
col,
match order {
crate::bulk_options::SortOrder::Ascending => "ASC",
crate::bulk_options::SortOrder::Descending => "DESC",
}
)
})
.join(", "),
);
query.push_str(")");
Expand Down
1 change: 1 addition & 0 deletions src/client/config/ado_net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{ConfigString, ServerDefinition};
use std::str::FromStr;

#[derive(Debug)]
pub struct AdoNetConfig {
dict: connection_string::AdoNetString,
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,19 @@ mod result;
mod row;
mod tds;

mod sql_browser;
mod bulk_options;
mod sql_browser;

pub use bulk_options::{ColumOrderHint, SortOrder, SqlBulkCopyOptions};
pub use client::{AuthMethod, Client, Config};
pub(crate) use error::Error;
pub use from_sql::{FromSql, FromSqlOwned};
pub use query::Query;
pub use result::*;
pub use row::{Column, ColumnType, Row};
pub use sql_browser::SqlBrowser;
use sql_read_bytes::*;
use tds::codec::*;
pub use tds::{
codec::{BulkLoadRequest, ColumnData, ColumnFlag, IntoRow, TokenRow, TypeLength},
numeric,
Expand All @@ -285,9 +288,6 @@ pub use tds::{
};
pub use to_sql::{IntoSql, ToSql};
pub use uuid::Uuid;
pub use bulk_options::{SqlBulkCopyOptions, SortOrder, ColumOrderHint};
use sql_read_bytes::*;
use tds::codec::*;

/// An alias for a result that holds crate's error type as the error.
pub type Result<T> = std::result::Result<T, Error>;
Expand Down
4 changes: 3 additions & 1 deletion src/tds/codec/column_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ impl<'a> Encode<BytesMutWithTypeInfo<'a>> for ColumnData<'a> {
true,
);
if let encoding_rs::EncoderResult::Unmappable(c) = res {
return Err(crate::Error::Encoding(format!("unrepresentable character:{}", c).into()));
return Err(crate::Error::Encoding(
format!("unrepresentable character:{}", c).into(),
));
}

if bytes.len() > vlc.len() {
Expand Down

0 comments on commit be864ac

Please sign in to comment.