cdbc_sqlite/options/
connect.rs1use 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 let mut init = String::new();
19
20 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 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}