Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-editor): double click on table should issue SELECT * FROM <<table>> query (#9594)

Liu Jied3ead8925b9
diff --git a/frontend/src/store/modules/sqlEditorTree.ts b/frontend/src/store/modules/sqlEditorTree.ts
index c12a99ae4e..f67a1fc9a3 100644
--- a/frontend/src/store/modules/sqlEditorTree.ts
+++ b/frontend/src/store/modules/sqlEditorTree.ts
@@ -365,7 +365,7 @@ export const idForSQLEditorTreeNodeTarget = <T extends NodeType>(
}
if (type === "expandable-text") {
const { text, type } = target as NodeTarget<"expandable-text">;
- return `texts-${type}/${text}`;
+ return `texts-${type}/${typeof text === "function" ? text() : text}`;
}
if (type === "dummy") {
const dummyType = (target as NodeTarget<"dummy">).type;
diff --git a/frontend/src/utils/v1/sql.ts b/frontend/src/utils/v1/sql.ts
index 49974738e2..f02a58426b 100644
--- a/frontend/src/utils/v1/sql.ts
+++ b/frontend/src/utils/v1/sql.ts
@@ -1,4 +1,5 @@
import { NullValue } from "@/types/proto/google/protobuf/struct";
+import { Engine } from "@/types/proto/v1/common";
import { RowValue } from "@/types/proto/v1/sql_service";
export const extractSQLRowValue = (value: RowValue) => {
@@ -18,3 +19,23 @@ export const extractSQLRowValue = (value: RowValue) => {
const key = keys[0];
return plainObject[key];
};
+
+export const wrapSQLIdentifier = (id: string, engine: Engine) => {
+ if (engine === Engine.MSSQL) {
+ return `[${id}]`;
+ }
+ if (
+ [
+ Engine.POSTGRES,
+ Engine.SQLITE,
+ Engine.SNOWFLAKE,
+ Engine.ORACLE,
+ Engine.OCEANBASE_ORACLE,
+ Engine.REDSHIFT,
+ ].includes(engine)
+ ) {
+ return `"${id}"`;
+ }
+
+ return "`" + id + "`";
+};
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index 266e993a11..2d14c1652a 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -68,6 +68,7 @@ import { NTree, NInput, NDropdown, DropdownOption, TreeOption } from "naive-ui";
import { storeToRefs } from "pinia";
import { ref, computed, nextTick, watch, h } from "vue";
import { useI18n } from "vue-i18n";
+import { useExecuteSQL } from "@/composables/useExecuteSQL";
import {
useActuatorV1Store,
useCurrentUserV1,
@@ -77,7 +78,10 @@ import {
useIsLoggedIn,
useTabStore,
} from "@/store";
-import { useSQLEditorTreeStore } from "@/store/modules/sqlEditorTree";
+import {
+ idForSQLEditorTreeNodeTarget,
+ useSQLEditorTreeStore,
+} from "@/store/modules/sqlEditorTree";
import type {
ComposedDatabase,
ComposedInstance,
@@ -91,10 +95,12 @@ import {
UNKNOWN_ID,
instanceOfSQLEditorTreeNode,
isConnectableSQLEditorTreeNode,
+ languageOfEngineV1,
} from "@/types";
import {
emptyConnection,
findAncestor,
+ formatEngineV1,
getSuggestedTabNameFromConnection,
hasWorkspacePermissionV1,
instanceV1AllowsCrossDatabaseQuery,
@@ -102,6 +108,7 @@ import {
instanceV1HasReadonlyMode,
isDescendantOf,
isSimilarTab,
+ wrapSQLIdentifier,
} from "@/utils";
import { useSQLEditorContext } from "../context";
import DatabaseHoverPanel from "./DatabaseHoverPanel.vue";
@@ -135,6 +142,7 @@ const instanceStore = useInstanceV1Store();
const dbSchemaV1Store = useDBSchemaV1Store();
const isLoggedIn = useIsLoggedIn();
const me = useCurrentUserV1();
+const { executeReadonly } = useExecuteSQL();
const { selectedDatabaseSchemaByDatabaseName, events: editorEvents } =
useSQLEditorContext();
const { node: hoverNode, update: updateHoverNode } = provideHoverStateContext();
@@ -372,6 +380,75 @@ const maybeSelectTable = async (node: SQLEditorTreeNode) => {
});
};
+const selectAllFromTableOrView = async (node: SQLEditorTreeNode) => {
+ const { type, target } = (node as SQLEditorTreeNode<"table" | "view">).meta;
+ const { database } = target;
+ const { engine } = database.instanceEntity;
+ const LIMIT = 50; // default pagesize of SQL Editor
+
+ const runQuery = async (query: string) => {
+ const tab: CoreTabInfo = {
+ connection: {
+ instanceId: database.instanceEntity.uid,
+ databaseId: database.uid,
+ },
+ mode: TabMode.ReadOnly,
+ };
+ if (tabStore.currentTab.isFreshNew) {
+ // If the current tab is "fresh new", update its connection directly.
+ tabStore.updateCurrentTab(tab);
+ } else {
+ // Otherwise select or add a new tab and set its connection
+ tabStore.addTab(
+ {
+ ...tab,
+ name: getSuggestedTabNameFromConnection(tab.connection),
+ statement: query,
+ },
+ /* beside */ true
+ );
+ }
+ await nextTick();
+ executeReadonly(query, {
+ databaseType: formatEngineV1(database.instanceEntity),
+ });
+ };
+
+ const language = languageOfEngineV1(engine);
+ if (language === "redis") {
+ return; // not supported
+ }
+ const tableOrViewName =
+ type === "table"
+ ? (target as SQLEditorTreeNodeTarget<"table">).table.name
+ : type === "view"
+ ? (target as SQLEditorTreeNodeTarget<"view">).view.name
+ : "";
+ if (!tableOrViewName) {
+ return;
+ }
+
+ if (language === "javascript" && type === "table") {
+ // mongodb
+ const query = `db["${tableOrViewName}"].find().limit(${LIMIT});`;
+ runQuery(query);
+ return;
+ }
+
+ if (type === "table") {
+ maybeSelectTable(node);
+ }
+
+ const tableNameParts: string[] = [];
+ if (target.schema.name) {
+ tableNameParts.push(wrapSQLIdentifier(target.schema.name, engine));
+ }
+ tableNameParts.push(wrapSQLIdentifier(tableOrViewName, engine));
+
+ const query = `SELECT * FROM ${tableNameParts.join(".")} LIMIT ${LIMIT}`;
+ runQuery(query);
+};
+
const nodeProps = ({ option }: { option: TreeOption }) => {
const node = option as any as SQLEditorTreeNode;
return {
@@ -427,7 +504,18 @@ const nodeProps = ({ option }: { option: TreeOption }) => {
onmouseleave() {
updateHoverNode(undefined, "after");
},
- "data-node-type": node.type,
+ ondblclick() {
+ if (node.meta.type === "table" || node.meta.type === "view") {
+ selectAllFromTableOrView(node);
+ }
+ },
+ // attrs below for trouble-shooting
+ "data-node-meta-type": node.meta.type,
+ "data-node-meta-id": idForSQLEditorTreeNodeTarget(
+ node.meta.type,
+ node.meta.target
+ ),
+ "data-node-key": node.key,
};
};