From 9258cec0303ba929bb021725c9b8fd64950b89af Mon Sep 17 00:00:00 2001 From: Nico Xiang <11958087+nicoxiang@users.noreply.github.com> Date: Mon, 9 Dec 2024 15:59:46 +0800 Subject: [PATCH] fix table keyword missing bug --- mainwindow.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 7fd76b4..d158521 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -64,7 +64,7 @@ QString MainWindow::addSchemaToSql(const QString &sql, const QString &schema) { // 匹配表名的正则表达式模式 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)(CREATE|ALTER|DROP)\s+(TABLE|VIEW|TRIGGER|PROCEDURE|FUNCTION)\s+(`?\w+`?))", // DDL语句 R"((?i)(INSERT\s+INTO)\s+(`?\w+`?))" // INSERT INTO table }; @@ -74,11 +74,20 @@ QString MainWindow::addSchemaToSql(const QString &sql, const QString &schema) { while (i.hasNext()) { QRegularExpressionMatch match = i.next(); - QString tableName = match.captured(2); - // 如果表名没有schema前缀,添加schema - if (!tableName.contains(".")) { - QString replacement = match.captured(1) + " " + schema + "." + tableName; + 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); + } } } }