Updated UI:
- SQL Generation Done - Paste to Shell Done - Execute Done TODO: - Fetch output into table
This commit is contained in:
parent
39bb997f99
commit
3336f12a18
108
gui/main.py
108
gui/main.py
|
|
@ -25,6 +25,10 @@ class MainWindow(QMainWindow):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.ui = Ui_MainWindow()
|
self.ui = Ui_MainWindow()
|
||||||
self.ui.setupUi(self)
|
self.ui.setupUi(self)
|
||||||
|
self.tableschema = []
|
||||||
|
|
||||||
|
# Hide outputLabel
|
||||||
|
self.ui.outputLabel.hide()
|
||||||
|
|
||||||
# Open Connection Window
|
# Open Connection Window
|
||||||
self.ui.actionConnect_DB.triggered.connect(self.open_connection)
|
self.ui.actionConnect_DB.triggered.connect(self.open_connection)
|
||||||
|
|
@ -35,6 +39,53 @@ class MainWindow(QMainWindow):
|
||||||
# Pressed Convert Button
|
# Pressed Convert Button
|
||||||
self.ui.convertButton.clicked.connect(self.on_convert_button_clicked)
|
self.ui.convertButton.clicked.connect(self.on_convert_button_clicked)
|
||||||
|
|
||||||
|
# Pressed Paste Button
|
||||||
|
self.ui.pasteButton.clicked.connect(self.on_paste_button_clicked)
|
||||||
|
|
||||||
|
# Pressed Execute Button
|
||||||
|
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):
|
||||||
|
try:
|
||||||
|
if self.dbtype == 0:
|
||||||
|
import database.postgresql as pg
|
||||||
|
self.db = pg.Postgres(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
|
||||||
|
self.tableschema = self.db.get_schema()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print('No Database Connection')
|
||||||
|
|
||||||
def open_connection(self):
|
def open_connection(self):
|
||||||
self.connection_window = ConnectionWindow(self)
|
self.connection_window = ConnectionWindow(self)
|
||||||
self.connection_window.show()
|
self.connection_window.show()
|
||||||
|
|
@ -44,9 +95,44 @@ class MainWindow(QMainWindow):
|
||||||
self.apikey_window.show()
|
self.apikey_window.show()
|
||||||
|
|
||||||
def on_convert_button_clicked(self):
|
def on_convert_button_clicked(self):
|
||||||
|
self.ui.outputLabel.show()
|
||||||
|
self.ui.outputLabel.setText("Converting...")
|
||||||
|
self.load_config()
|
||||||
print("Convert Button Clicked")
|
print("Convert Button Clicked")
|
||||||
print(self.ui.textInput.text())
|
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):
|
||||||
|
print("Execute Button Clicked")
|
||||||
|
ai = aisql.AI(self.apikey)
|
||||||
|
self.ui.outputLabel.setText("Executing...")
|
||||||
|
self.load_config()
|
||||||
|
sql = self.ui.shellInput.toPlainText()
|
||||||
|
# Check what method to use
|
||||||
|
decision = ai.decide(sql)
|
||||||
|
if "fetchall".casefold() in decision.casefold():
|
||||||
|
fetch = self.db.fetchall(sql)
|
||||||
|
print(fetch)
|
||||||
|
elif "fetchone".casefold() in decision.casefold():
|
||||||
|
fetch = self.db.fetchone(sql)
|
||||||
|
print(fetch)
|
||||||
|
elif "fetchmany".casefold() in decision.casefold():
|
||||||
|
size = decision.split("=")[1].strip("]")
|
||||||
|
fetch = elf.db.fetchmany(sql, int(size))
|
||||||
|
print(fetch)
|
||||||
|
elif "execute".casefold() in decision.casefold():
|
||||||
|
self.db.execute(sql)
|
||||||
|
elif "executemany".casefold() in decision.casefold():
|
||||||
|
size = decision.split("=")[1].strip("]")
|
||||||
|
self.db.executemany(sql, int(size))
|
||||||
|
|
||||||
|
self.ui.outputLabel.setText("Executed!")
|
||||||
|
|
||||||
### Connection Window ###
|
### Connection Window ###
|
||||||
class ConnectionWindow(QDialog):
|
class ConnectionWindow(QDialog):
|
||||||
|
|
@ -124,17 +210,33 @@ class ConnectionWindow(QDialog):
|
||||||
self.dbuser = self.ui.usernameInput.text()
|
self.dbuser = self.ui.usernameInput.text()
|
||||||
self.dbpass = self.ui.passwordInput.text()
|
self.dbpass = self.ui.passwordInput.text()
|
||||||
self.dbname = self.ui.databaseInput.text()
|
self.dbname = self.ui.databaseInput.text()
|
||||||
|
db = None
|
||||||
|
# Check type of Database
|
||||||
|
try:
|
||||||
if self.dbtype == 0: # PostgreSQL
|
if self.dbtype == 0: # PostgreSQL
|
||||||
import database.postgresql as postgresql
|
import database.postgresql as postgresql
|
||||||
db = postgresql.Postgre(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
|
db = postgresql.Postgres(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.ui.returnLabel.setText("Connection failed")
|
||||||
|
self.ui.returnLabel.setStyleSheet("color: red")
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
if db is not None:
|
||||||
|
try:
|
||||||
if db.test_connection():
|
if db.test_connection():
|
||||||
self.ui.returnLabel.setText("Connection successful")
|
self.ui.returnLabel.setText("Connection successful")
|
||||||
self.ui.returnLabel.setStyleSheet("color: green")
|
self.ui.returnLabel.setStyleSheet("color: green")
|
||||||
|
except Exception as e:
|
||||||
|
self.ui.returnLabel.setText("Connection failed")
|
||||||
|
self.ui.returnLabel.setStyleSheet("color: red")
|
||||||
|
print(e)
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
|
|
||||||
### Api Key Window ###
|
### Api Key Window ###
|
||||||
class ApiKeyWindow(QDialog):
|
class ApiKeyWindow(QDialog):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user