Skip to content

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

feat: allow set data source in sql editor (#9346)

boojackcf2c3a9ffc80
diff --git a/frontend/package.json b/frontend/package.json
index bcb0df065b..0c548fc01b 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -45,7 +45,7 @@
"jszip": "^3.10.1",
"libphonenumber-js": "^1.10.47",
"lodash-es": "4.17.21",
- "lucide-vue-next": "^0.279.0",
+ "lucide-vue-next": "^0.292.0",
"markdown-it": "^13.0.2",
"maz-ui": "^3.17.0",
"monaco-editor": "0.34.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index bb54dde524..307b8e8bf6 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -96,8 +96,8 @@ dependencies:
specifier: 4.17.21
version: 4.17.21
lucide-vue-next:
- specifier: ^0.279.0
- version: 0.279.0([email protected])
+ specifier: ^0.292.0
+ version: 0.292.0([email protected])
markdown-it:
specifier: ^13.0.2
version: 13.0.2
@@ -4018,8 +4018,8 @@ packages:
resolution: {integrity: sha512-AI2r85+4MquTw9ZYqabu4nMwy9Oftlfa/e/52t9IjtfG+mGBbTNdAoZ3RQKLHR6r0wQnwZnPIEh/Ya6XTWAKNA==}
dev: false
- resolution: {integrity: sha512-zsFptHEGB9Epm9MZUoiqNyvw2ddq/MLOUVy1BXT4TCNJ2hW9gcuKB2oZnMk8J1Yl94gjsX/vVFynY39i9ICLkw==}
+ resolution: {integrity: sha512-tVnEPLJvB9n+REfJsgJsG/zxGM0yamFs6WkOjuAvMG1ZlMS27soiKekuWk/+G2uLF4jqhEegQHVmcAJBlnJpeg==}
peerDependencies:
vue: '>=3.0.1'
dependencies:
diff --git a/frontend/src/components/ProvideSQLEditorContext.vue b/frontend/src/components/ProvideSQLEditorContext.vue
index d7b733de5e..fc54d9e58f 100644
--- a/frontend/src/components/ProvideSQLEditorContext.vue
+++ b/frontend/src/components/ProvideSQLEditorContext.vue
@@ -283,6 +283,7 @@ const syncURLWithConnection = () => {
// exceptions.
tabStore.updateCurrentTab({
connection: {
+ ...tabStore.currentTab.connection,
instanceId: instance.uid,
databaseId: database.uid,
},
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index b1a79619c7..0a49cb2661 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -163,13 +163,21 @@ const useExecuteSQL = () => {
});
continue;
}
+ const instanceId = isUnknownDatabase
+ ? tab.connection.instanceId
+ : database.instanceEntity.uid;
+ const databaseName = isUnknownDatabase ? "" : database.databaseName;
+ const dataSourceId =
+ instanceId === tab.connection.instanceId
+ ? tab.connection.dataSourceId
+ : undefined;
+
try {
const sqlResultSet = await sqlEditorStore.executeQuery(
{
- instanceId: isUnknownDatabase
- ? tab.connection.instanceId
- : database.instanceEntity.uid,
- databaseName: isUnknownDatabase ? "" : database.databaseName,
+ instanceId: instanceId,
+ databaseName: databaseName,
+ dataSourceId: dataSourceId,
statement: selectStatement,
},
abortController.signal
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 71648f685d..c376877442 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -38,8 +38,12 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
{
instanceId,
databaseName,
+ dataSourceId,
statement,
- }: Pick<QueryInfo, "instanceId" | "databaseName" | "statement">,
+ }: Pick<
+ QueryInfo,
+ "instanceId" | "databaseName" | "dataSourceId" | "statement"
+ >,
signal: AbortSignal
) {
const instance = useInstanceV1Store().getInstanceByUID(
@@ -49,6 +53,7 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
{
name: instance.name,
connectionDatabase: databaseName || "",
+ dataSourceId: dataSourceId || "",
statement,
limit: RESULT_ROWS_LIMIT,
},
diff --git a/frontend/src/store/modules/tab.ts b/frontend/src/store/modules/tab.ts
index 5fbf07743d..8f86085e4d 100644
--- a/frontend/src/store/modules/tab.ts
+++ b/frontend/src/store/modules/tab.ts
@@ -156,6 +156,7 @@ export const useTabStore = defineStore("tab", () => {
() => tab.name,
() => tab.sheetName,
() => tab.statement,
+ () => tab.connection,
() => tab.batchQueryContext,
];
watch(dirtyFields, () => {
diff --git a/frontend/src/types/sql.ts b/frontend/src/types/sql.ts
index 3116609b1d..0d78b00176 100644
--- a/frontend/src/types/sql.ts
+++ b/frontend/src/types/sql.ts
@@ -3,6 +3,7 @@ import { InstanceId } from "./id";
export type QueryInfo = {
instanceId: InstanceId;
databaseName?: string;
+ dataSourceId?: string;
statement: string;
limit?: number;
// exportFomat includes QUERY, CSV, JSON.
diff --git a/frontend/src/types/tab.ts b/frontend/src/types/tab.ts
index c563872119..d316cfa753 100644
--- a/frontend/src/types/tab.ts
+++ b/frontend/src/types/tab.ts
@@ -11,6 +11,7 @@ export type ExecuteOption = {
export type Connection = {
instanceId: string;
databaseId: string;
+ dataSourceId?: string;
};
export enum TabMode {
diff --git a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
index 9ce83e05b1..1d2b3f2ec9 100644
--- a/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/EditorAction.vue
@@ -6,23 +6,30 @@
<div
class="action-left gap-x-2 flex overflow-x-auto sm:overflow-x-hidden items-center"
>
- <NButton
- type="primary"
- size="small"
- :disabled="!allowQuery"
- @click="handleRunQuery"
- >
- <mdi:play class="-ml-1.5" />
- <span>
- {{
- showRunSelected ? $t("sql-editor.run-selected") : $t("common.run")
- }}
- </span>
+ <NButtonGroup>
+ <NButton
+ type="primary"
+ size="small"
+ :disabled="!allowQuery"
+ @click="handleRunQuery"
+ >
+ <template #icon>
+ <mdi:play class="-ml-1.5" />
+ </template>
+ <span>
+ {{
+ showRunSelected ? $t("sql-editor.run-selected") : $t("common.run")
+ }}
+ </span>
- <span v-show="showShortcutText" class="ml-1">
- ({{ keyboardShortcutStr("cmd_or_ctrl+⏎") }})
- </span>
- </NButton>
+ <span v-show="showShortcutText" class="ml-1">
+ ({{ keyboardShortcutStr("cmd_or_ctrl+⏎") }})
+ </span>
+ </NButton>
+ <QueryContextSettingPopover
+ v-if="showQueryContextSettingPopover && allowQuery"
+ />
+ </NButtonGroup>
<NButton size="small" :disabled="!allowQuery" @click="handleExplainQuery">
<mdi:play class="-ml-1.5" />
<span>Explain</span>
@@ -109,7 +116,7 @@
<script lang="ts" setup>
import { useElementSize } from "@vueuse/core";
-import { NButton, NPopover } from "naive-ui";
+import { NButtonGroup, NButton, NPopover } from "naive-ui";
import { computed, defineEmits, reactive, ref } from "vue";
import {
useTabStore,
@@ -125,6 +132,7 @@ import { TabMode, UNKNOWN_ID } from "@/types";
import { formatEngineV1, keyboardShortcutStr } from "@/utils";
import { useSQLEditorContext } from "../context";
import AdminModeButton from "./AdminModeButton.vue";
+import QueryContextSettingPopover from "./QueryContextSettingPopover.vue";
import SharePopover from "./SharePopover.vue";
interface LocalState {
@@ -218,6 +226,13 @@ const queryList = computed(() => {
);
});
+const showQueryContextSettingPopover = computed(() => {
+ return (
+ Boolean(selectedInstance.value) &&
+ tabStore.currentTab.mode !== TabMode.Admin
+ );
+});
+
const handleRunQuery = async () => {
const currentTab = tabStore.currentTab;
const statement = currentTab.statement;
diff --git a/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
new file mode 100644
index 0000000000..c83d5da690
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/QueryContextSettingPopover.vue
@@ -0,0 +1,117 @@
+<template>
+ <NPopover placement="bottom-center" trigger="click">
+ <template #trigger>
+ <NButton type="primary" class="!px-1" size="small">
+ <template #icon>
+ <ChevronDown />
+ </template>
+ </NButton>
+ </template>
+ <div>
+ <p class="mb-1">{{ $t("data-source.select-data-source") }}</p>
+ <NSelect
+ class="!w-40"
+ clearable
+ :options="dataSourceOptions"
+ :value="selectedDataSourceId"
+ :render-label="renderLabel"
+ @update:value="handleDataSourceSelect"
+ />
+ </div>
+ </NPopover>
+</template>
+
+<script lang="ts" setup>
+import { orderBy } from "lodash-es";
+import { ChevronDown } from "lucide-vue-next";
+import {
+ NButton,
+ NEllipsis,
+ NPopover,
+ NSelect,
+ SelectOption,
+ SelectRenderLabel,
+} from "naive-ui";
+import { computed, h } from "vue";
+import { useInstanceV1ByUID, useTabStore } from "@/store";
+import { DataSource, DataSourceType } from "@/types/proto/v1/instance_service";
+
+interface DataSourceSelectOption extends SelectOption {
+ value: string;
+ dataSource: DataSource;
+}
+
+const tabStore = useTabStore();
+const connection = computed(() => tabStore.currentTab.connection);
+const selectedDataSourceId = computed(() => {
+ return connection.value.dataSourceId;
+});
+
+const { instance: selectedInstance } = useInstanceV1ByUID(
+ computed(() => connection.value.instanceId)
+);
+
+const dataSources = computed(() => {
+ return orderBy(selectedInstance.value?.dataSources ?? [], "type");
+});
+
+const dataSourceOptions = computed(() => {
+ return dataSources.value.map((dataSource) => {
+ return {
+ value: dataSource.id,
+ label: dataSource.username,
+ dataSource: dataSource,
+ };
+ });
+});
+
+const renderLabel: SelectRenderLabel = (option) => {
+ const { dataSource } = option as DataSourceSelectOption;
+ if (!dataSource) {
+ return;
+ }
+
+ const children = [
+ h(
+ "span",
+ {
+ class: "text-gray-400 text-sm",
+ },
+ [readableDataSourceType(dataSource.type)]
+ ),
+ h(
+ "span",
+ {
+ class: "ml-1",
+ },
+ [dataSource.username]
+ ),
+ ];
+ return h(
+ NEllipsis,
+ {
+ class: "w-full",
+ },
+ children
+ );
+};
+
+const handleDataSourceSelect = (dataSourceId?: string) => {
+ tabStore.updateCurrentTab({
+ connection: {
+ ...connection.value,
+ dataSourceId: dataSourceId,
+ },
+ });
+};
+
+const readableDataSourceType = (type: DataSourceType): string => {
+ if (type === DataSourceType.ADMIN) {
+ return "Admin";
+ } else if (type === DataSourceType.READ_ONLY) {
+ return "RO";
+ } else {
+ return "Unknown";
+ }
+};
+</script>