sqlx_core_guts/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            // send an initial sql statement comprised of options
21            let mut init = String::new();
22
23            // This is a special case for sqlcipher. When the `key` pragma
24            // is set, we have to make sure it's executed first in order.
25            if let Some(pragma_key_password) = self.pragmas.get("key") {
26                write!(init, "PRAGMA key = {}; ", pragma_key_password).ok();
27            }
28
29            for (key, value) in &self.pragmas {
30                // Since we've already written the possible `key` pragma
31                // above, we shall skip it now.
32                if key == "key" {
33                    continue;
34                }
35                write!(init, "PRAGMA {} = {}; ", key, value).ok();
36            }
37
38            conn.execute(&*init).await?;
39
40            if !self.collations.is_empty() {
41                let mut locked = conn.lock_handle().await?;
42
43                for collation in &self.collations {
44                    collation.create(&mut locked.guard.handle)?;
45                }
46            }
47
48            Ok(conn)
49        })
50    }
51
52    fn log_statements(&mut self, level: LevelFilter) -> &mut Self {
53        self.log_settings.log_statements(level);
54        self
55    }
56
57    fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self {
58        self.log_settings.log_slow_statements(level, duration);
59        self
60    }
61}