def search(db_file_fullpath, query, filterExt = [], isRegex = False): query = query.lower() print("db.search (%s, %s)" % (db_file_fullpath, query)) if query is None or query == "": print("Inalid query: %s." % query) return None if isRegex: return searchUsingRegexScoopAllThenPyRegex(db_file_fullpath, query) # connect conn = sqlite3.connect(db_file_fullpath) c = conn.cursor() like_clause = "LIKE" # query table sql_cmd = """SELECT * FROM repository WHERE bytes %s (?) COLLATE NOCASE""" % (like_clause) print(sql_cmd) c.execute(sql_cmd, [('%%%s%%' % query)]) # @@optimize: do extension selection in SQL query, not after fetched all data! for db_match in c: try: if isMatchFilterExt(db_match[1], filterExt): matches = __refineSearch(db_match[3], query) for match in matches: (lineNo, line) = match decorate.printMatch(db_match[0], lineNo, line) except Exception as inst: print("Error searching file: '%s': %s." % (db_match[1], inst))
def searchUsingRegexScoopAllThenPyRegex(db_file_fullpath, query): print('searchUsingRegexScoopAllThenPyRegex') # connect conn = sqlite3.connect(db_file_fullpath) c = conn.cursor() # query table sql_cmd = """SELECT * FROM repository""" print(sql_cmd) c.execute(sql_cmd) for item in c: if item[3] is not None: try: matches = __regexSearch(item[3], query) for match in matches: (lineNo, line) = match decorate.printMatch(item[0], lineNo, line) except Exception as inst: print("Error searching file: '%s': %s." % (item[1], inst))
def searchUsingRegex(db_file_fullpath, query): # @@ regex doesn't work when embed in sqlite :( print('searchUsingRegex -- NOT SUPPOSED TO BE CALLED - BROKEN'*10) # connect conn = sqlite3.connect(db_file_fullpath) conn.create_function("TATA", 1, myregexp) c = conn.cursor() # query table like_clause = "TATA" if isRegex else "LIKE" sql_cmd = """SELECT * FROM repository WHERE bytes %s (?)""" % (like_clause) print(sql_cmd) c.execute(sql_cmd, [('%%%s%%' % query)]) for db_match in c: try: matches = __refineSearch(db_match[3], query) for match in matches: (lineNo, line) = match decorate.printMatch(db_match[0], lineNo, line) except Exception as inst: print("Error searching file: '%s': %s." % (db_match[1], inst))