I'm not sure this is the best way to find the desired query. Possibly there is a Python library that allows you to search a file more intelligently and bring the most relevant results. I am currently using the following code:
import re
from pathlib import Path #To find all the path to the results in the folder.
import os
from functools import reduce
def find_query(pattern):
path_queries = []
for filename in Path(r'\\path/where/the/queries/are/').rglob('*.sql'):
path_queries.append(filename.__str__())
for path in path_queries:
if os.path.exists(path):
conds = [(i.lower() in str(path).lower()) for i in pattern]
cond = reduce(lambda a, b: a and b, conds, True) # similar to foldl
if cond:
print(path)
try:
with open(path, encoding="Latin") as file:
content = file.read()
conds = [(re.search(i, content, re.I)) for i in pattern]
cond = reduce(lambda a, b: a and b, conds, True) # similar to foldl
if cond:
print(path)
except:
print("Error with file: ", path)
else:
print('The path: ', path, ' is wrong!')
find_query([r"max\(case"])#, r'another-keyword-of-interest'])