AAAAAAAAAAAAAAA

This commit is contained in:
crennis 2023-05-08 14:50:29 +02:00
parent 66bdb310a6
commit dae929607f
8 changed files with 235 additions and 65 deletions

View File

@ -21,7 +21,7 @@ class Postgres:
self.get_schema()
def __str__(self):
return f'PostgreSQL Server: {self.db_ip}:{self.db_port} as {self.db_username} on database {self.db_name}'
return f'PostgreSQL'
def get_schema(self) -> dict:
fetch = self.fetchall("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';")

View File

@ -50,7 +50,7 @@ class Ui_MainWindow(object):
self.outputTableLabel.setGeometry(QtCore.QRect(20, 250, 49, 16))
self.outputTableLabel.setObjectName("outputTableLabel")
self.shellInput = QtWidgets.QTextEdit(self.centralwidget)
self.shellInput.setGeometry(QtCore.QRect(410, 80, 375, 111))
self.shellInput.setGeometry(QtCore.QRect(410, 80, 371, 111))
self.shellInput.setObjectName("shellInput")
self.statementOutput = QtWidgets.QTextEdit(self.centralwidget)
self.statementOutput.setEnabled(True)
@ -81,7 +81,14 @@ class Ui_MainWindow(object):
self.menubar.addAction(self.menuSettings.menuAction())
self.retranslateUi(MainWindow)
self.textInput.returnPressed.connect(self.convertButton.click) # type: ignore
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.textInput, self.convertButton)
MainWindow.setTabOrder(self.convertButton, self.pasteButton)
MainWindow.setTabOrder(self.pasteButton, self.shellInput)
MainWindow.setTabOrder(self.shellInput, self.executeButton)
MainWindow.setTabOrder(self.executeButton, self.outputTable)
MainWindow.setTabOrder(self.outputTable, self.statementOutput)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate

View File

@ -143,7 +143,7 @@
<rect>
<x>410</x>
<y>80</y>
<width>375</width>
<width>371</width>
<height>111</height>
</rect>
</property>
@ -214,6 +214,32 @@ p, li { white-space: pre-wrap; }
</property>
</action>
</widget>
<tabstops>
<tabstop>textInput</tabstop>
<tabstop>convertButton</tabstop>
<tabstop>pasteButton</tabstop>
<tabstop>shellInput</tabstop>
<tabstop>executeButton</tabstop>
<tabstop>outputTable</tabstop>
<tabstop>statementOutput</tabstop>
</tabstops>
<resources/>
<connections/>
<connections>
<connection>
<sender>textInput</sender>
<signal>returnPressed()</signal>
<receiver>convertButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>262</x>
<y>69</y>
</hint>
<hint type="destinationlabel">
<x>724</x>
<y>67</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -32,6 +32,7 @@ class Ui_ApiKey(object):
self.outputLabel.setObjectName("outputLabel")
self.retranslateUi(ApiKey)
self.apikeyInput.returnPressed.connect(self.testButton.click) # type: ignore
QtCore.QMetaObject.connectSlotsByName(ApiKey)
def retranslateUi(self, ApiKey):

View File

@ -77,5 +77,22 @@
</widget>
</widget>
<resources/>
<connections/>
<connections>
<connection>
<sender>apikeyInput</sender>
<signal>returnPressed()</signal>
<receiver>testButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>153</x>
<y>27</y>
</hint>
<hint type="destinationlabel">
<x>91</x>
<y>53</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -68,7 +68,19 @@ class Ui_Connection(object):
self.dbtypeCombo.setObjectName("dbtypeCombo")
self.retranslateUi(Connection)
self.ipInput.returnPressed.connect(self.portInput.setFocus) # type: ignore
self.portInput.returnPressed.connect(self.usernameInput.setFocus) # type: ignore
self.usernameInput.returnPressed.connect(self.passwordInput.setFocus) # type: ignore
self.passwordInput.returnPressed.connect(self.databaseInput.setFocus) # type: ignore
self.databaseInput.returnPressed.connect(self.testButton.click) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Connection)
Connection.setTabOrder(self.dbtypeCombo, self.ipInput)
Connection.setTabOrder(self.ipInput, self.portInput)
Connection.setTabOrder(self.portInput, self.usernameInput)
Connection.setTabOrder(self.usernameInput, self.passwordInput)
Connection.setTabOrder(self.passwordInput, self.databaseInput)
Connection.setTabOrder(self.databaseInput, self.testButton)
Connection.setTabOrder(self.testButton, self.saveButton)
def retranslateUi(self, Connection):
_translate = QtCore.QCoreApplication.translate

View File

