Fixed Test Connection button,
Implemented Threading
This commit is contained in:
parent
3336f12a18
commit
07410e66ff
152
gui/main.py
152
gui/main.py
|
|
@ -2,6 +2,7 @@ import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from PyQt5.QtCore import QObject, QThread, pyqtSignal
|
||||||
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
|
||||||
|
|
||||||
import modules.aisql as aisql
|
import modules.aisql as aisql
|
||||||
|
|
@ -19,6 +20,57 @@ dbtypes = {
|
||||||
2: "SQLite"
|
2: "SQLite"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Load DB Config from file but check if file exists and create if not
|
||||||
|
def load_config():
|
||||||
|
if not os.path.exists(os.path.join(configfolder, configfile)):
|
||||||
|
os.makedirs(configfolder)
|
||||||
|
with open(os.path.join(configfolder, configfile), "w") as f:
|
||||||
|
json.dump({
|
||||||
|
"database": {
|
||||||
|
"dbtype": 0,
|
||||||
|
"ip": "",
|
||||||
|
"port": "",
|
||||||
|
"user": "",
|
||||||
|
"password": "",
|
||||||
|
"database": ""
|
||||||
|
},
|
||||||
|
"apikey": ""
|
||||||
|
}, f, indent=4)
|
||||||
|
else:
|
||||||
|
with open(os.path.join(configfolder, configfile), "r") as f:
|
||||||
|
config = json.load(f)
|
||||||
|
type = config["database"]["dbtype"]
|
||||||
|
ip = config["database"]["ip"]
|
||||||
|
port = config["database"]["port"]
|
||||||
|
user = config["database"]["user"]
|
||||||
|
password = config["database"]["password"]
|
||||||
|
name = config["database"]["database"]
|
||||||
|
apikey = config["apikey"]
|
||||||
|
|
||||||
|
return type, ip, port, user, password, name, apikey
|
||||||
|
|
||||||
|
|
||||||
|
class Worker(QObject):
|
||||||
|
finished = pyqtSignal()
|
||||||
|
test = False
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def test_db_connection(self, dbtype, dbip, dbport, dbuser, dbpass, dbname):
|
||||||
|
|
||||||
|
try:
|
||||||
|
if dbtype == 0:
|
||||||
|
import database.postgresql as pg
|
||||||
|
self.db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
||||||
|
|
||||||
|
if self.db.test_connection():
|
||||||
|
self.test = True
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
|
self.finished.emit()
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -45,44 +97,15 @@ class MainWindow(QMainWindow):
|
||||||
# Pressed Execute Button
|
# Pressed Execute Button
|
||||||
self.ui.executeButton.clicked.connect(self.on_execute_button_clicked)
|
self.ui.executeButton.clicked.connect(self.on_execute_button_clicked)
|
||||||
|
|
||||||
self.load_config()
|
|
||||||
|
|
||||||
def load_config(self):
|
|
||||||
# Load DB Config from file but check if file exists and create if not
|
|
||||||
if not os.path.exists(os.path.join(configfolder, configfile)):
|
|
||||||
os.makedirs(configfolder)
|
|
||||||
with open(os.path.join(configfolder, configfile), "w") as f:
|
|
||||||
json.dump({
|
|
||||||
"database": {
|
|
||||||
"dbtype": 0,
|
|
||||||
"ip": "",
|
|
||||||
"port": "",
|
|
||||||
"user": "",
|
|
||||||
"password": "",
|
|
||||||
"database": ""
|
|
||||||
},
|
|
||||||
"apikey": ""
|
|
||||||
}, f, indent=4)
|
|
||||||
else:
|
|
||||||
with open(os.path.join(configfolder, configfile), "r") as f:
|
|
||||||
self.config = json.load(f)
|
|
||||||
self.dbtype = self.config["database"]["dbtype"]
|
|
||||||
self.dbip = self.config["database"]["ip"]
|
|
||||||
self.dbport = self.config["database"]["port"]
|
|
||||||
self.dbuser = self.config["database"]["user"]
|
|
||||||
self.dbpass = self.config["database"]["password"]
|
|
||||||
self.dbname = self.config["database"]["database"]
|
|
||||||
self.apikey = self.config["apikey"]
|
|
||||||
|
|
||||||
self.try_to_connect()
|
|
||||||
|
|
||||||
def try_to_connect(self):
|
def try_to_connect(self):
|
||||||
|
dbtype, dbip, dbport, dbuser, dbpass, dbname, apikey = load_config()
|
||||||
try:
|
try:
|
||||||
if self.dbtype == 0:
|
if dbtype == 0:
|
||||||
import database.postgresql as pg
|
import database.postgresql as pg
|
||||||
self.db = pg.Postgres(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
|
self.db = pg.Postgres(dbip, dbport, dbuser, dbpass, dbname)
|
||||||
self.tableschema = self.db.get_schema()
|
self.tableschema = self.db.get_schema()
|
||||||
|
|
||||||
|
print('Database Connected')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('No Database Connection')
|
print('No Database Connection')
|
||||||
|
|
||||||
|
|
@ -110,6 +133,7 @@ class MainWindow(QMainWindow):
|
||||||
|
|
||||||
def on_execute_button_clicked(self):
|
def on_execute_button_clicked(self):
|
||||||
print("Execute Button Clicked")
|
print("Execute Button Clicked")
|
||||||
|
self.try_to_connect()
|
||||||
ai = aisql.AI(self.apikey)
|
ai = aisql.AI(self.apikey)
|
||||||
self.ui.outputLabel.setText("Executing...")
|
self.ui.outputLabel.setText("Executing...")
|
||||||
self.load_config()
|
self.load_config()
|
||||||
|
|
@ -124,7 +148,7 @@ class MainWindow(QMainWindow):
|
||||||
print(fetch)
|
print(fetch)
|
||||||
elif "fetchmany".casefold() in decision.casefold():
|
elif "fetchmany".casefold() in decision.casefold():
|
||||||
size = decision.split("=")[1].strip("]")
|
size = decision.split("=")[1].strip("]")
|
||||||
fetch = elf.db.fetchmany(sql, int(size))
|
fetch = self.db.fetchmany(sql, int(size))
|
||||||
print(fetch)
|
print(fetch)
|
||||||
elif "execute".casefold() in decision.casefold():
|
elif "execute".casefold() in decision.casefold():
|
||||||
self.db.execute(sql)
|
self.db.execute(sql)
|
||||||
|
|
@ -172,7 +196,7 @@ class ConnectionWindow(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_db_test_thread)
|
||||||
|
|
||||||
def on_text_changed(self):
|
def on_text_changed(self):
|
||||||
if self.ui.ipInput.text() == "" or self.ui.portInput.text() == "" or self.ui.databaseInput.text() == "":
|
if self.ui.ipInput.text() == "" or self.ui.portInput.text() == "" or self.ui.databaseInput.text() == "":
|
||||||
|
|
@ -202,40 +226,39 @@ class ConnectionWindow(QDialog):
|
||||||
|
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
# FIXME: Crash when testing connection
|
def start_db_test_thread(self):
|
||||||
def on_test_button_clicked(self):
|
self.ui.returnLabel.setText("Testing...")
|
||||||
self.dbtype = self.ui.dbtypeCombo.currentIndex()
|
self.ui.returnLabel.setStyleSheet("color: black;")
|
||||||
self.dbip = self.ui.ipInput.text()
|
self.ui.testButton.setEnabled(False)
|
||||||
self.dbport = self.ui.portInput.text()
|
|
||||||
self.dbuser = self.ui.usernameInput.text()
|
|
||||||
self.dbpass = self.ui.passwordInput.text()
|
|
||||||
self.dbname = self.ui.databaseInput.text()
|
|
||||||
db = None
|
|
||||||
# Check type of Database
|
|
||||||
try:
|
|
||||||
if self.dbtype == 0: # PostgreSQL
|
|
||||||
import database.postgresql as postgresql
|
|
||||||
db = postgresql.Postgres(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
|
|
||||||
|
|
||||||
except Exception as e:
|
dbtype = self.ui.dbtypeCombo.currentIndex()
|
||||||
self.ui.returnLabel.setText("Connection failed")
|
ip = self.ui.ipInput.text()
|
||||||
self.ui.returnLabel.setStyleSheet("color: red")
|
port = self.ui.portInput.text()
|
||||||
print(e)
|
user = self.ui.usernameInput.text()
|
||||||
|
password = self.ui.passwordInput.text()
|
||||||
|
database = self.ui.databaseInput.text()
|
||||||
|
|
||||||
if db is not None:
|
self.thread = QThread()
|
||||||
try:
|
self.worker = Worker()
|
||||||
if db.test_connection():
|
self.worker.moveToThread(self.thread)
|
||||||
self.ui.returnLabel.setText("Connection successful")
|
|
||||||
self.ui.returnLabel.setStyleSheet("color: green")
|
|
||||||
except Exception as e:
|
|
||||||
self.ui.returnLabel.setText("Connection failed")
|
|
||||||
self.ui.returnLabel.setStyleSheet("color: red")
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
self.ui.returnLabel.setText("Connection failed")
|
self.ui.returnLabel.setText("Connection Failed!")
|
||||||
self.ui.returnLabel.setStyleSheet("color: red")
|
self.ui.returnLabel.setStyleSheet("color: red;")
|
||||||
|
|
||||||
|
self.ui.testButton.setEnabled(True)
|
||||||
|
self.worker.deleteLater()
|
||||||
|
self.thread.deleteLater()
|
||||||
|
|
||||||
### Api Key Window ###
|
### Api Key Window ###
|
||||||
class ApiKeyWindow(QDialog):
|
class ApiKeyWindow(QDialog):
|
||||||
|
|
@ -295,7 +318,6 @@ class ApiKeyWindow(QDialog):
|
||||||
self.ui.outputLabel.setText("API Key is invalid")
|
self.ui.outputLabel.setText("API Key is invalid")
|
||||||
self.ui.outputLabel.setStyleSheet("color: red")
|
self.ui.outputLabel.setStyleSheet("color: red")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
window = MainWindow()
|
window = MainWindow()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user