sqlx_core_oldapi/
connection.rs1use crate::database::{Database, HasStatementCache};
2use crate::error::Error;
3use crate::transaction::Transaction;
4use futures_core::future::BoxFuture;
5use log::LevelFilter;
6use std::fmt::Debug;
7use std::str::FromStr;
8use std::time::Duration;
9
10pub trait Connection: Send {
12 type Database: Database;
13
14 type Options: ConnectOptions<Connection = Self>;
15
16 fn close(self) -> BoxFuture<'static, Result<(), Error>>;
22
23 #[doc(hidden)]
27 fn close_hard(self) -> BoxFuture<'static, Result<(), Error>>;
28
29 fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
31
32 fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
36 where
37 Self: Sized;
38
39 fn transaction<'a, F, R, E>(&'a mut self, callback: F) -> BoxFuture<'a, Result<R, E>>
60 where
61 for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>>
62 + 'a
63 + Send
64 + Sync,
65 Self: Sized,
66 R: Send,
67 E: From<Error> + Send,
68 {
69 Box::pin(async move {
70 let mut transaction = self.begin().await?;
71 let ret = callback(&mut transaction).await;
72
73 match ret {
74 Ok(ret) => {
75 transaction.commit().await?;
76
77 Ok(ret)
78 }
79 Err(err) => {
80 transaction.rollback().await?;
81
82 Err(err)
83 }
84 }
85 })
86 }
87
88 fn cached_statements_size(&self) -> usize
90 where
91 Self::Database: HasStatementCache,
92 {
93 0
94 }
95
96 fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
99 where
100 Self::Database: HasStatementCache,
101 {
102 Box::pin(async move { Ok(()) })
103 }
104
105 #[doc(hidden)]
106 fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
107
108 #[doc(hidden)]
109 fn should_flush(&self) -> bool;
110
111 #[inline]
116 fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
117 where
118 Self: Sized,
119 {
120 let options = url.parse();
121
122 Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
123 }
124
125 fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
127 where
128 Self: Sized,
129 {
130 options.connect()
131 }
132}
133
134#[derive(Clone, Debug)]
135pub(crate) struct LogSettings {
136 pub(crate) statements_level: LevelFilter,
137 pub(crate) slow_statements_level: LevelFilter,
138 pub(crate) slow_statements_duration: Duration,
139}
140
141impl Default for LogSettings {
142 fn default() -> Self {
143 LogSettings {
144 statements_level: LevelFilter::Info,
145 slow_statements_level: LevelFilter::Warn,
146 slow_statements_duration: Duration::from_secs(1),
147 }
148 }
149}
150
151impl LogSettings {
152 pub(crate) fn log_statements(&mut self, level: LevelFilter) {
153 self.statements_level = level;
154 }
155 pub(crate) fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
156 self.slow_statements_level = level;
157 self.slow_statements_duration = duration;
158 }
159}
160
161pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug + Clone {
162 type Connection: Connection + ?Sized;
163
164 fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
166 where
167 Self::Connection: Sized;
168
169 fn log_statements(&mut self, level: LevelFilter) -> &mut Self;
171
172 fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self;
175
176 fn disable_statement_logging(&mut self) -> &mut Self {
178 self.log_statements(LevelFilter::Off)
179 .log_slow_statements(LevelFilter::Off, Duration::default())
180 }
181}