welds_sqlx_mssql/connection/
mod.rs

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