diff --git a/gui/main.py b/gui/main.py index 7278d02..eb2b7db 100644 --- a/gui/main.py +++ b/gui/main.py @@ -25,6 +25,10 @@ class MainWindow(QMainWindow): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) + self.tableschema = [] + + # Hide outputLabel + self.ui.outputLabel.hide() # Open Connection Window self.ui.actionConnect_DB.triggered.connect(self.open_connection) @@ -35,6 +39,53 @@ class MainWindow(QMainWindow): # Pressed Convert Button 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): self.connection_window = ConnectionWindow(self) self.connection_window.show() @@ -44,9 +95,44 @@ class MainWindow(QMainWindow): 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") - 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 ### class ConnectionWindow(QDialog): @@ -124,16 +210,32 @@ class ConnectionWindow(QDialog): 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) - if self.dbtype == 0: # PostgreSQL - import database.postgresql as postgresql - db = postgresql.Postgre(self.dbip, self.dbport, self.dbuser, self.dbpass, self.dbname) - if db.test_connection(): - self.ui.returnLabel.setText("Connection successful") - self.ui.returnLabel.setStyleSheet("color: green") - else: + 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(): + 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) + + else: + self.ui.returnLabel.setText("Connection failed") + self.ui.returnLabel.setStyleSheet("color: red") + ### Api Key Window ### class ApiKeyWindow(QDialog):