sqlx_core/sqlite/options/
connect.rs

1use crate::connection::ConnectOptions;
2use crate::error::Error;
3use crate::executor::Executor;
4use crate::sqlite::{SqliteConnectOptions, SqliteConnection};
5use futures_core::future::BoxFuture;
6use log::LevelFilter;
7use std::fmt::Write;
8use std::time::Duration;
9
10impl ConnectOptions for SqliteConnectOptions {
11    type Connection = SqliteConnection;
12
13    fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
14    where
15        Self::Connection: Sized,
16    {
17        Box::pin(async move {
18            let mut conn = SqliteConnection::establish(self).await?;
19
20            // Execute PRAGMAs
21            conn.execute(&*self.pragma_string()).await?;
22
23            if !self.collations.is_empty() {
24                let mut locked = conn.lock_handle().await?;
25
26                for collation in &self.collations {
27                    collation.create(&mut locked.guard.handle)?;
28                }
29            }
30
31            Ok(conn)
32        })
33    }
34
35    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
36        self.log_settings.log_statements(level);
37        self
38    }
39
40    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
41        self.log_settings.log_slow_statements(level, duration);
42        self
43    }
44}
45
46impl SqliteConnectOptions {
47    /// Collect all `PRAMGA` commands into a single string
48    pub(crate) fn pragma_string(&self) -> String {
49        let mut string = String::new();
50
51        for (key, opt_value) in &self.pragmas {
52            if let Some(value) = opt_value {
53                write!(string, "PRAGMA {} = {}; ", key, value).ok();
54            }
55        }
56
57        string
58    }
59}