Skip to content

Commit 3532378

Browse files
cursoragentlovasoa
andcommitted
feat: Improve mssql port resolution logic
Co-authored-by: contact <[email protected]>
1 parent 59a12b9 commit 3532378

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

sqlx-core/src/mssql/connection/stream.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,27 @@ pub(crate) struct MssqlStream {
5151

5252
impl MssqlStream {
5353
pub(super) async fn connect(options: &MssqlConnectOptions) -> Result<Self, Error> {
54-
let port = if let Some(ref instance) = options.instance {
55-
super::ssrp::resolve_instance_port(&options.host, instance).await?
56-
} else {
57-
options.port
54+
let port = match (options.port, &options.instance) {
55+
(Some(port), _) => {
56+
log::debug!(
57+
"using explicitly specified port {} for host '{}'",
58+
port,
59+
options.host
60+
);
61+
port
62+
}
63+
(None, Some(instance)) => {
64+
super::ssrp::resolve_instance_port(&options.host, instance).await?
65+
}
66+
(None, None) => {
67+
const DEFAULT_PORT: u16 = 1433;
68+
log::debug!(
69+
"using default port {} for host '{}'",
70+
DEFAULT_PORT,
71+
options.host
72+
);
73+
DEFAULT_PORT
74+
}
5875
};
5976

6077
log::debug!("establishing TCP connection to {}:{}", options.host, port);

sqlx-core/src/mssql/options/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@ mod parse;
1313
/// mssql://[username[:password]@]host[:port]/database[?param1=value1&param2=value2...]
1414
/// ```
1515
///
16-
/// When connecting to a named instance, use the `instance` parameter:
16+
/// Port resolution priority:
17+
/// 1. If an explicit port is specified, it is always used
18+
/// 2. If a named instance is specified via `?instance=NAME`, the port is discovered via SSRP
19+
/// 3. Otherwise, the default port 1433 is used
20+
///
21+
/// Example with named instance (port auto-discovered):
1722
/// ```text
1823
/// mssql://user:pass@localhost/mydb?instance=SQLEXPRESS
1924
/// ```
20-
/// The port will be automatically discovered using the SQL Server Resolution Protocol (SSRP).
25+
///
26+
/// Example with explicit port (SSRP not used):
27+
/// ```text
28+
/// mssql://user:pass@localhost:1434/mydb?instance=SQLEXPRESS
29+
/// ```
2130
#[derive(Debug, Clone)]
2231
pub struct MssqlConnectOptions {
2332
pub(crate) host: String,
24-
pub(crate) port: u16,
33+
pub(crate) port: Option<u16>,
2534
pub(crate) username: String,
2635
pub(crate) database: String,
2736
pub(crate) password: Option<String>,
@@ -51,7 +60,7 @@ impl Default for MssqlConnectOptions {
5160
impl MssqlConnectOptions {
5261
pub fn new() -> Self {
5362
Self {
54-
port: 1433,
63+
port: None,
5564
host: String::from("localhost"),
5665
database: String::from("master"),
5766
username: String::from("sa"),
@@ -79,7 +88,7 @@ impl MssqlConnectOptions {
7988
}
8089

8190
pub fn port(mut self, port: u16) -> Self {
82-
self.port = port;
91+
self.port = Some(port);
8392
self
8493
}
8594

sqlx-core/src/mssql/options/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ impl FromStr for MssqlConnectOptions {
1919
/// - `username`: The username for SQL Server authentication.
2020
/// - `password`: The password for SQL Server authentication.
2121
/// - `host`: The hostname or IP address of the SQL Server.
22-
/// - `port`: The port number (default is 1433).
22+
/// - `port`: The port number. If not specified, defaults to 1433 or is discovered via SSRP when using named instances.
2323
/// - `database`: The name of the database to connect to.
2424
///
2525
/// Supported query parameters:
26-
/// - `instance`: SQL Server named instance. When specified, the port is automatically discovered using the SQL Server Resolution Protocol (SSRP).
26+
/// - `instance`: SQL Server named instance. When specified without an explicit port, the port is automatically discovered using the SQL Server Resolution Protocol (SSRP). If a port is explicitly specified, SSRP is not used.
2727
/// - `encrypt`: Controls connection encryption:
2828
/// - `strict`: Requires encryption and validates the server certificate.
2929
/// - `mandatory` or `true` or `yes`: Requires encryption but doesn't validate the server certificate.

0 commit comments

Comments
 (0)