From e32c0e2a708795663bd9dbdcce3eb80b6737ee7b Mon Sep 17 00:00:00 2001 From: EliasSchriefer Date: Mon, 12 Jul 2021 10:53:55 +0200 Subject: [PATCH] Move sqlite functions into their own module --- src/main.rs | 36 ++++-------------------------------- src/sqlite/mod.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 src/sqlite/mod.rs diff --git a/src/main.rs b/src/main.rs index 0dc1e9e..e3c1252 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,8 @@ use structopt::StructOpt; use std::{ - path::PathBuf, boxed::Box, error::Error, }; -use sqlx::{ - prelude::*, - sqlite::{ - SqliteConnection, - SqliteConnectOptions, - SqliteQueryResult, - }, -}; use tokio::{ sync::RwLock, fs::File, @@ -24,6 +15,8 @@ use crate::{ mod cli; mod config; +#[cfg(feature = "sqlite")] +mod sqlite; const DEFAULT_DATABASE_PATH: &'static str = "/var/lib/noise-server/noise-server.sqlite"; @@ -47,35 +40,14 @@ async fn main() -> Result<(), Box> { println!("{:?}", config); #[cfg(feature = "sqlite")] - let mut sqlite_connection = RwLock::new(connect_sqlite(&config.database_path).await?); + let mut sqlite_connection = RwLock::new(sqlite::connect(&config.database_path).await?); #[cfg(feature = "sqlite")] - load_sqlite_template(sqlite_connection.get_mut()).await?; + sqlite::load_template(sqlite_connection.get_mut()).await?; Ok(()) } -/// Prepare sqlite database connection -#[cfg(feature = "sqlite")] -async fn connect_sqlite(path: &PathBuf) -> sqlx::Result { - let connect_options = SqliteConnectOptions::new() - .filename(&path) - .create_if_missing(true); - SqliteConnection::connect_with(&connect_options).await -} - -#[cfg(feature = "sqlite")] -async fn load_sqlite_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Result { - sqlx::query(concat!( - include_str!("sqlite/users.sql"), - include_str!("sqlite/security_preferences.sql"), - include_str!("sqlite/privacy_preferences.sql"), - include_str!("sqlite/notification_preferences.sql"), - include_str!("sqlite/external_servers_privacy_preferences.sql"), - include_str!("sqlite/external_servers_preferences.sql"), - )).execute(sqlite_handle).await -} - async fn get_config(options: &Options) -> std::io::Result { if options.no_config { Ok(Config::default()) diff --git a/src/sqlite/mod.rs b/src/sqlite/mod.rs new file mode 100644 index 0000000..06e2738 --- /dev/null +++ b/src/sqlite/mod.rs @@ -0,0 +1,29 @@ +use std::path::PathBuf; +use sqlx::{ + prelude::*, + sqlite::{ + SqliteConnection, + SqliteConnectOptions, + SqliteQueryResult, + }, +}; + +/// Prepare sqlite database connection +pub async fn connect(path: &PathBuf) -> sqlx::Result { + let connect_options = SqliteConnectOptions::new() + .filename(&path) + .create_if_missing(true); + SqliteConnection::connect_with(&connect_options).await +} + +/// Create tables from template +pub async fn load_template(sqlite_handle: &mut SqliteConnection) -> sqlx::Result { + sqlx::query(concat!( + include_str!("users.sql"), + include_str!("security_preferences.sql"), + include_str!("privacy_preferences.sql"), + include_str!("notification_preferences.sql"), + include_str!("external_servers_privacy_preferences.sql"), + include_str!("external_servers_preferences.sql"), + )).execute(sqlite_handle).await +} \ No newline at end of file