sql-testing-tools 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sql_testing_tools/Helper.py +16 -216
- sql_testing_tools/TokenProcessing.py +361 -0
- sql_testing_tools-0.1.6.dist-info/METADATA +25 -0
- sql_testing_tools-0.1.6.dist-info/RECORD +8 -0
- sql_testing_tools-0.1.4.dist-info/METADATA +0 -10
- sql_testing_tools-0.1.4.dist-info/RECORD +0 -7
- {sql_testing_tools-0.1.4.dist-info → sql_testing_tools-0.1.6.dist-info}/WHEEL +0 -0
- {sql_testing_tools-0.1.4.dist-info → sql_testing_tools-0.1.6.dist-info}/top_level.txt +0 -0
sql_testing_tools/Helper.py
CHANGED
|
@@ -1,26 +1,18 @@
|
|
|
1
|
-
import os
|
|
2
|
-
from . import BaseAccess as Ba
|
|
3
|
-
|
|
4
|
-
|
|
5
1
|
try:
|
|
6
|
-
import
|
|
2
|
+
import BaseAccess as Ba
|
|
3
|
+
import TokenProcessing as Tp
|
|
7
4
|
except ImportError:
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import sqlparse
|
|
5
|
+
from . import BaseAccess as Ba
|
|
6
|
+
from . import TokenProcessing as Tp
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
import requests
|
|
15
|
-
except ImportError:
|
|
16
|
-
print("Trying to Install required module: requests\n")
|
|
17
|
-
os.system('pip install requests')
|
|
18
|
-
import requests
|
|
8
|
+
import requests
|
|
9
|
+
import sqlparse
|
|
19
10
|
|
|
20
11
|
from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis
|
|
21
12
|
from sqlparse.tokens import Keyword, DML, Name, Wildcard
|
|
22
13
|
|
|
23
14
|
|
|
15
|
+
|
|
24
16
|
def normalizeSQLQuery(query, baseDict):
|
|
25
17
|
try:
|
|
26
18
|
query = query.replace("\"", "'")
|
|
@@ -32,199 +24,6 @@ def normalizeSQLQuery(query, baseDict):
|
|
|
32
24
|
formatted_query = []
|
|
33
25
|
alias_map = {}
|
|
34
26
|
|
|
35
|
-
def process_identifier(identifier, alias_map, baseDict: dict):
|
|
36
|
-
if isinstance(identifier, Identifier):
|
|
37
|
-
if identifier.get_real_name() and identifier.get_parent_name() and identifier.get_parent_name().lower() in alias_map.keys():
|
|
38
|
-
return f"{alias_map[identifier.get_parent_name().lower()].lower()}.{identifier.get_real_name().lower()}"
|
|
39
|
-
elif identifier.get_real_name():
|
|
40
|
-
tables = findTableForColumn(baseDict, identifier.get_real_name(), alias_map.keys())
|
|
41
|
-
if len(tables) == 1:
|
|
42
|
-
alias_map[tables[0].lower()] = tables[0]
|
|
43
|
-
return f"{tables[0].lower()}.{identifier.get_real_name().lower()}"
|
|
44
|
-
else:
|
|
45
|
-
return f"{identifier.get_real_name().lower()}"
|
|
46
|
-
return str(identifier)
|
|
47
|
-
|
|
48
|
-
def process_select(select, alias_map, insideFunction=False):
|
|
49
|
-
select_tokens = []
|
|
50
|
-
for token in select.tokens:
|
|
51
|
-
if isinstance(token, IdentifierList):
|
|
52
|
-
for identifier in token.get_identifiers():
|
|
53
|
-
select_tokens.append(f"{process_identifier(identifier, alias_map, baseDict).lower()} as {identifier.get_real_name().lower()}")
|
|
54
|
-
elif isinstance(token, Identifier):
|
|
55
|
-
if insideFunction:
|
|
56
|
-
select_tokens.append(process_identifier(token, alias_map, baseDict).lower())
|
|
57
|
-
else:
|
|
58
|
-
select_tokens.append(f"{process_identifier(token, alias_map, baseDict).lower()} as {token.get_real_name().lower()}")
|
|
59
|
-
elif isinstance(token, Function):
|
|
60
|
-
#select_tokens.append(f"{token.get_name().lower()}({process_identifier(token.get_parameters(), alias_map, baseDict).lower()})")
|
|
61
|
-
for par in token.tokens:
|
|
62
|
-
if isinstance(par, Identifier):
|
|
63
|
-
select_tokens.append(par.get_name().lower()+"(")
|
|
64
|
-
if isinstance(par, Parenthesis):
|
|
65
|
-
select_tokens[-1] += process_select(par, alias_map, True)+") as funcResult"
|
|
66
|
-
elif token.ttype is Wildcard:
|
|
67
|
-
select_tokens.append("*")
|
|
68
|
-
else:
|
|
69
|
-
continue
|
|
70
|
-
select_tokens.sort()
|
|
71
|
-
return ",".join(select_tokens)
|
|
72
|
-
|
|
73
|
-
def process_from(from_, alias_map):
|
|
74
|
-
from_tokens = []
|
|
75
|
-
if hasattr(from_, 'tokens'):
|
|
76
|
-
for token in from_.tokens:
|
|
77
|
-
if isinstance(token, IdentifierList):
|
|
78
|
-
for identifier in token.get_identifiers():
|
|
79
|
-
alias_map[identifier.get_real_name().lower()] = identifier.get_alias() or identifier.get_real_name()
|
|
80
|
-
from_tokens.append(f"{identifier.get_real_name().lower()} {alias_map[identifier.get_real_name().lower()].lower()}")
|
|
81
|
-
elif isinstance(token, Identifier):
|
|
82
|
-
alias_map[token.get_real_name().lower()] = token.get_alias() or token.get_real_name()
|
|
83
|
-
from_tokens.append(f"{token.get_real_name().lower()} {alias_map[token.get_real_name().lower()].lower()}")
|
|
84
|
-
elif token.ttype is not None and token.ttype is Name:
|
|
85
|
-
alias_map[token.value.lower()] = token.value.lower()
|
|
86
|
-
from_tokens.append(f"{token.value.lower()} {alias_map[token.value.lower()].lower()}")
|
|
87
|
-
else:
|
|
88
|
-
continue
|
|
89
|
-
from_tokens.sort()
|
|
90
|
-
return ",".join(from_tokens)
|
|
91
|
-
|
|
92
|
-
def process_groupby(groupby_, alias_map):
|
|
93
|
-
groupby_tokens = []
|
|
94
|
-
if(isinstance(groupby_, Identifier)):
|
|
95
|
-
groupby_tokens.append(process_identifier(groupby_, alias_map, baseDict).lower())
|
|
96
|
-
elif isinstance(token, IdentifierList):
|
|
97
|
-
for identifier in token.get_identifiers():
|
|
98
|
-
groupby_tokens.append(process_identifier(identifier, alias_map, baseDict).lower())
|
|
99
|
-
groupby_tokens.sort()
|
|
100
|
-
return ",".join(groupby_tokens)
|
|
101
|
-
|
|
102
|
-
def process_condition(token, alias_map, baseDict):
|
|
103
|
-
left, operator, right = [t for t in token.tokens if not t.is_whitespace]
|
|
104
|
-
|
|
105
|
-
flipAllowed = True
|
|
106
|
-
leftLiteral = False
|
|
107
|
-
rightLiteral = False
|
|
108
|
-
|
|
109
|
-
if is_value(right):
|
|
110
|
-
flipAllowed = False
|
|
111
|
-
rightLiteral = True
|
|
112
|
-
|
|
113
|
-
if is_value(left):
|
|
114
|
-
left, right = right, left
|
|
115
|
-
if operator.value == ">":
|
|
116
|
-
operator.value = "<"
|
|
117
|
-
elif operator.value == "<":
|
|
118
|
-
operator.value = ">"
|
|
119
|
-
leftLiteral = rightLiteral
|
|
120
|
-
rightLiteral = True
|
|
121
|
-
flipAllowed = False
|
|
122
|
-
|
|
123
|
-
left = process_identifier(left, alias_map, baseDict)
|
|
124
|
-
right = process_identifier(right, alias_map, baseDict)
|
|
125
|
-
|
|
126
|
-
if flipAllowed and left.lower() >= right.lower():
|
|
127
|
-
left, right = right, left
|
|
128
|
-
if operator.value == ">":
|
|
129
|
-
operator = "<"
|
|
130
|
-
elif operator.value == "<":
|
|
131
|
-
operator = ">"
|
|
132
|
-
|
|
133
|
-
return f"{left if leftLiteral else left.lower()} {operator} {right if rightLiteral else right.lower()}"
|
|
134
|
-
|
|
135
|
-
def is_value(token):
|
|
136
|
-
return token.ttype in (sqlparse.tokens.Token.Literal.Number.Integer,
|
|
137
|
-
sqlparse.tokens.Token.Literal.Number.Float,
|
|
138
|
-
sqlparse.tokens.Token.Literal.String.Single,
|
|
139
|
-
sqlparse.tokens.Token.Literal.String.Symbol)
|
|
140
|
-
|
|
141
|
-
def process_paranthesis(parenthesis, alias_map, baseDict):
|
|
142
|
-
toks = []
|
|
143
|
-
|
|
144
|
-
bracketsRequired = True
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
for token in parenthesis.tokens:
|
|
148
|
-
if isinstance(token, Comparison):
|
|
149
|
-
toks.append(process_condition(token, alias_map, baseDict))
|
|
150
|
-
elif isinstance(token, Parenthesis):
|
|
151
|
-
toks.append(process_paranthesis(token, alias_map, baseDict))
|
|
152
|
-
|
|
153
|
-
elif token.value == '(':
|
|
154
|
-
toks.append(token.value)
|
|
155
|
-
else:
|
|
156
|
-
continue
|
|
157
|
-
x = parenthesis.flatten()
|
|
158
|
-
return " ".join(toks)
|
|
159
|
-
|
|
160
|
-
def process_where(where, alias_map, baseDict):
|
|
161
|
-
conditions = []
|
|
162
|
-
current_condition = []
|
|
163
|
-
|
|
164
|
-
for token in where.tokens:
|
|
165
|
-
if token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
|
|
166
|
-
continue
|
|
167
|
-
|
|
168
|
-
if token.ttype is Keyword and token.value.upper() in ('AND', 'OR'):
|
|
169
|
-
if current_condition:
|
|
170
|
-
conditions.append(''.join(str(t) for t in current_condition).strip())
|
|
171
|
-
current_condition = []
|
|
172
|
-
conditions.append(token.value.upper())
|
|
173
|
-
elif isinstance(token, Comparison):
|
|
174
|
-
current_condition.append(process_condition(token, alias_map, baseDict))
|
|
175
|
-
elif isinstance(token, Parenthesis):
|
|
176
|
-
current_condition.append(process_paranthesis(token, alias_map, baseDict))
|
|
177
|
-
else:
|
|
178
|
-
current_condition.append(token)
|
|
179
|
-
|
|
180
|
-
if current_condition:
|
|
181
|
-
conditions.append(''.join(str(t) for t in current_condition).strip())
|
|
182
|
-
|
|
183
|
-
sorted_conditions = []
|
|
184
|
-
current_group = []
|
|
185
|
-
last_connector = ""
|
|
186
|
-
|
|
187
|
-
if "OR" not in conditions:
|
|
188
|
-
for condition in conditions:
|
|
189
|
-
if condition == 'AND':
|
|
190
|
-
pass
|
|
191
|
-
else:
|
|
192
|
-
current_group.append(condition)
|
|
193
|
-
|
|
194
|
-
if current_group:
|
|
195
|
-
sorted_conditions.extend(sorted(current_group))
|
|
196
|
-
|
|
197
|
-
return " AND ".join(sorted_conditions)
|
|
198
|
-
else:
|
|
199
|
-
return " ".join(conditions)
|
|
200
|
-
|
|
201
|
-
where_index = query.upper().find('WHERE')
|
|
202
|
-
normalized_query = query[:where_index + 5] + ' ' + ' '.join(sorted_conditions)
|
|
203
|
-
|
|
204
|
-
def process_where_xx(where, alias_map, baseDict):
|
|
205
|
-
where_tokens = []
|
|
206
|
-
for token in where.tokens:
|
|
207
|
-
|
|
208
|
-
if isinstance(token, Comparison):
|
|
209
|
-
left, operator, right = [t for t in token.tokens if not t.is_whitespace]
|
|
210
|
-
left = process_identifier(left, alias_map, baseDict)
|
|
211
|
-
right = process_identifier(right, alias_map, baseDict)
|
|
212
|
-
if left.lower() >= right.lower():
|
|
213
|
-
left, right = right, left
|
|
214
|
-
if operator.value == ">":
|
|
215
|
-
operator = "<"
|
|
216
|
-
elif operator.value == "<":
|
|
217
|
-
operator = ">"
|
|
218
|
-
|
|
219
|
-
where_tokens.append(f"{left.lower()} {operator} {right.lower()}")
|
|
220
|
-
elif token.ttype is Keyword:
|
|
221
|
-
where_tokens.append(token.value.upper())
|
|
222
|
-
elif token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
|
|
223
|
-
continue
|
|
224
|
-
else:
|
|
225
|
-
where_tokens.append(str(token))
|
|
226
|
-
return " ".join(where_tokens)
|
|
227
|
-
|
|
228
27
|
# First pass to process FROM clause and populate alias_map
|
|
229
28
|
for token in parsed.tokens:
|
|
230
29
|
if token.is_whitespace:
|
|
@@ -234,11 +33,11 @@ def normalizeSQLQuery(query, baseDict):
|
|
|
234
33
|
pass
|
|
235
34
|
elif isinstance(token, Identifier) and formatted_query and formatted_query[-1] == 'FROM':
|
|
236
35
|
il = IdentifierList([token])
|
|
237
|
-
formatted_query.append(
|
|
238
|
-
|
|
36
|
+
formatted_query.append(Tp._from(il, alias_map, baseDict))
|
|
37
|
+
Tp._from(il, alias_map, baseDict)
|
|
239
38
|
elif isinstance(token, IdentifierList) and formatted_query and formatted_query[-1] == 'FROM':
|
|
240
|
-
formatted_query.append(
|
|
241
|
-
|
|
39
|
+
formatted_query.append(Tp._from(token, alias_map, baseDict))
|
|
40
|
+
Tp._from(token, alias_map, baseDict)
|
|
242
41
|
|
|
243
42
|
formatted_query = []
|
|
244
43
|
|
|
@@ -253,16 +52,16 @@ def normalizeSQLQuery(query, baseDict):
|
|
|
253
52
|
elif token.ttype is Keyword and token.value.upper() == 'GROUP BY':
|
|
254
53
|
formatted_query.append('GROUP BY')
|
|
255
54
|
elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'FROM':
|
|
256
|
-
formatted_query.append(
|
|
55
|
+
formatted_query.append(Tp._from(token, alias_map, baseDict))
|
|
257
56
|
elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'GROUP BY':
|
|
258
|
-
formatted_query.append(
|
|
57
|
+
formatted_query.append(Tp._groupby(token, alias_map, baseDict))
|
|
259
58
|
elif isinstance(token, Where):
|
|
260
59
|
formatted_query.append('WHERE')
|
|
261
|
-
formatted_query.append(
|
|
60
|
+
formatted_query.append(Tp._where(token, alias_map, baseDict))
|
|
262
61
|
elif formatted_query and formatted_query[-1] == 'SELECT' and (isinstance(token, IdentifierList) or isinstance(token, Function) or isinstance(token, Identifier)):
|
|
263
62
|
if isinstance(token, Function):
|
|
264
63
|
token = IdentifierList([token])
|
|
265
|
-
formatted_query.append(
|
|
64
|
+
formatted_query.append(Tp._select(token, alias_map, baseDict))
|
|
266
65
|
else:
|
|
267
66
|
formatted_query.append(str(token))
|
|
268
67
|
|
|
@@ -408,3 +207,4 @@ def checkEquality(sqlPath, solPath):
|
|
|
408
207
|
return "\n\nDie Abgabe stimmt nicht mit der Musterlösung überein."
|
|
409
208
|
return ""
|
|
410
209
|
|
|
210
|
+
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import sqlparse
|
|
2
|
+
from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis
|
|
3
|
+
from sqlparse.tokens import Keyword, DML, Name, Wildcard, Whitespace
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
import Helper as He
|
|
7
|
+
except ImportError:
|
|
8
|
+
from . import Helper as He
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
#####################################
|
|
14
|
+
#####################################
|
|
15
|
+
|
|
16
|
+
def _identifier(identifier, alias_map, baseDict: dict):
|
|
17
|
+
if isinstance(identifier, Identifier):
|
|
18
|
+
if identifier.get_real_name() and identifier.get_parent_name() and identifier.get_parent_name().lower() in alias_map.keys():
|
|
19
|
+
return f"{alias_map[identifier.get_parent_name().lower()].lower()}.{identifier.get_real_name().lower()}"
|
|
20
|
+
elif identifier.get_real_name():
|
|
21
|
+
tables = He.findTableForColumn(baseDict, identifier.get_real_name(), alias_map.keys())
|
|
22
|
+
if len(tables) == 1:
|
|
23
|
+
alias_map[tables[0].lower()] = tables[0]
|
|
24
|
+
return f"{tables[0].lower()}.{identifier.get_real_name().lower()}"
|
|
25
|
+
else:
|
|
26
|
+
return f"{identifier.get_real_name().lower()}"
|
|
27
|
+
return str(identifier)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
#####################################
|
|
31
|
+
#####################################
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _select(select, alias_map, baseDict: dict, insideFunction=False):
|
|
35
|
+
select_tokens = []
|
|
36
|
+
for token in select.tokens:
|
|
37
|
+
if isinstance(token, IdentifierList):
|
|
38
|
+
for identifier in token.get_identifiers():
|
|
39
|
+
select_tokens.append(f"{_identifier(identifier, alias_map, baseDict).lower()} as {identifier.get_real_name().lower()}")
|
|
40
|
+
elif isinstance(token, Identifier):
|
|
41
|
+
if insideFunction:
|
|
42
|
+
select_tokens.append(_identifier(token, alias_map, baseDict).lower())
|
|
43
|
+
else:
|
|
44
|
+
select_tokens.append(f"{_identifier(token, alias_map, baseDict).lower()} as {token.get_real_name().lower()}")
|
|
45
|
+
elif isinstance(token, Function):
|
|
46
|
+
#select_tokens.append(f"{token.get_name().lower()}({_identifier(token.get_parameters(), alias_map, baseDict).lower()})")
|
|
47
|
+
for par in token.tokens:
|
|
48
|
+
if isinstance(par, Identifier):
|
|
49
|
+
select_tokens.append(par.get_name().lower()+"(")
|
|
50
|
+
if isinstance(par, Parenthesis):
|
|
51
|
+
select_tokens[-1] += _select(par, alias_map, baseDict, True)+") as funcResult"
|
|
52
|
+
elif token.ttype is Wildcard:
|
|
53
|
+
select_tokens.append("*")
|
|
54
|
+
else:
|
|
55
|
+
continue
|
|
56
|
+
select_tokens.sort()
|
|
57
|
+
return ",".join(select_tokens)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
#####################################
|
|
61
|
+
#####################################
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _from(from_, alias_map, baseDict: dict):
|
|
65
|
+
from_tokens = []
|
|
66
|
+
if hasattr(from_, 'tokens'):
|
|
67
|
+
for token in from_.tokens:
|
|
68
|
+
if isinstance(token, IdentifierList):
|
|
69
|
+
for identifier in token.get_identifiers():
|
|
70
|
+
alias_map[identifier.get_real_name().lower()] = identifier.get_alias() or identifier.get_real_name()
|
|
71
|
+
from_tokens.append(f"{identifier.get_real_name().lower()} {alias_map[identifier.get_real_name().lower()].lower()}")
|
|
72
|
+
elif isinstance(token, Identifier):
|
|
73
|
+
alias_map[token.get_real_name().lower()] = token.get_alias() or token.get_real_name()
|
|
74
|
+
from_tokens.append(f"{token.get_real_name().lower()} {alias_map[token.get_real_name().lower()].lower()}")
|
|
75
|
+
elif token.ttype is not None and token.ttype is Name:
|
|
76
|
+
alias_map[token.value.lower()] = token.value.lower()
|
|
77
|
+
from_tokens.append(f"{token.value.lower()} {alias_map[token.value.lower()].lower()}")
|
|
78
|
+
else:
|
|
79
|
+
continue
|
|
80
|
+
from_tokens.sort()
|
|
81
|
+
return ",".join(from_tokens)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
#####################################
|
|
85
|
+
#####################################
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _groupby(groupby_, alias_map, baseDict: dict):
|
|
89
|
+
groupby_tokens = []
|
|
90
|
+
if(isinstance(groupby_, Identifier)):
|
|
91
|
+
groupby_tokens.append(_identifier(groupby_, alias_map, baseDict).lower())
|
|
92
|
+
elif isinstance(groupby_, IdentifierList):
|
|
93
|
+
for identifier in groupby_.get_identifiers():
|
|
94
|
+
groupby_tokens.append(_identifier(identifier, alias_map, baseDict).lower())
|
|
95
|
+
groupby_tokens.sort()
|
|
96
|
+
return ",".join(groupby_tokens)
|
|
97
|
+
|
|
98
|
+
def _condition(token, alias_map, baseDict: dict):
|
|
99
|
+
left, operator, right = [t for t in token.tokens if not t.is_whitespace]
|
|
100
|
+
|
|
101
|
+
flipAllowed = True
|
|
102
|
+
leftLiteral = False
|
|
103
|
+
rightLiteral = False
|
|
104
|
+
|
|
105
|
+
if is_value(right):
|
|
106
|
+
flipAllowed = False
|
|
107
|
+
rightLiteral = True
|
|
108
|
+
|
|
109
|
+
if is_value(left):
|
|
110
|
+
left, right = right, left
|
|
111
|
+
if operator.value == ">":
|
|
112
|
+
operator.value = "<"
|
|
113
|
+
elif operator.value == "<":
|
|
114
|
+
operator.value = ">"
|
|
115
|
+
leftLiteral = rightLiteral
|
|
116
|
+
rightLiteral = True
|
|
117
|
+
flipAllowed = False
|
|
118
|
+
|
|
119
|
+
left = _identifier(left, alias_map, baseDict)
|
|
120
|
+
right = _identifier(right, alias_map, baseDict)
|
|
121
|
+
|
|
122
|
+
if flipAllowed and left.lower() >= right.lower():
|
|
123
|
+
left, right = right, left
|
|
124
|
+
if operator.value == ">":
|
|
125
|
+
operator = "<"
|
|
126
|
+
elif operator.value == "<":
|
|
127
|
+
operator = ">"
|
|
128
|
+
|
|
129
|
+
return f"{left if leftLiteral else left.lower()} {operator} {right if rightLiteral else right.lower()}"
|
|
130
|
+
|
|
131
|
+
def is_value(token):
|
|
132
|
+
return token.ttype in (sqlparse.tokens.Token.Literal.Number.Integer,
|
|
133
|
+
sqlparse.tokens.Token.Literal.Number.Float,
|
|
134
|
+
sqlparse.tokens.Token.Literal.String.Single,
|
|
135
|
+
sqlparse.tokens.Token.Literal.String.Symbol)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
#####################################
|
|
140
|
+
#####################################
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _paranthesis(parenthesis, alias_map, baseDict: dict):
|
|
144
|
+
toks = []
|
|
145
|
+
|
|
146
|
+
bracketsRequired = True
|
|
147
|
+
closeBracketsAfterNext = False
|
|
148
|
+
|
|
149
|
+
and_count = sum(1 for token in parenthesis.tokens if token.ttype is Keyword and token.value == "AND") # Zähle "AND"-Tokens
|
|
150
|
+
or_count = sum(1 for token in parenthesis.tokens if token.ttype is Keyword and token.value == "OR") # Zähle "AND"-Tokens
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
if(and_count==1 and or_count == 0):
|
|
154
|
+
val = _2element_par(parenthesis, alias_map, baseDict, "AND")
|
|
155
|
+
return val
|
|
156
|
+
elif(and_count==0 and or_count == 1):
|
|
157
|
+
val = _2element_par(parenthesis, alias_map, baseDict, "OR")
|
|
158
|
+
return val
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
for token in parenthesis.tokens:
|
|
166
|
+
if isinstance(token, Comparison):
|
|
167
|
+
toks.append(_condition(token, alias_map, baseDict))
|
|
168
|
+
elif isinstance(token, Parenthesis):
|
|
169
|
+
toks.append(_paranthesis(token, alias_map, baseDict))
|
|
170
|
+
|
|
171
|
+
elif token.ttype is Keyword: #AND,OR
|
|
172
|
+
if token.value == "AND":
|
|
173
|
+
pass
|
|
174
|
+
elif token.value == "OR":
|
|
175
|
+
pass
|
|
176
|
+
elif token.value == "NOT":
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
elif token.value == '(':
|
|
180
|
+
toks.append(token.value)
|
|
181
|
+
else:
|
|
182
|
+
continue
|
|
183
|
+
x = parenthesis.flatten()
|
|
184
|
+
return " ".join(toks)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _2element_par(parenthesis, alias_map, baseDict: dict, keyword):
|
|
188
|
+
toks = []
|
|
189
|
+
|
|
190
|
+
for token in parenthesis.tokens:
|
|
191
|
+
if isinstance(token, Comparison):
|
|
192
|
+
toks.append(_condition(token, alias_map, baseDict))
|
|
193
|
+
elif isinstance(token, Parenthesis):
|
|
194
|
+
toks.append(_paranthesis(token, alias_map, baseDict))
|
|
195
|
+
toks = toks.sort()
|
|
196
|
+
fill = (" "+keyword+" ")
|
|
197
|
+
return fill.join(toks)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def count_keywordValues(tokens, values):
|
|
201
|
+
return len([token for token in tokens if token.ttype is Keyword and token.value.upper() in values])
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def find_index_of_keyword(tokens, keyword):
|
|
205
|
+
for index, token in enumerate(tokens):
|
|
206
|
+
if token.ttype is Keyword and token.value.upper() == keyword:
|
|
207
|
+
return index
|
|
208
|
+
return -1 # Wenn kein AND-Token gefunden wird
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _where(where, alias_map, baseDict: dict):
|
|
212
|
+
conditions = []
|
|
213
|
+
current_condition = []
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
isParanthesis = isinstance(where, Parenthesis)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
and_count = where.normalized.upper().count(" AND ")
|
|
221
|
+
or_count = where.normalized.upper().count(" OR ")
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
# No AND / OR
|
|
225
|
+
if and_count + or_count == 0:
|
|
226
|
+
return _where_simpleCondition(where, alias_map, baseDict)
|
|
227
|
+
|
|
228
|
+
# only ANDs / only ORs
|
|
229
|
+
elif and_count == 0 or or_count == 0:
|
|
230
|
+
return _where_sameStrengthKeywords(where, alias_map, baseDict, " AND " if and_count > 0 else " OR ")
|
|
231
|
+
|
|
232
|
+
# ANDs and ORs
|
|
233
|
+
|
|
234
|
+
where_tokens = [token for token in where.tokens if token.ttype is not Whitespace and token.value != "WHERE"]
|
|
235
|
+
|
|
236
|
+
# count keywords on top level
|
|
237
|
+
top_and_count = count_keywordValues(where_tokens, ["AND"])
|
|
238
|
+
top_or_count = count_keywordValues(where_tokens, ["OR"])
|
|
239
|
+
|
|
240
|
+
# two conditions on top level
|
|
241
|
+
if top_and_count + top_or_count == 1:
|
|
242
|
+
keyword = " AND " if top_and_count == 1 else " OR "
|
|
243
|
+
return _where_twoConditionsOnTopLevel(where, alias_map, baseDict, keyword, isParanthesis)
|
|
244
|
+
else:
|
|
245
|
+
where_tokens = _where_addBracketsAroundAND(where, alias_map, baseDict)
|
|
246
|
+
return _where(Where(where_tokens), alias_map, baseDict)
|
|
247
|
+
|
|
248
|
+
def _where_addBracketsAroundAND(where, alias_map, baseDict: dict):
|
|
249
|
+
index = find_index_of_keyword(where.tokens, "AND")
|
|
250
|
+
par = Parenthesis(where.tokens[index-2:index+3])
|
|
251
|
+
where_tokens = where.tokens[:index-2] + [par] + where.tokens[index+3:]
|
|
252
|
+
return where_tokens
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
# for token in where.tokens:
|
|
257
|
+
# if token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
|
|
258
|
+
# continue
|
|
259
|
+
|
|
260
|
+
# if token.ttype is Keyword and token.value.upper() in ('AND', 'OR'):
|
|
261
|
+
# if current_condition:
|
|
262
|
+
# conditions.append(''.join(str(t) for t in current_condition).strip())
|
|
263
|
+
# current_condition = []
|
|
264
|
+
# conditions.append(token.value.upper())
|
|
265
|
+
# else:
|
|
266
|
+
# current_condition.append(token)
|
|
267
|
+
|
|
268
|
+
# if current_condition:
|
|
269
|
+
# conditions.append(''.join(str(t) for t in current_condition).strip())
|
|
270
|
+
|
|
271
|
+
# sorted_conditions = []
|
|
272
|
+
# current_group = []
|
|
273
|
+
# last_connector = ""
|
|
274
|
+
|
|
275
|
+
# if "OR" not in conditions:
|
|
276
|
+
# for condition in conditions:
|
|
277
|
+
# if condition == 'AND':
|
|
278
|
+
# pass
|
|
279
|
+
# else:
|
|
280
|
+
# current_group.append(condition)
|
|
281
|
+
|
|
282
|
+
# if current_group:
|
|
283
|
+
# sorted_conditions.extend(sorted(current_group))
|
|
284
|
+
|
|
285
|
+
# return " AND ".join(sorted_conditions)
|
|
286
|
+
# else:
|
|
287
|
+
# return " ".join(conditions)
|
|
288
|
+
|
|
289
|
+
# where_index = query.upper().find('WHERE')
|
|
290
|
+
|
|
291
|
+
# normalized_query = query[:where_index + 5] + ' ' + ' '.join(sorted_conditions)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def _where_simpleCondition(where, alias_map, baseDict: dict):
|
|
296
|
+
cond = []
|
|
297
|
+
for tok in where.tokens:
|
|
298
|
+
if isinstance(tok, Comparison):
|
|
299
|
+
cond.append(_condition(tok, alias_map, baseDict))
|
|
300
|
+
elif isinstance(tok, Parenthesis):
|
|
301
|
+
for _tok in tok.tokens:
|
|
302
|
+
if isinstance(_tok, Comparison):
|
|
303
|
+
cond.append(_condition(_tok, alias_map, baseDict))
|
|
304
|
+
elif isinstance(_tok, Parenthesis):
|
|
305
|
+
tok = _tok
|
|
306
|
+
return "".join(cond)
|
|
307
|
+
|
|
308
|
+
def _where_sameStrengthKeywords(where, alias_map, baseDict: dict, keyword: str):
|
|
309
|
+
cond = []
|
|
310
|
+
for tok in where.tokens:
|
|
311
|
+
if isinstance(tok, Comparison):
|
|
312
|
+
cond.append(_condition(tok, alias_map, baseDict))
|
|
313
|
+
elif isinstance(tok, Parenthesis):
|
|
314
|
+
for _tok in tok.tokens:
|
|
315
|
+
if isinstance(_tok, Comparison):
|
|
316
|
+
cond.append(_condition(_tok, alias_map, baseDict))
|
|
317
|
+
elif isinstance(_tok, Parenthesis):
|
|
318
|
+
tok = _tok
|
|
319
|
+
cond.sort()
|
|
320
|
+
return keyword.join(cond)
|
|
321
|
+
|
|
322
|
+
def _where_twoConditionsOnTopLevel(where, alias_map, baseDict: dict, keyword, isParanthesis):
|
|
323
|
+
current_condition = []
|
|
324
|
+
for tok in where.tokens:
|
|
325
|
+
if isinstance(tok, Comparison):
|
|
326
|
+
current_condition.append(_condition(tok, alias_map, baseDict))
|
|
327
|
+
elif isinstance(tok, Parenthesis):
|
|
328
|
+
current_condition.append(_where(tok, alias_map, baseDict))
|
|
329
|
+
current_condition.sort()
|
|
330
|
+
if keyword == " AND " or isParanthesis:
|
|
331
|
+
return "(" + keyword.join(current_condition) + ")"
|
|
332
|
+
else:
|
|
333
|
+
return keyword.join(current_condition)
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
# def _where_xx(where, alias_map, baseDict: dict):
|
|
339
|
+
# where_tokens = []
|
|
340
|
+
# for token in where.tokens:
|
|
341
|
+
|
|
342
|
+
# if isinstance(token, Comparison):
|
|
343
|
+
# left, operator, right = [t for t in token.tokens if not t.is_whitespace]
|
|
344
|
+
# left = _identifier(left, alias_map, baseDict)
|
|
345
|
+
# right = _identifier(right, alias_map, baseDict)
|
|
346
|
+
# if left.lower() >= right.lower():
|
|
347
|
+
# left, right = right, left
|
|
348
|
+
# if operator.value == ">":
|
|
349
|
+
# operator = "<"
|
|
350
|
+
# elif operator.value == "<":
|
|
351
|
+
# operator = ">"
|
|
352
|
+
|
|
353
|
+
# where_tokens.append(f"{left.lower()} {operator} {right.lower()}")
|
|
354
|
+
# elif token.ttype is Keyword:
|
|
355
|
+
# where_tokens.append(token.value.upper())
|
|
356
|
+
# elif token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
|
|
357
|
+
# continue
|
|
358
|
+
# else:
|
|
359
|
+
# where_tokens.append(str(token))
|
|
360
|
+
# return " ".join(where_tokens)
|
|
361
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sql_testing_tools
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Description-Content-Type: text/markdown
|
|
5
|
+
Requires-Dist: sqlparse>=0.5.1
|
|
6
|
+
Requires-Dist: requests>=2.32.3
|
|
7
|
+
|
|
8
|
+
A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### [Beta in Development!]
|
|
12
|
+
[](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
|
|
13
|
+
[](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### V0.1.4 + V0.1.5
|
|
18
|
+
- V0.1.5 is just a reupload of V0.1.4
|
|
19
|
+
- Chained conditions (with AND,OR and Paranthesises) in WHERE statement
|
|
20
|
+
|
|
21
|
+
### V0.1.3
|
|
22
|
+
- SELECT: columns with our without table prefix
|
|
23
|
+
- FROM: one or more table from DB; no queries as tables!
|
|
24
|
+
- WHERE: single conditions; no Paranthesises!
|
|
25
|
+
- GROUP BY one or more columns
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
sql_testing_tools/BaseAccess.py,sha256=bx9TxrizEEODLNP9KWZZEHCFCdyYyUGYI_o162ODUaU,5222
|
|
2
|
+
sql_testing_tools/Helper.py,sha256=1MUB2EYcxOx4Gjk_BYlarFNwqPxZrnf1iCREBeZbqa4,7552
|
|
3
|
+
sql_testing_tools/TokenProcessing.py,sha256=-ei7BjmLKJh9-BEVEcWm140-3EyQcuThI4UW6KHPBdM,13587
|
|
4
|
+
sql_testing_tools/__init__.py,sha256=NYoXmLD8NmuU9TnMS46-gXmMutFHAXxuqaDlITZhRfY,52
|
|
5
|
+
sql_testing_tools-0.1.6.dist-info/METADATA,sha256=eOVClW3PYhVYKhJw4MI0Rq1hpCIxUmJaOGo7F6TH5MA,1115
|
|
6
|
+
sql_testing_tools-0.1.6.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
7
|
+
sql_testing_tools-0.1.6.dist-info/top_level.txt,sha256=qwEkmfJ_lYhIwcLXgfU75m0_JavVbOg96fN2zSatN3E,18
|
|
8
|
+
sql_testing_tools-0.1.6.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: sql_testing_tools
|
|
3
|
-
Version: 0.1.4
|
|
4
|
-
Description-Content-Type: text/markdown
|
|
5
|
-
Requires-Dist: sqlparse>=0.5.1
|
|
6
|
-
Requires-Dist: requests>=2.32.3
|
|
7
|
-
|
|
8
|
-
[Beta in Development]
|
|
9
|
-
|
|
10
|
-
A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
sql_testing_tools/BaseAccess.py,sha256=bx9TxrizEEODLNP9KWZZEHCFCdyYyUGYI_o162ODUaU,5222
|
|
2
|
-
sql_testing_tools/Helper.py,sha256=wEun9GAuEP6ibTAHoNnqWqEshWpiT1fJnicVIpwmtJM,16714
|
|
3
|
-
sql_testing_tools/__init__.py,sha256=NYoXmLD8NmuU9TnMS46-gXmMutFHAXxuqaDlITZhRfY,52
|
|
4
|
-
sql_testing_tools-0.1.4.dist-info/METADATA,sha256=FjlvnHWe89Hst7-94EuEhmzxLh5EPuUR17LD4DPwpJE,337
|
|
5
|
-
sql_testing_tools-0.1.4.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
|
|
6
|
-
sql_testing_tools-0.1.4.dist-info/top_level.txt,sha256=qwEkmfJ_lYhIwcLXgfU75m0_JavVbOg96fN2zSatN3E,18
|
|
7
|
-
sql_testing_tools-0.1.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|