sql-testing-tools 0.1.8__py3-none-any.whl → 0.2.0__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.
@@ -1,171 +1,171 @@
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
-
1
+ import sqlite3
2
+ import os
3
+ #from importlib import resources
4
+
5
+
6
+ __db=""
7
+
8
+ def setDBName(path: str):
9
+ global __db
10
+ print("Setting DB to: " + path)
11
+ __db = path
12
+ #with resources.path('sql_testing_tools', path) as db_path:
13
+ # __db = str(db_path)
14
+ # print(__db)
15
+
16
+
17
+ #setDBName("databases/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
171
  return table_dict