Split basic and regex search functions

This commit is contained in:
DarkPhoenix
2020-04-10 00:57:41 +03:00
parent 362923ac64
commit adf90a8263
2 changed files with 25 additions and 3 deletions

View File

@@ -303,8 +303,30 @@ def getItemsByCategory(filter, where=None, eager=None):
filter).all()
@cachedQuery(3, "where", "nameLike", "join")
def searchItems(nameLike, where=None, join=None, eager=None):
if not isinstance(nameLike, str):
raise TypeError("Need string as argument")
if join is None:
join = tuple()
if not hasattr(join, "__iter__"):
join = (join,)
items = gamedata_session.query(Item).options(*processEager(eager)).join(*join)
for token in nameLike.split(' '):
token_safe = "%{0}%".format(sqlizeString(token))
if where is not None:
items = items.filter(and_(Item.name.like(token_safe, escape="\\"), where))
else:
items = items.filter(Item.name.like(token_safe, escape="\\"))
items = items.limit(100).all()
return items
@cachedQuery(3, "tokens", "where", "join")
def searchItems(tokens, where=None, join=None, eager=None):
def searchItemsRegex(tokens, where=None, join=None, eager=None):
if not isinstance(tokens, (tuple, list)) or not all(isinstance(t, str) for t in tokens):
raise TypeError("Need tuple or list of strings as argument")