-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
362 lines (313 loc) · 13.2 KB
/
mainwindow.cpp
File metadata and controls
362 lines (313 loc) · 13.2 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QFileDialog>
#include <QHeaderView>
#include <QTextStream>
#include <QRegularExpression>
#include <filesystem>
#include <QProcess>
#include <QLibraryInfo>
#include <QCoreApplication>
#include <QFileInfo>
#include <QDir>
using namespace std;
namespace fs = std::filesystem;
// ===== Helpers locales (copian semántica de las del headers SQL, para no depender de privados) =====
std::string MainWindow::trim_local(const std::string& s){
size_t a = 0, b = s.size();
while (a<b && isspace((unsigned char)s[a])) ++a;
while (b>a && isspace((unsigned char)s[b-1])) --b;
return s.substr(a, b-a);
}
std::string MainWindow::to_upper_local(std::string s){
for (auto& c : s) c = (char)toupper((unsigned char)c);
return s;
}
std::vector<std::string> MainWindow::split_csv_local(const std::string& s){
std::vector<std::string> out; std::string cur; bool inq=false;
for(char c : s){
if (c=='\''){ inq=!inq; cur.push_back(c); }
else if (!inq && c==','){ out.push_back(trim_local(cur)); cur.clear(); }
else cur.push_back(c);
}
if (!cur.empty()) out.push_back(trim_local(cur));
return out;
}
// ===== MainWindow =====
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, executor_(std::cout) // por defecto escribe a stdout; ConsoleWidget intercepta redirección
{
// ---- Toolbar
tb_ = addToolBar("Acciones");
auto actCreateDb = tb_->addAction("Crear BD");
auto actCreateTbl = tb_->addAction("Crear Tabla");
auto actExec = tb_->addAction("Ejecutar");
auto actTerm = tb_->addAction("Abrir Terminal");
connect(actCreateDb, &QAction::triggered, this, &MainWindow::onCreateDatabase);
connect(actCreateTbl, &QAction::triggered, this, &MainWindow::onCreateTable);
connect(actExec, &QAction::triggered, this, &MainWindow::onExecuteSQL);
connect(actTerm, &QAction::triggered, this, &MainWindow::onOpenTerminal);
// ---- Dock izquierdo: Árbol de archivos
dockFiles_ = new QDockWidget("Archivos", this);
fsModel_ = new QFileSystemModel(this);
fsModel_->setRootPath(QDir::currentPath()); // raíz = cwd
tree_ = new QTreeView(dockFiles_);
tree_->setModel(fsModel_);
tree_->setRootIndex(fsModel_->index(QDir::currentPath()));
tree_->setHeaderHidden(false);
tree_->setColumnWidth(0, 280);
connect(tree_, &QTreeView::activated, this, &MainWindow::onTreeActivated);
dockFiles_->setWidget(tree_);
addDockWidget(Qt::LeftDockWidgetArea, dockFiles_);
// ---- Central: Editor SQL
editor_ = new QPlainTextEdit(this);
editor_->setPlaceholderText("-- Escribe comandos SQL aquí. Usa 'help' en la consola para ver ejemplos.");
highlighter_ = new SqlHighlighter(editor_->document());
setCentralWidget(editor_);
// ---- Dock inferior: Consola
dockConsole_ = new QDockWidget("Consola SQL", this);
console_ = new ConsoleWidget(this);
dockConsole_->setWidget(console_);
addDockWidget(Qt::BottomDockWidgetArea, dockConsole_);
// ---- Dock inferior: Resultados
dockResults_ = new QDockWidget("Resultados", this);
resultsView_ = new QTableView(this);
resultsModel_ = new ResultTableModel(this);
resultsView_->setModel(resultsModel_);
resultsView_->horizontalHeader()->setStretchLastSection(true);
dockResults_->setWidget(resultsView_);
addDockWidget(Qt::BottomDockWidgetArea, dockResults_);
// Consola: conectar ejecución directa (REPL)
connect(console_, &ConsoleWidget::executeRequested, this, [this](const QString& sqlLine){
executeAndMaybeShowTable(sqlLine);
});
// Atajo: Ctrl+Enter ejecuta
editor_->installEventFilter(this);
}
void MainWindow::onCreateDatabase() {
bool ok=false;
QString name = QInputDialog::getText(this, "Crear Base de Datos",
"Nombre de carpeta (DB):", QLineEdit::Normal,
"mi_db", &ok);
if (!ok || name.trimmed().isEmpty()) return;
QString cmd = "CREATE DATABASE " + name.trimmed();
console_->appendLine(">> " + cmd);
executor_.execute(cmd.toStdString());
// Auto-USE (opcional)
QString useCmd = "USE " + name.trimmed();
console_->appendLine(">> " + useCmd);
executor_.execute(useCmd.toStdString());
applyUseFromSQL(useCmd);
}
void MainWindow::onCreateTable() {
// Presenta un diálogo simple para escribir el CREATE TABLE completo
bool ok=false;
QString sql = QInputDialog::getMultiLineText(this, "Crear Tabla",
"Ejemplo:\nCREATE TABLE ventas (cliente CHAR(32), total FLOAT, producto INT)", "", &ok);
if (!ok || sql.trimmed().isEmpty()) return;
console_->appendLine(">> " + sql);
executor_.execute(sql.toStdString());
}
void MainWindow::onExecuteSQL() {
const QString sql = editor_->toPlainText().trimmed();
if (sql.isEmpty()) return;
executeAndMaybeShowTable(sql);
}
void MainWindow::onOpenTerminal() {
#ifdef Q_OS_WIN
const QString workbenchDir = QCoreApplication::applicationDirPath();
const QString demoCli = QDir(workbenchDir).filePath("demo_cli.exe");
if (!QFileInfo::exists(demoCli)) {
QMessageBox::warning(this, "Terminal",
QString("No se encontró demo_cli.exe en:\n%1").arg(demoCli));
return;
}
// Asegura que el proceso vea las mismas DLL (Qt/MinGW) que tu Workbench
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
const QString qtBin = QLibraryInfo::path(QLibraryInfo::BinariesPath);
QString path = env.value("PATH");
path = QDir::toNativeSeparators(workbenchDir) + ";" +
QDir::toNativeSeparators(qtBin) + ";" + path;
env.insert("PATH", path);
// Lanza demo_cli.exe directamente (es app de consola -> Windows abre su ventana)
auto *p = new QProcess(this);
p->setProcessEnvironment(env);
p->setWorkingDirectory(workbenchDir);
p->setProgram(demoCli);
if (!p->startDetached()) {
QMessageBox::warning(this, "Terminal",
QString("No se pudo iniciar:\n%1").arg(demoCli));
}
p->deleteLater();
#elif defined(Q_OS_MACOS)
// Abre Terminal y ejecuta el binario desde la carpeta del Workbench
const QString exe = QDir(QCoreApplication::applicationDirPath()).filePath("demo_cli");
QProcess::startDetached("open", {"-a", "Terminal", exe},
QCoreApplication::applicationDirPath());
#else
const QString exe = QDir(QCoreApplication::applicationDirPath()).filePath("demo_cli");
if (!QProcess::startDetached("gnome-terminal", {"--", exe},
QCoreApplication::applicationDirPath())) {
QProcess::startDetached("xterm", {"-e", exe},
QCoreApplication::applicationDirPath());
}
#endif
}
void MainWindow::onTreeActivated(const QModelIndex& idx) {
const QString path = fsModel_->filePath(idx);
if (QFileInfo(path).isDir()) {
// Sugerir USE db si parece ser carpeta de DB (tiene *.tbl dentro)
QDir d(path);
auto list = d.entryList(QStringList() << "*.tbl", QDir::Files);
if (!list.isEmpty()) {
QString cmd = "USE " + QFileInfo(path).fileName();
console_->appendLine(">> " + cmd);
executor_.execute(cmd.toStdString());
currentDbPath_ = path;
}
}
}
bool MainWindow::isSelect(const QString& sql) {
QString s = sql.trimmed();
return s.left(6).toUpper()=="SELECT";
}
void MainWindow::executeAndMaybeShowTable(const QString& sql) {
console_->appendLine(">> " + sql);
// 1) Ejecuta el SQL (si es USE, MiniDBSQL cambiará internamente)
executor_.execute(sql.toStdString());
// 2) Actualiza el GUI si era un USE (para que tryRenderSelect sepa la ruta)
applyUseFromSQL(sql);
// 3) Si es SELECT, intenta renderizarlo en la tabla
if (isSelect(sql)) {
if (!tryRenderSelect(sql)) {
// Si falla, deja solo el output textual del executor
// (opcional: console_->appendLine("[GUI] No se pudo renderizar SELECT");
}
}
}
// --------- SELECT renderer (no modifica tu executor; lee directamente la tabla) ----------
#include "MiniDBSQL.h" // reutilizamos helpers públicos
bool MainWindow::tryRenderSelect(const QString& qsql) {
using namespace sqlmini;
using gft::GenericFixedTable; using gft::Value; using gft::ColType;
std::string full = qsql.toStdString();
auto up = to_upper_local(full);
size_t psel = up.find("SELECT "); if (psel!=0) return false;
size_t pfrom = up.find(" FROM ", 7); if (pfrom==std::string::npos) return false;
auto proj = trim_local(full.substr(7, pfrom-7));
size_t pwhere = up.find(" WHERE ", pfrom+6);
std::string tname, wexpr;
if (pwhere==std::string::npos) tname = trim_local(full.substr(pfrom+6));
else {
tname = trim_local(full.substr(pfrom+6, pwhere-(pfrom+6)));
wexpr = trim_local(full.substr(pwhere+7));
}
// Localiza ruta de tabla bajo currentDbPath_
if (currentDbPath_.isEmpty()) return false;
fs::path tfile = fs::path(currentDbPath_.toStdString()) / tname / (tname + ".tbl");
// Reutilizar la lectura del esquema desde MiniDBSQL.h (función global)
sqlmini::TableSchema sc;
if (!sqlmini::load_schema_from_tbl(tfile, sc)) return false;
// Proyección
std::vector<int> proj_idx;
if (proj == "*") {
for (int i=0;i<sc.ncols;++i) proj_idx.push_back(i);
} else {
auto cs = split_csv_local(proj);
for (auto& c : cs){
int idx=-1;
for (int i=0;i<sc.ncols;++i) if (sc.cols[i].name==c){ idx=i; break; }
if (idx==-1) return false;
proj_idx.push_back(idx);
}
}
// Localiza índice de la columna 'id' (para filtrar borrados lógicos id == -1)
int id_idx = -1;
for (int i=0;i<sc.ncols;++i) {
if (sc.cols[i].name == "id" && sc.cols[i].type == ColType::INT32) { id_idx = i; break; }
}
auto is_deleted = [&](const std::vector<Value>& row)->bool {
if (id_idx < 0) return false;
return row[id_idx].i == -1;
};
// Cargar toda la tabla (para simplicidad; si quieres, puedes replicar el uso de índices aquí)
GenericFixedTable tbl(tfile.string(), tname, std::vector<gft::ColumnDef>{}, /*create_new*/false);
const long n = tbl.Count();
// Si hay WHERE, filtramos con helpers del executor:
std::vector<int> pids;
if (wexpr.empty()) {
for (long i=0;i<n;++i) {
std::vector<Value> row;
if (!tbl.ReadRowByPageID(i, row)) continue;
if (is_deleted(row)) continue; // <<--- filtra borradas
pids.push_back((int)i);
}
} else {
sqlmini::Where w{};
if (!sqlmini::parse_where(wexpr, w)) return false;
for (long i=0;i<n;++i){
std::vector<Value> row;
if (!tbl.ReadRowByPageID(i, row)) continue;
if (is_deleted(row)) continue; // <<--- filtra borradas antes de evaluar WHERE
bool r1 = w.p1 ? sqlmini::eval_pred_row(*w.p1, sc, row) : true;
bool r2 = w.p2 ? sqlmini::eval_pred_row(*w.p2, sc, row) : true;
bool ok = w.op=="AND" ? (r1 && r2) : (w.op=="OR" ? (r1 || r2) : r1);
if (ok) pids.push_back((int)i);
}
}
// Construye headers y filas para el QTableView
QStringList headers;
for (int j : proj_idx) headers << QString::fromStdString(sc.cols[j].name);
std::vector<std::vector<Value>> data;
data.reserve(pids.size());
for (int pid : pids){
std::vector<Value> row;
if (!tbl.ReadRowByPageID(pid, row)) continue;
if (is_deleted(row)) continue;
std::vector<Value> out;
out.reserve(proj_idx.size());
for (int j : proj_idx) out.push_back(row[j]);
data.push_back(std::move(out));
}
resultsModel_->setData(headers, data, sc); // muestra
resultsView_->resizeColumnsToContents();
return true;
}
bool MainWindow::applyUseFromSQL(const QString& sql) {
// Acepta "USE name" (con o sin ';'), espacios extras, case-insensitive.
QString s = sql.trimmed();
if (s.endsWith(';')) s.chop(1);
if (!s.startsWith("USE ", Qt::CaseInsensitive)) return false;
QString dbname = s.mid(4).trimmed(); // después de "USE "
if (dbname.isEmpty()) return false;
// Si es ruta absoluta, úsala; si es nombre, asume subcarpeta del cwd
QString path = dbname;
if (QDir::isAbsolutePath(dbname)) {
path = dbname;
} else {
path = QDir::current().absoluteFilePath(dbname);
}
// Verifica que exista y sea carpeta
QDir d(path);
if (!d.exists()) {
console_->appendLine(QString("[GUI] Carpeta DB no existe: %1").arg(path));
return false;
}
setCurrentDbPath(path);
return true;
}
void MainWindow::setCurrentDbPath(const QString& path) {
currentDbPath_ = QDir(path).absolutePath();
// Opcional: mover el árbol a esa carpeta
if (fsModel_) {
QModelIndex idx = fsModel_->index(currentDbPath_);
if (idx.isValid()) {
tree_->setRootIndex(idx);
}
}
// Feedback visual
console_->appendLine(QString("[GUI] DB actual: %1").arg(currentDbPath_));
}