pub struct Config { /* private fields */ }Expand description
The Config struct contains all configuration information
required for connecting to the database with a Client. It also provides
the server address when connecting to a TcpStream via the
get_addr method.
When using an ADO.NET connection string, it can be
constructed using the from_ado_string function.
Alternatively, a ConfigBuilder can be used for an ergonomic,
chainable construction. Create one via builder, call its
setter methods and finalize it with build.
Implementations§
Source§impl Config
impl Config
Sourcepub fn builder() -> ConfigBuilder
pub fn builder() -> ConfigBuilder
Create a new ConfigBuilder initialized with the default settings.
This provides an ergonomic, chainable alternative to constructing a
Config via its individual setter methods.
§Example
let config = Config::builder()
.host("localhost")
.port(1433)
.database("master")
.authentication(AuthMethod::sql_server("SA", "<password>"))
.build();
assert_eq!("localhost:1433", config.get_addr());Sourcepub fn host(&mut self, host: impl ToString)
pub fn host(&mut self, host: impl ToString)
A host or ip address to connect to.
- Defaults to
localhost.
Sourcepub fn database(&mut self, database: impl ToString)
pub fn database(&mut self, database: impl ToString)
The database to connect to.
- Defaults to
master.
Sourcepub fn instance_name(&mut self, name: impl ToString)
pub fn instance_name(&mut self, name: impl ToString)
The instance name as defined in the SQL Browser. Only available on Windows platforms.
If specified, the port is replaced with the value returned from the browser.
- Defaults to no name specified.
Sourcepub fn application_name(&mut self, name: impl ToString)
pub fn application_name(&mut self, name: impl ToString)
Sets the application name to the connection, queryable with the
APP_NAME() command.
- Defaults to no name specified.
Sourcepub fn packet_size(&mut self, size: u32)
pub fn packet_size(&mut self, size: u32)
Sets the TDS packet size for the connection.
Larger packet sizes can improve bulk insert performance by reducing the number of network round-trips. Valid values are 512 to 32767. The server may negotiate a different size.
- Defaults to 4096 bytes.
Sourcepub fn get_packet_size(&self) -> Option<u32>
pub fn get_packet_size(&self) -> Option<u32>
Gets the configured packet size, if set.
Sourcepub fn encryption(&mut self, encryption: EncryptionLevel)
pub fn encryption(&mut self, encryption: EncryptionLevel)
Set the preferred encryption level.
- With
tlsfeature, defaults toRequired. - Without
tlsfeature, defaults toNotSupported.
Sourcepub fn trust_cert(&mut self)
pub fn trust_cert(&mut self)
If set, the server certificate will not be validated and it is accepted as-is.
On production setting, the certificate should be added to the local key
storage (or use trust_cert_ca instead), using this setting is potentially dangerous.
§Panics
Will panic in case trust_cert_ca was called before.
- Defaults to
default, meaning server certificate is validated against system-truststore.
Sourcepub fn trust_cert_ca(&mut self, path: impl ToString)
pub fn trust_cert_ca(&mut self, path: impl ToString)
If set, the server certificate will be validated against the given CA certificate in in addition to the system-truststore. Useful when using self-signed certificates on the server without having to disable the trust-chain.
§Panics
Will panic in case trust_cert was called before.
- Defaults to validating the server certificate is validated against system’s certificate storage.
Sourcepub fn hostname_in_certificate(&mut self, hostname: impl ToString)
pub fn hostname_in_certificate(&mut self, hostname: impl ToString)
Sets the hostname that the server certificate is validated against,
instead of the value given to host.
This is useful when connecting through an IP address, a tunnel, or a load balancer whose certificate carries a different subject/SAN than the address used to reach it (see issue #340).
- Defaults to the value of
host.
Sourcepub fn client_name(&mut self, name: impl ToString)
pub fn client_name(&mut self, name: impl ToString)
Sets the client / workstation name reported to the server in the login
record (queryable with HOST_NAME()).
- Defaults to the local workstation id (the machine hostname).
Sourcepub fn authentication(&mut self, auth: AuthMethod)
pub fn authentication(&mut self, auth: AuthMethod)
Sets the authentication method.
- Defaults to
None.
Sourcepub fn multi_subnet_failover(&mut self, multi_subnet_failover: bool)
pub fn multi_subnet_failover(&mut self, multi_subnet_failover: bool)
Enable multi-subnet failover.
When enabled and the server host name resolves to more than one IP
address (for example, an Always On availability group listener spread
across subnets), connections are attempted to all resolved addresses in
parallel and the first one to succeed is used. This mirrors the ADO.NET
MultiSubnetFailover connection-string keyword.
- Defaults to
false.
Sourcepub fn get_multi_subnet_failover(&self) -> bool
pub fn get_multi_subnet_failover(&self) -> bool
Returns whether multi-subnet failover is enabled.
Sourcepub fn client_certificate(
&mut self,
cert: impl Into<PathBuf>,
key: impl Into<PathBuf>,
)
Available on crate features native-tls or rustls or vendored-openssl only.
pub fn client_certificate( &mut self, cert: impl Into<PathBuf>, key: impl Into<PathBuf>, )
native-tls or rustls or vendored-openssl only.Supplies a client certificate and private key used to authenticate the
client to the server during the TLS handshake (mutual TLS). This is
required for TDS 8.0 “strict” connections that use client-certificate
authentication (ENCRYPT_CLIENT_CERT), and may also be used with the
classic (pre-8.0) TLS handshake when the server requests a client
certificate.
Both arguments are paths to files:
cert: the client certificate, PEM (.pem/.crt) or DER (.der).key: the matching private key, PEM (.pem/.key) or DER (.der, PKCS#8).
Backend support:
-
rustls: PEM and DER certificate/key files. -
native-tls: PEM certificate + PEM PKCS#8 key only (DER files are rejected at connect time; useclient_certificate_pkcs12for a bundled DER identity). -
vendored-openssl(opentls): does not support separate certificate/key files; useclient_certificate_pkcs12instead. -
Defaults to no client certificate.
Sourcepub fn client_certificate_pkcs12(
&mut self,
path: impl Into<PathBuf>,
password: impl Into<String>,
)
Available on crate features native-tls or vendored-openssl only.
pub fn client_certificate_pkcs12( &mut self, path: impl Into<PathBuf>, password: impl Into<String>, )
native-tls or vendored-openssl only.Supplies a client identity from a PKCS#12 / PFX bundle (certificate,
private key and any chain, encrypted with password) used to
authenticate the client to the server during the TLS handshake (mutual
TLS).
Only supported by the native-tls and vendored-openssl backends; the
rustls backend rejects PKCS#12 identities at connect time (supply
separate PEM/DER files via client_certificate instead).
- Defaults to no client certificate.
Sourcepub fn from_ado_string(s: &str) -> Result<Self>
pub fn from_ado_string(s: &str) -> Result<Self>
Creates a new Config from an ADO.NET connection string.
§Supported parameters
All parameter keys are handled case-insensitive.
| Parameter | Allowed values | Description |
|---|---|---|
server | <string> | The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name. The correct form of this parameter is either tcp:host,port or tcp:host\\instance |
IntegratedSecurity | true,false,yes,no | Toggle between Windows/Kerberos authentication and SQL authentication. |
uid,username,user,user id | <string> | The SQL Server login account. |
password,pwd | <string> | The password for the SQL Server account logging on. |
database | <string> | The name of the database. |
TrustServerCertificate | true,false,yes,no | Specifies whether the driver trusts the server certificate when connecting using TLS. Cannot be used toghether with TrustServerCertificateCA |
TrustServerCertificateCA | <path> | Path to a pem, crt or der certificate file. Cannot be used together with TrustServerCertificate |
encrypt | strict,true,false,yes,no,DANGER_PLAINTEXT | Specifies whether the driver uses TLS to encrypt communication. strict (TDS 8.0) requires the tds80 feature. |
Application Name, ApplicationName | <string> | Sets the application name for the connection. |
HostNameInCertificate, HostName In Certificate | <string> | The hostname the server certificate is validated against. Defaults to server. |
WorkstationID, Workstation ID | <string> | The client / workstation name reported to the server. |
MultiSubnetFailover | true,false,yes,no | When enabled, connections are attempted in parallel to all IP addresses the server resolves to, and the first to succeed is used. |
Sourcepub fn from_jdbc_string(s: &str) -> Result<Self>
pub fn from_jdbc_string(s: &str) -> Result<Self>
Creates a new Config from a JDBC connection string.
See from_ado_string method for supported parameters.