Get/Set Properties

This commit is contained in:
Hanh 2023-03-14 09:58:16 +10:00
parent a2f6917ce8
commit dbaa1c02c9
4 changed files with 47 additions and 3 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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<u8> {
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()

View File

@ -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<String> {
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(())
}