From db8bf60aef75e163221b1fc111c83337db4f8c1c Mon Sep 17 00:00:00 2001 From: krateng Date: Sat, 23 Apr 2022 16:36:35 +0200 Subject: [PATCH] Moved search functionality to database --- maloja/database/__init__.py | 4 ++-- maloja/database/sqldb.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index d83c8f3..6595e88 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -423,7 +423,7 @@ def start_db(): def db_search(query,type=None): results = [] if type=="ARTIST": - results = [a for a in sqldb.get_artists() if sqldb.normalize_name(query) in sqldb.normalize_name(a)] + results = sqldb.search_artist(query) if type=="TRACK": - results = [t for t in sqldb.get_tracks() if sqldb.normalize_name(query) in sqldb.normalize_name(t['title'])] + results = sqldb.search_track(query) return results diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index e0954d9..4ee49ca 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -691,7 +691,25 @@ def get_artist(id,dbconn=None): return artist_db_to_dict(artistinfo,dbconn=dbconn) +@cached_wrapper +@connection_provider +def search_artist(searchterm,dbconn=None): + op = DB['artists'].select().where( + DB['artists'].c.name_normalized.ilike(normalize_name(f"%{searchterm}%")) + ) + result = dbconn.execute(op).all() + return [get_artist(row.id,dbconn=dbconn) for row in result] + +@cached_wrapper +@connection_provider +def search_track(searchterm,dbconn=None): + op = DB['tracks'].select().where( + DB['tracks'].c.title_normalized.ilike(normalize_name(f"%{searchterm}%")) + ) + result = dbconn.execute(op).all() + + return [get_track(row.id,dbconn=dbconn) for row in result] ##### MAINTENANCE