cdbc_sqlite/options/
connect.rs

1use cdbc::connection::ConnectOptions;
2use cdbc::error::Error;
3use cdbc::executor::Executor;
4use crate::{SqliteConnectOptions, SqliteConnection};
5use std::time::Duration;
6use std::fmt::Write;
7
8impl ConnectOptions for SqliteConnectOptions {
9    type Connection = SqliteConnection;
10
11    fn connect(&self,d:Duration) -> Result<Self::Connection, Error>
12        where
13            Self::Connection: Sized,
14    {
15            let mut conn = SqliteConnection::establish(self)?;
16
17            // send an initial sql statement comprised of options
18            let mut init = String::new();
19
20            // This is a special case for sqlcipher. When the `key` pragma
21            // is set, we have to make sure it's executed first in order.
22            if let Some(pragma_key_password) = self.pragmas.get("key") {
23                write!(init, "PRAGMA key = {}; ", pragma_key_password).ok();
24            }
25
26            for (key, value) in &self.pragmas {
27                // Since we've already written the possible `key` pragma
28                // above, we shall skip it now.
29                if key == "key" {
30                    continue;
31                }
32                write!(init, "PRAGMA {} = {}; ", key, value).ok();
33            }
34
35            conn.execute(&*init)?;
36
37            if !self.collations.is_empty() {
38                let mut locked = conn.lock_handle()?;
39
40                for collation in &self.collations {
41                    collation.create(&mut locked.guard.handle)?;
42                }
43            }
44
45            Ok(conn)
46    }
47}