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): route Function clicks to a source-viewer tab

DangHuynh7627c740d1dc
diff --git a/windows/Gridex/ClickHouseAdapter.cpp b/windows/Gridex/ClickHouseAdapter.cpp
index 919a49a..eb9f27b 100644
--- a/windows/Gridex/ClickHouseAdapter.cpp
+++ b/windows/Gridex/ClickHouseAdapter.cpp
@@ -522,11 +522,19 @@ namespace DBModels
return funcs;
}
- // ClickHouse does not expose source for built-in functions.
+ // For user-defined functions (CREATE FUNCTION ...), system.functions
+ // stores the full DDL in `create_query`. Built-ins have no source
+ // exposed — callers get an empty string and should render a stub.
std::wstring ClickHouseAdapter::getFunctionSource(
- const std::wstring& /*name*/, const std::wstring& /*schema*/)
+ const std::wstring& name, const std::wstring& /*schema*/)
{
- return L"";
+ ensureConnected();
+ auto result = executeInternal(
+ "SELECT create_query FROM system.functions "
+ "WHERE name = " + quoteLiteral(name) + " LIMIT 1");
+ if (result.rows.empty()) return L"";
+ auto it = result.rows[0].find(L"create_query");
+ return (it != result.rows[0].end()) ? it->second : L"";
}
std::wstring ClickHouseAdapter::getCreateTableSQL(
diff --git a/windows/Gridex/SidebarPanel.cpp b/windows/Gridex/SidebarPanel.cpp
index 7d70334..3088ff5 100644
--- a/windows/Gridex/SidebarPanel.cpp
+++ b/windows/Gridex/SidebarPanel.cpp
@@ -618,7 +618,7 @@ namespace winrt::Gridex::implementation
item.type == DBModels::SidebarItemType::Function)
{
if (OnItemSelected)
- OnItemSelected(item.title, item.schema);
+ OnItemSelected(item.title, item.schema, item.type);
}
}
}
diff --git a/windows/Gridex/SidebarPanel.h b/windows/Gridex/SidebarPanel.h
index 3138146..4b19d7d 100644
--- a/windows/Gridex/SidebarPanel.h
+++ b/windows/Gridex/SidebarPanel.h
@@ -19,8 +19,12 @@ namespace winrt::Gridex::implementation
// SQL Open/Export/Import).
void SetDatabaseType(DBModels::DatabaseType type) { currentDbType_ = type; }
- // Callback invoked when user clicks a table/view/function
- std::function<void(const std::wstring& name, const std::wstring& schema)> OnItemSelected;
+ // Callback invoked when user clicks a table/view/function. The
+ // item type lets callers route Function clicks to a source-
+ // viewer flow instead of fetchRows.
+ std::function<void(const std::wstring& name,
+ const std::wstring& schema,
+ DBModels::SidebarItemType type)> OnItemSelected;
// Callback invoked when schema picker changes
std::function<void(const std::wstring& schema)> OnSchemaChanged;
diff --git a/windows/Gridex/WorkspacePage.cpp b/windows/Gridex/WorkspacePage.cpp
index 1247884..19983fa 100644
--- a/windows/Gridex/WorkspacePage.cpp
+++ b/windows/Gridex/WorkspacePage.cpp
@@ -1616,9 +1616,14 @@ namespace winrt::Gridex::implementation
}
Sidebar().as<SidebarPanel>()->SetItems(state_.sidebarItems);
- Sidebar().as<SidebarPanel>()->OnItemSelected = [this](const std::wstring& name, const std::wstring& schema)
+ Sidebar().as<SidebarPanel>()->OnItemSelected =
+ [this](const std::wstring& name, const std::wstring& schema,
+ DBModels::SidebarItemType type)
{
- OnTableSelected(name, schema);
+ if (type == DBModels::SidebarItemType::Function)
+ ShowFunctionSource(name, schema);
+ else
+ OnTableSelected(name, schema);
};
// Feed table/function names to query editor autocomplete
@@ -2009,6 +2014,31 @@ namespace winrt::Gridex::implementation
SwitchContentView();
}
+ void WorkspacePage::ShowFunctionSource(
+ const std::wstring& name, const std::wstring& schema)
+ {
+ // Pull source via the active adapter. Each dialect maps this
+ // to its own metadata query (pg_proc / system.functions /
+ // mysql.proc / sys.sql_modules / ...).
+ std::wstring source;
+ try
+ {
+ auto adapter = connMgr_.getActiveAdapter();
+ if (adapter) source = adapter->getFunctionSource(name, schema);
+ }
+ catch (const std::exception&) { /* fall through — show stub */ }
+
+ if (source.empty())
+ source = L"-- " + name + L"\n-- (no source available for this function)";
+
+ // Reuse the query-tab flow: open a fresh tab, drop the DDL
+ // into the shared QueryEditorView. Matches how DataGrip /
+ // Navicat surface UDF bodies — user can tweak + re-run
+ // CREATE FUNCTION in place.
+ OpenNewQueryTab();
+ QueryEditor().as<QueryEditorView>()->SetSql(source);
+ }
+
// ── CRUD Operations ─────────────────────────────────
DBModels::TableRow WorkspacePage::ExtractPrimaryKey(int rowIndex)
@@ -4384,9 +4414,14 @@ g.er-selected > foreignObject > div.er-card{
state_.sidebarItems = { functionsGroup, tablesGroup, viewsGroup };
Sidebar().as<SidebarPanel>()->SetItems(state_.sidebarItems);
- Sidebar().as<SidebarPanel>()->OnItemSelected = [this](const std::wstring& name, const std::wstring& schema)
+ Sidebar().as<SidebarPanel>()->OnItemSelected =
+ [this](const std::wstring& name, const std::wstring& schema,
+ DBModels::SidebarItemType type)
{
- OnTableSelected(name, schema);
+ if (type == DBModels::SidebarItemType::Function)
+ ShowFunctionSource(name, schema);
+ else
+ OnTableSelected(name, schema);
};
}
diff --git a/windows/Gridex/WorkspacePage.h b/windows/Gridex/WorkspacePage.h
index 567f056..632e7ce 100644
--- a/windows/Gridex/WorkspacePage.h
+++ b/windows/Gridex/WorkspacePage.h
@@ -112,6 +112,10 @@ namespace winrt::Gridex::implementation
void SwitchContentView();
void OnTableSelected(const std::wstring& tableName, const std::wstring& schema);
void OpenNewQueryTab();
+ // Opens a read-only query tab showing the function's source
+ // (CREATE FUNCTION DDL or body) fetched via
+ // DatabaseAdapter::getFunctionSource.
+ void ShowFunctionSource(const std::wstring& name, const std::wstring& schema);
// ── CRUD Operations ─────────────────────────
void DeleteSelectedRow();