Skip to content

Commit

Permalink
fixed query variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Herrmann committed Jan 21, 2025
1 parent 62b66fe commit 026afa6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def normalizeSQLQuery(query, baseDict):
formatted_query.append(Tp.process_groupby(token, alias_map, baseDict))
elif isinstance(token, Where):
formatted_query.append('WHERE')
formatted_query.append(Tp.process_where(token, alias_map, baseDict))
formatted_query.append(Tp.process_where(token, alias_map, baseDict, query))
elif formatted_query and formatted_query[-1] == 'SELECT' and (isinstance(token, IdentifierList) or isinstance(token, Function) or isinstance(token, Identifier)):
if isinstance(token, Function):
token = IdentifierList([token])
Expand Down
73 changes: 37 additions & 36 deletions TokenProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlparse.tokens import Keyword, DML, Name, Wildcard

import Helper as He


#####################################
#####################################
Expand Down Expand Up @@ -188,50 +189,50 @@ def process_2element_par(parenthesis, alias_map, baseDict: dict, keyword):
return (" "+keyword+" ").join(toks)


def process_where(where, alias_map, baseDict: dict):
conditions = []
current_condition = []
def process_where(where, alias_map, baseDict: dict, query: str):
conditions = []
current_condition = []

for token in where.tokens:
if token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
continue
for token in where.tokens:
if token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
continue

if token.ttype is Keyword and token.value.upper() in ('AND', 'OR'):
if current_condition:
conditions.append(''.join(str(t) for t in current_condition).strip())
current_condition = []
conditions.append(token.value.upper())
elif isinstance(token, Comparison):
current_condition.append(process_condition(token, alias_map, baseDict))
elif isinstance(token, Parenthesis):
current_condition.append(process_paranthesis(token, alias_map, baseDict))
else:
current_condition.append(token)
if token.ttype is Keyword and token.value.upper() in ('AND', 'OR'):
if current_condition:
conditions.append(''.join(str(t) for t in current_condition).strip())
current_condition = []
conditions.append(token.value.upper())
elif isinstance(token, Comparison):
current_condition.append(process_condition(token, alias_map, baseDict))
elif isinstance(token, Parenthesis):
current_condition.append(process_paranthesis(token, alias_map, baseDict))
else:
current_condition.append(token)

if current_condition:
conditions.append(''.join(str(t) for t in current_condition).strip())
if current_condition:
conditions.append(''.join(str(t) for t in current_condition).strip())

sorted_conditions = []
current_group = []
last_connector = ""
sorted_conditions = []
current_group = []
last_connector = ""

if "OR" not in conditions:
for condition in conditions:
if condition == 'AND':
pass
else:
current_group.append(condition)
if "OR" not in conditions:
for condition in conditions:
if condition == 'AND':
pass
else:
current_group.append(condition)

if current_group:
sorted_conditions.extend(sorted(current_group))
if current_group:
sorted_conditions.extend(sorted(current_group))

return " AND ".join(sorted_conditions)
else:
return " ".join(conditions)
return " AND ".join(sorted_conditions)
else:
return " ".join(conditions)

where_index = query.upper().find('WHERE')
normalized_query = query[:where_index + 5] + ' ' + ' '.join(sorted_conditions)
where_index = query.upper().find('WHERE')

normalized_query = query[:where_index + 5] + ' ' + ' '.join(sorted_conditions)

def process_where_xx(where, alias_map, baseDict: dict):
where_tokens = []
Expand Down

0 comments on commit 026afa6

Please sign in to comment.