Skip to content

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

feat(sql-editor): Improve experience of MongoDB query output (#7293)

Liu Jif9d659019599
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
index 02125c9b8c..3b7495ab9e 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
@@ -61,13 +61,21 @@
v-for="(row, rowIndex) of table.getRowModel().rows"
:key="rowIndex"
class="group"
+ :data-row-index="offset + rowIndex"
>
<td
v-for="(cell, cellIndex) of row.getVisibleCells()"
:key="cellIndex"
- class="px-2 py-1 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border border-block-border group-last:border-b-0 group-even:bg-gray-50/50 dark:group-even:bg-gray-700/50"
+ class="p-0 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border border-block-border group-last:border-b-0 group-even:bg-gray-50/50 dark:group-even:bg-gray-700/50"
+ :data-col-index="cellIndex"
>
- <TableCell :html="renderCellValue(cell.getValue())" />
+ <TableCell
+ :value="cell.getValue()"
+ :keyword="keyword"
+ :set-index="setIndex"
+ :row-index="offset + rowIndex"
+ :col-index="cellIndex"
+ />
</td>
</tr>
</tbody>
@@ -86,12 +94,10 @@
<script lang="ts" setup>
import { computed, nextTick, PropType, ref, watch } from "vue";
import { ColumnDef, Table } from "@tanstack/vue-table";
-import { escape } from "lodash-es";
import useTableColumnWidthLogic from "./useTableResize";
import SensitiveDataIcon from "./SensitiveDataIcon.vue";
import TableCell from "./TableCell.vue";
-import { getHighlightHTMLByRegExp } from "@/utils";
export type DataTableColumn = {
key: string;
@@ -123,6 +129,14 @@ const props = defineProps({
type: String,
default: "",
},
+ setIndex: {
+ type: Number,
+ default: 0,
+ },
+ offset: {
+ type: Number,
+ default: 0,
+ },
});
const scrollerRef = ref<HTMLDivElement>();
@@ -145,24 +159,6 @@ const isColumnMissingSensitive = (index: number): boolean => {
return (props.sensitive[index] ?? false) && !isSensitiveColumn(index);
};
-const renderCellValue = (value: any) => {
- const str = String(value);
- if (str.length === 0) {
- return `<br style="min-width: 1rem; display: inline-flex;" />`;
- }
-
- const { keyword } = props;
- if (!keyword) {
- return escape(str);
- }
-
- return getHighlightHTMLByRegExp(
- escape(str),
- escape(keyword),
- false /* !caseSensitive */
- );
-};
-
const scrollTo = (x: number, y: number) => {
scrollerRef.value?.scroll(x, y);
};
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
index bb8cd14417..db2f8b0073 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
@@ -1,50 +1,40 @@
<template>
- <div class="relative" :class="[disallowCopyingData && 'select-none']">
+ <div class="relative px-2 py-1" :class="classes" @click="handleClick">
<!-- eslint-disable-next-line vue/no-v-html -->
<div ref="wrapperRef" class="overflow-hidden" v-html="html"></div>
- <div v-if="truncated" class="absolute right-0 top-1/2 translate-y-[-50%]">
- <NButton
- size="tiny"
- circle
- class="dark:!bg-dark-bg"
- @click="showModal = true"
- >
+ <div v-if="clickable" class="absolute right-0 top-1/2 translate-y-[-50%]">
+ <NButton size="tiny" circle class="dark:!bg-dark-bg" @click="showDetail">
<template #icon>
<heroicons:arrows-pointing-out class="w-4 h-4" />
</template>
</NButton>
</div>
-
- <BBModal
- v-if="showModal"
- :title="$t('common.detail')"
- @close="showModal = false"
- >
- <!-- eslint-disable vue/no-v-html -->
- <div
- class="w-[100vw-8rem] min-w-[20rem] md:max-w-[40rem] max-h-[100vh-12rem] overflow-auto whitespace-pre-wrap text-sm text-main"
- v-html="html"
- ></div>
- </BBModal>
</div>
</template>
<script setup lang="ts">
-import { ref } from "vue";
+import { computed, ref } from "vue";
import { NButton } from "naive-ui";
import { useResizeObserver } from "@vueuse/core";
+import { escape } from "lodash-es";
-import { BBModal } from "@/bbkit";
import { useSQLResultViewContext } from "../context";
+import { getHighlightHTMLByRegExp } from "@/utils";
+import { useDatabaseV1Store, useTabStore } from "@/store";
+import { UNKNOWN_ID } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
-defineProps<{
- html?: string;
+const props = defineProps<{
+ value: unknown;
+ keyword?: string;
+ setIndex: number;
+ rowIndex: number;
+ colIndex: number;
}>();
-const { disallowCopyingData } = useSQLResultViewContext();
+const { dark, disallowCopyingData, detail } = useSQLResultViewContext();
const wrapperRef = ref<HTMLDivElement>();
const truncated = ref(false);
-const showModal = ref(false);
useResizeObserver(wrapperRef, (entries) => {
const div = entries[0].target as HTMLDivElement;
@@ -56,4 +46,60 @@ useResizeObserver(wrapperRef, (entries) => {
truncated.value = false;
}
});
+
+const clickable = computed(() => {
+ if (truncated.value) return true;
+ const conn = useTabStore().currentTab.connection;
+ if (conn.databaseId !== String(UNKNOWN_ID)) {
+ const db = useDatabaseV1Store().getDatabaseByUID(conn.databaseId);
+ if (db.instanceEntity.engine === Engine.MONGODB) {
+ return true;
+ }
+ }
+ return false;
+});
+
+const classes = computed(() => {
+ const classes: string[] = [];
+ if (disallowCopyingData.value) {
+ classes.push("select-none");
+ }
+ if (clickable.value) {
+ classes.push("cursor-pointer");
+ classes.push(dark.value ? "hover:!bg-white/20" : "hover:!bg-black/5");
+ }
+ return classes;
+});
+
+const html = computed(() => {
+ const str = String(props.value);
+ if (str.length === 0) {
+ return `<br style="min-width: 1rem; display: inline-flex;" />`;
+ }
+
+ const { keyword } = props;
+ if (!keyword) {
+ return escape(str);
+ }
+
+ return getHighlightHTMLByRegExp(
+ escape(str),
+ escape(keyword),
+ false /* !caseSensitive */
+ );
+});
+
+const handleClick = () => {
+ if (!clickable.value) return;
+ showDetail();
+};
+
+const showDetail = () => {
+ detail.value = {
+ show: true,
+ set: props.setIndex,
+ row: props.rowIndex,
+ col: props.colIndex,
+ };
+};
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
index 87b3913bfc..05513e0f8f 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/useTableResize.ts
@@ -35,7 +35,16 @@ const useTableResize = (options: TableResizeOptions) => {
const table = computed(() => options.tableRef.value!);
+ const containerWidth = computed(() => {
+ return options.scrollerRef?.value?.scrollWidth || 0;
+ });
+
const normalizeWidth = (width: number) => {
+ if (state.columns.length === 1) {
+ // When there is only one column, display it with full width.
+ // minus 1px to avoid unexpected horizontal scrollbar.
+ return containerWidth.value - 1;
+ }
const { maxWidth, minWidth } = options;
if (width > maxWidth) return maxWidth;
if (width < minWidth) return minWidth;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue
new file mode 100644
index 0000000000..0a6772feee
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel.vue
@@ -0,0 +1,133 @@
+<template>
+ <DrawerContent
+ :title="$t('common.detail')"
+ class="w-[100vw-4rem] min-w-[20rem] max-w-[100vw-4rem] md:w-[33vw]"
+ >
+ <div
+ class="h-full flex flex-col gap-y-2"
+ :class="dark ? 'text-white' : 'text-main'"
+ >
+ <div class="flex items-center justify-between gap-x-4">
+ <div class="flex items-center gap-x-2">
+ <NButton size="tiny" :disabled="detail.row === 0" @click="move(-1)">
+ <template #icon>
+ <heroicons:chevron-up />
+ </template>
+ </NButton>
+ <NButton
+ size="tiny"
+ :disabled="detail.row === totalCount - 1"
+ @click="move(1)"
+ >
+ <template #icon>
+ <heroicons:chevron-down />
+ </template>
+ </NButton>
+ <div class="text-xs text-control-light flex items-center gap-x-1">
+ <span>{{ detail.row + 1 }}</span>
+ <span>/</span>
+ <span>{{ totalCount }}</span>
+ </div>
+ </div>
+
+ <div>
+ <NButton
+ v-if="!disallowCopyingData"
+ size="small"
+ type="primary"
+ @click="handleCopy"
+ >
+ {{ $t("common.copy") }}
+ </NButton>
+ </div>
+ </div>
+ <!-- eslint-disable vue/no-v-html -->
+ <div
+ class="flex-1 overflow-auto whitespace-pre-wrap text-sm"
+ :class="disallowCopyingData && 'select-none'"
+ v-html="html"
+ ></div>
+ </div>
+ </DrawerContent>
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import { escape, get } from "lodash-es";
+import { NButton } from "naive-ui";
+import { onKeyStroke, useClipboard } from "@vueuse/core";
+import { useI18n } from "vue-i18n";
+
+import { DrawerContent } from "@/components/v2";
+import { useSQLResultViewContext } from "./context";
+import { SQLResultSetV1 } from "@/types";
+import { extractSQLRowValue } from "@/utils";
+import { QueryResult, RowValue } from "@/types/proto/v1/sql_service";
+import { pushNotification } from "@/store";
+
+const props = defineProps<{
+ resultSet?: SQLResultSetV1;
+}>();
+
+const { t } = useI18n();
+const { dark, detail, disallowCopyingData } = useSQLResultViewContext();
+
+const value = computed(() => {
+ const { resultSet } = props;
+ const { set, row, col } = detail.value;
+ const cell: RowValue =
+ get(resultSet, `results.${set}.rows.${row}.values.${col}`) ??
+ RowValue.fromJSON({});
+ return extractSQLRowValue(cell);
+});
+
+const totalCount = computed(() => {
+ const result: QueryResult =
+ get(props.resultSet, `results.${detail.value.set}`) ??
+ QueryResult.fromJSON({});
+ return result.rows.length;
+});
+
+const html = computed(() => {
+ const str = String(value.value);
+ if (str.length === 0) {
+ return `<br style="min-width: 1rem; display: inline-flex;" />`;
+ }
+
+ return escape(str);
+});
+
+const { copy, copied } = useClipboard({
+ source: computed(() => {
+ return String(value.value);
+ }),
+});
+const handleCopy = () => {
+ copy().then(() => {
+ if (copied.value) {
+ pushNotification({
+ module: "bytebase",
+ style: "INFO",
+ title: t("common.copied"),
+ });
+ }
+ });
+};
+
+const move = (offset: number) => {
+ const target = detail.value.row + offset;
+ if (target < 0 || target >= totalCount.value) return;
+ detail.value.row = target;
+};
+
+onKeyStroke("ArrowUp", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ move(-1);
+});
+onKeyStroke("ArrowDown", (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ move(1);
+});
+</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
index 6c073016b3..7764a942d9 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
@@ -9,6 +9,7 @@
<SingleResultViewV1
:params="executeParams"
:result="resultSet.results[0]"
+ :set-index="0"
/>
</template>
<template v-else-if="viewMode === 'MULTI-RESULT'">
@@ -23,7 +24,11 @@
:name="tabName(result, i)"
class="flex-1 flex flex-col overflow-hidden"
>
- <SingleResultViewV1 :params="executeParams" :result="result" />
+ <SingleResultViewV1
+ :params="executeParams"
+ :result="result"
+ :set-index="i"
+ />
</NTabPane>
</NTabs>
</template>
@@ -55,22 +60,33 @@
{{ $t("sql-editor.table-empty-placeholder") }}
</template>
</div>
+
+ <Drawer
+ v-model:show="detail.show"
+ :close-on-esc="true"
+ :trap-focus="true"
+ @close="detail.show = false"
+ >
+ <DetailPanel v-if="detail.show" :result-set="resultSet" />
+ </Drawer>
</NConfigProvider>
</template>
<script lang="ts" setup>
-import { computed, PropType, toRef } from "vue";
+import { computed, PropType, ref, toRef } from "vue";
import { useI18n } from "vue-i18n";
import { darkTheme, NConfigProvider, NTabs, NTabPane } from "naive-ui";
import { Status } from "nice-grpc-common";
import { darkThemeOverrides } from "@/../naive-ui.config";
+import { Drawer } from "@/components/v2";
import SingleResultViewV1 from "./SingleResultViewV1.vue";
import EmptyView from "./EmptyView.vue";
import { ExecuteConfig, ExecuteOption, SQLResultSetV1 } from "@/types";
-import { provideSQLResultViewContext } from "./context";
+import { provideSQLResultViewContext, SQLResultViewContext } from "./context";
import ErrorView from "./ErrorView.vue";
import RequestQueryButton from "./RequestQueryButton.vue";
+import DetailPanel from "./DetailPanel.vue";
import { QueryResult } from "@/types/proto/v1/sql_service";
import {
useCurrentUserV1,
@@ -109,6 +125,13 @@ const props = defineProps({
const { t } = useI18n();
const currentUser = useCurrentUserV1();
const connection = computed(() => useTabStore().currentTab.connection);
+const keyword = ref("");
+const detail: SQLResultViewContext["detail"] = ref({
+ show: false,
+ set: 0,
+ row: 0,
+ col: 0,
+});
const viewMode = computed((): ViewMode => {
const { resultSet } = props;
@@ -173,5 +196,7 @@ const disallowCopyingData = computed(() => {
provideSQLResultViewContext({
dark: toRef(props, "dark"),
disallowCopyingData,
+ keyword,
+ detail,
});
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index d840aff195..932fa65dd6 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -6,11 +6,12 @@
<div class="flex flex-row justify-start items-center mr-2 shrink-0">
<NInput
v-if="showSearchFeature"
- v-model:value="state.search"
+ :value="state.search"
class="!max-w-[10rem]"
size="small"
type="text"
:placeholder="t('sql-editor.search-results')"
+ @update:value="debouncedUpdateKeyword"
>
<template #prefix>
<heroicons-outline:search class="h-5 w-5 text-gray-300" />
@@ -32,8 +33,8 @@
v-if="showPagination"
:simple="true"
:item-count="table.getCoreRowModel().rows.length"
- :page="table.getState().pagination.pageIndex + 1"
- :page-size="table.getState().pagination.pageSize"
+ :page="pageIndex + 1"
+ :page-size="pageSize"
@update-page="handleChangePage"
/>
<NButton
@@ -75,7 +76,8 @@
:data="data"
:masked="props.result.masked"
:sensitive="props.result.sensitive"
- :keyword="state.search"
+ :set-index="setIndex"
+ :offset="pageIndex * pageSize"
/>
</div>
@@ -118,9 +120,9 @@
<script lang="ts" setup>
import { computed, reactive, ref } from "vue";
-import { NPagination } from "naive-ui";
+import { NInput, NPagination } from "naive-ui";
import { useI18n } from "vue-i18n";
-import { debouncedRef } from "@vueuse/core";
+import { useDebounceFn } from "@vueuse/core";
import {
ColumnDef,
getCoreRowModel,
@@ -181,6 +183,7 @@ const props = defineProps<{
option?: Partial<ExecuteOption> | undefined;
};
result: QueryResult;
+ setIndex: number;
}>();
const state = reactive<LocalState>({
@@ -188,7 +191,7 @@ const state = reactive<LocalState>({
showRequestExportPanel: false,
});
-const { dark } = useSQLResultViewContext();
+const { dark, keyword } = useSQLResultViewContext();
const { t } = useI18n();
const tabStore = useTabStore();
@@ -271,10 +274,9 @@ const allowToExportData = computed(() => {
});
// use a debounced value to improve performance when typing rapidly
-const keyword = debouncedRef(
- computed(() => state.search),
- 200
-);
+const debouncedUpdateKeyword = useDebounceFn((value: string) => {
+ keyword.value = value;
+}, 200);
const columns = computed(() => {
const columns = props.result.columnNames;
@@ -317,6 +319,13 @@ const table = useVueTable<string[]>({
table.setPageSize(DEFAULT_PAGE_SIZE);
+const pageIndex = computed(() => {
+ return table.getState().pagination.pageIndex;
+});
+const pageSize = computed(() => {
+ return table.getState().pagination.pageSize;
+});
+
const exportDropdownOptions = computed(() => [
{
label: t("sql-editor.download-as-file", { file: "CSV" }),
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
index 575e77e370..4228694a21 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/context.ts
@@ -3,6 +3,13 @@ import { type Ref, type InjectionKey, provide, inject } from "vue";
export type SQLResultViewContext = {
dark: Ref<boolean>;
disallowCopyingData: Ref<boolean>;
+ keyword: Ref<string>;
+ detail: Ref<{
+ show: boolean;
+ set: number; // The index of selected result set.
+ row: number; // The row index of selected record.
+ col: number; // The column index of selected cell.
+ }>;
};
export const KEY = Symbol(