sqlx_core_oldapi/mssql/connection/
mod.rs

1use crate::common::StatementCache;
2use crate::connection::{Connection, LogSettings};
3use crate::error::Error;
4use crate::executor::Executor;
5use crate::mssql::connection::stream::MssqlStream;
6use crate::mssql::statement::MssqlStatementMetadata;
7use crate::mssql::{Mssql, MssqlConnectOptions};
8use crate::transaction::Transaction;
9use futures_core::future::BoxFuture;
10use futures_util::{FutureExt, TryFutureExt};
11use std::fmt::{self, Debug, Formatter};
12use std::sync::Arc;
13
14mod establish;
15mod executor;
16mod prepare;
17mod ssrp;
18mod stream;
19mod tls_prelogin_stream_wrapper;
20
21pub struct MssqlConnection {
22    pub(crate) stream: MssqlStream,
23    pub(crate) cache_statement: StatementCache<Arc<MssqlStatementMetadata>>,
24    log_settings: LogSettings,
25}
26
27impl Debug for MssqlConnection {
28    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
29        f.debug_struct("MssqlConnection").finish()
30    }
31}
32
33impl Connection for MssqlConnection {
34    type Database = Mssql;
35
36    type Options = MssqlConnectOptions;
37
38    #[allow(unused_mut)]
39    fn close(mut self) -> BoxFuture<'static, Result<(), Error>> {
40        // NOTE: there does not seem to be a clean shutdown packet to send to MSSQL
41
42        #[cfg(feature = "_rt-async-std")]
43        {
44            use std::future::ready;
45            use std::net::Shutdown;
46
47            ready(self.stream.shutdown(Shutdown::Both).map_err(Into::into)).boxed()
48        }
49
50        #[cfg(feature = "_rt-tokio")]
51        {
52            use sqlx_rt::AsyncWriteExt;
53
54            // FIXME: This is equivalent to Shutdown::Write, not Shutdown::Both like above
55            // https://docs.rs/tokio/1.0.1/tokio/io/trait.AsyncWriteExt.html#method.shutdown
56            async move { self.stream.shutdown().await.map_err(Into::into) }.boxed()
57        }
58    }
59
60    fn close_hard(self) -> BoxFuture<'static, Result<(), Error>> {
61        self.close()
62    }
63
64    fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>> {
65        // NOTE: we do not use `SELECT 1` as that *could* interact with any ongoing transactions
66        self.execute("/* SQLx ping */").map_ok(|_| ()).boxed()
67    }
68
69    fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
70    where
71        Self: Sized,
72    {
73        Transaction::begin(self)
74    }
75
76    #[doc(hidden)]
77    fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>> {
78        self.stream.wait_until_ready().boxed()
79    }
80
81    #[doc(hidden)]
82    fn should_flush(&self) -> bool {
83        !self.stream.wbuf.is_empty()
84    }
85
86    fn dbms_name(&mut self) -> BoxFuture<'_, Result<String, Error>> {
87        futures_util::future::ready(Ok("Microsoft SQL Server".to_string())).boxed()
88    }
89}