Compare commits
No commits in common. "3336f12a18c1afd2b4df9acecc1f201431ccc01d" and "9afa8bec497ee73f8959eb6e1d42af8dbba0ab8e" have entirely different histories.
3336f12a18
...
9afa8bec49
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"database":
|
||||
{
|
||||
"dbtype": 0,
|
||||
"dbtype": "PostgreSQL",
|
||||
"ip": "localhost",
|
||||
"password": "password",
|
||||
"user": "admin",
|
||||
|
|
|
|||
118
gui/main.py
118
gui/main.py
|
|
@ -25,10 +25,6 @@ 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)
|
||||
|
|
@ -39,53 +35,6 @@ 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()
|
||||
|
|
@ -95,44 +44,9 @@ 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")
|
||||
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!")
|
||||
print(self.ui.textInput.text())
|
||||
|
||||
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):
|
||||
|
|
@ -210,32 +124,16 @@ 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)
|
||||
|
||||
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:
|
||||
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:
|
||||
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):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user