From 058e2e9b2b8f38531e85b5b4e1539d808c0206ac Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Wed, 4 Aug 2010 19:01:53 +0200 Subject: [PATCH] Add config variables save/test/clear for 64bit integer as hex string --- src/db.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/db.h | 9 +++++ 2 files changed, 128 insertions(+) diff --git a/src/db.c b/src/db.c index 1cb710a8..2b302380 100644 --- a/src/db.c +++ b/src/db.c @@ -3010,6 +3010,42 @@ db_config_save_int(const char *key, int val) #undef Q_TMPL } +int +db_config_save_hex64(const char *key, uint64_t val) +{ +#define Q_TMPL "INSERT OR REPLACE INTO admin (key, value) VALUES ('%q', '%016" PRIx64 "');" + char *query; + char *errmsg; + int ret; + + query = sqlite3_mprintf(Q_TMPL, key, val); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); + + errmsg = NULL; + ret = db_exec(query, &errmsg); + if (ret != SQLITE_OK) + { + DPRINTF(E_LOG, L_DB, "Error saving hex64 value: %s\n", errmsg); + + sqlite3_free(errmsg); + sqlite3_free(query); + return -1; + } + + sqlite3_free(query); + + return 0; + +#undef Q_TMPL +} + int db_config_fetch_int(const char *key, int *val) { @@ -3061,6 +3097,89 @@ db_config_fetch_int(const char *key, int *val) #undef Q_TMPL } +int +db_config_has_tuple_hex64(const char *key, uint64_t val) +{ +#define Q_TMPL "SELECT 1 FROM admin WHERE key = '%q' AND value = '%016" PRIx64 "';" + sqlite3_stmt *stmt; + char *query; + int ret; + + query = sqlite3_mprintf(Q_TMPL, key, val); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return -1; + } + + DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); + + ret = db_blocking_prepare_v2(query, -1, &stmt, NULL); + if (ret != SQLITE_OK) + { + DPRINTF(E_LOG, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl)); + + ret = -1; + goto out; + } + + ret = db_blocking_step(stmt); + + switch (ret) + { + case SQLITE_ROW: + ret = 1; + break; + + case SQLITE_DONE: + ret = 0; + break; + + default: + DPRINTF(E_LOG, L_DB, "Could not step: %s\n", sqlite3_errmsg(hdl)); + ret = -1; + break; + } + + sqlite3_finalize(stmt); + + out: + sqlite3_free(query); + return ret; + +#undef Q_TMPL +} + +void +db_config_clear_key(const char *key) +{ +#define Q_TMPL "DELETE FROM admin WHERE key = '%q';" + + char *query; + char *errmsg; + int ret; + + query = sqlite3_mprintf(Q_TMPL, key); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); + + return; + } + + DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); + + ret = db_exec(query, &errmsg); + if (ret != SQLITE_OK) + DPRINTF(E_LOG, L_DB, "Error clearing config key %s: %s\n", key, errmsg); + + sqlite3_free(errmsg); + sqlite3_free(query); + +#undef Q_TMPL +} + /* Inotify */ int diff --git a/src/db.h b/src/db.h index 5ab9fee5..756120f9 100644 --- a/src/db.h +++ b/src/db.h @@ -406,9 +406,18 @@ db_pairing_fetch_byguid(struct pairing_info *pi); int db_config_save_int(const char *key, int val); +int +db_config_save_hex64(const char *key, uint64_t val); + int db_config_fetch_int(const char *key, int *val); +int +db_config_has_tuple_hex64(const char *key, uint64_t val); + +void +db_config_clear_key(const char *key); + /* Inotify */ int db_watch_clear(void);