sticker/Song: add enum StickerOperator to sticker_song_find()

This commit is contained in:
Max Kellermann 2014-12-12 13:47:57 +01:00
parent 71ece56470
commit fed44e95b3
7 changed files with 60 additions and 5 deletions

View File

@ -356,6 +356,7 @@ libmpd_a_SOURCES += \
src/command/StickerCommands.cxx src/command/StickerCommands.hxx \ src/command/StickerCommands.cxx src/command/StickerCommands.hxx \
src/lib/sqlite/Domain.cxx src/lib/sqlite/Domain.hxx \ src/lib/sqlite/Domain.cxx src/lib/sqlite/Domain.hxx \
src/lib/sqlite/Util.hxx \ src/lib/sqlite/Util.hxx \
src/sticker/Match.hxx \
src/sticker/StickerDatabase.cxx src/sticker/StickerDatabase.hxx \ src/sticker/StickerDatabase.cxx src/sticker/StickerDatabase.hxx \
src/sticker/StickerPrint.cxx src/sticker/StickerPrint.hxx \ src/sticker/StickerPrint.cxx src/sticker/StickerPrint.hxx \
src/sticker/SongSticker.cxx src/sticker/SongSticker.hxx src/sticker/SongSticker.cxx src/sticker/SongSticker.hxx

View File

@ -143,6 +143,9 @@ handle_sticker_song(Client &client, ConstBuffer<const char *> args)
const char *const base_uri = args[2]; const char *const base_uri = args[2];
StickerOperator op = StickerOperator::EXISTS;
const char *value = nullptr;
bool success; bool success;
struct sticker_song_find_data data = { struct sticker_song_find_data data = {
client, client,
@ -150,6 +153,7 @@ handle_sticker_song(Client &client, ConstBuffer<const char *> args)
}; };
success = sticker_song_find(*db, base_uri, data.name, success = sticker_song_find(*db, base_uri, data.name,
op, value,
sticker_song_find_print_cb, &data, sticker_song_find_print_cb, &data,
error); error);
if (!success) { if (!success) {

31
src/sticker/Match.hxx Normal file
View File

@ -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

View File

@ -97,6 +97,7 @@ sticker_song_find_cb(const char *uri, const char *value, void *user_data)
bool bool
sticker_song_find(const Database &db, const char *base_uri, const char *name, 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 (*func)(const LightSong &song, const char *value,
void *user_data), void *user_data),
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); 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, sticker_song_find_cb, &data,
error); error);
free(allocated); free(allocated);

View File

@ -20,6 +20,7 @@
#ifndef MPD_SONG_STICKER_HXX #ifndef MPD_SONG_STICKER_HXX
#define MPD_SONG_STICKER_HXX #define MPD_SONG_STICKER_HXX
#include "Match.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <string> #include <string>
@ -30,7 +31,8 @@ class Database;
class Error; 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 gcc_pure
std::string std::string
@ -81,6 +83,7 @@ sticker_song_get(const LightSong &song, Error &error);
*/ */
bool bool
sticker_song_find(const Database &db, const char *base_uri, const char *name, 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 (*func)(const LightSong &song, const char *value,
void *user_data), void *user_data),
void *user_data, void *user_data,

View File

@ -370,6 +370,7 @@ sticker_load(const char *type, const char *uri, Error &error)
static sqlite3_stmt * static sqlite3_stmt *
BindFind(const char *type, const char *base_uri, const char *name, BindFind(const char *type, const char *base_uri, const char *name,
StickerOperator op, const char *value,
Error &error) Error &error)
{ {
assert(type != nullptr); assert(type != nullptr);
@ -378,12 +379,21 @@ BindFind(const char *type, const char *base_uri, const char *name,
if (base_uri == nullptr) if (base_uri == nullptr)
base_uri = ""; base_uri = "";
switch (op) {
case StickerOperator::EXISTS:
return BindAllOrNull(error, sticker_stmt[STICKER_SQL_FIND], return BindAllOrNull(error, sticker_stmt[STICKER_SQL_FIND],
type, base_uri, name); type, base_uri, name);
}
(void)value;
assert(false);
gcc_unreachable();
} }
bool bool
sticker_find(const char *type, const char *base_uri, const char *name, 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 (*func)(const char *uri, const char *value,
void *user_data), void *user_data),
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(func != nullptr);
assert(sticker_enabled()); 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) if (stmt == nullptr)
return false; return false;

View File

@ -42,6 +42,7 @@
#ifndef MPD_STICKER_DATABASE_HXX #ifndef MPD_STICKER_DATABASE_HXX
#define MPD_STICKER_DATABASE_HXX #define MPD_STICKER_DATABASE_HXX
#include "Match.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <string> #include <string>
@ -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 * @param base_uri the URI prefix of the resources, or nullptr if all
* resources should be searched * resources should be searched
* @param name the name of the sticker * @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 * @return true on success (even if no sticker was found), false on
* failure * failure
*/ */
bool bool
sticker_find(const char *type, const char *base_uri, const char *name, 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 (*func)(const char *uri, const char *value,
void *user_data), void *user_data),
void *user_data, void *user_data,