Skip to content

Commit

Permalink
Send ReadOnlyIntent when ApplicationIntent=ReadOnly specified (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
suzak committed Jun 2, 2023
1 parent 3110856 commit 98943b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/client/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Config {
pub(crate) encryption: EncryptionLevel,
pub(crate) trust: TrustConfig,
pub(crate) auth: AuthMethod,
pub(crate) readonly: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Default for Config {
encryption: EncryptionLevel::NotSupported,
trust: TrustConfig::Default,
auth: AuthMethod::None,
readonly: false,
}
}
}
Expand Down Expand Up @@ -161,6 +163,13 @@ impl Config {
self.auth = auth;
}

/// Sets ApplicationIntent readonly.
///
/// - Defaults to `false`.
pub fn readonly(&mut self, readnoly: bool) {
self.readonly = readnoly;
}

pub(crate) fn get_host(&self) -> &str {
self.host
.as_deref()
Expand Down Expand Up @@ -257,6 +266,8 @@ impl Config {

builder.encryption(s.encrypt()?);

builder.readonly(s.readonly());

Ok(builder)
}
}
Expand Down Expand Up @@ -369,4 +380,11 @@ pub(crate) trait ConfigString {
)),
}
}

fn readonly(&self) -> bool {
self.dict()
.get("applicationintent")
.filter(|val| *val == "ReadOnly")
.is_some()
}
}
4 changes: 4 additions & 0 deletions src/client/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> Connection<S> {
config.database,
config.host,
config.application_name,
config.readonly,
prelogin,
)
.await?;
Expand Down Expand Up @@ -290,6 +291,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> Connection<S> {
db: Option<String>,
server_name: Option<String>,
application_name: Option<String>,
readonly: bool,
prelogin: PreloginMessage,
) -> crate::Result<Self> {

Check warning on line 296 in src/client/connection.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many arguments (8/7)

warning: this function has too many arguments (8/7) --> src/client/connection.rs:287:5 | 287 | / async fn login<'a>( 288 | | mut self, 289 | | auth: AuthMethod, 290 | | encryption: EncryptionLevel, ... | 295 | | prelogin: PreloginMessage, 296 | | ) -> crate::Result<Self> { | |____________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments = note: `#[warn(clippy::too_many_arguments)]` on by default
let mut login_message = LoginMessage::new();
Expand All @@ -306,6 +308,8 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send> Connection<S> {
login_message.app_name(app_name);
}

login_message.readonly(readonly);

match auth {
#[cfg(all(windows, feature = "winauth"))]
AuthMethod::Integrated => {
Expand Down
8 changes: 8 additions & 0 deletions src/tds/codec/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ impl<'a> LoginMessage<'a> {
nonce,
})
}

pub fn readonly(&mut self, readonly: bool) {
if readonly {
self.type_flags.insert(LoginTypeFlag::ReadOnlyIntent);
} else {
self.type_flags.remove(LoginTypeFlag::ReadOnlyIntent);
}
}
}

impl<'a> Encode<BytesMut> for LoginMessage<'a> {
Expand Down

0 comments on commit 98943b2

Please sign in to comment.