-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
112 lines (85 loc) · 3.75 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sys
import pandas as pd
from PyQt6 import QtCore
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QIcon
from PyQt6.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from tikToolsUI import Ui_MainWindow
from tools import genDDL_mysql, genDDL_psql, genSelect, genDBT_yml
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def data(self, index, role):
if role == Qt.ItemDataRole.DisplayRole:
value = self._data.iloc[index.row(), index.column()]
return str(value)
def rowCount(self, index):
return self._data.shape[0]
def columnCount(self, index):
return self._data.shape[1]
def headerData(self, section, orientation, role):
# section is the index of the column/row.
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return str(self._data.columns[section])
if orientation == Qt.Orientation.Vertical:
return str(self._data.index[section])
# 自定义一个窗口类
class TikTools(Ui_MainWindow, QMainWindow):
def __init__(self):
super().__init__() # 调用父类的构造方法
icon = QIcon(r"/res/icon.png") # pyinstaller在打包成exe时, 需使用绝对路径才能生效
self.setWindowIcon(icon)
self.setupUi(self) # 初始化构造界面
self.btn_select_file.clicked.connect(self.open_excel_file) # 事件绑定
self.btn_gen_ddl.clicked.connect(self.gen_ddl_content) # 事件绑定
self.radioBtn_msql.clicked.connect(self.set_db_type_msql)
self.radioBtn_psql.clicked.connect(self.set_db_type_psql)
self.radioBtn_dbt.clicked.connect(self.set_db_type_dbt)
self.menu_about.triggered.connect(self.about_info)
self.btn_gen_select.clicked.connect(self.gen_select_content)
self.db_type = 'dbt'
def open_excel_file(self):
home_dir = self.txt_file_name.text()
self.fname = QFileDialog.getOpenFileName(self, 'Open file', home_dir,filter="Excel Files (*.xlsx)")
if self.fname[0]:
self.txt_file_name.setText(self.fname[0])
df = pd.read_excel(self.fname[0],sheet_name='目录',usecols=[0,3,4,5],nrows=21)
df.dropna(subset=['表名'],inplace=True)
df.index = df.index + 1
self.model = TableModel(df)
self.table_file_content.setModel(self.model)
header = self.table_file_content.horizontalHeader()
font = QFont()
font.setBold(True)
header.setFont(font)
def gen_ddl_content(self):
fname = self.fname[0]
if self.db_type == 'psql':
mes_info, sql_content = genDDL_psql.gen_ddl(fname)
sql_content = mes_info + sql_content
elif self.db_type == 'msql':
mes_info, sql_content = genDDL_mysql.gen_ddl(fname)
sql_content = mes_info + sql_content
else:
mes_info, sql_content = genDBT_yml.gen_ddl(fname)
self.txt_ddl_content.setText(sql_content)
QMessageBox.information(self, "成功提示!", mes_info[:13])
def about_info(self):
QMessageBox.information(self, "关于", f"""Author:tik.xie\nVersion:v1.1""")
def gen_select_content(self):
sql = genSelect.gen_select()
self.txt_gen_select.setText(sql)
def set_db_type_psql(self):
self.db_type = 'psql'
def set_db_type_msql(self):
self.db_type = 'msql'
def set_db_type_dbt(self):
self.db_type = 'dbt'
# 主程序入口
if __name__ == '__main__':
app = QApplication(sys.argv)
window = TikTools()
window.show()
sys.exit(app.exec())