[db] Functions to read/store int in the admin table and introduce

constants for admin table keys
This commit is contained in:
chme 2017-11-19 07:46:28 +01:00 committed by ejurgensen
parent 5315bdf83e
commit 38ab4b9676
6 changed files with 57 additions and 68 deletions

View File

@ -3771,6 +3771,18 @@ db_admin_set(const char *key, const char *value)
#undef Q_TMPL
}
int
db_admin_setint(const char *key, int value)
{
#define Q_TMPL "INSERT OR REPLACE INTO admin (key, value) VALUES ('%q', '%d');"
char *query;
query = sqlite3_mprintf(Q_TMPL, key, value);
return db_query_run(query, 1, 0);
#undef Q_TMPL
}
int
db_admin_setint64(const char *key, int64_t value)
{
@ -3889,6 +3901,22 @@ db_admin_get(const char *key)
return value;
}
int
db_admin_getint(const char *key)
{
int value = 0;
int ret;
ret = admin_get(key, DB_TYPE_INT, &value);
if (ret < 0)
{
DPRINTF(E_LOG, L_DB, "Error reading admin value: %s\n", key);
return 0;
}
return value;
}
int64_t
db_admin_getint64(const char *key)
{
@ -3999,34 +4027,6 @@ db_speaker_clear_all(void)
/* Queue */
/*
* Returns the queue version from the admin table
*
* @return queue version
*/
int
db_queue_get_version()
{
char *version;
int32_t queue_version;
int ret;
queue_version = 0;
version = db_admin_get("queue_version");
if (version)
{
ret = safe_atoi32(version, &queue_version);
free(version);
if (ret < 0)
{
DPRINTF(E_LOG, L_DB, "Could not get playlist version\n");
return -1;
}
}
return queue_version;
}
/*
* Increments the version of the queue in the admin table and notifies listener of LISTENER_QUEUE
* about the change.
@ -4038,25 +4038,14 @@ static void
queue_inc_version_and_notify()
{
int queue_version;
char version[10];
int ret;
db_transaction_begin();
queue_version = db_queue_get_version();
if (queue_version < 0)
queue_version = 0;
queue_version = db_admin_getint(ADMIN_QUEUE_VERSION);
queue_version++;
ret = snprintf(version, sizeof(version), "%d", queue_version);
if (ret >= sizeof(version))
{
DPRINTF(E_LOG, L_DB, "Error incrementing queue version. Could not convert version to string: %d\n", queue_version);
db_transaction_rollback();
return;
}
ret = db_admin_set("queue_version", version);
ret = db_admin_setint(ADMIN_QUEUE_VERSION, queue_version);
if (ret < 0)
{
DPRINTF(E_LOG, L_DB, "Error incrementing queue version. Could not update version in admin table: %d\n", queue_version);
@ -6192,7 +6181,6 @@ static int
db_check_version(void)
{
#define Q_VACUUM "VACUUM;"
char *buf;
char *errmsg;
int db_ver_major;
int db_ver_minor;
@ -6202,24 +6190,14 @@ db_check_version(void)
vacuum = cfg_getbool(cfg_getsec(cfg, "sqlite"), "vacuum");
buf = db_admin_get("schema_version_major");
if (!buf)
buf = db_admin_get("schema_version"); // Pre schema v15.1
db_ver_major = db_admin_getint(ADMIN_SCHEMA_VERSION_MAJOR);
if (!db_ver_major)
db_ver_major = db_admin_getint(ADMIN_SCHEMA_VERSION); // Pre schema v15.1
if (!buf)
if (!db_ver_major)
return 1; // Will create new database
safe_atoi32(buf, &db_ver_major);
free(buf);
buf = db_admin_get("schema_version_minor");
if (buf)
{
safe_atoi32(buf, &db_ver_minor);
free(buf);
}
else
db_ver_minor = 0;
db_ver_minor = db_admin_getint(ADMIN_SCHEMA_VERSION_MINOR);
db_ver = db_ver_major * 100 + db_ver_minor;

View File

@ -63,6 +63,14 @@ enum query_type {
#define ARTWORK_SPOTIFY 6
#define ARTWORK_HTTP 7
#define ADMIN_SCHEMA_VERSION_MAJOR "schema_version_major"
#define ADMIN_SCHEMA_VERSION_MINOR "schema_version_minor"
#define ADMIN_SCHEMA_VERSION "schema_version"
#define ADMIN_QUEUE_VERSION "queue_version"
#define ADMIN_DB_UPDATE "db_update"
#define ADMIN_LASTFM_SESSION_KEY "lastfm_sk"
#define ADMIN_SPOTIFY_REFRESH_TOKEN "spotify_refresh_token"
struct query_params {
/* Query parameters, filled in by caller */
enum query_type type;
@ -680,12 +688,18 @@ db_spotify_files_delete(void);
int
db_admin_set(const char *key, const char *value);
int
db_admin_setint(const char *key, int value);
int
db_admin_setint64(const char *key, int64_t value);
char *
db_admin_get(const char *key);
int
db_admin_getint(const char *key);
int64_t
db_admin_getint64(const char *key);
@ -703,9 +717,6 @@ void
db_speaker_clear_all(void);
/* Queue */
int
db_queue_get_version();
int
db_queue_update_item(struct db_queue_item *queue_item);

View File

@ -184,7 +184,7 @@ response_process(struct http_client_ctx *ctx, char **errmsg)
if (sk)
{
DPRINTF(E_LOG, L_LASTFM, "Got session key from LastFM: %s\n", sk);
db_admin_set("lastfm_sk", sk);
db_admin_set(ADMIN_LASTFM_SESSION_KEY, sk);
if (lastfm_session_key)
free(lastfm_session_key);
@ -341,7 +341,7 @@ stop_scrobbling()
// Disable LastFM, will be enabled after successful login request
lastfm_disabled = true;
db_admin_delete("lastfm_sk");
db_admin_delete(ADMIN_LASTFM_SESSION_KEY);
}
/* Thread: filescanner, httpd */
@ -422,7 +422,7 @@ lastfm_is_enabled(void)
int
lastfm_init(void)
{
lastfm_session_key = db_admin_get("lastfm_sk");
lastfm_session_key = db_admin_get(ADMIN_LASTFM_SESSION_KEY);
if (!lastfm_session_key)
{
DPRINTF(E_DBG, L_LASTFM, "No valid LastFM session key\n");

View File

@ -104,7 +104,7 @@ handle_deferred_update_notifications(void)
DPRINTF(E_DBG, L_LIB, "Database changed (%d changes)\n", deferred_update_notifications);
deferred_update_notifications = 0;
db_admin_setint64("db_update", (int64_t) time(NULL));
db_admin_setint64(ADMIN_DB_UPDATE, (int64_t) time(NULL));
}
return ret;

View File

@ -741,7 +741,7 @@ mpd_command_status(struct evbuffer *evbuf, int argc, char **argv, char **errmsg,
break;
}
queue_version = db_queue_get_version();
queue_version = db_admin_getint(ADMIN_QUEUE_VERSION);
queue_length = db_queue_get_count();
evbuffer_add_printf(evbuf,
@ -842,7 +842,7 @@ mpd_command_stats(struct evbuffer *evbuf, int argc, char **argv, char **errmsg,
fci.count,
4,
(fci.length / 1000),
(time_t) db_admin_getint64("db_update"),
(time_t) db_admin_getint64(ADMIN_DB_UPDATE),
7);
return 0;

View File

@ -642,7 +642,7 @@ tokens_get(struct keyval *kv, const char **err)
token_requested = time(NULL);
if (spotify_refresh_token)
db_admin_set("spotify_refresh_token", spotify_refresh_token);
db_admin_set(ADMIN_SPOTIFY_REFRESH_TOKEN, spotify_refresh_token);
request_user_info();
@ -701,7 +701,7 @@ spotifywebapi_token_refresh(char **user)
return 0;
}
refresh_token = db_admin_get("spotify_refresh_token");
refresh_token = db_admin_get(ADMIN_SPOTIFY_REFRESH_TOKEN);
if (!refresh_token)
{
DPRINTF(E_LOG, L_SPOTIFY, "No spotify refresh token found\n");