tiberius/tds/
context.rs

1use super::codec::*;
2use std::sync::Arc;
3
4/// Context, that might be required to make sure we understand and are understood by the server
5#[derive(Debug)]
6pub(crate) struct Context {
7    version: FeatureLevel,
8    packet_size: u32,
9    packet_id: u8,
10    transaction_desc: [u8; 8],
11    last_meta: Option<Arc<TokenColMetaData<'static>>>,
12    spn: Option<String>,
13}
14
15impl Context {
16    pub fn new() -> Context {
17        Context {
18            version: FeatureLevel::SqlServerN,
19            packet_size: 4096,
20            packet_id: 0,
21            transaction_desc: [0; 8],
22            last_meta: None,
23            spn: None,
24        }
25    }
26
27    pub fn next_packet_id(&mut self) -> u8 {
28        let id = self.packet_id;
29        self.packet_id = self.packet_id.wrapping_add(1);
30        id
31    }
32
33    pub fn set_last_meta(&mut self, meta: Arc<TokenColMetaData<'static>>) {
34        self.last_meta.replace(meta);
35    }
36
37    pub fn last_meta(&self) -> Option<Arc<TokenColMetaData<'static>>> {
38        self.last_meta.clone()
39    }
40
41    pub fn packet_size(&self) -> u32 {
42        self.packet_size
43    }
44
45    pub fn set_packet_size(&mut self, new_size: u32) {
46        self.packet_size = new_size;
47    }
48
49    pub fn transaction_descriptor(&self) -> [u8; 8] {
50        self.transaction_desc
51    }
52
53    pub fn set_transaction_descriptor(&mut self, desc: [u8; 8]) {
54        self.transaction_desc = desc;
55    }
56
57    pub fn version(&self) -> FeatureLevel {
58        self.version
59    }
60
61    pub fn set_spn(&mut self, host: impl AsRef<str>, port: u16) {
62        self.spn = Some(format!("MSSQLSvc/{}:{}", host.as_ref(), port));
63    }
64
65    #[cfg(any(windows, all(unix, feature = "integrated-auth-gssapi")))]
66    pub fn spn(&self) -> &str {
67        self.spn.as_deref().unwrap_or("")
68    }
69}