From fed44e95b3c4bc74c8bd96eaf6f08bc6ad01ed55 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 12 Dec 2014 13:47:57 +0100 Subject: [PATCH] sticker/Song: add enum StickerOperator to sticker_song_find() --- Makefile.am | 1 + src/command/StickerCommands.cxx | 4 ++++ src/sticker/Match.hxx | 31 +++++++++++++++++++++++++++++++ src/sticker/SongSticker.cxx | 3 ++- src/sticker/SongSticker.hxx | 5 ++++- src/sticker/StickerDatabase.cxx | 17 ++++++++++++++--- src/sticker/StickerDatabase.hxx | 4 ++++ 7 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 src/sticker/Match.hxx diff --git a/Makefile.am b/Makefile.am index b9ee8d82f..9196fc358 100644 --- a/Makefile.am +++ b/Makefile.am @@ -356,6 +356,7 @@ libmpd_a_SOURCES += \ src/command/StickerCommands.cxx src/command/StickerCommands.hxx \ src/lib/sqlite/Domain.cxx src/lib/sqlite/Domain.hxx \ src/lib/sqlite/Util.hxx \ + src/sticker/Match.hxx \ src/sticker/StickerDatabase.cxx src/sticker/StickerDatabase.hxx \ src/sticker/StickerPrint.cxx src/sticker/StickerPrint.hxx \ src/sticker/SongSticker.cxx src/sticker/SongSticker.hxx diff --git a/src/command/StickerCommands.cxx b/src/command/StickerCommands.cxx index f2fad86cc..b8eee55c5 100644 --- a/src/command/StickerCommands.cxx +++ b/src/command/StickerCommands.cxx @@ -143,6 +143,9 @@ handle_sticker_song(Client &client, ConstBuffer args) const char *const base_uri = args[2]; + StickerOperator op = StickerOperator::EXISTS; + const char *value = nullptr; + bool success; struct sticker_song_find_data data = { client, @@ -150,6 +153,7 @@ handle_sticker_song(Client &client, ConstBuffer args) }; success = sticker_song_find(*db, base_uri, data.name, + op, value, sticker_song_find_print_cb, &data, error); if (!success) { diff --git a/src/sticker/Match.hxx b/src/sticker/Match.hxx new file mode 100644 index 000000000..f91e70b40 --- /dev/null +++ b/src/sticker/Match.hxx @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_STICKER_MATCH_HXX +#define MPD_STICKER_MATCH_HXX + +enum class StickerOperator { + /** + * Matches if a sticker with the specified name exists. The + * "value" parameter is ignored (must be nullptr). + */ + EXISTS, +}; + +#endif diff --git a/src/sticker/SongSticker.cxx b/src/sticker/SongSticker.cxx index 0b317bf9e..5e5793b70 100644 --- a/src/sticker/SongSticker.cxx +++ b/src/sticker/SongSticker.cxx @@ -97,6 +97,7 @@ sticker_song_find_cb(const char *uri, const char *value, void *user_data) bool sticker_song_find(const Database &db, const char *base_uri, const char *name, + StickerOperator op, const char *value, void (*func)(const LightSong &song, const char *value, void *user_data), void *user_data, @@ -119,7 +120,7 @@ sticker_song_find(const Database &db, const char *base_uri, const char *name, data.base_uri_length = strlen(data.base_uri); - bool success = sticker_find("song", data.base_uri, name, + bool success = sticker_find("song", data.base_uri, name, op, value, sticker_song_find_cb, &data, error); free(allocated); diff --git a/src/sticker/SongSticker.hxx b/src/sticker/SongSticker.hxx index 918046d0e..ec4f1b344 100644 --- a/src/sticker/SongSticker.hxx +++ b/src/sticker/SongSticker.hxx @@ -20,6 +20,7 @@ #ifndef MPD_SONG_STICKER_HXX #define MPD_SONG_STICKER_HXX +#include "Match.hxx" #include "Compiler.h" #include @@ -30,7 +31,8 @@ class Database; class Error; /** - * Returns one value from a song's sticker record. + * Returns one value from a song's sticker record. The caller must + * free the return value with g_free(). */ gcc_pure std::string @@ -81,6 +83,7 @@ sticker_song_get(const LightSong &song, Error &error); */ bool sticker_song_find(const Database &db, const char *base_uri, const char *name, + StickerOperator op, const char *value, void (*func)(const LightSong &song, const char *value, void *user_data), void *user_data, diff --git a/src/sticker/StickerDatabase.cxx b/src/sticker/StickerDatabase.cxx index e1e5d76ab..3c1245a6e 100644 --- a/src/sticker/StickerDatabase.cxx +++ b/src/sticker/StickerDatabase.cxx @@ -370,6 +370,7 @@ sticker_load(const char *type, const char *uri, Error &error) static sqlite3_stmt * BindFind(const char *type, const char *base_uri, const char *name, + StickerOperator op, const char *value, Error &error) { assert(type != nullptr); @@ -378,12 +379,21 @@ BindFind(const char *type, const char *base_uri, const char *name, if (base_uri == nullptr) base_uri = ""; - return BindAllOrNull(error, sticker_stmt[STICKER_SQL_FIND], - type, base_uri, name); + switch (op) { + case StickerOperator::EXISTS: + return BindAllOrNull(error, sticker_stmt[STICKER_SQL_FIND], + type, base_uri, name); + } + + (void)value; + + assert(false); + gcc_unreachable(); } bool sticker_find(const char *type, const char *base_uri, const char *name, + StickerOperator op, const char *value, void (*func)(const char *uri, const char *value, void *user_data), void *user_data, @@ -392,7 +402,8 @@ sticker_find(const char *type, const char *base_uri, const char *name, assert(func != nullptr); assert(sticker_enabled()); - sqlite3_stmt *const stmt = BindFind(type, base_uri, name, error); + sqlite3_stmt *const stmt = BindFind(type, base_uri, name, op, value, + error); if (stmt == nullptr) return false; diff --git a/src/sticker/StickerDatabase.hxx b/src/sticker/StickerDatabase.hxx index 38c7d6f95..d3cfe885e 100644 --- a/src/sticker/StickerDatabase.hxx +++ b/src/sticker/StickerDatabase.hxx @@ -42,6 +42,7 @@ #ifndef MPD_STICKER_DATABASE_HXX #define MPD_STICKER_DATABASE_HXX +#include "Match.hxx" #include "Compiler.h" #include @@ -154,11 +155,14 @@ sticker_load(const char *type, const char *uri, * @param base_uri the URI prefix of the resources, or nullptr if all * resources should be searched * @param name the name of the sticker + * @param op the comparison operator + * @param value the operand * @return true on success (even if no sticker was found), false on * failure */ bool sticker_find(const char *type, const char *base_uri, const char *name, + StickerOperator op, const char *value, void (*func)(const char *uri, const char *value, void *user_data), void *user_data,