mirror of
https://github.com/owntone/owntone-server.git
synced 2025-04-15 00:35:55 -04:00
[db] Add functions to read/store int64 values in admin table
This commit is contained in:
parent
37aaf73f6a
commit
ea0cc64aa3
96
src/db.c
96
src/db.c
@ -3768,13 +3768,28 @@ db_admin_set(const char *key, const char *value)
|
|||||||
#undef Q_TMPL
|
#undef Q_TMPL
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
int
|
||||||
db_admin_get(const char *key)
|
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';"
|
#define Q_TMPL "SELECT value FROM admin a WHERE a.key = '%q';"
|
||||||
char *query;
|
char *query;
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
char *res;
|
char *cval;
|
||||||
|
int32_t *ival;
|
||||||
|
int64_t *i64val;
|
||||||
|
char **strval;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
query = sqlite3_mprintf(Q_TMPL, key);
|
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");
|
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);
|
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));
|
DPRINTF(E_WARN, L_DB, "Could not prepare statement: %s\n", sqlite3_errmsg(hdl));
|
||||||
|
|
||||||
sqlite3_free(query);
|
sqlite3_free(query);
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = db_blocking_step(stmt);
|
ret = db_blocking_step(stmt);
|
||||||
@ -3806,12 +3821,41 @@ db_admin_get(const char *key)
|
|||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_free(query);
|
sqlite3_free(query);
|
||||||
return NULL;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = (char *)sqlite3_column_text(stmt, 0);
|
switch (type)
|
||||||
if (res)
|
{
|
||||||
res = strdup(res);
|
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
|
#ifdef DB_PROFILE
|
||||||
while (db_blocking_step(stmt) == SQLITE_ROW)
|
while (db_blocking_step(stmt) == SQLITE_ROW)
|
||||||
@ -3821,11 +3865,43 @@ db_admin_get(const char *key)
|
|||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
sqlite3_free(query);
|
sqlite3_free(query);
|
||||||
|
|
||||||
return res;
|
return ret;
|
||||||
|
|
||||||
#undef Q_TMPL
|
#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
|
int
|
||||||
db_admin_delete(const char *key)
|
db_admin_delete(const char *key)
|
||||||
{
|
{
|
||||||
|
6
src/db.h
6
src/db.h
@ -680,9 +680,15 @@ db_spotify_files_delete(void);
|
|||||||
int
|
int
|
||||||
db_admin_set(const char *key, const char *value);
|
db_admin_set(const char *key, const char *value);
|
||||||
|
|
||||||
|
int
|
||||||
|
db_admin_setint64(const char *key, int64_t value);
|
||||||
|
|
||||||
char *
|
char *
|
||||||
db_admin_get(const char *key);
|
db_admin_get(const char *key);
|
||||||
|
|
||||||
|
int64_t
|
||||||
|
db_admin_getint64(const char *key);
|
||||||
|
|
||||||
int
|
int
|
||||||
db_admin_delete(const char *key);
|
db_admin_delete(const char *key);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user