@@ -89,22 +89,20 @@ def get_connection_string(dburl):
8989def get_oss_info_by_tlsh_and_filename (file_name , checksum_value , tlsh_value ):
9090 sql_statement = "SELECT filename,pathname,checksum,tlshchecksum,ossname,ossversion,\
9191 license,platformname,platformversion FROM lgematching "
92- sql_statement_checksum = " WHERE filename='{fname}' AND checksum='{checksum}';" .format (fname = file_name ,
93- checksum = checksum_value ) # Checking checksum first.
94- sql_statement_filename = "SELECT DISTINCT ON (tlshchecksum) tlshchecksum FROM lgematching WHERE filename='{fname}';" .format (
95- fname = file_name ) # For getting tlsh values of file.
92+ sql_statement_checksum = " WHERE filename=%s AND checksum=%s;" # Using parameterized query
93+ sql_statement_filename = "SELECT DISTINCT ON (tlshchecksum) tlshchecksum FROM lgematching WHERE filename=%s;" # Using parameterized query
9694
9795 final_result_item = ""
9896
9997 df_result = get_list_by_using_query (
100- sql_statement + sql_statement_checksum , columns )
98+ sql_statement + sql_statement_checksum , columns , ( file_name , checksum_value ) )
10199 # Found a file with the same checksum.
102100 if df_result is not None and len (df_result ) > 0 :
103101 final_result_item = df_result
104102 else :
105103 # Match tlsh and fileName
106104 df_result = get_list_by_using_query (
107- sql_statement_filename , ['tlshchecksum' ])
105+ sql_statement_filename , ['tlshchecksum' ], ( file_name ,) )
108106 if df_result is None or len (df_result ) <= 0 :
109107 final_result_item = ""
110108 elif tlsh_value == TLSH_CHECKSUM_NULL : # Couldn't get the tlsh of a file.
@@ -124,20 +122,25 @@ def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value):
124122 logger .warning (f"* (Minor) Error_tlsh_comparison: { ex } " )
125123 if matched_tlsh != "" :
126124 final_result_item = get_list_by_using_query (
127- sql_statement + " WHERE filename='{fname}' AND tlshchecksum='{tlsh}';" .format (fname = file_name ,
128- tlsh = matched_tlsh ),
129- columns )
125+ sql_statement + " WHERE filename=%s AND tlshchecksum=%s;" , columns , (file_name , matched_tlsh ))
130126
131127 return final_result_item
132128
133129
134- def get_list_by_using_query (sql_query , columns ):
130+ def get_list_by_using_query (sql_query , columns , params = None ):
135131 result_rows = "" # DataFrame
136- cur .execute (sql_query )
137- rows = cur .fetchall ()
132+ try :
133+ if params :
134+ cur .execute (sql_query , params )
135+ else :
136+ cur .execute (sql_query )
137+ rows = cur .fetchall ()
138138
139- if rows is not None and len (rows ) > 0 :
140- result_rows = pd .DataFrame (data = rows , columns = columns )
139+ if rows is not None and len (rows ) > 0 :
140+ result_rows = pd .DataFrame (data = rows , columns = columns )
141+ except Exception as ex :
142+ logger .error (f"Database query error: { ex } " )
143+ result_rows = ""
141144 return result_rows
142145
143146
0 commit comments