Improve key-value-searching `title` field when no `title=` argument is given
Current implementation details
Currently when the dsc ls receives search_terms it separates fields and values by looking for =
The CLI handler (Click 8.x framework) is configured to accept any number of arguments and does NOT require them to be surrounded by quotes. It receives any args that are not part of an option already, as a tuple. E.g this is valid:
dsc ls --order-by price --reverse --limit 10 word1 word2 word3
and the tuple will look like this:
("word1", "word2" "word3"). It's passed to the command function in a variable called search_terms
There is one special case handled that grabs all parts of the search_terms that are standalone, for example album name (no =) and creates a title=%album%name% out of that to be successfully be parsed by furhter processing that require a field name to work.
Feature suggestion
There is a caveat with this solution an SQL select containing LIKE album_title = "%album%name" would find an album named "album name" but not one named "name album".
This could be improved by producing an SQL select that looks similar to this: LIKE album_title = "%album%" AND album_title = "%name%"
That way, also a release titled "name album" would be found.
Also, this change would pave the way for handling additional - let,s call them - auto-search fields, for example artist and catalog number
Relevant code parts (current implementation)
CLI level
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/cmd23/ls.py#L16
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/cmd23/ls.py#L30-L31
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/cmd23/ls.py#L51-L54
Happening here is: Separating keys and values by delimiter = and enriching standalone arguments with a key, currently hardcoded to title:
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/ctrl/collection.py#L1224-L1244
The final result for a query like this: dsc ls word1 status=draft word2 word3
is a dictionary (kv) that looks like this:
{
'status': 'draft',
'title': 'word1%word2%word3',
}
which is then finally passed to the controller level: https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/cmd23/ls.py#L56-L62
Controller level
a proper key/value dictionary is expected here:
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/ctrl/collection.py#L1246-L1249
Model/Database abstraction level
https://github.com/JOJ0/discodos/blob/9740f83c962ade411fb690d7103ebeca6b0bf0b9/discodos/model/collection.py#L840-L843
Proposed solution
The first step in improving all this as pointed out in Feature suggestion would be to refactor prepare_key_value_search() to produce a dictionary like this::
{
'status': 'draft',
'title': ['word1', 'word2', 'word3'],
}
That way the database query function receiving the dictionary could easily interpret this to handle the title list as AND-concatenated SQL select parts.