From bd665587fa0304aa4b5fe3ab2dcd612997a89d5d Mon Sep 17 00:00:00 2001 From: Xiaoying Wang Date: Fri, 5 Nov 2021 05:38:28 +0000 Subject: [PATCH] 0.2.2-alpha.3: add mssql instance name --- Cargo.lock | 4 +-- connectorx-python/Cargo.toml | 2 +- connectorx-python/pyproject.toml | 2 +- connectorx-python/src/source_router.rs | 17 ++--------- connectorx/Cargo.toml | 2 +- connectorx/src/sources/mssql/mod.rs | 42 +++++++++++++++++--------- 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f2ec41ba..46b7ed3c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -537,7 +537,7 @@ dependencies = [ [[package]] name = "connectorx" -version = "0.2.2-alpha.2" +version = "0.2.2-alpha.3" dependencies = [ "anyhow", "arrow", @@ -584,7 +584,7 @@ dependencies = [ [[package]] name = "connectorx-python" -version = "0.2.2-alpha.2" +version = "0.2.2-alpha.3" dependencies = [ "anyhow", "arrow", diff --git a/connectorx-python/Cargo.toml b/connectorx-python/Cargo.toml index 313d84966..a7d7022d1 100644 --- a/connectorx-python/Cargo.toml +++ b/connectorx-python/Cargo.toml @@ -2,7 +2,7 @@ authors = ["Weiyuan Wu "] edition = "2018" name = "connectorx-python" -version = "0.2.2-alpha.2" +version = "0.2.2-alpha.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/connectorx-python/pyproject.toml b/connectorx-python/pyproject.toml index f19f76e25..9910edd09 100644 --- a/connectorx-python/pyproject.toml +++ b/connectorx-python/pyproject.toml @@ -19,7 +19,7 @@ license = "MIT" maintainers = ["Weiyuan Wu "] name = "connectorx" readme = "README.md" # Markdown files are supported -version = "0.2.2-alpha.2" +version = "0.2.2-alpha.3" [tool.poetry.dependencies] dask = {version = "^2021", optional = true, extras = ["dataframe"]} diff --git a/connectorx-python/src/source_router.rs b/connectorx-python/src/source_router.rs index ec9c6f4a9..edf2b78ec 100644 --- a/connectorx-python/src/source_router.rs +++ b/connectorx-python/src/source_router.rs @@ -2,7 +2,7 @@ use crate::errors::{ConnectorXPythonError, Result}; use anyhow::anyhow; use connectorx::{ sources::{ - mssql::{FloatN, IntN, MsSQLTypeSystem}, + mssql::{mssql_config, FloatN, IntN, MsSQLTypeSystem}, mysql::{MySQLSourceError, MySQLTypeSystem}, oracle::OracleDialect, postgres::{rewrite_tls_args, PostgresTypeSystem}, @@ -235,21 +235,10 @@ fn mysql_get_partition_range(conn: &Url, query: &str, col: &str) -> (i64, i64) { #[throws(ConnectorXPythonError)] fn mssql_get_partition_range(conn: &Url, query: &str, col: &str) -> (i64, i64) { - use tiberius::{AuthMethod, Client, Config, EncryptionLevel}; + use tiberius::Client; use tokio::runtime::Runtime; let rt = Runtime::new().unwrap(); - let mut config = Config::new(); - - config.host(decode(conn.host_str().unwrap_or("localhost"))?.into_owned()); - config.port(conn.port().unwrap_or(1433)); - config.authentication(AuthMethod::sql_server( - decode(conn.username())?.into_owned(), - decode(conn.password().unwrap_or(""))?.into_owned(), - )); - - config.database(&conn.path()[1..]); // remove the leading "/" - config.encryption(EncryptionLevel::NotSupported); - + let config = mssql_config(conn)?; let tcp = rt.block_on(TcpStream::connect(config.get_addr()))?; tcp.set_nodelay(true)?; diff --git a/connectorx/Cargo.toml b/connectorx/Cargo.toml index 08be677c4..89606df8c 100644 --- a/connectorx/Cargo.toml +++ b/connectorx/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT" name = "connectorx" readme = "../README.md" repository = "https://github.com/sfu-db/connector-x" -version = "0.2.2-alpha.2" +version = "0.2.2-alpha.3" [dependencies] anyhow = "1" diff --git a/connectorx/src/sources/mssql/mod.rs b/connectorx/src/sources/mssql/mod.rs index 4e713c84f..f7462c436 100644 --- a/connectorx/src/sources/mssql/mod.rs +++ b/connectorx/src/sources/mssql/mod.rs @@ -41,24 +41,38 @@ pub struct MsSQLSource { buf_size: usize, } +#[throws(MsSQLSourceError)] +pub fn mssql_config(url: &Url) -> Config { + let mut config = Config::new(); + + let host = decode(url.host_str().unwrap_or("localhost"))?.into_owned(); + let hosts: Vec<&str> = host.split('\\').collect(); + match hosts.len() { + 1 => config.host(host), + 2 => { + // SQL Server support instance name: `server\instance:port` + config.host(hosts[0]); + config.instance_name(hosts[1]); + } + _ => throw!(anyhow!("MsSQL hostname parse error: {}", host)), + } + config.port(url.port().unwrap_or(1433)); + // remove the leading "/" + config.database(&url.path()[1..]); + // Using SQL Server authentication. + config.authentication(AuthMethod::sql_server( + decode(url.username())?.to_owned(), + decode(url.password().unwrap_or(""))?.to_owned(), + )); + config.encryption(EncryptionLevel::NotSupported); + config +} + impl MsSQLSource { #[throws(MsSQLSourceError)] pub fn new(rt: Arc, conn: &str, nconn: usize) -> Self { - let mut config = Config::new(); let url = Url::parse(conn)?; - - config.host(decode(url.host_str().unwrap_or("localhost"))?.into_owned()); - config.port(url.port().unwrap_or(1433)); - - // Using SQL Server authentication. - config.authentication(AuthMethod::sql_server( - decode(url.username())?.to_owned(), - decode(url.password().unwrap_or(""))?.to_owned(), - )); - - config.database(&url.path()[1..]); // remove the leading "/" - config.encryption(EncryptionLevel::NotSupported); - + let config = mssql_config(&url)?; let manager = bb8_tiberius::ConnectionManager::new(config); let pool = rt.block_on(Pool::builder().max_size(nconn as u32).build(manager))?;