breez_sdk_core/persist/
settings.rs1use serde::Serialize;
2
3use super::{db::SqliteStorage, error::PersistResult};
4
5#[allow(dead_code)]
6#[derive(Serialize)]
7pub struct SettingItem {
8 key: String,
9 value: String,
10}
11
12impl SqliteStorage {
13 pub fn update_setting(&self, key: String, value: String) -> PersistResult<()> {
14 self.get_connection()?.execute(
15 "INSERT OR REPLACE INTO settings (key, value) VALUES (?1,?2)",
16 (key, value),
17 )?;
18 Ok(())
19 }
20
21 pub fn get_setting(&self, key: String) -> PersistResult<Option<String>> {
22 let res = self.get_connection()?.query_row(
23 "SELECT value FROM settings WHERE key = ?1",
24 [key],
25 |row| row.get(0),
26 );
27 Ok(res.ok())
28 }
29
30 #[allow(dead_code)]
31 pub fn delete_setting(&self, key: String) -> PersistResult<()> {
32 self.get_connection()?
33 .execute("DELETE FROM settings WHERE key = ?1", [key])?;
34 Ok(())
35 }
36
37 #[allow(dead_code)]
38 pub fn list_settings(&self) -> PersistResult<Vec<SettingItem>> {
39 let con = self.get_connection()?;
40 let mut stmt = con.prepare("SELECT * FROM settings ORDER BY key")?;
41 let vec = stmt
42 .query_map([], |row| {
43 Ok(SettingItem {
44 key: row.get(0)?,
45 value: row.get(1)?,
46 })
47 })?
48 .map(|i| i.unwrap())
49 .collect();
50
51 Ok(vec)
52 }
53
54 pub fn set_lsp(&self, lsp_id: String, pubkey: Option<String>) -> PersistResult<()> {
55 if let Some(pubkey) = pubkey {
56 self.update_setting("lsp".to_string(), lsp_id)?;
57 self.update_setting("lsp-pubkey".to_string(), pubkey)?;
58 return Ok(());
59 }
60
61 match self.get_setting("lsp".to_string())? {
62 Some(old_lsp_id) => {
63 if old_lsp_id != lsp_id {
64 self.update_setting("lsp".to_string(), lsp_id)?;
65 self.delete_setting("lsp-pubkey".to_string())?;
66 }
67 }
68 None => {
69 self.update_setting("lsp".to_string(), lsp_id)?;
70 self.delete_setting("lsp-pubkey".to_string())?;
71 }
72 };
73
74 Ok(())
75 }
76
77 pub fn get_lsp_id(&self) -> PersistResult<Option<String>> {
78 self.get_setting("lsp".to_string())
79 }
80
81 pub fn get_lsp_pubkey(&self) -> PersistResult<Option<String>> {
82 self.get_setting("lsp-pubkey".to_string())
83 }
84}
85
86#[test]
87fn test_settings() {
88 use crate::persist::test_utils;
89
90 let storage = SqliteStorage::new(test_utils::create_test_sql_dir());
91 storage.init().unwrap();
92 storage
93 .update_setting("key1".to_string(), "val1".to_string())
94 .unwrap();
95 storage
96 .update_setting("key2".to_string(), "val2".to_string())
97 .unwrap();
98 storage
99 .update_setting("key2".to_string(), "val3".to_string())
100 .unwrap();
101 storage
102 .update_setting("key4".to_string(), "val4".to_string())
103 .unwrap();
104 storage.delete_setting("key4".to_string()).unwrap();
105
106 let setting_item = storage.get_setting("key1".to_string()).unwrap().unwrap();
107 assert_eq!(setting_item, "val1");
108 let settings = storage.list_settings().unwrap();
109 assert_eq!(settings.len(), 2);
110 assert_eq!(settings[0].key, "key1");
111 assert_eq!(settings[0].value, "val1");
112 assert_eq!(settings[1].key, "key2");
113 assert_eq!(settings[1].value, "val3");
114}