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