diff --git a/src/db.c b/src/db.c index c1bcded9..5d584ae4 100644 --- a/src/db.c +++ b/src/db.c @@ -3768,13 +3768,28 @@ db_admin_set(const char *key, const char *value) #undef Q_TMPL } -char * -db_admin_get(const char *key) +int +db_admin_setint64(const char *key, int64_t value) +{ +#define Q_TMPL "INSERT OR REPLACE INTO admin (key, value) VALUES ('%q', '%" PRIi64 "');" + char *query; + + query = sqlite3_mprintf(Q_TMPL, key, value); + + return db_query_run(query, 1, 0); +#undef Q_TMPL +} + +static int +admin_get(const char *key, short type, void *value) { #define Q_TMPL "SELECT value FROM admin a WHERE a.key = '%q';" char *query; sqlite3_stmt *stmt; - char *res; + char *cval; + int32_t *ival; + int64_t *i64val; + char **strval; int ret; query = sqlite3_mprintf(Q_TMPL, key); @@ -3782,7 +3797,7 @@ db_admin_get(const char *key) { DPRINTF(E_LOG, L_DB, "Out of memory for query string\n"); - return NULL; + return -1; } DPRINTF(E_DBG, L_DB, "Running query '%s'\n", query); @@ -3793,7 +3808,7 @@ db_admin_get(const char *key) DPRINTF(E_WARN, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl)); sqlite3_free(query); - return NULL; + return -1; } ret = db_blocking_step(stmt); @@ -3806,12 +3821,41 @@ db_admin_get(const char *key) sqlite3_finalize(stmt); sqlite3_free(query); - return NULL; + return -1; } - res = (char *)sqlite3_column_text(stmt, 0); - if (res) - res = strdup(res); + switch (type) + { + case DB_TYPE_CHAR: + cval = (char *) value; + *cval = sqlite3_column_int(stmt, 0); + break; + + case DB_TYPE_INT: + ival = (int32_t *) value; + + *ival = sqlite3_column_int(stmt, 0); + break; + + case DB_TYPE_INT64: + i64val = (int64_t *) value; + + *i64val = sqlite3_column_int64(stmt, 0); + break; + + case DB_TYPE_STRING: + strval = (char **) value; + + cval = (char *)sqlite3_column_text(stmt, 0); + if (cval) + *strval = strdup(cval); + break; + + default: + DPRINTF(E_LOG, L_DB, "BUG: Unknown type %d in admin_set\n", type); + + ret = -2; + } #ifdef DB_PROFILE while (db_blocking_step(stmt) == SQLITE_ROW) @@ -3821,11 +3865,43 @@ db_admin_get(const char *key) sqlite3_finalize(stmt); sqlite3_free(query); - return res; + return ret; #undef Q_TMPL } +char * +db_admin_get(const char *key) +{ + char *value = NULL; + int ret; + + ret = admin_get(key, DB_TYPE_STRING, &value); + if (ret < 0) + { + DPRINTF(E_LOG, L_DB, "Error reading admin value: %s\n", key); + return NULL; + } + + return value; +} + +int64_t +db_admin_getint64(const char *key) +{ + int64_t value = 0; + int ret; + + ret = admin_get(key, DB_TYPE_INT64, &value); + if (ret < 0) + { + DPRINTF(E_LOG, L_DB, "Error reading admin value: %s\n", key); + return 0; + } + + return value; +} + int db_admin_delete(const char *key) { diff --git a/src/db.h b/src/db.h index 5be667fb..1cfafc0b 100644 --- a/src/db.h +++ b/src/db.h @@ -680,9 +680,15 @@ db_spotify_files_delete(void); int db_admin_set(const char *key, const char *value); +int +db_admin_setint64(const char *key, int64_t value); + char * db_admin_get(const char *key); +int64_t +db_admin_getint64(const char *key); + int db_admin_delete(const char *key);