sql-testing-tools 0.1.8__tar.gz → 0.1.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,29 +1,35 @@
1
- Metadata-Version: 2.1
2
- Name: sql_testing_tools
3
- Version: 0.1.8
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
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
13
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
14
-
15
- ### V 0.1.8
16
- - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
17
-
18
- ### V 0.1.6 + V 0.1.7
19
- - Fixed import error to ensure imports working in different environemnts
20
-
21
- ### V 0.1.4 + V 0.1.5
22
- - V0.1.5 is just a reupload of V 0.1.4
23
- - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
24
-
25
- ### V 0.1.3
26
- - SELECT: columns with our without table prefix
27
- - FROM: one or more table from DB; no queries as tables!
28
- - WHERE: single conditions; no Paranthesises!
29
- - GROUP BY one or more columns
1
+ Metadata-Version: 2.2
2
+ Name: sql_testing_tools
3
+ Version: 0.1.9
4
+ Description-Content-Type: text/markdown
5
+ Requires-Dist: sqlparse>=0.5.1
6
+ Requires-Dist: requests>=2.32.3
7
+ Dynamic: description
8
+ Dynamic: description-content-type
9
+ Dynamic: requires-dist
10
+
11
+ A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
12
+
13
+
14
+ ### [Beta in Development!]
15
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
16
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
17
+
18
+ ### V 0.1.9
19
+ - Support GROUP BY
20
+
21
+ ### V 0.1.8
22
+ - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
23
+
24
+ ### V 0.1.6 + V 0.1.7
25
+ - Fixed import error to ensure imports working in different environemnts
26
+
27
+ ### V 0.1.4 + V 0.1.5
28
+ - V0.1.5 is just a reupload of V 0.1.4
29
+ - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
30
+
31
+ ### V 0.1.3
32
+ - SELECT: columns with our without table prefix
33
+ - FROM: one or more table from DB; no queries as tables!
34
+ - WHERE: single conditions; no Paranthesises!
35
+ - GROUP BY one or more columns
@@ -0,0 +1,25 @@
1
+ A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
2
+
3
+
4
+ ### [Beta in Development!]
5
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
6
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
7
+
8
+ ### V 0.1.9
9
+ - Support GROUP BY
10
+
11
+ ### V 0.1.8
12
+ - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
13
+
14
+ ### V 0.1.6 + V 0.1.7
15
+ - Fixed import error to ensure imports working in different environemnts
16
+
17
+ ### V 0.1.4 + V 0.1.5
18
+ - V0.1.5 is just a reupload of V 0.1.4
19
+ - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
20
+
21
+ ### V 0.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
@@ -1,4 +1,4 @@
1
- [egg_info]
2
- tag_build =
3
- tag_date = 0
4
-
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -1,17 +1,17 @@
1
- from setuptools import setup, find_packages
2
-
3
- with open("sql_testing_tools/README.md", "r") as fh:
4
- description = fh.read()
5
-
6
-
7
- setup(
8
- name='sql_testing_tools',
9
- version='0.1.8',
10
- packages=find_packages(),
11
- install_requires=[
12
- 'sqlparse>=0.5.1',
13
- 'requests>=2.32.3'
14
- ],
15
- long_description=description,
16
- long_description_content_type="text/markdown"
1
+ from setuptools import setup, find_packages
2
+
3
+ with open("README.md", "r") as fh:
4
+ description = fh.read()
5
+
6
+
7
+ setup(
8
+ name='sql_testing_tools',
9
+ version='0.1.9',
10
+ packages=find_packages(),
11
+ install_requires=[
12
+ 'sqlparse>=0.5.1',
13
+ 'requests>=2.32.3'
14
+ ],
15
+ long_description=description,
16
+ long_description_content_type="text/markdown"
17
17
  )
@@ -1,29 +1,35 @@
1
- Metadata-Version: 2.1
2
- Name: sql_testing_tools
3
- Version: 0.1.8
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
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
13
- [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
14
-
15
- ### V 0.1.8
16
- - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
17
-
18
- ### V 0.1.6 + V 0.1.7
19
- - Fixed import error to ensure imports working in different environemnts
20
-
21
- ### V 0.1.4 + V 0.1.5
22
- - V0.1.5 is just a reupload of V 0.1.4
23
- - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
24
-
25
- ### V 0.1.3
26
- - SELECT: columns with our without table prefix
27
- - FROM: one or more table from DB; no queries as tables!
28
- - WHERE: single conditions; no Paranthesises!
29
- - GROUP BY one or more columns
1
+ Metadata-Version: 2.2
2
+ Name: sql_testing_tools
3
+ Version: 0.1.9
4
+ Description-Content-Type: text/markdown
5
+ Requires-Dist: sqlparse>=0.5.1
6
+ Requires-Dist: requests>=2.32.3
7
+ Dynamic: description
8
+ Dynamic: description-content-type
9
+ Dynamic: requires-dist
10
+
11
+ A library that normalizes simple SQL queries and compares them first by equality of the normalized string and then using the cosette API.
12
+
13
+
14
+ ### [Beta in Development!]
15
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-build.yml)
16
+ [![Build and Test](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml/badge.svg)](https://github.com/ValentinHerrmann/sql_testing_tools/actions/workflows/python-unittests.yml)
17
+
18
+ ### V 0.1.9
19
+ - Support GROUP BY
20
+
21
+ ### V 0.1.8
22
+ - Fixed linebreak problems: Linebreaks are now converted into whitespaces before parsing where tokens
23
+
24
+ ### V 0.1.6 + V 0.1.7
25
+ - Fixed import error to ensure imports working in different environemnts
26
+
27
+ ### V 0.1.4 + V 0.1.5
28
+ - V0.1.5 is just a reupload of V 0.1.4
29
+ - Chained conditions (with AND,OR and Paranthesises) in WHERE statement
30
+
31
+ ### V 0.1.3
32
+ - SELECT: columns with our without table prefix
33
+ - FROM: one or more table from DB; no queries as tables!
34
+ - WHERE: single conditions; no Paranthesises!
35
+ - GROUP BY one or more columns
@@ -1,9 +1,5 @@
1
1
  README.md
2
2
  setup.py
3
- sql_testing_tools/BaseAccess.py
4
- sql_testing_tools/Helper.py
5
- sql_testing_tools/TokenProcessing.py
6
- sql_testing_tools/__init__.py
7
3
  sql_testing_tools.egg-info/PKG-INFO
8
4
  sql_testing_tools.egg-info/SOURCES.txt
9
5
  sql_testing_tools.egg-info/dependency_links.txt
@@ -1,3 +0,0 @@
1
- **[Beta in Development!]**
2
-
3
- This project is a wrapper for packaging [SQL_Testing_Tools](https://github.com/ValentinHerrmann/sql_testing_tools) into a Wheel and upload it to PyPi.
@@ -1,171 +0,0 @@
1
- import sqlite3
2
- import os
3
- #from importlib import resources
4
-
5
-
6
- __db=""
7
-
8
- def setDBName(name: str):
9
- global __db
10
- print("Setting DB to: " + name)
11
- __db = "databases/"+name
12
- #with resources.path('sql_testing_tools', name) as db_path:
13
- # __db = str(db_path)
14
- # print(__db)
15
-
16
-
17
- setDBName("bayern.db")
18
-
19
- def run(sql: str):
20
- con = sqlite3.connect(__db)
21
- cur = con.cursor()
22
- cur.execute(sql)
23
- con.commit()
24
- return cur
25
-
26
- def runFromFile(path: str):
27
- sql = getSQLFromFile(path)
28
- if not sql:
29
- raise Exception("\nSQL-Datei ist leer. Aufgabe wurde noch nicht bearbeitet.")
30
-
31
-
32
- if sql.lower().find("drop") != -1:
33
- raise Exception("Guter Versuch, aber die Datenbank wird bei jedem Upload zurückgesetzt ;)")
34
-
35
- try:
36
- res = run(sql)
37
- except Exception as e:
38
- raise Exception(f"\n\nSyntax-Fehler in der SQL-Abfrage:\n{str(e)}")
39
-
40
- headers = [h[0] for h in res.description] if res.description else []
41
- rows = res.fetchall() if res else []
42
-
43
- return headers, rows
44
-
45
-
46
- def runAndGetStringTable_fromFile(path: str, count: int = 5, maxLineLength: int = 85):
47
- try:
48
- headers, rows = runFromFile(path)
49
- resultCount = len(rows)
50
- rows = rows[:count]
51
- s = ""
52
-
53
- matrix = [] #[[]*(len(rows)+1)]*len(headers)
54
-
55
- for col in range(len(headers)):
56
- matrix.append([])
57
- matrix[-1].append(headers[col])
58
-
59
- if rows is not None:
60
- for col in range(len(matrix)):
61
- for row in range(0, len(rows)):
62
- if len(rows[row]) > col:
63
- matrix[col].append(rows[row][col])
64
-
65
- for col in range(len(matrix)):
66
- maxLength = max([len(str(x)) for x in matrix[col]])
67
-
68
- for row in range(0, len(matrix[col])):
69
- matrix[col][row] = str(matrix[col][row]).ljust(maxLength)
70
-
71
- normalizedRows = []
72
- spacing = " "
73
- spacingLength = len(spacing) * (len(matrix[0]) - 1)
74
-
75
- lineLength = sum([len(x[0]) for x in matrix]) + spacingLength
76
-
77
-
78
-
79
- if lineLength > maxLineLength:
80
- valLength = lineLength - spacingLength
81
- maxColLength = int(maxLineLength/len(matrix))
82
-
83
- for col in range(len(matrix)):
84
- for row in range(0, len(matrix[col])):
85
- cutContent = len(matrix[col][row].strip()) > maxColLength
86
- if cutContent:
87
- matrix[col][row] = matrix[col][row][:maxColLength-2] + ".."
88
- else:
89
- matrix[col][row] = matrix[col][row][:maxColLength]
90
-
91
-
92
- for row in range(min(count+1, len(matrix[0]))):
93
- normalizedRows.append(spacing.join([str(matrix[col][row]) for col in range(len(matrix))]))
94
- normalizedRows.insert(1, len(normalizedRows[0])*"-")
95
-
96
- s = "\n".join(normalizedRows[:count+2])
97
- s += "\n" + normalizedRows[1]
98
-
99
- if (resultCount > count):
100
- s += "\n... " + str(resultCount - count) + " weitere Zeilen"
101
- else:
102
- s += "\n... keine weiteren Zeilen"
103
-
104
- #longestLineInS = 85 # max([len(x.replace("\t", " ")) for x in s.split("\n")])
105
-
106
- #s = "-" * longestLineInS + "\n" + s + "\n" + "-" * longestLineInS
107
- s = "\n\nDiese Meldung sagt nichts über die Korrektheit der Abgabe aus!\nDie ersten " + str(
108
- count) + " Zeilen des Ergebnisses der SQL-Abfrage:\n\n" + s
109
-
110
- return s.replace("None", "NULL")
111
- except Exception as ex:
112
- raise ex
113
-
114
- def getSQLFromFile(path: str):
115
- try:
116
- with open(path, "r") as f:
117
- s = f.read()
118
- if not s:
119
- raise Exception("\nSQL-Datei ist leer. Aufgabe wurde noch nicht bearbeitet.")
120
- return s
121
- except FileNotFoundError:
122
- raise Exception(f"\nSQL-Datei nicht gefunden! Überprüfe, dass der Name korrekt ist ({path.split('/')[-1]}) und die Datei nicht gelöscht oder in einen Unterordner verschoben wurde.")
123
-
124
-
125
-
126
- def getWorkingDir():
127
- return os.getcwd()
128
-
129
-
130
- def getWorkingDirFiles():
131
- return os.listdir()
132
-
133
- def mapDatabaseTypes(t):
134
- # Map SQLite types to Python types
135
- type_mapping = {
136
- "INTEGER": "int",
137
- "TEXT": "str",
138
- "VARCHAR": "str",
139
- "REAL": "float",
140
- "BLOB": "bytes",
141
- "NUMERIC": "float",
142
- "DOUBLE": "float"
143
- }
144
- t = t.upper()
145
- t = type_mapping.get(t, t)
146
-
147
- if "INT" in t:
148
- t = "int"
149
- elif "VARCHAR" in t:
150
- t = "str"
151
- elif "CHAR" in t:
152
- t = "str"
153
-
154
- return t
155
-
156
-
157
-
158
-
159
- def getTableDict():
160
- res = run("SELECT name FROM sqlite_master WHERE type='table';")
161
- tables = res.fetchall()
162
-
163
- table_dict = {}
164
- for table in tables:
165
- res = run(f"PRAGMA table_info({table[0]})")
166
- r = res.fetchall()
167
- if table[0] == "sqlite_sequence":
168
- continue
169
- table_dict[table[0]] = [[c[1], mapDatabaseTypes(c[2])] for c in r]
170
-
171
- return table_dict
@@ -1,210 +0,0 @@
1
- try:
2
- import BaseAccess as Ba
3
- import TokenProcessing as Tp
4
- except ImportError:
5
- from . import BaseAccess as Ba
6
- from . import TokenProcessing as Tp
7
-
8
- import requests
9
- import sqlparse
10
-
11
- from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis
12
- from sqlparse.tokens import Keyword, DML, Name, Wildcard
13
-
14
-
15
-
16
- def normalizeSQLQuery(query, baseDict):
17
- try:
18
- query = query.replace("\"", "'")
19
- parsed = sqlparse.parse(query)[0]
20
- parsed.tokens = [token for token in parsed.tokens if not token.is_whitespace]
21
- except Exception as e:
22
- raise Exception(f"\nSyntax-Fehler in der SQL-Abfrage.")
23
-
24
- formatted_query = []
25
- alias_map = {}
26
-
27
- # First pass to process FROM clause and populate alias_map
28
- for token in parsed.tokens:
29
- if token.is_whitespace:
30
- continue
31
- elif token.ttype is Keyword and token.value.upper() == 'FROM':
32
- formatted_query.append('FROM')
33
- pass
34
- elif isinstance(token, Identifier) and formatted_query and formatted_query[-1] == 'FROM':
35
- il = IdentifierList([token])
36
- formatted_query.append(Tp._from(il, alias_map, baseDict))
37
- Tp._from(il, alias_map, baseDict)
38
- elif isinstance(token, IdentifierList) and formatted_query and formatted_query[-1] == 'FROM':
39
- formatted_query.append(Tp._from(token, alias_map, baseDict))
40
- Tp._from(token, alias_map, baseDict)
41
-
42
- formatted_query = []
43
-
44
- # Second pass to process SELECT and WHERE clauses
45
- for token in parsed.tokens:
46
- if token.is_whitespace:
47
- continue
48
- elif token.ttype is DML and token.value.upper() == 'SELECT':
49
- formatted_query.append('SELECT')
50
- elif token.ttype is Keyword and token.value.upper() == 'FROM':
51
- formatted_query.append('FROM')
52
- elif token.ttype is Keyword and token.value.upper() == 'GROUP BY':
53
- formatted_query.append('GROUP BY')
54
- elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'FROM':
55
- formatted_query.append(Tp._from(token, alias_map, baseDict))
56
- elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'GROUP BY':
57
- formatted_query.append(Tp._groupby(token, alias_map, baseDict))
58
- elif isinstance(token, Where):
59
- formatted_query.append('WHERE')
60
- formatted_query.append(Tp._where(token, alias_map, baseDict))
61
- elif formatted_query and formatted_query[-1] == 'SELECT' and (isinstance(token, IdentifierList) or isinstance(token, Function) or isinstance(token, Identifier)):
62
- if isinstance(token, Function):
63
- token = IdentifierList([token])
64
- formatted_query.append(Tp._select(token, alias_map, baseDict))
65
- else:
66
- formatted_query.append(str(token))
67
-
68
- return " ".join(formatted_query)
69
-
70
-
71
- def findTableForColumn(data_dict, target_value, relevantTables):
72
- l = []
73
- for key, value_list in data_dict.items():
74
- if key.lower() in relevantTables:
75
- for sublist in value_list:
76
- if sublist and sublist[0].lower() == target_value.lower():
77
- l.append(key)
78
- if len(l) == 0:
79
- for key, value_list in data_dict.items():
80
- for sublist in value_list:
81
- if sublist and sublist[0].lower() == target_value.lower():
82
- l.append(key)
83
- return l
84
-
85
-
86
- def getTableScheme(table_name: str, tableDict: dict):
87
- tab = tableDict[table_name]
88
-
89
- # Format the schema
90
- schema = "(" + ",".join([f"{col[0]}:{col[1]}" for col in tab]) + ")"
91
- return schema
92
-
93
- def getCosetteKeyFromFile():
94
- try:
95
- with open("cosette_apikey.txt", "r") as f:
96
- return f.read()
97
- except FileNotFoundError:
98
- return "NOKEY"
99
-
100
- def buildAndSendCosetteRequest(baseDict, sql, sol):
101
-
102
- err = ""
103
- for i in range(2):
104
- try:
105
- apiKey=getCosetteKeyFromFile()
106
-
107
-
108
- schema = ""
109
- for tab in baseDict.keys():
110
- schema += f"schema sch{tab}{getTableScheme(tab, baseDict)};\n"
111
- for tab in baseDict.keys():
112
- schema += f"table {tab}(sch{tab});\n"
113
-
114
- q1 = "query q1\n`"+sql+"`;\n"
115
- q2 = "query q2\n`"+sol+"`;\n"
116
-
117
- cosette = "-- random Kommentar\n" + schema + q1 + q2 + "verify q1 q2;\n"
118
- print(cosette)
119
-
120
- r = requests.post("https://demo.cosette.cs.washington.edu/solve",data={"api_key": apiKey, "query": cosette}, verify=False)
121
-
122
- print(r.text)
123
- return (r.json()['result'],r.text)
124
- #return r.json()['result']
125
-
126
- except Exception as e:
127
- err = str(e)
128
- return ("ERR", err)
129
-
130
-
131
- def checkColumns(sqlPath, solPath):
132
- bd = Ba.getTableDict()
133
- sql = normalizeSQLQuery(Ba.getSQLFromFile(sqlPath), bd)
134
- sol = normalizeSQLQuery(Ba.getSQLFromFile(solPath), bd)
135
-
136
- if("SELECT" in sql and "FROM" in sql):
137
- start = str.find(sql, "SELECT")
138
- end = str.find(sql, "FROM")
139
- submission = str.strip(sql[start:end])
140
- print("'"+submission+"'")
141
-
142
- start = str.find(sol, "SELECT")
143
- end = str.find(sol, "FROM")
144
- solution = str.strip(sol[start:end])
145
- print("'"+solution+"'")
146
-
147
- if submission == solution:
148
- return ""
149
- return "Ausgegebene Spalten sind nicht korrekt (oder nicht automatisch überprüfbar)."
150
-
151
-
152
- def checkTables(sqlPath, solPath):
153
- bd = Ba.getTableDict()
154
- sql = normalizeSQLQuery(Ba.getSQLFromFile(sqlPath), bd)
155
- sol = normalizeSQLQuery(Ba.getSQLFromFile(solPath), bd)
156
-
157
- if("SELECT" in sql and "FROM" in sql):
158
- endFromKeywords = ["WHERE", "GROUP", "ORDER", "LIMIT", ";"]
159
-
160
- start = str.find(sql, "FROM")
161
- end = -1
162
-
163
-
164
- for keyword in endFromKeywords:
165
- if(str.find(sql, keyword) != -1):
166
- end = str.find(sql, keyword)
167
- break
168
- if(end == -1):
169
- end = len(sql)
170
-
171
- submission = str.strip(sql[start:end])
172
- print("'"+submission+"'")
173
-
174
- start = str.find(sol, "FROM")
175
- end = -1
176
-
177
- for keyword in endFromKeywords:
178
- if(str.find(sol, keyword) != -1):
179
- end = str.find(sol, keyword)
180
- break
181
- if(end == -1):
182
- end = len(sol)
183
-
184
- solution = str.strip(sol[start:end])
185
- print("'"+solution+"'")
186
-
187
- if submission == solution:
188
- return ""
189
- return "Verwendete Tabellen sind nicht korrekt (oder nicht automatisch überprüfbar)."
190
-
191
-
192
- def checkEquality(sqlPath, solPath):
193
- bd = Ba.getTableDict()
194
- sql = normalizeSQLQuery(Ba.getSQLFromFile(sqlPath), bd)
195
- sol = normalizeSQLQuery(Ba.getSQLFromFile(solPath), bd)
196
- if(sql=='' or sol==''):
197
- return "\n\nSQL-Datei ist leer. Aufgabe wurde noch nicht bearbeitet."
198
-
199
- if(sql==sol):
200
- return ""
201
-
202
- result = buildAndSendCosetteRequest(bd, sql, sol)
203
-
204
- if(result[0] == "ERR"):
205
- return "\n\nFehler bei der automatischen Überprüfung der Abgabe. Es kann keine Aussage über die Korrektheit der Abgabe getroffen werden."
206
- elif(result[0] != "EQ"):
207
- return "\n\nDie Abgabe stimmt nicht mit der Musterlösung überein."
208
- return ""
209
-
210
-
@@ -1,359 +0,0 @@
1
- import sqlparse
2
- from sqlparse.sql import Identifier, IdentifierList, Where, Comparison, Function, Parenthesis
3
- from sqlparse.tokens import Keyword, DML, Name, Wildcard, Whitespace, Newline
4
-
5
- try:
6
- import Helper as He
7
- except ImportError:
8
- from . import Helper as He
9
-
10
-
11
- #####################################
12
- #####################################
13
-
14
- def _identifier(identifier, alias_map, baseDict: dict):
15
- if isinstance(identifier, Identifier):
16
- if identifier.get_real_name() and identifier.get_parent_name() and identifier.get_parent_name().lower() in alias_map.keys():
17
- return f"{alias_map[identifier.get_parent_name().lower()].lower()}.{identifier.get_real_name().lower()}"
18
- elif identifier.get_real_name():
19
- tables = He.findTableForColumn(baseDict, identifier.get_real_name(), alias_map.keys())
20
- if len(tables) == 1:
21
- alias_map[tables[0].lower()] = tables[0]
22
- return f"{tables[0].lower()}.{identifier.get_real_name().lower()}"
23
- else:
24
- return f"{identifier.get_real_name().lower()}"
25
- return str(identifier)
26
-
27
-
28
- #####################################
29
- #####################################
30
-
31
-
32
- def _select(select, alias_map, baseDict: dict, insideFunction=False):
33
- select_tokens = []
34
- for token in select.tokens:
35
- if isinstance(token, IdentifierList):
36
- for identifier in token.get_identifiers():
37
- select_tokens.append(f"{_identifier(identifier, alias_map, baseDict).lower()} as {identifier.get_real_name().lower()}")
38
- elif isinstance(token, Identifier):
39
- if insideFunction:
40
- select_tokens.append(_identifier(token, alias_map, baseDict).lower())
41
- else:
42
- select_tokens.append(f"{_identifier(token, alias_map, baseDict).lower()} as {token.get_real_name().lower()}")
43
- elif isinstance(token, Function):
44
- #select_tokens.append(f"{token.get_name().lower()}({_identifier(token.get_parameters(), alias_map, baseDict).lower()})")
45
- for par in token.tokens:
46
- if isinstance(par, Identifier):
47
- select_tokens.append(par.get_name().lower()+"(")
48
- if isinstance(par, Parenthesis):
49
- select_tokens[-1] += _select(par, alias_map, baseDict, True)+") as funcResult"
50
- elif token.ttype is Wildcard:
51
- select_tokens.append("*")
52
- else:
53
- continue
54
- select_tokens.sort()
55
- return ",".join(select_tokens)
56
-
57
-
58
- #####################################
59
- #####################################
60
-
61
-
62
- def _from(from_, alias_map, baseDict: dict):
63
- from_tokens = []
64
- if hasattr(from_, 'tokens'):
65
- for token in from_.tokens:
66
- if isinstance(token, IdentifierList):
67
- for identifier in token.get_identifiers():
68
- alias_map[identifier.get_real_name().lower()] = identifier.get_alias() or identifier.get_real_name()
69
- from_tokens.append(f"{identifier.get_real_name().lower()} {alias_map[identifier.get_real_name().lower()].lower()}")
70
- elif isinstance(token, Identifier):
71
- alias_map[token.get_real_name().lower()] = token.get_alias() or token.get_real_name()
72
- from_tokens.append(f"{token.get_real_name().lower()} {alias_map[token.get_real_name().lower()].lower()}")
73
- elif token.ttype is not None and token.ttype is Name:
74
- alias_map[token.value.lower()] = token.value.lower()
75
- from_tokens.append(f"{token.value.lower()} {alias_map[token.value.lower()].lower()}")
76
- else:
77
- continue
78
- from_tokens.sort()
79
- return ",".join(from_tokens)
80
-
81
-
82
- #####################################
83
- #####################################
84
-
85
-
86
- def _groupby(groupby_, alias_map, baseDict: dict):
87
- groupby_tokens = []
88
- if(isinstance(groupby_, Identifier)):
89
- groupby_tokens.append(_identifier(groupby_, alias_map, baseDict).lower())
90
- elif isinstance(groupby_, IdentifierList):
91
- for identifier in groupby_.get_identifiers():
92
- groupby_tokens.append(_identifier(identifier, alias_map, baseDict).lower())
93
- groupby_tokens.sort()
94
- return ",".join(groupby_tokens)
95
-
96
- def _condition(token, alias_map, baseDict: dict):
97
- left, operator, right = [t for t in token.tokens if not t.is_whitespace]
98
-
99
- flipAllowed = True
100
- leftLiteral = False
101
- rightLiteral = False
102
-
103
- if is_value(right):
104
- flipAllowed = False
105
- rightLiteral = True
106
-
107
- if is_value(left):
108
- left, right = right, left
109
- if operator.value == ">":
110
- operator.value = "<"
111
- elif operator.value == "<":
112
- operator.value = ">"
113
- leftLiteral = rightLiteral
114
- rightLiteral = True
115
- flipAllowed = False
116
-
117
- left = _identifier(left, alias_map, baseDict)
118
- right = _identifier(right, alias_map, baseDict)
119
-
120
- if flipAllowed and left.lower() >= right.lower():
121
- left, right = right, left
122
- if operator.value == ">":
123
- operator = "<"
124
- elif operator.value == "<":
125
- operator = ">"
126
-
127
- return f"{left if leftLiteral else left.lower()} {operator} {right if rightLiteral else right.lower()}"
128
-
129
- def is_value(token):
130
- return token.ttype in (sqlparse.tokens.Token.Literal.Number.Integer,
131
- sqlparse.tokens.Token.Literal.Number.Float,
132
- sqlparse.tokens.Token.Literal.String.Single,
133
- sqlparse.tokens.Token.Literal.String.Symbol)
134
-
135
-
136
-
137
- #####################################
138
- #####################################
139
-
140
-
141
- def _paranthesis(parenthesis, alias_map, baseDict: dict):
142
- toks = []
143
-
144
- bracketsRequired = True
145
- closeBracketsAfterNext = False
146
-
147
- and_count = sum(1 for token in parenthesis.tokens if token.ttype is Keyword and token.value == "AND") # Zähle "AND"-Tokens
148
- or_count = sum(1 for token in parenthesis.tokens if token.ttype is Keyword and token.value == "OR") # Zähle "AND"-Tokens
149
-
150
-
151
- if(and_count==1 and or_count == 0):
152
- val = _2element_par(parenthesis, alias_map, baseDict, "AND")
153
- return val
154
- elif(and_count==0 and or_count == 1):
155
- val = _2element_par(parenthesis, alias_map, baseDict, "OR")
156
- return val
157
-
158
-
159
-
160
-
161
-
162
-
163
- for token in parenthesis.tokens:
164
- if isinstance(token, Comparison):
165
- toks.append(_condition(token, alias_map, baseDict))
166
- elif isinstance(token, Parenthesis):
167
- toks.append(_paranthesis(token, alias_map, baseDict))
168
-
169
- elif token.ttype is Keyword: #AND,OR
170
- if token.value == "AND":
171
- pass
172
- elif token.value == "OR":
173
- pass
174
- elif token.value == "NOT":
175
- pass
176
-
177
- elif token.value == '(':
178
- toks.append(token.value)
179
- else:
180
- continue
181
- x = parenthesis.flatten()
182
- return " ".join(toks)
183
-
184
-
185
- def _2element_par(parenthesis, alias_map, baseDict: dict, keyword):
186
- toks = []
187
-
188
- for token in parenthesis.tokens:
189
- if isinstance(token, Comparison):
190
- toks.append(_condition(token, alias_map, baseDict))
191
- elif isinstance(token, Parenthesis):
192
- toks.append(_paranthesis(token, alias_map, baseDict))
193
- toks = toks.sort()
194
- fill = (" "+keyword+" ")
195
- return fill.join(toks)
196
-
197
-
198
- def count_keywordValues(tokens, values):
199
- return len([token for token in tokens if token.ttype is Keyword and token.value.upper() in values])
200
-
201
-
202
- def find_index_of_keyword(tokens, keyword):
203
- for index, token in enumerate(tokens):
204
- if token.ttype is Keyword and token.value.upper() == keyword:
205
- return index
206
- return -1 # Wenn kein AND-Token gefunden wird
207
-
208
-
209
- def _where(where, alias_map, baseDict: dict):
210
- conditions = []
211
- current_condition = []
212
-
213
-
214
-
215
- isParanthesis = isinstance(where, Parenthesis)
216
-
217
- where_tokens = [token for token in where.tokens if (token.ttype is not Whitespace and token.ttype is not Newline)and token.value != "WHERE"]
218
- where_string = " ".join([str(token) for token in where_tokens])
219
-
220
- and_count = where_string.upper().count(" AND ")
221
- or_count = where_string.upper().count(" OR ")
222
-
223
- # No AND / OR
224
- if and_count + or_count == 0:
225
- return _where_simpleCondition(where, alias_map, baseDict)
226
-
227
- # only ANDs / only ORs
228
- elif and_count == 0 or or_count == 0:
229
- return _where_sameStrengthKeywords(where_tokens, alias_map, baseDict, " AND " if and_count > 0 else " OR ")
230
-
231
- # ANDs and ORs
232
-
233
-
234
- # count keywords on top level
235
- top_and_count = count_keywordValues(where_tokens, ["AND"])
236
- top_or_count = count_keywordValues(where_tokens, ["OR"])
237
-
238
- # two conditions on top level
239
- if top_and_count + top_or_count == 1:
240
- keyword = " AND " if top_and_count == 1 else " OR "
241
- return _where_twoConditionsOnTopLevel(where, alias_map, baseDict, keyword, isParanthesis)
242
- else:
243
- where_tokens = _where_addBracketsAroundAND(where, alias_map, baseDict)
244
- return _where(Where(where_tokens), alias_map, baseDict)
245
-
246
- def _where_addBracketsAroundAND(where, alias_map, baseDict: dict):
247
- index = find_index_of_keyword(where.tokens, "AND")
248
- par = Parenthesis(where.tokens[index-2:index+3])
249
- where_tokens = where.tokens[:index-2] + [par] + where.tokens[index+3:]
250
- return where_tokens
251
-
252
-
253
-
254
- # for token in where.tokens:
255
- # if token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
256
- # continue
257
-
258
- # if token.ttype is Keyword and token.value.upper() in ('AND', 'OR'):
259
- # if current_condition:
260
- # conditions.append(''.join(str(t) for t in current_condition).strip())
261
- # current_condition = []
262
- # conditions.append(token.value.upper())
263
- # else:
264
- # current_condition.append(token)
265
-
266
- # if current_condition:
267
- # conditions.append(''.join(str(t) for t in current_condition).strip())
268
-
269
- # sorted_conditions = []
270
- # current_group = []
271
- # last_connector = ""
272
-
273
- # if "OR" not in conditions:
274
- # for condition in conditions:
275
- # if condition == 'AND':
276
- # pass
277
- # else:
278
- # current_group.append(condition)
279
-
280
- # if current_group:
281
- # sorted_conditions.extend(sorted(current_group))
282
-
283
- # return " AND ".join(sorted_conditions)
284
- # else:
285
- # return " ".join(conditions)
286
-
287
- # where_index = query.upper().find('WHERE')
288
-
289
- # normalized_query = query[:where_index + 5] + ' ' + ' '.join(sorted_conditions)
290
-
291
-
292
-
293
- def _where_simpleCondition(where_tokens, alias_map, baseDict: dict):
294
- cond = []
295
- for tok in where_tokens:
296
- if isinstance(tok, Comparison):
297
- cond.append(_condition(tok, alias_map, baseDict))
298
- elif isinstance(tok, Parenthesis):
299
- for _tok in tok.tokens:
300
- if isinstance(_tok, Comparison):
301
- cond.append(_condition(_tok, alias_map, baseDict))
302
- elif isinstance(_tok, Parenthesis):
303
- tok = _tok
304
- return "".join(cond)
305
-
306
- def _where_sameStrengthKeywords(where_tokens, alias_map, baseDict: dict, keyword: str):
307
- cond = []
308
- for tok in where_tokens:
309
- if isinstance(tok, Comparison):
310
- cond.append(_condition(tok, alias_map, baseDict))
311
- elif isinstance(tok, Parenthesis):
312
- for _tok in tok.tokens:
313
- if isinstance(_tok, Comparison):
314
- cond.append(_condition(_tok, alias_map, baseDict))
315
- elif isinstance(_tok, Parenthesis):
316
- tok = _tok
317
- cond.sort()
318
- return keyword.join(cond)
319
-
320
- def _where_twoConditionsOnTopLevel(where, alias_map, baseDict: dict, keyword, isParanthesis):
321
- current_condition = []
322
- for tok in where.tokens:
323
- if isinstance(tok, Comparison):
324
- current_condition.append(_condition(tok, alias_map, baseDict))
325
- elif isinstance(tok, Parenthesis):
326
- current_condition.append(_where(tok, alias_map, baseDict))
327
- current_condition.sort()
328
- if keyword == " AND " or isParanthesis:
329
- return "(" + keyword.join(current_condition) + ")"
330
- else:
331
- return keyword.join(current_condition)
332
-
333
-
334
-
335
-
336
- # def _where_xx(where, alias_map, baseDict: dict):
337
- # where_tokens = []
338
- # for token in where.tokens:
339
-
340
- # if isinstance(token, Comparison):
341
- # left, operator, right = [t for t in token.tokens if not t.is_whitespace]
342
- # left = _identifier(left, alias_map, baseDict)
343
- # right = _identifier(right, alias_map, baseDict)
344
- # if left.lower() >= right.lower():
345
- # left, right = right, left
346
- # if operator.value == ">":
347
- # operator = "<"
348
- # elif operator.value == "<":
349
- # operator = ">"
350
-
351
- # where_tokens.append(f"{left.lower()} {operator} {right.lower()}")
352
- # elif token.ttype is Keyword:
353
- # where_tokens.append(token.value.upper())
354
- # elif token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
355
- # continue
356
- # else:
357
- # where_tokens.append(str(token))
358
- # return " ".join(where_tokens)
359
-
@@ -1,3 +0,0 @@
1
- from .BaseAccess import *
2
- from .Helper import *
3
-
@@ -1 +0,0 @@
1
- sql_testing_tools