Merge branch 'sticker_filters' of https://github.com/jcorporation/MPD

This commit is contained in:
Max Kellermann 2024-04-08 08:20:28 +02:00
commit b3733ef32a
5 changed files with 43 additions and 1 deletions

View File

@ -1500,7 +1500,7 @@ the database for songs).
Searches for stickers with the given value.
Other supported operators are:
"``<``", "``>``" for strings and "``eq``", "``lt``", "``gt``" to cast the value to an integer.
"``<``", "``>``", "``contains``", "``starts_with``" for strings and "``eq``", "``lt``", "``gt``" to cast the value to an integer.
Examples:

View File

@ -439,6 +439,10 @@ handle_sticker(Client &client, Request args, Response &r)
op = StickerOperator::LESS_THAN_INT;
else if (StringIsEqual(op_s, "gt"))
op = StickerOperator::GREATER_THAN_INT;
else if (StringIsEqual(op_s, "contains"))
op = StickerOperator::CONTAINS;
else if (StringIsEqual(op_s, "starts_with"))
op = StickerOperator::STARTS_WITH;
else {
r.FmtError(ACK_ERROR_ARG, "bad operator \"{}\"", op_s);
return CommandResult::ERROR;

View File

@ -28,6 +28,9 @@ enum sticker_sql_find {
STICKER_SQL_FIND_LT_INT,
STICKER_SQL_FIND_GT_INT,
STICKER_SQL_FIND_CONTAINS,
STICKER_SQL_FIND_STARTS_WITH,
STICKER_SQL_FIND_COUNT
};
@ -68,6 +71,12 @@ static constexpr auto sticker_sql_find = std::array {
//[STICKER_SQL_FIND_GT_INT] =
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND CAST(value AS INT)>?",
//[STICKER_SQL_FIND_CONTAINS] =
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND value LIKE ('%' || ? || '%')",
//[STICKER_SQL_FIND_STARTS_WITH] =
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND value LIKE (? || '%')",
};
static constexpr auto sticker_sql = std::array {
@ -392,6 +401,20 @@ StickerDatabase::BindFind(const char *type, const char *base_uri,
sql = Prepare(db, sql_str.c_str());
BindAll(sql, type, base_uri, name, value);
return sql;
case StickerOperator::CONTAINS:
sql_str = fmt::format("{} {} {}",
sticker_sql_find[STICKER_SQL_FIND_CONTAINS], order_by, offset);
sql = Prepare(db, sql_str.c_str());
BindAll(sql, type, base_uri, name, value);
return sql;
case StickerOperator::STARTS_WITH:
sql_str = fmt::format("{} {} {}",
sticker_sql_find[STICKER_SQL_FIND_STARTS_WITH], order_by, offset);
sql = Prepare(db, sql_str.c_str());
BindAll(sql, type, base_uri, name, value);
return sql;
}
assert(false);

View File

@ -66,6 +66,9 @@ class StickerDatabase {
SQL_FIND_LT_INT,
SQL_FIND_GT_INT,
SQL_FIND_CONTAINS,
SQL_FIND_STARTS_WITH,
SQL_FIND_COUNT
};

View File

@ -46,6 +46,18 @@ enum class StickerOperator {
* integer value bigger than the specified one.
*/
GREATER_THAN_INT,
/**
* Matches if a sticker with the specified name exists and value
* contains given string.
*/
CONTAINS,
/**
* Matches if a sticker with the specified name exits and value
* starts with given string.
*/
STARTS_WITH,
};
#endif