[db] Don't zero play_count, skip_count, seek, time_played, time_skipped on update

If a file gets updated/rescanned we generally don't want to reset the above
values. This commit adds DB_FLAG_NO_ZERO, which marks a field so that
db_file_update() will only update it if the new value is non-zero (i.e. the
caller probably has a "better" value).
This commit is contained in:
ejurgensen 2018-12-30 21:55:55 +01:00
parent 734b4e9851
commit 9929832b5f
2 changed files with 54 additions and 11 deletions

View File

@ -208,6 +208,34 @@ sqlext_daap_songalbumid_xfunc(sqlite3_context *pv, int n, sqlite3_value **ppv)
sqlite3_result_int64(pv, result);
}
static void
sqlext_daap_no_zero_xfunc(sqlite3_context *pv, int n, sqlite3_value **ppv)
{
sqlite3_int64 new_value;
sqlite3_int64 old_value;
if (n != 2)
{
sqlite3_result_error(pv, "daap_no_zero() requires 2 parameters, new_value and old_value", -1);
return;
}
if ((sqlite3_value_type(ppv[0]) != SQLITE_INTEGER)
|| (sqlite3_value_type(ppv[1]) != SQLITE_INTEGER))
{
sqlite3_result_error(pv, "daap_no_zero() requires 2 integer parameters", -1);
return;
}
new_value = sqlite3_value_int64(ppv[0]);
old_value = sqlite3_value_int64(ppv[1]);
if (new_value != 0)
sqlite3_result_int64(pv, new_value);
else
sqlite3_result_int64(pv, old_value);
}
static int
sqlext_daap_unicode_xcollation(void *notused, int llen, const void *left, int rlen, const void *right)
{
@ -259,6 +287,15 @@ sqlite3_extension_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines
return -1;
}
ret = sqlite3_create_function(db, "daap_no_zero", 2, SQLITE_UTF8, NULL, sqlext_daap_no_zero_xfunc, NULL, NULL);
if (ret != SQLITE_OK)
{
if (pzErrMsg)
*pzErrMsg = sqlite3_mprintf("Could not create daap_no_zero function: %s\n", sqlite3_errmsg(db));
return -1;
}
ret = sqlite3_create_collation(db, "DAAP", SQLITE_UTF8, NULL, sqlext_daap_unicode_xcollation);
if (ret != SQLITE_OK)
{

View File

@ -63,7 +63,10 @@
#define DB_TYPE_INT64 3
#define DB_TYPE_STRING 4
#define DB_FLAG_AUTO 1 // Flags that column value is set automatically by the db, e.g. by a trigger
// Flags that column value is set automatically by the db, e.g. by a trigger
#define DB_FLAG_AUTO (1 << 0)
// Flags that we will only update column value if we have non-zero value (to avoid zeroing e.g. rating)
#define DB_FLAG_NO_ZERO (1 << 1)
enum group_type {
G_ALBUMS = 1,
@ -173,10 +176,10 @@ static const struct col_type_map mfi_cols_map[] =
{ "bpm", mfi_offsetof(bpm), DB_TYPE_INT },
{ "compilation", mfi_offsetof(compilation), DB_TYPE_CHAR },
{ "artwork", mfi_offsetof(artwork), DB_TYPE_CHAR },
{ "rating", mfi_offsetof(rating), DB_TYPE_INT },
{ "play_count", mfi_offsetof(play_count), DB_TYPE_INT },
{ "skip_count", mfi_offsetof(skip_count), DB_TYPE_INT },
{ "seek", mfi_offsetof(seek), DB_TYPE_INT },
{ "rating", mfi_offsetof(rating), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "play_count", mfi_offsetof(play_count), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "skip_count", mfi_offsetof(skip_count), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "seek", mfi_offsetof(seek), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "data_kind", mfi_offsetof(data_kind), DB_TYPE_INT },
{ "media_kind", mfi_offsetof(media_kind), DB_TYPE_INT, DB_FIXUP_MEDIA_KIND },
{ "item_kind", mfi_offsetof(item_kind), DB_TYPE_INT, DB_FIXUP_ITEM_KIND },
@ -184,8 +187,8 @@ static const struct col_type_map mfi_cols_map[] =
{ "db_timestamp", mfi_offsetof(db_timestamp), DB_TYPE_INT },
{ "time_added", mfi_offsetof(time_added), DB_TYPE_INT, DB_FIXUP_TIME_ADDED },
{ "time_modified", mfi_offsetof(time_modified), DB_TYPE_INT, DB_FIXUP_TIME_MODIFIED },
{ "time_played", mfi_offsetof(time_played), DB_TYPE_INT },
{ "time_skipped", mfi_offsetof(time_skipped), DB_TYPE_INT },
{ "time_played", mfi_offsetof(time_played), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "time_skipped", mfi_offsetof(time_skipped), DB_TYPE_INT, DB_FIXUP_STANDARD, DB_FLAG_NO_ZERO },
{ "disabled", mfi_offsetof(disabled), DB_TYPE_INT },
{ "sample_count", mfi_offsetof(sample_count), DB_TYPE_INT64 },
{ "codectype", mfi_offsetof(codectype), DB_TYPE_STRING, DB_FIXUP_CODECTYPE },
@ -1087,7 +1090,7 @@ bind_mfi(sqlite3_stmt *stmt, struct media_file_info *mfi)
for (i = 0, n = 1; i < ARRAY_SIZE(mfi_cols_map); i++)
{
if (mfi_cols_map[i].flag == DB_FLAG_AUTO)
if (mfi_cols_map[i].flag & DB_FLAG_AUTO)
continue;
ptr = (char *)mfi + mfi_cols_map[i].offset;
@ -6860,7 +6863,7 @@ db_statements_prepare(void)
memset(valstr, 0, sizeof(valstr));
for (i = 0; i < ARRAY_SIZE(mfi_cols_map); i++)
{
if (mfi_cols_map[i].flag == DB_FLAG_AUTO)
if (mfi_cols_map[i].flag & DB_FLAG_AUTO)
continue;
CHECK_ERR(L_DB, safe_snprintf_cat(keystr, sizeof(keystr), "%s, ", mfi_cols_map[i].name));
@ -6887,10 +6890,13 @@ db_statements_prepare(void)
memset(keystr, 0, sizeof(keystr));
for (i = 0; i < ARRAY_SIZE(mfi_cols_map); i++)
{
if (mfi_cols_map[i].flag == DB_FLAG_AUTO)
if (mfi_cols_map[i].flag & DB_FLAG_AUTO)
continue;
CHECK_ERR(L_DB, safe_snprintf_cat(keystr, sizeof(keystr), "%s = ?, ", mfi_cols_map[i].name));
if (mfi_cols_map[i].flag & DB_FLAG_NO_ZERO)
CHECK_ERR(L_DB, safe_snprintf_cat(keystr, sizeof(keystr), "%s = daap_no_zero(?, %s), ", mfi_cols_map[i].name, mfi_cols_map[i].name));
else
CHECK_ERR(L_DB, safe_snprintf_cat(keystr, sizeof(keystr), "%s = ?, ", mfi_cols_map[i].name));
}
// Terminate at the ending ", "