From dbaa1c02c93bbc8546c66e4478802528a6bb1256 Mon Sep 17 00:00:00 2001 From: Hanh Date: Tue, 14 Mar 2023 09:58:16 +1000 Subject: [PATCH] Get/Set Properties --- binding.h | 6 +++++- cbindgen.toml | 2 +- src/api/dart_ffi.rs | 28 ++++++++++++++++++++++++++++ src/db/read.rs | 14 +++++++++++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/binding.h b/binding.h index 1e65acf..42542eb 100644 --- a/binding.h +++ b/binding.h @@ -1,4 +1,4 @@ -#if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE) +#if !defined(__APPLE__) || !defined(TARGET_OS_IPHONE) typedef char int8_t; typedef unsigned char uint8_t; typedef short int uint16_t; @@ -401,6 +401,10 @@ struct CResult_bool decrypt_db(char *db_path, char *passwd); struct CResult_u8 clone_db_with_passwd(uint8_t coin, char *temp_path, char *passwd); +struct CResult_____c_char get_property(uint8_t coin, char *name); + +struct CResult_u8 set_property(uint8_t coin, char *name, char *value); + bool has_cuda(void); bool has_metal(void); diff --git a/cbindgen.toml b/cbindgen.toml index cc82a57..7eb611f 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -1,7 +1,7 @@ language = "C" no_includes = true after_includes = """ -#if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE) +#if !defined(__APPLE__) || !defined(TARGET_OS_IPHONE) typedef char int8_t; typedef unsigned char uint8_t; typedef short int uint16_t; diff --git a/src/api/dart_ffi.rs b/src/api/dart_ffi.rs index 17000ed..db23f85 100644 --- a/src/api/dart_ffi.rs +++ b/src/api/dart_ffi.rs @@ -1141,6 +1141,34 @@ pub unsafe extern "C" fn clone_db_with_passwd( to_cresult(with_coin(coin, res)) } +#[no_mangle] +pub unsafe extern "C" fn get_property( + coin: u8, + name: *mut c_char, +) -> CResult<*mut c_char> { + from_c_str!(name); + let res = |connection: &Connection| { + let value = crate::db::read::get_property(connection, &name)?; + Ok(value) + }; + to_cresult_str(with_coin(coin, res)) +} + +#[no_mangle] +pub unsafe extern "C" fn set_property( + coin: u8, + name: *mut c_char, + value: *mut c_char, +) -> CResult { + from_c_str!(name); + from_c_str!(value); + let res = |connection: &Connection| { + crate::db::read::set_property(connection, &name, &value)?; + Ok(0) + }; + to_cresult(with_coin(coin, res)) +} + #[no_mangle] pub unsafe extern "C" fn has_cuda() -> bool { crate::gpu::has_cuda() diff --git a/src/db/read.rs b/src/db/read.rs index fd8c043..5ba3070 100644 --- a/src/db/read.rs +++ b/src/db/read.rs @@ -659,4 +659,16 @@ pub fn resolve_addresses( Ok(addresses) } -// TODO: In get_tx, resolve addresses to contact +pub fn get_property(connection: &Connection, name: &str) -> anyhow::Result { + let url = connection.query_row("SELECT value FROM properties WHERE name = ?1", [name], |row| { + let url: String = row.get(0)?; + Ok(url) + }).optional()?; + Ok(url.unwrap_or(String::new())) +} + +pub fn set_property(connection: &Connection, name: &str, value: &str) -> anyhow::Result<()> { + connection.execute("INSERT INTO properties(name, value) VALUES (?1, ?2) ON CONFLICT (name) \ + DO UPDATE SET value = excluded.value", params![name, value])?; + Ok(()) +}