sql-testing-tools 0.2.7__tar.gz → 0.2.9__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: sql_testing_tools
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Description-Content-Type: text/markdown
5
5
  Requires-Dist: sqlparse>=0.5.1
6
6
  Requires-Dist: requests>=2.32.3
@@ -136,6 +136,12 @@ res = checkEquality()
136
136
 
137
137
  ### Changelog
138
138
 
139
+ ##### V 0.2.9
140
+ - added NOT support
141
+ - improved Parentheses handling
142
+
143
+ ##### V 0.2.8
144
+ - ignore case in '=' string comparison and convert to LIKE (if no '%', '_' in String)
139
145
 
140
146
  ##### V 0.2.7
141
147
  - Normalize String unequality: ...<>..., ...!=..., ...NOT LIKE... --> NOT...LIKE...
@@ -126,6 +126,12 @@ res = checkEquality()
126
126
 
127
127
  ### Changelog
128
128
 
129
+ ##### V 0.2.9
130
+ - added NOT support
131
+ - improved Parentheses handling
132
+
133
+ ##### V 0.2.8
134
+ - ignore case in '=' string comparison and convert to LIKE (if no '%', '_' in String)
129
135
 
130
136
  ##### V 0.2.7
131
137
  - Normalize String unequality: ...<>..., ...!=..., ...NOT LIKE... --> NOT...LIKE...
@@ -6,7 +6,7 @@ with open("README.md", "r") as fh:
6
6
 
