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++

feat(sidebar): Truncate Table menu entry + handler

DangHuynh4071c7122944
diff --git a/windows/Gridex/SidebarPanel.cpp b/windows/Gridex/SidebarPanel.cpp
index f5e0ba7..c84c742 100644
--- a/windows/Gridex/SidebarPanel.cpp
+++ b/windows/Gridex/SidebarPanel.cpp
@@ -446,14 +446,22 @@ namespace winrt::Gridex::implementation
{ if (OnImportTable) OnImportTable(exportName, exportSchema); });
contextMenu.Items().Append(importItem);
- // Delete Table — destructive. Only offered for real tables
- // (not views) since DROP VIEW needs a different code path.
- // Host shows a confirmation dialog before actually running
- // DROP TABLE.
+ // Truncate + Delete — destructive. Only offered for
+ // real tables (not views) since views can't be truncated
+ // and DROP VIEW needs a different code path. Host shows
+ // a confirmation dialog before running the statement.
if (item.type == DBModels::SidebarItemType::Table)
{
contextMenu.Items().Append(muxc::MenuFlyoutSeparator());
+ muxc::MenuFlyoutItem truncateItem;
+ truncateItem.Text(L"Truncate Table...");
+ truncateItem.Icon(muxc::FontIcon());
+ truncateItem.Icon().as<muxc::FontIcon>().Glyph(L"\xE74D"); // eraser-ish; trash is close enough
+ truncateItem.Click([this, exportName, exportSchema](auto&&, auto&&)
+ { if (OnTruncateTable) OnTruncateTable(exportName, exportSchema); });
+ contextMenu.Items().Append(truncateItem);
+
muxc::MenuFlyoutItem deleteItem;
deleteItem.Text(L"Delete Table...");
deleteItem.Icon(muxc::FontIcon());
diff --git a/windows/Gridex/SidebarPanel.h b/windows/Gridex/SidebarPanel.h
index b121214..7567ff7 100644
--- a/windows/Gridex/SidebarPanel.h
+++ b/windows/Gridex/SidebarPanel.h
@@ -28,6 +28,9 @@ namespace winrt::Gridex::implementation
// Callbacks for add/delete table buttons
std::function<void()> OnAddTable;
std::function<void(const std::wstring& tableName, const std::wstring& schema)> OnDeleteTable;
+ // Truncate — same signature as OnDeleteTable. Host confirms
+ // + emits TRUNCATE TABLE (or DELETE FROM on SQLite).
+ std::function<void(const std::wstring& tableName, const std::wstring& schema)> OnTruncateTable;
// Callback for export table (tableName, schema, format: "csv"/"json"/"sql")
std::function<void(const std::wstring& tableName, const std::wstring& schema,
diff --git a/windows/Gridex/WorkspacePage.cpp b/windows/Gridex/WorkspacePage.cpp
index db1ba7c..7c82e54 100644
--- a/windows/Gridex/WorkspacePage.cpp
+++ b/windows/Gridex/WorkspacePage.cpp
@@ -609,6 +609,106 @@ namespace winrt::Gridex::implementation
});
};
+ // Sidebar truncate table — confirm then TRUNCATE (DELETE on SQLite).
+ // Wipes all rows, keeps the schema. Identical confirmation UX to
+ // OnDeleteTable; only the SQL emitted differs, and the tab-close
+ // snapshot trick is skipped (the tab stays valid, it just shows
+ // zero rows after the refresh).
+ Sidebar().as<SidebarPanel>()->OnTruncateTable = [this](const std::wstring& tableName, const std::wstring& schema)
+ {
+ if (!connMgr_.isConnected() || tableName.empty()) return;
+
+ muxc::ContentDialog dlg;
+ dlg.Title(winrt::box_value(winrt::hstring(L"Truncate Table")));
+ dlg.Content(winrt::box_value(winrt::hstring(
+ L"Wipe all rows from \"" + (schema.empty() ? L"" : schema + L"\".\"") + tableName + L"\"?\n\n"
+ L"The schema stays; this is NOT reversible.")));
+ dlg.PrimaryButtonText(L"Truncate");
+ dlg.CloseButtonText(L"Cancel");
+ dlg.DefaultButton(muxc::ContentDialogButton::Close);
+ dlg.XamlRoot(this->XamlRoot());
+
+ auto op = dlg.ShowAsync();
+ op.Completed([this, tableName, schema](auto&& asyncOp, auto&&)
+ {
+ if (asyncOp.GetResults() != muxc::ContentDialogResult::Primary) return;
+ this->DispatcherQueue().TryEnqueue([this, tableName, schema]()
+ {
+ auto adapter = connMgr_.getActiveAdapter();
+ if (!adapter) return;
+ try
+ {
+ const auto dbType = state_.connection.databaseType;
+ // Quote identifiers per dialect (same pattern as
+ // the Drop handler below).
+ std::wstring qt, qs;
+ if (dbType == DBModels::DatabaseType::MySQL)
+ {
+ qt = L"`" + tableName + L"`";
+ if (!schema.empty()) qs = L"`" + schema + L"`.";
+ }
+ else if (dbType == DBModels::DatabaseType::SQLite)
+ {
+ qt = L"\"" + tableName + L"\""; // no schemas
+ }
+ else // Postgres / MSSQL / anything SQL-ish
+ {
+ qt = L"\"" + tableName + L"\"";
+ if (!schema.empty()) qs = L"\"" + schema + L"\".";
+ }
+
+ // SQLite has no TRUNCATE — bare DELETE is the
+ // documented equivalent and the engine runs it
+ // with the fast "truncate optimization" when the
+ // table has no FK dependencies.
+ std::wstring sql = (dbType == DBModels::DatabaseType::SQLite)
+ ? (L"DELETE FROM " + qt)
+ : (L"TRUNCATE TABLE " + qs + qt);
+
+ auto result = adapter->execute(sql);
+ LogQuery(result);
+ if (result.success)
+ {
+ LoadSidebarFromDB(); // refresh row counts
+ // If a DataGrid tab is open on this table,
+ // refetch so the grid reflects the wipe.
+ for (const auto& t : state_.tabs)
+ {
+ if (t.type == DBModels::TabType::DataGrid &&
+ t.tableName == tableName &&
+ t.schema == schema &&
+ t.id == state_.activeTabId)
+ {
+ SwitchContentView();
+ break;
+ }
+ }
+ }
+ else
+ {
+ muxc::ContentDialog errDlg;
+ errDlg.Title(winrt::box_value(winrt::hstring(L"Truncate Failed")));
+ errDlg.Content(winrt::box_value(winrt::hstring(result.error)));
+ errDlg.CloseButtonText(L"OK");
+ errDlg.XamlRoot(this->XamlRoot());
+ errDlg.ShowAsync();
+ }
+ }
+ catch (const std::exception& e)
+ {
+ muxc::ContentDialog errDlg;
+ errDlg.Title(winrt::box_value(winrt::hstring(L"Truncate Failed")));
+ errDlg.Content(winrt::box_value(winrt::hstring(
+ std::wstring(L"Unexpected error: ") + std::wstring(
+ std::string(e.what()).begin(), std::string(e.what()).end()))));
+ errDlg.CloseButtonText(L"OK");
+ errDlg.XamlRoot(this->XamlRoot());
+ errDlg.ShowAsync();
+ }
+ });
+ });
+ };
+
// Sidebar delete table — confirm then DROP
Sidebar().as<SidebarPanel>()->OnDeleteTable = [this](const std::wstring& tableName, const std::wstring& schema)
{