cdbc_mssql/connection/
mod.rs

1use cdbc::utils::statement_cache::StatementCache;
2use cdbc::connection::{Connection};
3use cdbc::executor::Executor;
4use crate::connection::stream::MssqlStream;
5use crate::statement::MssqlStatementMetadata;
6use crate::{Mssql, MssqlConnectOptions};
7use cdbc::transaction::Transaction;
8use std::fmt::{self, Debug, Formatter};
9use std::net::Shutdown;
10use std::sync::Arc;
11use either::Either;
12use cdbc::database::{Database, HasStatement};
13use cdbc::describe::Describe;
14use cdbc::Execute;
15use cdbc::io::chan_stream::ChanStream;
16
17mod establish;
18mod executor;
19mod prepare;
20mod stream;
21
22pub struct MssqlConnection {
23    pub stream: MssqlStream,
24    pub cache_statement: StatementCache<Arc<MssqlStatementMetadata>>,
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 Executor for &mut MssqlConnection {
34    type Database = Mssql;
35
36    fn fetch_many<'q, E: 'q>(&mut self, query: E) -> ChanStream<Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>> where E: Execute<'q, Self::Database> {
37        MssqlConnection::fetch_many(self, query)
38    }
39
40    fn fetch_optional<'q, E: 'q>(&mut self, query: E) -> Result<Option<<Self::Database as Database>::Row>, cdbc::Error> where E: Execute<'q, Self::Database> {
41        MssqlConnection::fetch_optional(self, query)
42    }
43
44    fn prepare_with<'q>(&mut self, sql: &'q str, parameters: &'q [<Self::Database as Database>::TypeInfo]) -> Result<<Self::Database as HasStatement>::Statement, cdbc::Error> {
45        MssqlConnection::prepare_with(self, sql, parameters)
46    }
47
48    fn describe(&mut self, sql: &str) -> Result<Describe<Self::Database>, cdbc::Error> {
49        MssqlConnection::describe(self, sql)
50    }
51}
52
53impl Connection for MssqlConnection {
54    type Options = MssqlConnectOptions;
55
56    #[allow(unused_mut)]
57    fn close(mut self) -> Result<(), cdbc::Error> {
58        Ok(self.stream.shutdown(Shutdown::Both)?)
59    }
60
61    fn ping(&mut self) -> Result<(), cdbc::Error> {
62        // NOTE: we do not use `SELECT 1` as that *could* interact with any ongoing transactions
63        self.execute("/* SQLx ping */")?;
64        Ok(())
65    }
66
67    fn begin(&mut self) -> Result<Transaction<'_, Self::Database>, cdbc::Error>
68        where
69            Self: Sized,
70    {
71        Ok(Transaction::begin(self)?)
72    }
73
74    #[doc(hidden)]
75    fn flush(&mut self) -> Result<(), cdbc::Error> {
76        self.stream.wait_until_ready()
77    }
78
79    #[doc(hidden)]
80    fn should_flush(&self) -> bool {
81        !self.stream.wbuf.is_empty()
82    }
83}