@ -209,6 +209,97 @@
</property>
</widget>
</widget>
<tabstops>
<tabstop>dbtypeCombo</tabstop>
<tabstop>ipInput</tabstop>
<tabstop>portInput</tabstop>
<tabstop>usernameInput</tabstop>
<tabstop>passwordInput</tabstop>
<tabstop>databaseInput</tabstop>
<tabstop>testButton</tabstop>
<tabstop>saveButton</tabstop>
</tabstops>
<resources/>
<connections/>
<connections>
<connection>
<sender>ipInput</sender>
<signal>returnPressed()</signal>
<receiver>portInput</receiver>
<slot>setFocus()</slot>
<hints>
<hint type="sourcelabel">
<x>167</x>
<y>43</y>
</hint>
<hint type="destinationlabel">
<x>167</x>
<y>87</y>
</hint>
</hints>
</connection>
<connection>
<sender>portInput</sender>
<signal>returnPressed()</signal>
<receiver>usernameInput</receiver>
<slot>setFocus()</slot>
<hints>
<hint type="sourcelabel">
<x>200</x>
<y>83</y>
</hint>
<hint type="destinationlabel">
<x>205</x>
<y>111</y>
</hint>
</hints>
</connection>
<connection>
<sender>usernameInput</sender>
<signal>returnPressed()</signal>
<receiver>passwordInput</receiver>
<slot>setFocus()</slot>
<hints>
<hint type="sourcelabel">
<x>140</x>
<y>117</y>
</hint>
<hint type="destinationlabel">
<x>138</x>
<y>137</y>
</hint>
</hints>
</connection>
<connection>
<sender>passwordInput</sender>
<signal>returnPressed()</signal>
<receiver>databaseInput</receiver>
<slot>setFocus()</slot>
<hints>
<hint type="sourcelabel">
<x>187</x>
<y>143</y>
</hint>
<hint type="destinationlabel">
<x>184</x>
<y>167</y>
</hint>
</hints>
</connection>
<connection>
<sender>databaseInput</sender>
<signal>returnPressed()</signal>
<receiver>testButton</receiver>
<slot>click()</slot>
<hints>
<hint type="sourcelabel">
<x>157</x>
<y>173</y>
</hint>
<hint type="destinationlabel">
<x>84</x>
<y>205</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -3,7 +3,7 @@ import os
import sys
from PyQt5.QtCore import QObject, QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QTableWidgetItem, QTableWidget
import modules.aisql as aisql
@ -98,6 +98,7 @@ class Worker(QObject):
test = False
db = None
tableschema = []
result = ""
def __init__(self):
super().__init__()
@ -121,24 +122,40 @@ class Worker(QObject):
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...")
# Load Config
self.connect_db_worker()
dbtype, dbip, dbport, dbuser, dbpassword, dbname, apikey = load_config()
ai = aisql.AI(apikey)
self.result = ai.humantosql(text, str(self.db), self.tableschema)
self.finished.emit()
def run_sql(self, sql):
print("Running SQL...")
db, tableschema = connect_db()
ai = aisql.AI(api_key=load_config()[6])
choice = ai.decide(sql)
print(choice)
if "fetchall".casefold() in choice.casefold():
self.result = db.fetchall(sql)
elif "fetchone".casefold() in choice.casefold():
self.result = db.fetchone(sql)
elif "execute".casefold() in choice.casefold():
self.result = db.execute(sql)
else:
print("error")
print(self.result)
self.finished.emit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi()
def setupUi(self):
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.tableschema = []
@ -153,13 +170,19 @@ class MainWindow(QMainWindow):
self.ui.actionConnect_API_Key.triggered.connect(self.open_apikey)
# Pressed Convert Button
self.ui.convertButton.clicked.connect(self.on_convert_button_clicked)
# FIXME: UI still freezes when clicked, QThreads not working properly
self.ui.convertButton.clicked.connect(self.convert)
# Pressed Paste Button
self.ui.pasteButton.clicked.connect(self.on_paste_button_clicked)
# TODO: Functioning Execute Button
# Pressed Execute Button
self.ui.executeButton.clicked.connect(self.on_execute_button_clicked)
self.ui.executeButton.clicked.connect(self.execute)
self.ui.outputTable.setColumnCount(2)
self.ui.outputTable.setHorizontalHeaderLabels(["Column", "Value"])
def try_to_connect(self):
# TODO: Rewrite to use a Worker
@ -179,71 +202,59 @@ class MainWindow(QMainWindow):
self.apikey_window = ApiKeyWindow(self)
self.apikey_window.show()
def on_execute_button_clicked(self):
# TODO: Make this work
def execute(self):
self.ui.outputLabel.setText("Executing...")
self.ui.outputLabel.show()
self.ui.executeButton.setEnabled(False)
sql = self.ui.shellInput.toPlainText()
self.thread = QThread()
self.worker = Worker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.testing)
self.worker.finished.connect(self.execute_finish)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.started.connect(lambda: self.worker.run_sql(self.ui.shellInput.toPlainText()))
self.worker.finished.connect(self.execute_finished)
self.thread.start()
def execute_finish(self):
def execute_finished(self):
self.data = self.worker.result
self.ui.outputLabel.setText("Finished!")
self.ui.outputLabel.show()
self.ui.executeButton.setEnabled(True)
print("finished")
self.worker.deleteLater()
self.thread.deleteLater()
print("deleted")
def on_convert_button_clicked(self):
def convert(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.worker = Worker()
self.thread = QThread()
self.worker.moveToThread(self.thread)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.started.connect(lambda: self.worker.translate(self.ui.textInput.text()))
self.worker.finished.connect(self.convert_finished)
self.thread.start()
def convert_finished(self):
self.ui.statementOutput.setText(self.worker.result)
self.ui.outputLabel.setText("Finished!")
self.ui.outputLabel.show()
self.ui.convertButton.setEnabled(True)
# TODO: Convert Function Threading
# FIXME: Still freezing UI even with threading
### Connection Window ###
@ -252,10 +263,9 @@ class ConnectionWindow(QDialog):
super().__init__(parent)
self.ui = ConnectionForm()
self.ui.setupUi(self)
self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
self.ui.returnLabel.setText("")
# Load DB Config from file
with open(os.path.join(configfolder, configfile), "r") as f:
self.config = json.load(f)
@ -274,6 +284,12 @@ class ConnectionWindow(QDialog):
self.ui.passwordInput.setText(self.dbpass)
self.ui.databaseInput.setText(self.dbname)
if (self.ui.ipInput or self.ui.portInput or self.ui.databaseInput) == "":
self.ui.saveButton.setEnabled(False)
self.ui.testButton.setEnabled(False)
else:
self.ui.saveButton.setEnabled(True)
self.ui.testButton.setEnabled(True)
# Unlock Buttons if ip, port and database is not empty
self.ui.ipInput.textChanged.connect(self.on_text_changed)