Skip to content

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

chore: allow select data source type for batch query (#16258)

ecmadao000401e5040b
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index cc930226b8..d12f1648dd 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -1,5 +1,5 @@
import Emittery from "emittery";
-import { head, isEmpty } from "lodash-es";
+import { head, isEmpty, cloneDeep } from "lodash-es";
import { Status } from "nice-grpc-common";
import { v4 as uuidv4 } from "uuid";
import { markRaw, reactive } from "vue";
@@ -14,14 +14,18 @@ import {
useSQLEditorQueryHistoryStore,
useAppFeature,
hasFeature,
+ batchGetOrFetchDatabases,
+ useDatabaseV1Store,
} from "@/store";
import type {
ComposedDatabase,
SQLResultSetV1,
BBNotificationStyle,
SQLEditorQueryParams,
+ SQLEditorConnection,
SQLEditorTab,
SQLEditorDatabaseQueryContext,
+ QueryDataSourceType,
} from "@/types";
import { isValidDatabaseName } from "@/types";
import { Engine } from "@/types/proto/v1/common";
@@ -59,6 +63,7 @@ const useExecuteSQL = () => {
lastQueryTime?: number;
}>({});
const dbGroupStore = useDBGroupStore();
+ const dbStore = useDatabaseV1Store();
const tabStore = useSQLEditorTabStore();
const sqlEditorStore = useSQLEditorStore();
const queryHistoryStore = useSQLEditorQueryHistoryStore();
@@ -164,6 +169,22 @@ const useExecuteSQL = () => {
}
};
+ const getDataSourceId = (
+ database: ComposedDatabase,
+ connection: SQLEditorConnection,
+ mode?: QueryDataSourceType
+ ) => {
+ return (
+ ensureDataSourceSelection(
+ database.instance === connection.instance
+ ? connection.dataSourceId
+ : undefined,
+ database,
+ mode
+ ) ?? ""
+ );
+ };
+
const preExecute = async (params: SQLEditorQueryParams) => {
const now = Date.now();
if (
@@ -246,6 +267,9 @@ const useExecuteSQL = () => {
}
}
+ const isBatch = batchQueryDatabaseSet.size > 1;
+ await batchGetOrFetchDatabases([...batchQueryDatabaseSet.keys()]);
+
for (const databaseName of batchQueryDatabaseSet.values()) {
if (!databaseQueryContexts.has(databaseName)) {
databaseQueryContexts.set(databaseName, []);
@@ -255,9 +279,20 @@ const useExecuteSQL = () => {
databaseQueryContexts.get(databaseName)?.pop();
}
+ const database = dbStore.getDatabaseByName(databaseName);
+
databaseQueryContexts.get(databaseName)?.unshift({
id: uuidv4(),
- params,
+ params: Object.assign(cloneDeep(params), {
+ connection: {
+ ...params.connection,
+ dataSourceId: getDataSourceId(
+ database,
+ params.connection,
+ isBatch ? tab.batchQueryContext?.dataSourceType : undefined
+ ),
+ },
+ }),
status: "PENDING",
});
}
@@ -335,17 +370,7 @@ const useExecuteSQL = () => {
}
}
- const instance = isValidDatabaseName(database.name)
- ? database.instance
- : context.params.connection.instance;
- const dataSourceId =
- ensureDataSourceSelection(
- instance === context.params.connection.instance
- ? context.params.connection.dataSourceId
- : undefined,
- database
- ) ?? "";
-
+ const dataSourceId = context.params.connection.dataSourceId;
if (!dataSourceId) {
return finish({
advices: [],
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index b96f8a110c..2f8dc10155 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1804,7 +1804,14 @@
"batch": "Batch",
"select-database-group": "You can {select-database-group} to batch query",
"description": "Batch query {database} additional databases and {group} database groups under {project}.",
- "show-or-hide-empty-query-results": "Show / hide empty query results"
+ "show-or-hide-empty-query-results": "Show / hide empty query results",
+ "select-data-source": {
+ "self": "Select data source for batch",
+ "tooltip": "Will find available data sources with restriction policy, then apply the selection.",
+ "admin": "Admin",
+ "readonly": "Read-only",
+ "not-match": "Data source not match, expect {expect} but got {actual}."
+ }
},
"execute-query": "Execute query",
"pending-query": "Pending query",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index f66b345d26..6a5ce5e14b 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -1804,7 +1804,14 @@
"batch": "Lote",
"select-database-group": "Puedes {select-database-group} realizar consultas por lotes",
"description": "Consulta por lotes {database} bases de datos adicionales y {group} grupos de bases de datos bajo {project}.",
- "show-or-hide-empty-query-results": "Mostrar/ocultar resultados de consulta vacíos"
+ "show-or-hide-empty-query-results": "Mostrar/ocultar resultados de consulta vacíos",
+ "select-data-source": {
+ "self": "Seleccionar la fuente de datos para el lote",
+ "tooltip": "Encontrará fuentes de datos disponibles con política de restricción y luego aplicará la selección.",
+ "admin": "Administración",
+ "readonly": "Sólo lectura",
+ "not-match": "La fuente de datos no coincide, se esperaba {expect} pero se obtuvo {actual}."
+ }
},
"execute-query": "Ejecutar consulta",
"pending-query": "Consulta pendiente",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 7f0da4f188..bca28b3f95 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -1804,7 +1804,14 @@
"batch": "バッチ",
"select-database-group": "バッチクエリを実行するには{select-database-group}を使用します",
"description": "{project} の下の {database} 個の追加データベースと {group} 個のデータベース グループに対してバッチ クエリを実行します。",
- "show-or-hide-empty-query-results": "空のクエリ結果の表示/非表示"
+ "show-or-hide-empty-query-results": "空のクエリ結果の表示/非表示",
+ "select-data-source": {
+ "self": "バッチのデータソースを選択",
+ "tooltip": "制限ポリシーが設定された利用可能なデータ ソースを見つけて、選択を適用します。",
+ "admin": "管理者",
+ "readonly": "読み取り専用",
+ "not-match": "データ ソースが一致しません。{expect} を期待しましたが、{actual} を取得しました。"
+ }
},
"execute-query": "クエリを実行する",
"pending-query": "保留中のクエリ",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index 26bba1e498..987b5615a5 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -1804,7 +1804,14 @@
"batch": "Hàng loạt",
"select-database-group": "Bạn có thể {select-database-group} để truy vấn hàng loạt",
"description": "Truy vấn hàng loạt {database} cơ sở dữ liệu bổ sung và {group} nhóm cơ sở dữ liệu trong {project}.",
- "show-or-hide-empty-query-results": "Hiển thị / ẩn kết quả truy vấn trống"
+ "show-or-hide-empty-query-results": "Hiển thị / ẩn kết quả truy vấn trống",
+ "select-data-source": {
+ "self": "Chọn nguồn dữ liệu cho lô",
+ "tooltip": "Sẽ tìm các nguồn dữ liệu có sẵn với chính sách hạn chế, sau đó áp dụng lựa chọn.",
+ "admin": "Quản trị viên",
+ "readonly": "Chỉ đọc",
+ "not-match": "Nguồn dữ liệu không khớp, mong đợi {expect} nhưng lại nhận được {actual}."
+ }
},
"execute-query": "Thực hiện truy vấn",
"pending-query": "Đang chờ truy vấn",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 997bb1325e..9db41cb3a9 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1804,7 +1804,14 @@
"batch": "批量",
"select-database-group": "您可以{select-database-group}进行批量查询",
"description": "批量查询 {project} 下的 {database} 个数据库和 {group} 个数据库组。",
- "show-or-hide-empty-query-results": "显示/隐藏空查询结果"
+ "show-or-hide-empty-query-results": "显示/隐藏空查询结果",
+ "select-data-source": {
+ "self": "选择批量查询使用的数据源",
+ "tooltip": "将先根据限制策略寻找数据源,然后应用该选择。",
+ "admin": "主数据源",
+ "readonly": "只读数据源",
+ "not-match": "数据源不匹配,预期为 {expect},但实际使用 {actual}。"
+ }
},
"execute-query": "执行查询",
"pending-query": "待处理查询",
diff --git a/frontend/src/store/modules/sqlEditor/tab.ts b/frontend/src/store/modules/sqlEditor/tab.ts
index 8c390a9759..506c2705c2 100644
--- a/frontend/src/store/modules/sqlEditor/tab.ts
+++ b/frontend/src/store/modules/sqlEditor/tab.ts
@@ -8,8 +8,10 @@ import type {
CoreSQLEditorTab,
SQLEditorTab,
SQLEditorDatabaseQueryContext,
+ BatchQueryContext,
} from "@/types";
import { DEFAULT_SQL_EDITOR_TAB_MODE, isValidDatabaseName } from "@/types";
+import { DataSourceType } from "@/types/proto/v1/instance_service";
import {
WebStorageHelper,
defaultSQLEditorTab,
@@ -24,6 +26,7 @@ import {
useDatabaseV1ByName,
useEnvironmentV1Store,
extractUserId,
+ hasFeature,
} from "../v1";
import { useSQLEditorStore } from "./editor";
import {
@@ -198,6 +201,24 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
return tabById(currId);
});
+ const isInBatchMode = computed(() => {
+ if (!currentTab.value) {
+ return false;
+ }
+ if (!hasFeature("bb.feature.batch-query")) {
+ return false;
+ }
+ const { batchQueryContext } = currentTab.value;
+ if (!batchQueryContext) {
+ return false;
+ }
+ const { databaseGroups = [], databases = [] } = batchQueryContext;
+ if (!hasFeature("bb.feature.database-grouping")) {
+ return databases.length > 1;
+ }
+ return databaseGroups.length > 0 || databases.length > 1;
+ });
+
// actions
/**
*
@@ -247,8 +268,23 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
if (!id) return;
updateTab(id, payload);
};
+ const updateBatchQueryContext = (payload: Partial<BatchQueryContext>) => {
+ const tab = currentTab.value;
+ if (!tab) {
+ return;
+ }
+ updateTab(tab.id, {
+ batchQueryContext: {
+ databases: tab.batchQueryContext?.databases ?? [],
+ dataSourceType:
+ tab.batchQueryContext?.dataSourceType ?? DataSourceType.READ_ONLY,
+ ...tab.batchQueryContext,
+ ...payload,
+ },
+ });
+ };
- const updateQueryContext = ({
+ const updateDatabaseQueryContext = ({
database,
contextId,
context,
@@ -409,13 +445,15 @@ export const useSQLEditorTabStore = defineStore("sqlEditorTab", () => {
removeTab,
updateTab,
updateCurrentTab,
- updateQueryContext,
+ updateBatchQueryContext,
+ updateDatabaseQueryContext,
setCurrentTabId,
selectOrAddSimilarNewTab,
maybeInitProject,
reset,
isDisconnected,
isSwitchingTab,
+ isInBatchMode,
};
});
diff --git a/frontend/src/types/sqlEditor/tab.ts b/frontend/src/types/sqlEditor/tab.ts
index 67e65145fa..0c1b12798f 100644
--- a/frontend/src/types/sqlEditor/tab.ts
+++ b/frontend/src/types/sqlEditor/tab.ts
@@ -2,6 +2,8 @@ import type {
AdviceOption,
Selection as MonacoSelection,
} from "@/components/MonacoEditor";
+import { t } from "@/plugins/i18n";
+import { DataSourceType } from "@/types/proto/v1/instance_service";
import type { SQLResultSetV1 } from "../v1/sql";
import type { SQLEditorConnection, SQLEditorQueryParams } from "./editor";
@@ -12,6 +14,20 @@ export type SQLEditorTabStatus =
export type SQLEditorTabMode = "WORKSHEET" | "ADMIN";
export const DEFAULT_SQL_EDITOR_TAB_MODE: SQLEditorTabMode = "WORKSHEET";
+export type QueryDataSourceType =
+ | DataSourceType.ADMIN
+ | DataSourceType.READ_ONLY;
+
+export const getDataSourceTypeI18n = (dsType?: DataSourceType) => {
+ switch (dsType) {
+ case DataSourceType.ADMIN:
+ return t("sql-editor.batch-query.select-data-source.admin");
+ case DataSourceType.READ_ONLY:
+ return t("sql-editor.batch-query.select-data-source.readonly");
+ default:
+ return "Unknown";
+ }
+};
export type BatchQueryContext = {
// databases is used to store the selected database names.
@@ -21,6 +37,8 @@ export type BatchQueryContext = {
// databaseGroups is used to store the selected database group names for batch request.
// Format: projects/{project}/databaseGroups/{databaseGroup}
databaseGroups?: string[];
+
+ dataSourceType?: QueryDataSourceType;
};
export type SQLEditorDatabaseQueryContext = {
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index 8930f1958a..b4a2f534e2 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -12,6 +12,7 @@ import type {
CoreSQLEditorTab,
SQLEditorConnection,
SQLEditorTab,
+ QueryDataSourceType,
} from "@/types";
import {
DEFAULT_SQL_EDITOR_TAB_MODE,
@@ -22,6 +23,7 @@ import {
import {
DataSourceType,
type InstanceResource,
+ type DataSource,
} from "@/types/proto/v1/instance_service";
import {
DataSourceQueryPolicy_Restriction,
@@ -194,18 +196,24 @@ export const getAdminDataSourceRestrictionOfDatabase = (
};
};
-export const ensureDataSourceSelection = (
- current: string | undefined,
- database: ComposedDatabase
+const getDataSourceIdByType = (
+ dataSource: {
+ admin: DataSource;
+ readonly: DataSource[];
+ },
+ type?: QueryDataSourceType
) => {
- const restriction = getAdminDataSourceRestrictionOfDatabase(database);
- const adminDataSource = database.instanceResource.dataSources.find(
- (ds) => ds.type === DataSourceType.ADMIN
- )!;
- const readonlyDataSources = database.instanceResource.dataSources.filter(
- (ds) => ds.type === DataSourceType.READ_ONLY
- );
+ switch (type) {
+ case DataSourceType.ADMIN:
+ return dataSource.admin.id;
+ default:
+ // try to use read-only data source first.
+ return head(dataSource.readonly)?.id ?? dataSource.admin.id;
+ }
+};
+const getDataSourceBehavior = (database: ComposedDatabase) => {
+ const restriction = getAdminDataSourceRestrictionOfDatabase(database);
let behavior: "RO" | "FALLBACK" | "ALLOW_ADMIN";
if (
restriction.environmentPolicy ===
@@ -222,39 +230,56 @@ export const ensureDataSourceSelection = (
} else {
behavior = "ALLOW_ADMIN";
}
+ return behavior;
+};
- if (behavior === "ALLOW_ADMIN") {
- if (current) {
- return current;
- }
- return head(readonlyDataSources)?.id ?? adminDataSource.id;
- }
+export const ensureDataSourceSelection = (
+ current: string | undefined,
+ database: ComposedDatabase,
+ type?: QueryDataSourceType
+) => {
+ const adminDataSource = database.instanceResource.dataSources.find(
+ (ds) => ds.type === DataSourceType.ADMIN
+ )!;
+ const readonlyDataSources = database.instanceResource.dataSources.filter(
+ (ds) => ds.type === DataSourceType.READ_ONLY
+ );
- if (behavior === "FALLBACK") {
- if (
- current &&
- (current !== adminDataSource.id || readonlyDataSources.length === 0)
- ) {
- return current;
- }
- return head(readonlyDataSources)?.id ?? adminDataSource.id;
- }
+ const behavior = getDataSourceBehavior(database);
- if (behavior === "RO") {
- if (
- current &&
- readonlyDataSources.findIndex((ds) => ds.id === current) >= 0
- ) {
- return current;
+ switch (behavior) {
+ case "ALLOW_ADMIN": {
+ // ALLOW_ADMIN means no policy restriction.
+ if (current) {
+ return current;
+ }
+ return getDataSourceIdByType(
+ { admin: adminDataSource, readonly: readonlyDataSources },
+ type
+ );
+ }
+ case "FALLBACK": {
+ // FALLBACK means try to use read-only data source, it can also accept admin data source if no read-only data source exists.
+ if (
+ current &&
+ (current !== adminDataSource.id || readonlyDataSources.length === 0)
+ ) {
+ return current;
+ }
+ return getDataSourceIdByType(
+ { admin: adminDataSource, readonly: readonlyDataSources },
+ type
+ );
+ }
+ case "RO": {
+ // RO only accept the read-only data source.
+ if (
+ current &&
+ readonlyDataSources.findIndex((ds) => ds.id === current) >= 0
+ ) {
+ return current;
+ }
+ return head(readonlyDataSources)?.id;
}
- return head(readonlyDataSources)?.id;
}
- console.warn(
- "[SQL Editor] failed to ensureDataSourceSelection",
- current,
- behavior,
- database,
- restriction
- );
- return undefined;
};
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/BatchQueryDatabaseGroupSelector.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/BatchQueryDatabaseGroupSelector.vue
index 76bfcc898c..ca92262c45 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/BatchQueryDatabaseGroupSelector.vue
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/BatchQueryDatabaseGroupSelector.vue
@@ -130,11 +130,8 @@ const handleTriggerClick = () => {
watch(
() => state.selectedDatabaseGroupNames,
() => {
- tabStore.updateCurrentTab({
- batchQueryContext: {
- databases: tabStore.currentTab?.batchQueryContext?.databases ?? [],
- databaseGroups: state.selectedDatabaseGroupNames,
- },
+ tabStore.updateBatchQueryContext({
+ databaseGroups: state.selectedDatabaseGroupNames,
});
}
);
diff --git a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
index d6a617b240..1bcec8dcea 100644
--- a/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
+++ b/frontend/src/views/sql-editor/ConnectionPanel/ConnectionPane/ConnectionPane.vue
@@ -49,6 +49,26 @@
@uncheck="handleUncheckDatabaseGroup"
/>
</template>
+ <NDivider v-if="tabStore.isInBatchMode" class="!my-2" />
+ <div v-if="tabStore.isInBatchMode" class="w-full">
+ <div class="textinfolabel flex items-center gap-x-1">
+ {{ $t("sql-editor.batch-query.select-data-source.self") }}
+ <NTooltip>
+ <template #trigger>
+ <InfoIcon class="w-4" />
+ </template>
+ {{ $t("sql-editor.batch-query.select-data-source.tooltip") }}
+ </NTooltip>
+ </div>
+ <NRadioGroup v-model:value="state.batchQueryDataSourceType">
+ <NRadio :value="DataSourceType.ADMIN">
+ {{ getDataSourceTypeI18n(DataSourceType.ADMIN) }}
+ </NRadio>
+ <NRadio :value="DataSourceType.READ_ONLY">
+ {{ getDataSourceTypeI18n(DataSourceType.READ_ONLY) }}
+ </NRadio>
+ </NRadioGroup>
+ </div>
</div>
</div>
<NDivider v-if="tabStore.currentTab" class="!my-3" />
@@ -147,6 +167,7 @@
<script lang="ts" setup>
import { useElementSize } from "@vueuse/core";
import { head, isEqual } from "lodash-es";
+import { InfoIcon } from "lucide-vue-next";
import {
NTag,
NButton,
@@ -154,6 +175,9 @@ import {
NDropdown,
NCheckbox,
NDivider,
+ NRadioGroup,
+ NRadio,
+ NTooltip,
type TreeOption,
} from "naive-ui";
import { storeToRefs } from "pinia";
@@ -184,9 +208,15 @@ import type {
ComposedDatabase,
SQLEditorTreeNode,
CoreSQLEditorTab,
+ QueryDataSourceType,
+} from "@/types";
+import {
+ DEFAULT_SQL_EDITOR_TAB_MODE,
+ isValidDatabaseName,
+ getDataSourceTypeI18n,
} from "@/types";
-import { DEFAULT_SQL_EDITOR_TAB_MODE, isValidDatabaseName } from "@/types";
import { engineFromJSON } from "@/types/proto/v1/common";
+import { DataSourceType } from "@/types/proto/v1/instance_service";
import {
findAncestor,
isDescendantOf,
@@ -212,6 +242,7 @@ interface LocalState {
selectedDatabases: Set<string>;
params: SearchParams;
showFeatureModal: boolean;
+ batchQueryDataSourceType?: QueryDataSourceType;
}
const treeStore = useSQLEditorTreeStore();
@@ -230,8 +261,19 @@ const state = reactive<LocalState>({
scopes: [],
},
showFeatureModal: false,
+ batchQueryDataSourceType: DataSourceType.READ_ONLY,
});
+watch(
+ () => state.batchQueryDataSourceType,
+ (dataSourceType) => {
+ tabStore.updateBatchQueryContext({
+ dataSourceType,
+ });
+ },
+ { immediate: true }
+);
+
watch(
() => tabStore.currentTab?.id,
async () => {
@@ -269,12 +311,8 @@ watch(
};
tryConnectToCoreSQLEditorTab(coreTab);
}
- tabStore.updateCurrentTab({
- batchQueryContext: {
- databases: selectedDatabases,
- databaseGroups:
- tabStore.currentTab?.batchQueryContext?.databaseGroups ?? [],
- },
+ tabStore.updateBatchQueryContext({
+ databases: selectedDatabases,
});
}
);
@@ -284,13 +322,10 @@ const handleUncheckDatabase = (database: string) => {
};
const handleUncheckDatabaseGroup = (databaseGroupName: string) => {
- tabStore.updateCurrentTab({
- batchQueryContext: {
- databases: tabStore.currentTab?.batchQueryContext?.databases ?? [],
- databaseGroups: (
- tabStore.currentTab?.batchQueryContext?.databaseGroups ?? []
- ).filter((name) => name !== databaseGroupName),
- },
+ tabStore.updateBatchQueryContext({
+ databaseGroups: (
+ tabStore.currentTab?.batchQueryContext?.databaseGroups ?? []
+ ).filter((name) => name !== databaseGroupName),
});
};
@@ -371,11 +406,9 @@ const connect = (node: SQLEditorTreeNode) => {
},
context: editorContext,
});
- tabStore.updateCurrentTab({
- batchQueryContext: {
- databases: [],
- databaseGroups: [],
- },
+ tabStore.updateBatchQueryContext({
+ databases: [],
+ databaseGroups: [],
});
showConnectionPanel.value = false;
};
diff --git a/frontend/src/views/sql-editor/EditorCommon/DatabaseChooser.vue b/frontend/src/views/sql-editor/EditorCommon/DatabaseChooser.vue
index a1b44c7e0f..c69522fbe4 100644
--- a/frontend/src/views/sql-editor/EditorCommon/DatabaseChooser.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/DatabaseChooser.vue
@@ -21,7 +21,7 @@
"
class="flex flex-row items-center text-main"
>
- <NPopover v-if="isBatchRequest" placement="bottom">
+ <NPopover v-if="isInBatchMode" placement="bottom">
<template #trigger>
<SquareStackIcon class="w-4 h-4 mr-1 text-accent" />
</template>
@@ -58,19 +58,17 @@
import { ChevronRightIcon, SquareStackIcon } from "lucide-vue-next";
import { NButton, NPopover } from "naive-ui";
import { storeToRefs } from "pinia";
-import { computed } from "vue";
import { DatabaseIcon } from "@/components/Icon";
import { InstanceV1EngineIcon, EnvironmentV1Name } from "@/components/v2";
import {
useConnectionOfCurrentSQLEditorTab,
useSQLEditorStore,
useSQLEditorTabStore,
- hasFeature,
} from "@/store";
import { isValidDatabaseName, isValidInstanceName } from "@/types";
import { useSQLEditorContext } from "../context";
-const { currentTab } = storeToRefs(useSQLEditorTabStore());
+const { currentTab, isInBatchMode } = storeToRefs(useSQLEditorTabStore());
const { showConnectionPanel } = useSQLEditorContext();
const { projectContextReady } = storeToRefs(useSQLEditorStore());
@@ -83,24 +81,6 @@ const changeConnection = () => {
defineProps<{
disabled?: boolean;
}>();
-
-const isBatchRequest = computed(() => {
- if (!currentTab.value) {
- return false;
- }
- if (!hasFeature("bb.feature.batch-query")) {
- return false;
- }
- const { batchQueryContext } = currentTab.value;
- if (!batchQueryContext) {
- return false;
- }
- const { databaseGroups = [], databases = [] } = batchQueryContext;
- if (!hasFeature("bb.feature.database-grouping")) {
- return databases.length > 1;
- }
- return databaseGroups.length > 0 || databases.length > 1;
-});
</script>
<style lang="postcss" scoped>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/ErrorView/ErrorView.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/ErrorView/ErrorView.vue
index ce42830f4a..8a3a8ec779 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/ErrorView/ErrorView.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/ErrorView/ErrorView.vue
@@ -1,6 +1,6 @@
<template>
<div
- class="text-md font-normal flex flex-col gap-2 text-sm"
+ class="w-full text-md font-normal flex flex-col gap-2 text-sm"
:class="[dark ? 'text-matrix-green-hover' : 'text-control-light']"
>
<template v-if="resultSet && resultSet.advices.length > 0">
@@ -11,14 +11,9 @@
:execute-params="executeParams"
/>
</template>
- <template v-else>
- <div class="flex items-center gap-2">
- <div class="shrink-0 flex items-center h-6">
- <CircleAlertIcon class="w-6 h-6 text-error" />
- </div>
- <span>{{ error }}</span>
- </div>
- </template>
+ <BBAttention v-else class="w-full" type="error">
+ {{ error }}
+ </BBAttention>
<div v-if="$slots.suffix">
<slot name="suffix" />
</div>
@@ -32,11 +27,11 @@
</template>
<script lang="ts" setup>
-import { CircleAlertIcon } from "lucide-vue-next";
import { NButton } from "naive-ui";
import { Status } from "nice-grpc-common";
import { storeToRefs } from "pinia";
import { computed } from "vue";
+import { BBAttention } from "@/bbkit";
import { useAppFeature, useSQLEditorTabStore } from "@/store";
import type { SQLEditorQueryParams, SQLResultSetV1 } from "@/types";
import { DatabaseChangeMode } from "@/types/proto/v1/setting_service";
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index 23ca360aba..adfd79cbf0 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -235,7 +235,6 @@ import {
hasPermissionToCreateRequestGrantIssue,
instanceV1HasStructuredQueryResult,
isNullOrUndefined,
- ensureDataSourceSelection,
} from "@/utils";
import DataBlock from "./DataBlock.vue";
import DataTable from "./DataTable";
@@ -459,18 +458,10 @@ const handleExportBtnClick = async (
const admin = tabStore.currentTab?.mode === "ADMIN";
const limit = options.limit ?? (admin ? 0 : editorStore.resultRowsLimit);
- const dataSourceId =
- ensureDataSourceSelection(
- props.database?.instance === props.params.connection.instance
- ? props.params.connection.dataSourceId
- : undefined,
- props.database
- ) ?? "";
-
try {
const content = await useSQLStore().exportData({
name: databaseName,
- dataSourceId,
+ dataSourceId: props.params.connection.dataSourceId ?? "",
format: options.format,
statement,
limit,
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
index 07ea2c14b2..eb67731067 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
@@ -37,9 +37,9 @@
@click="$emit('update:selected-database', item.database)"
>
<RichDatabaseName :database="item.database" />
- <InfoIcon
+ <CircleAlertIcon
v-if="isDatabaseQueryFailed(item)"
- class="ml-1 text-yellow-600 w-4 h-auto"
+ class="ml-1 text-red-600 w-4 h-auto"
/>
<span
v-if="isEmptyQueryItem(item)"
@@ -60,16 +60,12 @@
<script setup lang="ts">
import { useLocalStorage } from "@vueuse/core";
import { head } from "lodash-es";
-import { EyeIcon, EyeOffIcon, InfoIcon, XIcon } from "lucide-vue-next";
+import { EyeIcon, EyeOffIcon, CircleAlertIcon, XIcon } from "lucide-vue-next";
import { NButton, NTooltip, NScrollbar } from "naive-ui";
import { storeToRefs } from "pinia";
-import { computed, watch, watchEffect } from "vue";
+import { computed, watch } from "vue";
import { RichDatabaseName } from "@/components/v2";
-import {
- useDatabaseV1Store,
- useSQLEditorTabStore,
- batchGetOrFetchDatabases,
-} from "@/store";
+import { useDatabaseV1Store, useSQLEditorTabStore } from "@/store";
import type { ComposedDatabase, SQLEditorDatabaseQueryContext } from "@/types";
import { hexToRgb } from "@/utils";
@@ -98,10 +94,6 @@ const queriedDatabaseNames = computed(() =>
Array.from(tab.value?.databaseQueryContexts?.keys() || [])
);
-watchEffect(async () => {
- await batchGetOrFetchDatabases(queriedDatabaseNames.value);
-});
-
const items = computed(() => {
return queriedDatabaseNames.value.map<BatchQueryItem>((name) => {
const database = databaseStore.getDatabaseByName(name);
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
index a649513ea6..0506460cdd 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
@@ -76,7 +76,7 @@ const queryElapsedTime = computed(() => {
const cancelQuery = () => {
props.context.abortController?.abort();
- tabStore.updateQueryContext({
+ tabStore.updateDatabaseQueryContext({
database: props.database.name,
contextId: props.context.id,
context: {
@@ -96,7 +96,7 @@ watch(
);
const execQuery = async (params: SQLEditorQueryParams) => {
- const context = tabStore.updateQueryContext({
+ const context = tabStore.updateDatabaseQueryContext({
database: props.database.name,
contextId: props.context.id,
context: {
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
index c3af7396b8..4511d478d1 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
@@ -23,9 +23,9 @@
<template #trigger>
<div class="flex items-center space-x-2">
<span>{{ tabName(context) }}</span>
- <Info
+ <CircleAlertIcon
v-if="context.resultSet?.error"
- class="text-yellow-600 w-4 h-auto"
+ class="text-red-600 w-4 h-auto"
/>
<BBSpin v-if="context.status === 'EXECUTING'" :size="10" />
</div>
@@ -33,6 +33,20 @@
{{ context.params.statement }}
</NTooltip>
</template>
+ <BBAttention
+ v-if="i === 0 && !isMatchedDataSource(context)"
+ type="warning"
+ class="mb-2"
+ >
+ {{
+ $t("sql-editor.batch-query.select-data-source.not-match", {
+ expect: getDataSourceTypeI18n(
+ tabStore.currentTab?.batchQueryContext?.dataSourceType
+ ),
+ actual: getDataSourceTypeI18n(dataSourceInContext(context)?.type),
+ })
+ }}
+ </BBAttention>
<DatabaseQueryContext
class="w-full h-auto grow"
:database="selectedDatabase"
@@ -46,12 +60,14 @@
<script lang="ts" setup>
import dayjs from "dayjs";
import { head } from "lodash-es";
-import { Info } from "lucide-vue-next";
+import { CircleAlertIcon } from "lucide-vue-next";
import { NTabs, NTabPane, NTooltip } from "naive-ui";
import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { BBSpin } from "@/bbkit";
+import { BBAttention } from "@/bbkit";
import { useSQLEditorTabStore } from "@/store";
+import { getDataSourceTypeI18n } from "@/types";
import type { ComposedDatabase, SQLEditorDatabaseQueryContext } from "@/types";
import BatchQuerySelect from "./BatchQuerySelect.vue";
import DatabaseQueryContext from "./DatabaseQueryContext.vue";
@@ -85,6 +101,29 @@ const tabName = (context: SQLEditorDatabaseQueryContext) => {
}
};
+const dataSourceInContext = (context: SQLEditorDatabaseQueryContext) => {
+ const dataSourceId = context.params.connection.dataSourceId;
+ return selectedDatabase.value?.instanceResource.dataSources.find(
+ (ds) => ds.id === dataSourceId
+ );
+};
+
+const isMatchedDataSource = (context: SQLEditorDatabaseQueryContext) => {
+ if (!tabStore.isInBatchMode) {
+ return true;
+ }
+ const mode = tabStore.currentTab?.batchQueryContext?.dataSourceType;
+ if (!mode) {
+ return true;
+ }
+ const dataSource = dataSourceInContext(context);
+ if (!dataSource) {
+ return true;
+ }
+
+ return dataSource.type === mode;
+};
+
watch(
() => head(queryContexts.value)?.id,
(id) => {