Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

v0.1.9: reserved-keyword query log hint + auto-quote identifiers (#40)

haidang1810b401754dfb6c
diff --git a/windows/Gridex/WorkspacePage.cpp b/windows/Gridex/WorkspacePage.cpp
index d98b793..a48649d 100644
--- a/windows/Gridex/WorkspacePage.cpp
+++ b/windows/Gridex/WorkspacePage.cpp
@@ -368,7 +368,16 @@ namespace winrt::Gridex::implementation
{
try
{
- auto result = adapter->execute(sql);
+ // Pre-process: wrap reserved-keyword identifiers
+ // (FROM order → FROM "order") so PG/MySQL stop
+ // rejecting them. The rewrite is conservative —
+ // only triggers after FROM/JOIN/INTO/UPDATE/TABLE.
+ bool rewritten = false;
+ std::wstring effective = AutoQuoteReservedIdentifiers(
+ sql, state_.connection.databaseType, rewritten);
+
+ auto result = adapter->execute(effective);
+ if (rewritten) result.sql = effective; // log rewritten form
LogQuery(result);
return result;
}
@@ -2648,6 +2657,213 @@ namespace winrt::Gridex::implementation
// ── Query Log ───────────────────────────────────────
+ namespace {
+ // Words we'll wrap when they appear as identifiers. Same shared
+ // subset the hint helper uses — see ReservedKeywordHint comment.
+ const std::vector<std::wstring>& kAutoQuoteReserved()
+ {
+ static const std::vector<std::wstring> kw = {
+ L"order", L"user", L"group", L"table", L"column", L"index",
+ L"key", L"primary", L"references", L"limit", L"offset",
+ L"join", L"left", L"right", L"inner", L"outer", L"check",
+ L"unique", L"foreign", L"timestamp", L"date", L"time",
+ L"int", L"integer", L"text", L"value", L"values", L"asc",
+ L"desc", L"default", L"null",
+ };
+ return kw;
+ }
+
+ // Anchors after which the next token is *always* an identifier.
+ // SELECT/WHERE column lists are intentionally excluded — too
+ // many false positives without a real parser.
+ bool isIdentifierAnchor(const std::wstring& upper)
+ {
+ return upper == L"FROM" || upper == L"JOIN" ||
+ upper == L"INTO" || upper == L"UPDATE" ||
+ upper == L"TABLE";
+ }
+
+ bool isWordChar(wchar_t c)
+ {
+ return (c >= L'a' && c <= L'z') ||
+ (c >= L'A' && c <= L'Z') ||
+ (c >= L'0' && c <= L'9') || c == L'_';
+ }
+ }
+
+ std::wstring WorkspacePage::AutoQuoteReservedIdentifiers(
+ const std::wstring& sql,
+ DBModels::DatabaseType dbType,
+ bool& wasRewritten)
+ {
+ wasRewritten = false;
+ if (sql.empty()) return sql;
+
+ const wchar_t q = (dbType == DBModels::DatabaseType::MySQL ||
+ dbType == DBModels::DatabaseType::ClickHouse)
+ ? L'`' : L'"';
+
+ std::wstring out;
+ out.reserve(sql.size() + 16);
+
+ const auto& reserved = kAutoQuoteReserved();
+ std::wstring prevWordUpper; // last identifier-shaped token
+ size_t i = 0;
+ const size_t n = sql.size();
+
+ while (i < n)
+ {
+ wchar_t c = sql[i];
+
+ // Pass through string literals untouched ('...', "...", `...`).
+ if (c == L'\'' || c == L'"' || c == L'`')
+ {
+ wchar_t open = c;
+ out += c; ++i;
+ while (i < n)
+ {
+ wchar_t cc = sql[i];
+ out += cc; ++i;
+ if (cc == L'\\' && i < n) { out += sql[i++]; continue; }
+ if (cc == open) break;
+ }
+ prevWordUpper.clear();
+ continue;
+ }
+
+ // Pass through line + block comments.
+ if (c == L'-' && i + 1 < n && sql[i + 1] == L'-')
+ {
+ while (i < n && sql[i] != L'\n') { out += sql[i++]; }
+ continue;
+ }
+ if (c == L'/' && i + 1 < n && sql[i + 1] == L'*')
+ {
+ out += sql[i++]; out += sql[i++];
+ while (i + 1 < n && !(sql[i] == L'*' && sql[i + 1] == L'/'))
+ out += sql[i++];
+ if (i + 1 < n) { out += sql[i++]; out += sql[i++]; }
+ continue;
+ }
+
+ // Word token: collect [A-Za-z0-9_]+
+ if (isWordChar(c) && !(c >= L'0' && c <= L'9'))
+ {
+ size_t start = i;
+ while (i < n && isWordChar(sql[i])) ++i;
+ std::wstring tok = sql.substr(start, i - start);
+
+ // Lower / upper for matching only, output keeps original case.
+ std::wstring lower = tok;
+ std::transform(lower.begin(), lower.end(), lower.begin(),
+ [](wchar_t cc) { return std::towlower(cc); });
+ std::wstring upper = tok;
+ std::transform(upper.begin(), upper.end(), upper.begin(),
+ [](wchar_t cc) { return std::towupper(cc); });
+
+ bool quoted = false;
+ if (isIdentifierAnchor(prevWordUpper))
+ {
+ for (auto& kw : reserved)
+ {
+ if (kw == lower)
+ {
+ out += q; out += tok; out += q;
+ wasRewritten = true;
+ quoted = true;
+ break;
+ }
+ }
+ }
+ if (!quoted) out += tok;
+ prevWordUpper = upper;
+ continue;
+ }
+
+ // Anything else: punctuation, whitespace, digits, etc.
+ // Whitespace / punctuation doesn't reset prevWordUpper —
+ // we still want `FROM order` and `FROM\norder` to work.
+ // But hitting a comma / semicolon / paren means the
+ // anchor relationship is broken.
+ if (c == L',' || c == L';' || c == L'(' || c == L')')
+ prevWordUpper.clear();
+
+ out += c;
+ ++i;
+ }
+
+ return out;
+ }
+
+ std::wstring WorkspacePage::ReservedKeywordHint(
+ const std::wstring& sql,
+ const std::wstring& error,
+ DBModels::DatabaseType dbType)
+ {
+ // Tight common subset shared across PG/MySQL/MSSQL/SQLite.
+ // Adding every dialect's full reserved list bloats the binary
+ // for marginal value — these are the words users actually
+ // collide with when naming tables/columns.
+ static const std::vector<std::wstring> kReserved = {
+ L"order", L"user", L"group", L"select", L"from", L"where",
+ L"table", L"column", L"index", L"key", L"primary", L"references",
+ L"limit", L"offset", L"join", L"left", L"right", L"inner",
+ L"outer", L"on", L"and", L"or", L"not", L"null", L"default",
+ L"check", L"unique", L"foreign", L"timestamp", L"date", L"time",
+ L"int", L"integer", L"text", L"value", L"values", L"asc", L"desc",
+ };
+
+ // Lower-cased copies for case-insensitive matching.
+ auto toLower = [](std::wstring s) {
+ std::transform(s.begin(), s.end(), s.begin(),
+ [](wchar_t c) { return std::towlower(c); });
+ return s;
+ };
+ const std::wstring sqlLower = toLower(sql);
+ const std::wstring errLower = toLower(error);
+
+ // Heuristic: the error mentions "syntax error" + "at or near"
+ // (PG / MySQL phrasing). Pull the keyword out of the quoted
+ // segment if present.
+ if (errLower.find(L"syntax error") == std::wstring::npos) return L"";
+
+ std::wstring suspect;
+ // Try "at or near \"X\"" first (PG)
+ auto p = errLower.find(L"at or near \"");
+ if (p != std::wstring::npos)
+ {
+ auto end = errLower.find(L'"', p + 12);
+ if (end != std::wstring::npos)
+ suspect = errLower.substr(p + 12, end - (p + 12));
+ }
+ // Fallback: first reserved keyword that appears unquoted in SQL
+ if (suspect.empty())
+ {
+ for (auto& kw : kReserved)
+ if (sqlLower.find(L" " + kw + L" ") != std::wstring::npos ||
+ sqlLower.find(L" " + kw) != std::wstring::npos)
+ {
+ suspect = kw; break;
+ }
+ }
+ if (suspect.empty()) return L"";
+
+ // Confirm it's actually a reserved word we know about.
+ bool isReserved = false;
+ for (auto& kw : kReserved)
+ if (kw == suspect) { isReserved = true; break; }
+ if (!isReserved) return L"";
+
+ // Pick the dialect's identifier quote char.
+ const wchar_t q = (dbType == DBModels::DatabaseType::MySQL ||
+ dbType == DBModels::DatabaseType::ClickHouse)
+ ? L'`' : L'"';
+
+ return L"`" + suspect + L"` is a reserved SQL keyword. Wrap it in "
+ + std::wstring(1, q) + suspect + std::wstring(1, q)
+ + L" to use it as an identifier.";
+ }
+
void WorkspacePage::LogQuery(const DBModels::QueryResult& result, double renderTimeMs)
{
DBModels::QueryLogEntry entry;
@@ -2734,6 +2950,27 @@ namespace winrt::Gridex::implementation
winrt::Windows::UI::ColorHelper::FromArgb(255, 196, 43, 28)));
errTb.Opacity(0.8);
QueryLogEntries().Children().Append(errTb);
+
+ // Reserved-keyword hint: PG / MySQL frequently produce
+ // a generic "syntax error at or near 'X'" for a token
+ // that is actually a reserved word being used as an
+ // unquoted identifier (e.g. `select id from order`).
+ // Surface a one-line hint so the user knows to wrap
+ // the offending name in the dialect's quote char.
+ std::wstring hint = ReservedKeywordHint(
+ entry.sql, entry.error, state_.connection.databaseType);
+ if (!hint.empty())
+ {
+ muxc::TextBlock hintTb;
+ hintTb.Text(winrt::hstring(L"-- HINT: " + hint));
+ hintTb.FontSize(11);
+ hintTb.FontFamily(mux::Media::FontFamily(L"Cascadia Code,Consolas,monospace"));
+ hintTb.IsTextSelectionEnabled(true);
+ hintTb.Foreground(muxm::SolidColorBrush(
+ winrt::Windows::UI::ColorHelper::FromArgb(255, 200, 150, 40)));
+ hintTb.Opacity(0.9);
+ QueryLogEntries().Children().Append(hintTb);
+ }
}
}
diff --git a/windows/Gridex/WorkspacePage.h b/windows/Gridex/WorkspacePage.h
index 9e1339c..cdb8189 100644
--- a/windows/Gridex/WorkspacePage.h
+++ b/windows/Gridex/WorkspacePage.h
@@ -116,6 +116,25 @@ namespace winrt::Gridex::implementation
// (CREATE FUNCTION DDL or body) fetched via
// DatabaseAdapter::getFunctionSource.
void ShowFunctionSource(const std::wstring& name, const std::wstring& schema);
+ // Inspect a failed query's error text for the typical
+ // "syntax error at or near 'X'" pattern. If X is a known
+ // reserved keyword for the active dialect AND it appears
+ // unquoted in the SQL, return a short user-facing hint
+ // suggesting the right quote char. Empty string = no hint.
+ static std::wstring ReservedKeywordHint(
+ const std::wstring& sql,
+ const std::wstring& error,
+ DBModels::DatabaseType dbType);
+ // Scan SQL for reserved keywords appearing in unambiguous
+ // identifier positions (after FROM / JOIN / INTO / UPDATE /
+ // TABLE) and wrap them with the dialect's quote char so
+ // queries like `select id from order` execute without forcing
+ // the user to remember the quoting rules. Returns the rewritten
+ // SQL and sets `wasRewritten` if anything changed.
+ static std::wstring AutoQuoteReservedIdentifiers(
+ const std::wstring& sql,
+ DBModels::DatabaseType dbType,
+ bool& wasRewritten);
// ── CRUD Operations ─────────────────────────
void DeleteSelectedRow();
diff --git a/windows/README.md b/windows/README.md
index 1884a0a..4235ef6 100644
--- a/windows/README.md
+++ b/windows/README.md
@@ -66,29 +66,29 @@ windows/scripts/
**Git Bash / MSYS2**:
```bash
cd /e/dev/be/vura
-powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-and-pack.ps1 -Version "0.1.8"
+powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-and-pack.ps1 -Version "0.1.9"
```
**PowerShell (5.1 / 7+)**:
```powershell
cd E:\dev\be\vura
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.8"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.9"
```
**CMD**:
```cmd
cd /d E:\dev\be\vura
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.8"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.9"
```
Script sẽ:
1. Cài/update `vpk` global tool (skip nếu đã có)
-2. Build `Gridex.exe` unpackaged, stamp version `0.1.8` vào About section
-3. Pack thành `Setup.exe` qua Velopack với metadata version `0.1.8`
+2. Build `Gridex.exe` unpackaged, stamp version `0.1.9` vào About section
+3. Pack thành `Setup.exe` qua Velopack với metadata version `0.1.9`
**Output**: `windows/Releases/`
- `Gridex-stable-Setup.exe` — installer user-facing
-- `Gridex-0.1.8-stable-full.nupkg` — package Velopack
+- `Gridex-0.1.9-stable-full.nupkg` — package Velopack
- `releases.stable.json`, `RELEASES-stable` — feed manifests
- `Gridex-stable-Portable.zip` — bản portable
@@ -140,7 +140,7 @@ powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\install-vp
powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-unpackaged.ps1
# Build với version cụ thể — About section hiện đúng string
-powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-unpackaged.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-unpackaged.ps1 -Version "0.1.9-test"
```
**PowerShell**:
@@ -149,7 +149,7 @@ powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-unpa
powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1
# Build với version cụ thể — About section hiện đúng string
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1 -Version "0.1.9-test"
```
**CMD**:
@@ -158,7 +158,7 @@ REM Dev build — About section hiện "0.0.0-dev"
powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1
REM Build với version cụ thể — About section hiện đúng string
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-unpackaged.ps1 -Version "0.1.9-test"
```
Chạy trực tiếp exe để test nhanh mà không cần cài installer:
@@ -177,17 +177,17 @@ Chạy trực tiếp exe để test nhanh mà không cần cài installer:
**Git Bash / MSYS2**:
```bash
-powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/pack-velopack.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/pack-velopack.ps1 -Version "0.1.9-test"
```
**PowerShell**:
```powershell
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\pack-velopack.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\pack-velopack.ps1 -Version "0.1.9-test"
```
**CMD**:
```cmd
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\pack-velopack.ps1 -Version "0.1.8-test"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\pack-velopack.ps1 -Version "0.1.9-test"
```
**Lưu ý**: `-Version` truyền vào `pack-velopack.ps1` **phải khớp** với `-Version` truyền vào `build-unpackaged.ps1`, nếu không thì Velopack sẽ in một số, còn About section của app sẽ in số khác. Dùng `build-and-pack.ps1` ở trên để tránh trường hợp này.
@@ -196,10 +196,10 @@ powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\pack-velop
- `windows/Gridex/GridexVersion.h` — include unconditional file generated
- `windows/Gridex/GridexVersion.generated.h` — **auto-generated, gitignored**, do `build-unpackaged.ps1` ghi mỗi lần chạy
- - Có `-Version` → `#define GRIDEX_VERSION L"0.1.8"`
+ - Có `-Version` → `#define GRIDEX_VERSION L"0.1.9"`
- Không `-Version` → `#define GRIDEX_VERSION L"0.0.0-dev"`
- `windows/Gridex/SettingsPage.xaml.cpp` — đọc `GRIDEX_VERSION` và set vào `VersionText` text block
-- CI workflow `.github/workflows/windows-release.yml` tự parse version từ git tag (`v0.1.8` → `0.1.8`) và forward sang script
+- CI workflow `.github/workflows/windows-release.yml` tự parse version từ git tag (`v0.1.9` → `0.1.9`) và forward sang script
Build script chỉ rewrite `GridexVersion.generated.h` khi nội dung khác → không force recompile vô ích.
@@ -209,17 +209,17 @@ Mặc định Velopack dùng channel `stable`. Nếu muốn phát hành kênh ri
**Git Bash / MSYS2**:
```bash
-powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-and-pack.ps1 -Version "0.1.8-beta.1" -Channel "beta"
+powershell -NoProfile -ExecutionPolicy Bypass -File ./windows/scripts/build-and-pack.ps1 -Version "0.1.9-beta.1" -Channel "beta"
```
**PowerShell**:
```powershell
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.8-beta.1" -Channel "beta"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.9-beta.1" -Channel "beta"
```
**CMD**:
```cmd
-powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.8-beta.1" -Channel "beta"
+powershell -NoProfile -ExecutionPolicy Bypass -File .\windows\scripts\build-and-pack.ps1 -Version "0.1.9-beta.1" -Channel "beta"
```
Output sẽ là `Gridex-beta-Setup.exe` + `releases.beta.json` — auto-updater phân luồng theo channel.
@@ -253,7 +253,7 @@ Chưa cài đủ vcpkg deps. Xem section "Cài vcpkg deps" ở đầu file.
### Gridex.exe crash ngay khi khởi động với `MSVCP140.dll not found`
Máy thiếu VC++ 2015-2022 Redistributable. Script `build-unpackaged.ps1` tự copy CRT DLLs vào output folder → nếu vẫn lỗi thì check VS install path / logs script.
-### Setup.exe cài xong nhưng About hiện `0.0.0-dev` dù pack với `-Version 0.1.8`
+### Setup.exe cài xong nhưng About hiện `0.0.0-dev` dù pack với `-Version 0.1.9`
Dùng `build-and-pack.ps1` thay vì gọi `pack-velopack.ps1` trực tiếp. Trường hợp chạy `pack-velopack.ps1` với version khác version lúc build `Gridex.exe` → xóa `windows\Gridex\x64\Release\` rồi build lại đúng thứ tự.
### Lỗi `pwsh.exe exited with code 9009` trong vcpkg applocal step
@@ -288,8 +288,8 @@ del /q .\windows\Gridex\GridexVersion.generated.h
CI tự chạy khi push tag matching `v*.*.*`:
```bash
-git tag v0.1.8
-git push origin v0.1.8
+git tag v0.1.9
+git push origin v0.1.9
```
GitHub Actions workflow `windows-release.yml` sẽ tự build + pack + upload assets lên GitHub Release. Xem chi tiết ở `.github/workflows/windows-release.yml`.