Config

Struct Config 

Source
#[non_exhaustive]
pub struct Config {
Show 18 fields pub host: String, pub port: u16, pub database: Option<String>, pub credentials: Credentials, pub tls: TlsConfig, pub application_name: String, pub connect_timeout: Duration, pub command_timeout: Duration, pub packet_size: u16, pub strict_mode: bool, pub trust_server_certificate: bool, pub instance: Option<String>, pub mars: bool, pub encrypt: bool, pub redirect: RedirectConfig, pub retry: RetryPolicy, pub timeouts: TimeoutConfig, pub tds_version: TdsVersion,
}
Expand description

Configuration for connecting to SQL Server.

This struct is marked #[non_exhaustive] to allow adding new fields in future releases without breaking semver. Use Config::default() or Config::from_connection_string() to construct instances.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§host: String

Server hostname or IP address.

§port: u16

Server port (default: 1433).

§database: Option<String>

Database name.

§credentials: Credentials

Authentication credentials.

§tls: TlsConfig

TLS configuration.

§application_name: String

Application name (shown in SQL Server management tools).

§connect_timeout: Duration

Connection timeout.

§command_timeout: Duration

Command timeout.

§packet_size: u16

TDS packet size.

§strict_mode: bool

Whether to use TDS 8.0 strict mode.

§trust_server_certificate: bool

Whether to trust the server certificate.

§instance: Option<String>

Instance name (for named instances).

§mars: bool

Whether to enable MARS (Multiple Active Result Sets).

§encrypt: bool

Whether to require encryption (TLS). When true, the connection will use TLS even if the server doesn’t require it. When false, encryption is used only if the server requires it.

§redirect: RedirectConfig

Redirect handling configuration (for Azure SQL).

§retry: RetryPolicy

Retry policy for transient error handling.

§timeouts: TimeoutConfig

Timeout configuration for various connection phases.

§tds_version: TdsVersion

Requested TDS protocol version.

This specifies which TDS protocol version to request during connection. The server may negotiate a lower version if it doesn’t support the requested version.

Supported versions:

  • TdsVersion::V7_3A - SQL Server 2008
  • TdsVersion::V7_3B - SQL Server 2008 R2
  • TdsVersion::V7_4 - SQL Server 2012+ (default)
  • TdsVersion::V8_0 - SQL Server 2022+ strict mode (requires strict_mode = true)

Note: When strict_mode is enabled, this is ignored and TDS 8.0 is used.

Implementations§

Source§

impl Config

Source

pub fn new() -> Self

Create a new configuration with default values.

Source

pub fn from_connection_string(conn_str: &str) -> Result<Self, Error>

Parse a connection string into configuration.

Supports ADO.NET-style connection strings:

Server=localhost;Database=mydb;User Id=sa;Password=secret;
Source

pub fn host(self, host: impl Into<String>) -> Self

Set the server host.

Source

pub fn port(self, port: u16) -> Self

Set the server port.

Source

pub fn database(self, database: impl Into<String>) -> Self

Set the database name.

Source

pub fn credentials(self, credentials: Credentials) -> Self

Set the credentials.

Source

pub fn application_name(self, name: impl Into<String>) -> Self

Set the application name.

Source

pub fn connect_timeout(self, timeout: Duration) -> Self

Set the connect timeout.

Source

pub fn trust_server_certificate(self, trust: bool) -> Self

Set trust server certificate option.

Source

pub fn strict_mode(self, enabled: bool) -> Self

Enable TDS 8.0 strict mode.

Source

pub fn tds_version(self, version: TdsVersion) -> Self

Set the TDS protocol version.

This specifies which TDS protocol version to request during connection. The server may negotiate a lower version if it doesn’t support the requested version.

§Examples
use mssql_client::Config;
use tds_protocol::version::TdsVersion;

// Connect to SQL Server 2008
let config = Config::new()
    .host("legacy-server")
    .tds_version(TdsVersion::V7_3A);

// Connect to SQL Server 2008 R2
let config = Config::new()
    .host("legacy-server")
    .tds_version(TdsVersion::V7_3B);

Note: When strict_mode is enabled, this is ignored and TDS 8.0 is used.

Source

pub fn encrypt(self, enabled: bool) -> Self

Enable or disable TLS encryption.

When true (default), the connection will use TLS encryption. When false, encryption is used only if the server requires it.

Warning: Disabling encryption is insecure and should only be used for development/testing on trusted networks.

Source

pub fn with_host(self, host: &str) -> Self

Create a new configuration with a different host (for routing).

Source

pub fn with_port(self, port: u16) -> Self

Create a new configuration with a different port (for routing).

Source

pub fn redirect(self, redirect: RedirectConfig) -> Self

Set the redirect handling configuration.

Source

pub fn max_redirects(self, max: u8) -> Self

Set the maximum number of redirect attempts.

Source

pub fn retry(self, retry: RetryPolicy) -> Self

Set the retry policy for transient error handling.

Source

pub fn max_retries(self, max: u32) -> Self

Set the maximum number of retry attempts.

Source

pub fn timeouts(self, timeouts: TimeoutConfig) -> Self

Set the timeout configuration.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more