-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDatabaseHandlerAPI.py
152 lines (105 loc) · 4.04 KB
/
DatabaseHandlerAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import sqlite3
import os
import shutil
def create_database():
if os.path.exists("_temp_.db"):
os.remove("_temp_.db")
conn = sqlite3.connect("_temp_.db")
def create_table(name: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"""CREATE TABLE \"{name}\" (
script_name text,
script_path text,
venv text,
venv_path text,
cmd text,
thumbnail_path text,
latest_script_name text)""")
conn.commit()
conn.close()
def add_script(table: str, script_name: str, script_path: str, venv: str, latest_script_name: str, venv_path="", cmd="",thumbnail_path="icons/default_thumbnail.jpg"):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"""INSERT INTO \"{table}\" (script_name, script_path, venv, venv_path, thumbnail_path, cmd, latest_script_name)
VALUES (?, ?, ?, ?, ?, ?, ?)""", (script_name, script_path, venv, venv_path, thumbnail_path, cmd, latest_script_name))
conn.commit()
conn.close()
def delete_script(table: str, script_name: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"DELETE FROM \"{table}\" WHERE script_name=\"{script_name}\"")
conn.commit()
conn.close()
def delete_table(table: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"""DROP TABLE \"{table}\"""")
conn.commit()
conn.close()
def read_to_run_script(table: str, script_name: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT script_path, venv, venv_path, cmd FROM \"{table}\" WHERE script_name=\"{script_name}\"")
script_info = c.fetchone()
conn.commit()
conn.close()
return script_info
def read_to_run_all_scripts(table: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT script_path, venv, venv_path, cmd FROM \"{table}\"")
script_info = c.fetchall()
conn.commit()
conn.close()
return script_info
def read_all_scripts(table: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT script_name, thumbnail_path, latest_script_name FROM \"{table}\"")
script_info = c.fetchall()
conn.commit()
conn.close()
return script_info
def read_all_tables():
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT name from sqlite_master where type=\"table\"")
tables = c.fetchall()
scripts = []
for table in tables:
c.execute(f"SELECT script_name FROM \"{table[0]}\"")
scripts.append(c.fetchall())
conn.commit()
conn.close()
return tables, scripts
def get_script_info(table: str, script: str):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT * FROM \"{table}\" WHERE script_name=\"{script}\"")
script_info = c.fetchone()
conn.commit()
conn.close()
return script_info
def save_all(file_path: str):
original = os.path.abspath("_temp_.db")
target = file_path
shutil.copyfile(original, target)
def read_all():
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"SELECT name from sqlite_master where type=\"table\"")
tables = c.fetchall()
scripts = []
for table in tables:
c.execute(f"SELECT * FROM \"{table[0]}\"")
scripts.append(c.fetchall())
conn.commit()
conn.close()
return tables, scripts
def update_script(table: str, old_script_name: str, latest_script_name: str, script_path: str, thumbnail_path="icons/default_thumbnail.jpg", cmd="", venv=str, venv_path=""):
conn = sqlite3.connect("_temp_.db")
c = conn.cursor()
c.execute(f"""UPDATE \"{table}\" SET script_path = ?, venv = ?, venv_path = ?, cmd = ?, thumbnail_path = ?, latest_script_name = ? WHERE script_name = \"{old_script_name}\"""", (script_path, venv, venv_path, cmd, thumbnail_path, latest_script_name))
conn.commit()
conn.close()