Created functions to reduce repititive code
Trying to add a worker thread for executing sql statements I dont know what i am doing anymore :/ please send help
This commit is contained in:
parent
07410e66ff
commit
66bdb310a6
225
gui/main.py
225
gui/main.py
|
|
@ -21,7 +21,7 @@ dbtypes = {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load DB Config from file but check if file exists and create if not
|
# Load DB Config from file but check if file exists and create if not
|
||||||
def load_config():
|
def load_config() -> tuple:
|
||||||
if not os.path.exists(os.path.join(configfolder, configfile)):
|
if not os.path.exists(os.path.join(configfolder, configfile)):
|
||||||
os.makedirs(configfolder)
|
os.makedirs(configfolder)
|
||||||
with open(os.path.join(configfolder, configfile), "w") as f:
|
with open(os.path.join(configfolder, configfile), "w") as f:
|
||||||
|
|
@ -49,27 +49,91 @@ def load_config():
|
||||||
|
|
||||||
return type, ip, port, user, password, name, apikey
|
return type, ip, port, user, password, name, apikey
|
||||||
|
|
||||||
|
def save_config(dbtype, dbip, dbport, dbuser, dbpass, dbname, apikey) -> None:
|
||||||
|
with open(os.path.join(configfolder, configfile), "w") as f:
|
||||||
|
json.dump({
|
||||||
|
"database": {
|
||||||
|
"dbtype": dbtype,
|
||||||
|
"ip": dbip,
|
||||||
|
"port": dbport,
|
||||||
|
"user": dbuser,
|
||||||
|
"password": dbpass,
|
||||||
|
"database": dbname
|
||||||
|
},
|
||||||
|
"apikey": apikey
|
||||||
|
}, f, indent=4)
|
||||||
|
|
||||||
|
def connect_db() -> tuple:
|
||||||
|
db = None
|
||||||
|
tableschema = []
|
||||||
|
dbtype, dbip, dbport, dbuser, dbpass, dbname, apikey = load_config()
|
||||||
|
try:
|
||||||
|
if dbtype == 0:
|
||||||
|
import database.postgresql as pg
|
||||||
|
db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
||||||
|
tableschema = db.get_schema()
|
||||||
|
|
||||||
|
print('Database Connected')
|
||||||
|
return db, tableschema
|
||||||
|
except Exception as e:
|
||||||
|
print('No Database Connection')
|
||||||
|
print(e)
|
||||||
|
return db, tableschema
|
||||||
|
|
||||||
|
def test_connection(dbtype, dbip, dbport, dbuser, dbpass, dbname) -> bool:
|
||||||
|
db = None
|
||||||
|
try:
|
||||||
|
if dbtype == 0:
|
||||||
|
import database.postgresql as pg
|
||||||
|
db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
||||||
|
if db.test_connection():
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class Worker(QObject):
|
class Worker(QObject):
|
||||||
finished = pyqtSignal()
|
finished = pyqtSignal()
|
||||||
test = False
|
test = False
|
||||||
|
db = None
|
||||||
|
tableschema = []
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def test_db_connection(self, dbtype, dbip, dbport, dbuser, dbpass, dbname):
|
def test_db_connection(self, dbtype, dbip, dbport, dbuser, dbpass, dbname):
|
||||||
|
connection = test_connection(dbtype, dbip, dbport, dbuser, dbpass, dbname)
|
||||||
|
if connection:
|
||||||
|
self.test = True
|
||||||
|
self.finished.emit()
|
||||||
|
|
||||||
|
def test_api_connection(self, apikey):
|
||||||
try:
|
try:
|
||||||
if dbtype == 0:
|
ai = aisql.AI(apikey)
|
||||||
import database.postgresql as pg
|
if ai.test_key():
|
||||||
self.db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
|
||||||
|
|
||||||
if self.db.test_connection():
|
|
||||||
self.test = True
|
self.test = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
self.finished.emit()
|
||||||
|
|
||||||
|
def connect_db_worker(self):
|
||||||
|
self.db, self.tableschema = connect_db()
|
||||||
|
|
||||||
|
|
||||||
|
def testing(self):
|
||||||
|
self.connect_db_worker()
|
||||||
|
print('test')
|
||||||
|
self.finished.emit()
|
||||||
|
|
||||||
|
# TODO: Add a translation Worker and a SQL Worker
|
||||||
|
def translate(self, text):
|
||||||
|
print("Translating...")
|
||||||
|
self.finished.emit()
|
||||||
|
|
||||||
|
def run_sql(self, sql):
|
||||||
|
print("Running SQL...")
|
||||||
self.finished.emit()
|
self.finished.emit()
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
|
|
@ -98,13 +162,9 @@ class MainWindow(QMainWindow):
|
||||||
self.ui.executeButton.clicked.connect(self.on_execute_button_clicked)
|
self.ui.executeButton.clicked.connect(self.on_execute_button_clicked)
|
||||||
|
|
||||||
def try_to_connect(self):
|
def try_to_connect(self):
|
||||||
dbtype, dbip, dbport, dbuser, dbpass, dbname, apikey = load_config()
|
# TODO: Rewrite to use a Worker
|
||||||
try:
|
try:
|
||||||
if dbtype == 0:
|
self.db = connect_db()
|
||||||
import database.postgresql as pg
|
|
||||||
self.db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
|
||||||
self.tableschema = self.db.get_schema()
|
|
||||||
|
|
||||||
print('Database Connected')
|
print('Database Connected')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('No Database Connection')
|
print('No Database Connection')
|
||||||
|
|
@ -113,50 +173,78 @@ class MainWindow(QMainWindow):
|
||||||
self.connection_window = ConnectionWindow(self)
|
self.connection_window = ConnectionWindow(self)
|
||||||
self.connection_window.show()
|
self.connection_window.show()
|
||||||
|
|
||||||
|
def on_paste_button_clicked(self):
|
||||||
|
self.ui.shellInput.setText(self.ui.statementOutput.toPlainText())
|
||||||
def open_apikey(self):
|
def open_apikey(self):
|
||||||
self.apikey_window = ApiKeyWindow(self)
|
self.apikey_window = ApiKeyWindow(self)
|
||||||
self.apikey_window.show()
|
self.apikey_window.show()
|
||||||
|
|
||||||
def on_convert_button_clicked(self):
|
|
||||||
self.ui.outputLabel.show()
|
|
||||||
self.ui.outputLabel.setText("Converting...")
|
|
||||||
self.load_config()
|
|
||||||
print("Convert Button Clicked")
|
|
||||||
ai = aisql.AI(self.apikey)
|
|
||||||
prompt = self.ui.textInput.text()
|
|
||||||
sql = ai.humantosql(prompt, dbtypes[self.dbtype], self.tableschema)
|
|
||||||
self.ui.statementOutput.setText(sql)
|
|
||||||
self.ui.outputLabel.setText("Converted!")
|
|
||||||
|
|
||||||
def on_paste_button_clicked(self):
|
|
||||||
self.ui.shellInput.setText(self.ui.statementOutput.toPlainText())
|
|
||||||
|
|
||||||
def on_execute_button_clicked(self):
|
def on_execute_button_clicked(self):
|
||||||
print("Execute Button Clicked")
|
|
||||||
self.try_to_connect()
|
|
||||||
ai = aisql.AI(self.apikey)
|
|
||||||
self.ui.outputLabel.setText("Executing...")
|
self.ui.outputLabel.setText("Executing...")
|
||||||
self.load_config()
|
self.ui.outputLabel.show()
|
||||||
sql = self.ui.shellInput.toPlainText()
|
self.ui.executeButton.setEnabled(False)
|
||||||
# Check what method to use
|
|
||||||
decision = ai.decide(sql)
|
sql = self.ui.shellInput.toPlainText()
|
||||||
if "fetchall".casefold() in decision.casefold():
|
|
||||||
fetch = self.db.fetchall(sql)
|
self.thread = QThread()
|
||||||
print(fetch)
|
self.worker = Worker()
|
||||||
elif "fetchone".casefold() in decision.casefold():
|
self.worker.moveToThread(self.thread)
|
||||||
fetch = self.db.fetchone(sql)
|
|
||||||
print(fetch)
|
self.thread.started.connect(self.worker.testing)
|
||||||
elif "fetchmany".casefold() in decision.casefold():
|
self.worker.finished.connect(self.execute_finish)
|
||||||
size = decision.split("=")[1].strip("]")
|
|
||||||
fetch = self.db.fetchmany(sql, int(size))
|
self.thread.start()
|
||||||
print(fetch)
|
|
||||||
elif "execute".casefold() in decision.casefold():
|
def execute_finish(self):
|
||||||
self.db.execute(sql)
|
self.ui.outputLabel.setText("Finished!")
|
||||||
elif "executemany".casefold() in decision.casefold():
|
self.ui.outputLabel.show()
|
||||||
size = decision.split("=")[1].strip("]")
|
self.ui.executeButton.setEnabled(True)
|
||||||
self.db.executemany(sql, int(size))
|
print("finished")
|
||||||
|
|
||||||
|
self.worker.deleteLater()
|
||||||
|
self.thread.deleteLater()
|
||||||
|
print("deleted")
|
||||||
|
|
||||||
|
def on_convert_button_clicked(self):
|
||||||
|
self.ui.outputLabel.setText("Converting...")
|
||||||
|
self.ui.outputLabel.show()
|
||||||
|
self.ui.convertButton.setEnabled(False)
|
||||||
|
|
||||||
|
# def start_db_test_thread(self):
|
||||||
|
# self.ui.returnLabel.setText("Testing...")
|
||||||
|
# self.ui.returnLabel.setStyleSheet("color: black;")
|
||||||
|
# self.ui.testButton.setEnabled(False)
|
||||||
|
#
|
||||||
|
# dbtype = self.ui.dbtypeCombo.currentIndex()
|
||||||
|
# ip = self.ui.ipInput.text()
|
||||||
|
# port = self.ui.portInput.text()
|
||||||
|
# user = self.ui.usernameInput.text()
|
||||||
|
# password = self.ui.passwordInput.text()
|
||||||
|
# database = self.ui.databaseInput.text()
|
||||||
|
#
|
||||||
|
# self.thread = QThread()
|
||||||
|
# self.worker = Worker()
|
||||||
|
# self.worker.moveToThread(self.thread)
|
||||||
|
#
|
||||||
|
# self.thread.started.connect(
|
||||||
|
# lambda: self.worker.test_db_connection(dbtype, ip, port, user, password, database))
|
||||||
|
# self.worker.finished.connect(self.thread.quit)
|
||||||
|
# self.thread.finished.connect(self.thread_complete)
|
||||||
|
#
|
||||||
|
# self.thread.start()
|
||||||
|
#
|
||||||
|
# def thread_complete(self):
|
||||||
|
# if self.worker.test:
|
||||||
|
# self.ui.returnLabel.setText("Connection Success!")
|
||||||
|
# self.ui.returnLabel.setStyleSheet("color: green;")
|
||||||
|
# else:
|
||||||
|
# self.ui.returnLabel.setText("Connection Failed!")
|
||||||
|
# self.ui.returnLabel.setStyleSheet("color: red;")
|
||||||
|
#
|
||||||
|
# self.ui.testButton.setEnabled(True)
|
||||||
|
# self.worker.deleteLater()
|
||||||
|
# self.thread.deleteLater()
|
||||||
|
|
||||||
self.ui.outputLabel.setText("Executed!")
|
|
||||||
|
|
||||||
### Connection Window ###
|
### Connection Window ###
|
||||||
class ConnectionWindow(QDialog):
|
class ConnectionWindow(QDialog):
|
||||||
|
|
@ -288,7 +376,7 @@ class ApiKeyWindow(QDialog):
|
||||||
self.ui.saveButton.clicked.connect(self.on_save_button_clicked)
|
self.ui.saveButton.clicked.connect(self.on_save_button_clicked)
|
||||||
|
|
||||||
# Pressed Test Button
|
# Pressed Test Button
|
||||||
self.ui.testButton.clicked.connect(self.on_test_button_clicked)
|
self.ui.testButton.clicked.connect(self.start_api_test_thread)
|
||||||
|
|
||||||
def on_text_changed(self):
|
def on_text_changed(self):
|
||||||
if self.ui.apikeyInput.text() == "":
|
if self.ui.apikeyInput.text() == "":
|
||||||
|
|
@ -308,15 +396,36 @@ class ApiKeyWindow(QDialog):
|
||||||
# Close Window
|
# Close Window
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def on_test_button_clicked(self):
|
def start_api_test_thread(self):
|
||||||
test_key = self.ui.apikeyInput.text()
|
self.ui.outputLabel.setText("Testing...")
|
||||||
ai = aisql.AI(test_key)
|
self.ui.outputLabel.setStyleSheet("color: black;")
|
||||||
if ai.test_key():
|
self.ui.testButton.setEnabled(False)
|
||||||
self.ui.outputLabel.setText("API Key is valid")
|
|
||||||
self.ui.outputLabel.setStyleSheet("color: green")
|
apikey = self.ui.apikeyInput.text()
|
||||||
|
|
||||||
|
self.thread = QThread()
|
||||||
|
self.worker = Worker()
|
||||||
|
self.worker.moveToThread(self.thread)
|
||||||
|
|
||||||
|
self.thread.started.connect(lambda: self.worker.test_api_connection(apikey))
|
||||||
|
self.worker.finished.connect(self.thread.quit)
|
||||||
|
self.thread.finished.connect(self.thread_complete)
|
||||||
|
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
|
def thread_complete(self):
|
||||||
|
if self.worker.test:
|
||||||
|
self.ui.outputLabel.setText("Connection Success!")
|
||||||
|
self.ui.outputLabel.setStyleSheet("color: green;")
|
||||||
else:
|
else:
|
||||||
self.ui.outputLabel.setText("API Key is invalid")
|
self.ui.outputLabel.setText("Connection Failed!")
|
||||||
self.ui.outputLabel.setStyleSheet("color: red")
|
self.ui.outputLabel.setStyleSheet("color: red;")
|
||||||
|
|
||||||
|
self.ui.outputLabel.setEnabled(True)
|
||||||
|
self.worker.deleteLater()
|
||||||
|
self.thread.deleteLater()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user