Skip to main content

sqlitegraph/config/
sqlite.rs

1//! SQLite backend configuration.
2
3use std::collections::HashMap;
4
5/// Configuration for SQLite backend operations.
6#[derive(Clone, Debug, Default)]
7pub struct SqliteConfig {
8    /// Skip schema initialization during opening
9    pub without_migrations: bool,
10    /// Optional cache size for prepared statements
11    pub cache_size: Option<usize>,
12    /// Optional pool size for connection pooling (default: 5)
13    pub pool_size: Option<usize>,
14    /// Additional SQLite PRAGMA settings
15    pub pragma_settings: HashMap<String, String>,
16}
17
18impl SqliteConfig {
19    /// Create a new SQLite config with default settings
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Set whether to skip schema initialization (builder pattern)
25    pub fn with_migrations_disabled(mut self, without_migrations: bool) -> Self {
26        self.without_migrations = without_migrations;
27        self
28    }
29
30    /// Set the prepared statement cache size (builder pattern)
31    pub fn with_cache_size(mut self, cache_size: usize) -> Self {
32        self.cache_size = Some(cache_size);
33        self
34    }
35
36    /// Add a PRAGMA setting (builder pattern)
37    pub fn with_pragma(mut self, key: &str, value: &str) -> Self {
38        self.pragma_settings
39            .insert(key.to_string(), value.to_string());
40        self
41    }
42
43    /// Configure for WAL mode (builder pattern convenience method)
44    pub fn with_wal_mode(mut self) -> Self {
45        self.pragma_settings
46            .insert("journal_mode".to_string(), "WAL".to_string());
47        self
48    }
49
50    /// Configure for better performance with some safety trade-offs
51    pub fn with_performance_mode(mut self) -> Self {
52        self.pragma_settings
53            .insert("journal_mode".to_string(), "WAL".to_string());
54        self.pragma_settings
55            .insert("synchronous".to_string(), "NORMAL".to_string());
56        self
57    }
58
59    /// Set the connection pool size (builder pattern)
60    ///
61    /// # Arguments
62    ///
63    /// * `size` - Maximum number of connections in the pool
64    ///
65    /// # Example
66    ///
67    /// ```rust,ignore
68    /// let cfg = SqliteConfig::new().with_pool_size(10);
69    /// ```
70    pub fn with_pool_size(mut self, size: usize) -> Self {
71        self.pool_size = Some(size);
72        self
73    }
74
75    /// Set the maximum number of connections (alias for with_pool_size)
76    ///
77    /// # Arguments
78    ///
79    /// * `max` - Maximum number of connections in the pool
80    ///
81    /// # Example
82    ///
83    /// ```rust,ignore
84    /// let cfg = SqliteConfig::new().with_max_connections(10);
85    /// ```
86    pub fn with_max_connections(mut self, max: usize) -> Self {
87        self.pool_size = Some(max);
88        self
89    }
90}