common.rs 1008 B

1234567891011121314151617181920212223242526272829303132333435
  1. use std::str::FromStr;
  2. use std::time::Duration;
  3. use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
  4. use sqlx::{Error, Pool, Sqlite};
  5. #[inline(always)]
  6. pub async fn create_sqlite_pool(
  7. path: &str,
  8. #[cfg(feature = "sqlcipher")] password: String,
  9. ) -> Result<Pool<Sqlite>, Error> {
  10. let db_options = SqliteConnectOptions::from_str(path)?
  11. .busy_timeout(Duration::from_secs(10))
  12. .read_only(false)
  13. .pragma("busy_timeout", "5000")
  14. .pragma("journal_mode", "wal")
  15. .pragma("synchronous", "normal")
  16. .pragma("temp_store", "memory")
  17. .pragma("mmap_size", "30000000000")
  18. .shared_cache(true)
  19. .create_if_missing(true);
  20. #[cfg(feature = "sqlcipher")]
  21. let db_options = db_options.pragma("key", password);
  22. let pool = SqlitePoolOptions::new()
  23. .min_connections(1)
  24. .max_connections(1)
  25. .idle_timeout(None)
  26. .max_lifetime(None)
  27. .connect_with(db_options)
  28. .await?;
  29. Ok(pool)
  30. }