New command "stickernames" lists uniq and sorted sticker names

This commit is contained in:
jcorporation 2023-10-21 18:08:14 +02:00
parent 97da29cc90
commit f4f79a3d5f
6 changed files with 69 additions and 0 deletions

View File

@ -1513,6 +1513,9 @@ Examples:
sticker: name_1=value_1
OK
:command:`stickernames`
Gets a list of uniq sticker names.
Connection settings
===================

View File

@ -186,6 +186,7 @@ static constexpr struct command commands[] = {
{ "status", PERMISSION_READ, 0, 0, handle_status },
#ifdef ENABLE_SQLITE
{ "sticker", PERMISSION_ADMIN, 3, -1, handle_sticker },
{ "stickernames", PERMISSION_ADMIN, 0, 0, handle_sticker_names },
#endif
{ "stop", PERMISSION_PLAYER, 0, 0, handle_stop },
{ "subscribe", PERMISSION_READ, 1, 1, handle_subscribe },

View File

@ -102,6 +102,24 @@ public:
return CommandResult::OK;
}
virtual CommandResult Names() {
auto data = CallbackContext{
.name = "",
.sticker_type = sticker_type,
.response = response,
.is_song = StringIsEqual("song", sticker_type)
};
auto callback = [](const char *found_value, void *user_data) {
auto context = reinterpret_cast<CallbackContext *>(user_data);
context->response.Fmt("name: {}\n", found_value);
};
sticker_database.Names(callback, &data);
return CommandResult::OK;
}
protected:
DomainHandler(Response &_response,
const Database &_database,
@ -292,6 +310,24 @@ private:
} // namespace
CommandResult
handle_sticker_names(Client &client, Request args, Response &r)
{
(void) args;
auto &instance = client.GetInstance();
if (!instance.HasStickerDatabase()) {
r.Error(ACK_ERROR_UNKNOWN, "sticker database is disabled");
return CommandResult::ERROR;
}
auto &db = client.GetPartition().GetDatabaseOrThrow();
auto &sticker_database = *instance.sticker_database;
std::unique_ptr<DomainHandler> handler = std::make_unique<SongHandler>(r, db, sticker_database);
return handler->Names();
}
CommandResult
handle_sticker(Client &client, Request args, Response &r)
{

View File

@ -12,5 +12,7 @@ class Response;
CommandResult
handle_sticker(Client &client, Request request, Response &response);
CommandResult
handle_sticker_names(Client &client, Request request, Response &response);
#endif

View File

@ -32,6 +32,7 @@ enum sticker_sql {
STICKER_SQL_TRANSACTION_BEGIN,
STICKER_SQL_TRANSACTION_COMMIT,
STICKER_SQL_TRANSACTION_ROLLBACK,
STICKER_SQL_NAMES,
STICKER_SQL_COUNT
};
@ -72,6 +73,9 @@ static constexpr auto sticker_sql = std::array {
//[STICKER_SQL_TRANSACTION_ROLLBACK]
"ROLLBACK",
//[STICKER_SQL_NAMES]
"SELECT DISTINCT name FROM sticker order by name",
};
static constexpr const char sticker_sql_create[] =
@ -366,6 +370,23 @@ StickerDatabase::GetUniqueStickers()
return result;
}
void
StickerDatabase::Names(void (*func)(const char *value, void *user_data), void *user_data)
{
assert(func != nullptr);
sqlite3_stmt *const s = stmt[STICKER_SQL_NAMES];
assert(s != nullptr);
AtScopeExit(s) {
sqlite3_reset(s);
};
ExecuteForEach(s, [s, func, user_data](){
func((const char*)sqlite3_column_text(s, 0), user_data);
});
}
void
StickerDatabase::BatchDeleteNoIdle(const std::list<StickerTypeUriPair> &stickers)
{

View File

@ -54,6 +54,7 @@ class StickerDatabase {
SQL_TRANSACTION_BEGIN,
SQL_TRANSACTION_COMMIT,
SQL_TRANSACTION_ROLLBACK,
SQL_NAMES,
SQL_COUNT
};
@ -146,6 +147,11 @@ public:
void *user_data),
void *user_data);
/**
* Uniq and sorted list of all sticker names
*/
void Names(void (*func)(const char *value, void *user_data), void *user_data);
using StickerTypeUriPair = std::pair<std::string, std::string>;
/**