Python: Is there any Pythonic way that allows searching through SQL queries in a directory? How to find the indicated query among a bunch of files?

Viewed 63

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'])
1 Answers
files_path = [os.path.join(path, file) for path, subdirs, files in os.walk(yourPath) for file in files if file.split(".")[-1] == "sql"]

This will return all the .sql files from yourPath
os.walk() returns the path to each file/folder, all folders and all files from the location
for file in files if file.split(".")[-1] == "sql" checks all files and returns only those that have .sql extenstion
os.path.join(path, file) will build a correct full path to those files

The only way to find all sql files is to check each file if it's an sql one. You can improve it if you know some rules about the structure (for example, if a folder contains a sql file, all are sql files, or if a folder contains a non-sql file, then for sure there are no sql files)

Related