async def _alias_match_name(name: str): """ Searches the alias table for aliases that match the name :param name: The name to find the alias of """ params = (name, ) * 2 sql = 'SELECT * ' \ 'FROM aliases ' \ 'JOIN _items ON item_id = id ' \ 'WHERE alias LIKE %s ' \ 'OR SOUNDEX(alias) LIKE SOUNDEX(%s) ' \ 'LIMIT 1' alias = await objects.execute(Alias.raw(sql, *params)) # No alis found if not alias: return None # Building item from join item = Item() item.name = alias.name item.id = alias.id item.members = alias.members item.high_alch = alias.high_alch item.low_alch = alias.low_alch item.description = alias.description item.price = alias.price item.category = alias.category item.last_updated = alias.last_updated item.runeday = alias.runeday return item
async def fuzzy_match_name(name: str): """ Fuzzy matches a "Maybe" partial name :param name: The name to match :return: The item and name of the matched item or none if the name was not matched """ # Checking DB first ret = await objects.execute( Item.raw( 'SELECT * ' 'FROM _items ' 'WHERE name LIKE %s ' 'OR SOUNDEX(name) LIKE SOUNDEX(%s) ' 'ORDER BY sys.jaro_winkler(Name, %s) DESC ' 'LIMIT 1', name, name, name)) # Returning if name was matched to something if ret: return ret[0] # Trying aliases ret = await objects.execute( Alias.raw( 'SELECT * ' 'FROM aliases ' 'JOIN _items ON item_id = id ' 'WHERE alias LIKE %s ' 'OR SOUNDEX(alias) LIKE SOUNDEX(%s) ' 'LIMIT 1', name, name)) if ret: ret = ret[0] item = Item() item.name = ret.name item.id = ret.id item.members = ret.members item.high_alch = ret.high_alch item.low_alch = ret.low_alch item.description = ret.description item.price = ret.price item.category = ret.category item.last_updated = ret.last_updated item.runeday = ret.runeday return item # Slowly fuzzy matching DB sanitized = db.get_conn().escape_string(name) ret = await objects.execute( Item.raw( 'SELECT * ' 'FROM _items ' 'WHERE name LIKE \'%%{}%%\'' 'ORDER BY sys.jaro_winkler(name, %s) DESC ' 'LIMIT 1'.format(sanitized), name)) if not ret: return None return ret[0]