Skip to content

Commit

Permalink
Merge branch 'sticker_filters' of https://github.com/jcorporation/MPD
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Apr 8, 2024
2 parents b0cd456 + c613d25 commit b3733ef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/protocol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 4 additions & 0 deletions src/command/StickerCommands.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/sticker/Database.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/sticker/Database.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class StickerDatabase {
SQL_FIND_LT_INT,
SQL_FIND_GT_INT,

SQL_FIND_CONTAINS,
SQL_FIND_STARTS_WITH,

SQL_FIND_COUNT
};

Expand Down
12 changes: 12 additions & 0 deletions src/sticker/Match.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b3733ef

Please sign in to comment.