sqlx_core_oldapi/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() || !self.functions.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                for function in &self.functions {
31                    function.create(&mut locked.guard.handle)?;
32                }
33            }
34
35            Ok(conn)
36        })
37    }
38
39    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
40        self.log_settings.log_statements(level);
41        self
42    }
43
44    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
45        self.log_settings.log_slow_statements(level, duration);
46        self
47    }
48}
49
50impl SqliteConnectOptions {
51    /// Collect all `PRAMGA` commands into a single string
52    pub(crate) fn pragma_string(&self) -> String {
53        let mut string = String::new();
54
55        for (key, opt_value) in &self.pragmas {
56            if let Some(value) = opt_value {
57                write!(string, "PRAGMA {} = {}; ", key, value).ok();
58            }
59        }
60
61        string
62    }
63}