-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
168 lines (141 loc) · 5.5 KB
/
mainwindow.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "mainwindow.h"
#include <QFileDialog>
#include <QScrollArea>
#include <QFile>
#include <QTextStream>
#include <QStandardPaths>
#include <QDir>
#include <QRegularExpression>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setWindowTitle("MySQL Add Schema工具");
setGeometry(100, 100, 800, 600);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QFont buttonFont;
buttonFont.setPointSize(12);
fileBtn = new QPushButton("选择SQL文件", this);
fileBtn->setFont(buttonFont);
fileBtn->setMinimumHeight(50);
fileBtn->setStyleSheet("QPushButton { padding: 10px; }");
connect(fileBtn, &QPushButton::clicked, this, &MainWindow::selectFile);
layout->addWidget(fileBtn);
QScrollArea *scroll = new QScrollArea;
QWidget *scrollWidget = new QWidget;
schemaLayout = new QVBoxLayout(scrollWidget);
scroll->setWidget(scrollWidget);
scroll->setWidgetResizable(true);
layout->addWidget(scroll);
QPushButton *confirmBtn = new QPushButton("生成SQL", this);
confirmBtn->setFont(buttonFont);
confirmBtn->setMinimumHeight(50);
confirmBtn->setStyleSheet("QPushButton { padding: 10px; background-color: #4CAF50; color: black; }");
connect(confirmBtn, &QPushButton::clicked, this, &MainWindow::generateSql);
layout->addWidget(confirmBtn);
loadSchemas();
}
void MainWindow::showWarning(const QString &message) {
QMessageBox::warning(this, "警告", message);
}
void MainWindow::selectFile() {
QString fileName = QFileDialog::getOpenFileName(this,
tr("选择SQL文件"),
QString(),
tr("SQL files (*.sql);;All Files (*)"));
if (!fileName.isEmpty()) {
selectedFile = fileName;
fileBtn->setText("已选择: " + QFileInfo(fileName).fileName());
}
}
QString MainWindow::addSchemaToSql(const QString &sql, const QString &schema) {
QString modifiedSql = sql;
// 匹配表名的正则表达式模式
QStringList patterns = {
R"((?i)(FROM|JOIN|UPDATE|INTO)\s+(`?\w+`?))", // FROM table, JOIN table, UPDATE table, INTO table
R"((?i)(CREATE|ALTER|DROP)\s+(TABLE|VIEW|TRIGGER|PROCEDURE|FUNCTION)\s+(`?\w+`?))", // DDL语句
R"((?i)(INSERT\s+INTO)\s+(`?\w+`?))" // INSERT INTO table
};
for (const QString &pattern : patterns) {
QRegularExpression regex(pattern);
QRegularExpressionMatchIterator i = regex.globalMatch(modifiedSql);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString tableName;
if (pattern.contains("CREATE|ALTER|DROP")) {
tableName = match.captured(3);
// 保留关键字(TABLE等)
QString keyword = match.captured(2);
QString replacement = match.captured(1) + " " + keyword + " " + schema + "." + tableName;
modifiedSql.replace(match.captured(0), replacement);
} else {
tableName = match.captured(2);
// 如果表名没有schema前缀,添加schema
if (!tableName.contains(".")) {
QString replacement = match.captured(1) + " " + schema + "." + tableName;
modifiedSql.replace(match.captured(0), replacement);
}
}
}
}
return modifiedSql;
}
void MainWindow::generateSql() {
// 检查是否选择了SQL文件
if (selectedFile.isEmpty()) {
showWarning("请先选择SQL文件!");
return;
}
// 检查是否选择了至少一个schema
QStringList selectedSchemas;
for (QCheckBox *checkbox : schemaCheckboxes) {
if (checkbox->isChecked()) {
selectedSchemas.append(checkbox->text());
}
}
if (selectedSchemas.isEmpty()) {
showWarning("请至少选择一个Schema!");
return;
}
QFile inputFile(selectedFile);
if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
showWarning("无法打开SQL文件!");
return;
}
QString sqlContent = QTextStream(&inputFile).readAll();
inputFile.close();
QStringList sqlStatements = sqlContent.split(';', Qt::SkipEmptyParts);
QString outputFileName = selectedFile;
outputFileName.replace(".sql", "_generated.sql");
QFile outputFile(outputFileName);
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
showWarning("无法创建输出文件!");
return;
}
QTextStream out(&outputFile);
for (const QString &schema : selectedSchemas) {
for (QString stmt : sqlStatements) {
stmt = stmt.trimmed();
if (!stmt.isEmpty()) {
out << addSchemaToSql(stmt, schema) << ";\n\n";
}
}
}
outputFile.close();
QMessageBox::information(this, "成功", "文件生成成功!");
}
void MainWindow::loadSchemas() {
QString configPath = QDir::home().filePath("schemas.conf");
QFile file(configPath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
while (!in.atEnd()) {
QString schema = in.readLine().trimmed();
if (!schema.isEmpty()) {
QCheckBox *checkbox = new QCheckBox(schema);
schemaCheckboxes.append(checkbox);
schemaLayout->addWidget(checkbox);
}
}
file.close();
}
}