7
7
  setup(
8
8
  name='sql_testing_tools',
9
- version='0.2.7',
9
+ version='0.2.9',
10
10
  packages=find_packages(),
11
11
  install_requires=[
12
12
  'sqlparse>=0.5.1',
@@ -166,7 +166,7 @@ def checkKeywords(startWord:str, endWords:list):
166
166
  end = len(sql)
167
167
 
168
168
  submission = str.strip(sql[start:end])
169
- print("'"+submission+"'")
169
+ #print("'"+submission+"'")
170
170
 
171
171
  start = str.find(sol, startWord)
172
172
  end = -1
@@ -179,7 +179,7 @@ def checkKeywords(startWord:str, endWords:list):
179
179
  end = len(sol)
180
180
 
181
181
  solution = str.strip(sol[start:end])
182
- print("'"+solution+"'")
182
+ #print("'"+solution+"'")
183
183
 
184
184
  if submission == solution:
185
185
  return ""
@@ -1,8 +1,7 @@
1
1
  import sqlparse
2
- from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis
2
+ from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis, Token
3
3
  from sqlparse.tokens import Keyword, DML, Name, Wildcard, Whitespace, Newline
4
4
 
5
-
6
5
  try:
7
6
  import Helper as He
8
7
  except ImportError:
@@ -115,6 +114,25 @@ def _orderby(orderby_, alias_map, baseDict: dict):
115
114
  # orderby_tokens.sort()
116
115
  return ",".join(orderby_tokens)
117
116
 
117
+ def replace_not_with_parenthesis(where_tokens):
118
+ new_tokens = []
119
+ i = 0
120
+ while i < len(where_tokens):
121
+ if where_tokens[i].ttype is Keyword and where_tokens[i].value.upper() == "NOT":
122
+ if i + 1 < len(where_tokens):
123
+ not_token = where_tokens[i]
124
+ next_token = where_tokens[i + 1]
125
+ parenthesis = Parenthesis([not_token, Token(Whitespace, ' '), next_token])
126
+ new_tokens.append(parenthesis)
127
+ i += 2 # Skip the next token as it is already added inside the parenthesis
128
+ else:
129
+ new_tokens.append(where_tokens[i])
130
+ i += 1
131
+ else:
132
+ new_tokens.append(where_tokens[i])
133
+ i += 1
134
+ return new_tokens
135
+
118
136
  def _condition(token, alias_map, baseDict: dict):
119
137
  left, operator, right = [t for t in token.tokens if not t.is_whitespace]
120
138
 
@@ -143,10 +161,14 @@ def _condition(token, alias_map, baseDict: dict):
143
161
  left = _identifier(left, alias_map, baseDict)
144
162
  right = _identifier(right, alias_map, baseDict)
145
163
 
146
- if operator.value in ("!=", "<>"):
147
- return f"NOT {left} LIKE {right}"
148
- if operator.value in ("NOT LIKE"):
149
- return f"NOT {left} LIKE {right}"
164
+ if operator.value.strip() in ("!=", "<>"):
165
+ return f"(NOT {left.lower()} LIKE {right})"
166
+ if operator.value.lower().strip() == "not like":
167
+ return f"(NOT {left.lower()} LIKE {right})"
168
+ if operator.value.strip() == "=" and "%" not in right and "_" not in right:
169
+ return f"{left.lower()} LIKE {right}"
170
+
171
+
150
172
 
151
173
  if flipAllowed and left.lower() >= right.lower():
152
174
  left, right = right, left
@@ -159,7 +181,7 @@ def _condition(token, alias_map, baseDict: dict):
159
181
  elif operator.value == "<=":
160
182
  operator = ">="
161
183
 
162
- return f"{left if leftLiteral else left.lower()} {operator} {right if rightLiteral else right.lower()}"
184
+ return f"{left if leftLiteral else left.lower()} {operator.value.upper()} {right if rightLiteral else right.lower()}"
163
185
 
164
186
  def is_value(token):
165
187
  return token.ttype in (sqlparse.tokens.Token.Literal.Number.Integer,
@@ -249,11 +271,14 @@ def _where(where, alias_map, baseDict: dict):
249
271
 
250
272
  isParanthesis = isinstance(where, Parenthesis)
251
273
 
252
- where_tokens = [token for token in where.tokens if (token.ttype is not Whitespace and token.ttype is not Newline)and token.value != "WHERE"]
274
+ where_tokens = [token for token in where.tokens if (token.ttype is not Whitespace and token.ttype is not Newline) and token.value != "WHERE"]
275
+ where_tokens = replace_not_with_parenthesis(where_tokens)
276
+
277
+ # No parentheses in where_string!
253
278
  where_string = " ".join([str(token) for token in where_tokens])
254
279
 
255
280
  and_count = where_string.upper().count(" AND ")
256
- or_count = where_string.upper().count(" OR ")
281
+ or_count = where_string.upper().count(" OR ")
257
282
 
258
283
  # No AND / OR
259
284
  if and_count + or_count == 0:
@@ -328,6 +353,8 @@ def _where_addBracketsAroundAND(where, alias_map, baseDict: dict):
328
353
  def _where_simpleCondition(where_tokens, alias_map, baseDict: dict):
329
354
  cond = []
330
355
  for tok in where_tokens:
356
+ if tok.ttype == Keyword and tok.value.upper() == "NOT":
357
+ cond.append("NOT ")
331
358
  if isinstance(tok, Comparison):
332
359
  cond.append(_condition(tok, alias_map, baseDict))
333
360
  elif isinstance(tok, Parenthesis):
@@ -342,13 +369,10 @@ def _where_sameStrengthKeywords(where_tokens, alias_map, baseDict: dict, keyword
342
369
  cond = []
343
370
  for tok in where_tokens:
344
371
  if isinstance(tok, Comparison):
345
- cond.append(_condition(tok, alias_map, baseDict))
372
+ cond.append(_where_simpleCondition([tok], alias_map, baseDict))
346
373
  elif isinstance(tok, Parenthesis):
347
- for _tok in tok.tokens:
348
- if isinstance(_tok, Comparison):
349
- cond.append(_condition(_tok, alias_map, baseDict))
350
- elif isinstance(_tok, Parenthesis):
351
- tok = _tok
374
+ isNot = tok.tokens[0].ttype == Keyword and tok.tokens[0].value.upper() == "NOT"
375
+ cond.append(("(" if isNot else "") + _where(tok, alias_map, baseDict) + (")" if isNot else ""))
352
376
  cond.sort()
353
377
  return keyword.join(cond)
354
378
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: sql_testing_tools
3
- Version: 0.2.7
3
+ Version: 0.2.9
4
4
  Description-Content-Type: text/markdown
5
5
  Requires-Dist: sqlparse>=0.5.1
6
6
  Requires-Dist: requests>=2.32.3
@@ -136,6 +136,12 @@ res = checkEquality()
136
136
 
137
137
  ### Changelog
138
138
 
139
+ ##### V 0.2.9
140
+ - added NOT support
141
+ - improved Parentheses handling
142
+
143
+ ##### V 0.2.8
144
+ - ignore case in '=' string comparison and convert to LIKE (if no '%', '_' in String)
139
145
 
140
146
  ##### V 0.2.7
141
147
  - Normalize String unequality: ...<>..., ...!=..., ...NOT LIKE... --> NOT...LIKE...