Skip to content

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

feat(sql-editor): select and copy data by row/column (#14762)

Liu Jibded5b88c5c3
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/DataTable.vue
index a719858fe6..eb6eb66145 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/DataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/DataTable.vue
@@ -29,13 +29,21 @@
<th
v-for="header of table.getFlatHeaders()"
:key="header.index"
- class="relative px-2 py-2 min-w-[2rem] text-left bg-gray-50 dark:bg-gray-700 text-xs font-medium text-gray-500 dark:text-gray-300 tracking-wider border border-t-0 border-block-border border-b-0"
+ class="group relative px-2 py-2 min-w-[2rem] text-left bg-gray-50 dark:bg-gray-700 text-xs font-medium text-gray-500 dark:text-gray-300 tracking-wider border border-t-0 border-block-border border-b-0"
+ :class="{
+ 'cursor-pointer': !selectionDisabled,
+ }"
+ @click.stop="selectColumn(header.index)"
v-bind="tableResize.getColumnProps(header.index)"
>
<div
- class="flex items-center overflow-hidden cursor-pointer"
- @click="header.column.getToggleSortingHandler()?.($event)"
- >
+ v-if="!selectionDisabled"
+ class="absolute inset-0 group-hover:bg-accent/10 pointer-events-none"
+ :class="
+ selectionState.column === header.index && 'bg-accent/10'
+ "
+ />
+ <div class="flex items-center overflow-hidden">
<span class="flex flex-row items-center select-none">
<template
v-if="String(header.column.columnDef.header).length > 0"
@@ -63,7 +71,10 @@
/>
</template>
- <ColumnSortedIcon :is-sorted="header.column.getIsSorted()" />
+ <ColumnSortedIcon
+ :is-sorted="header.column.getIsSorted()"
+ @click="header.column.getToggleSortingHandler()?.($event)"
+ />
</div>
<!-- The drag-to-resize handler -->
@@ -85,7 +96,7 @@
<td
v-for="(cell, cellIndex) of row.getVisibleCells()"
:key="cellIndex"
- class="p-0 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border border-block-border border-y-0 group-even:bg-gray-100/50 dark:group-even:bg-gray-700/50"
+ class="relative p-0 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border border-block-border border-y-0 group-even:bg-gray-100/50 dark:group-even:bg-gray-700/50"
:data-col-index="cellIndex"
>
<TableCell
@@ -125,7 +136,7 @@
import type { Table } from "@tanstack/vue-table";
import { useElementSize } from "@vueuse/core";
import { NEmpty, NScrollbar } from "naive-ui";
-import { computed, nextTick, ref, watch } from "vue";
+import { computed, nextTick, ref, toRef, watch } from "vue";
import {
FeatureBadge,
FeatureBadgeForInstanceLicense,
@@ -136,6 +147,7 @@ import { usePreventBackAndForward } from "@/utils";
import { useSQLResultViewContext } from "../../context";
import ColumnSortedIcon from "../common/ColumnSortedIcon.vue";
import SensitiveDataIcon from "../common/SensitiveDataIcon.vue";
+import { provideSelectionContext } from "../common/selection-logic";
import TableCell from "./TableCell.vue";
import useTableColumnWidthLogic from "./useTableResize";
@@ -153,6 +165,11 @@ const props = defineProps<{
maxHeight?: number;
}>();
+const {
+ state: selectionState,
+ disabled: selectionDisabled,
+ selectColumn,
+} = provideSelectionContext(toRef(props, "table"));
const containerRef = ref<HTMLDivElement>();
const scrollbarRef = ref<InstanceType<typeof NScrollbar>>();
const tableRef = ref<HTMLTableElement>();
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/TableCell.vue
index 2f626fbcb2..d5aad422be 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable/TableCell.vue
@@ -24,7 +24,7 @@
<script setup lang="ts">
import { type Table } from "@tanstack/vue-table";
import { useResizeObserver } from "@vueuse/core";
-import { escape } from "lodash-es";
+import { escape, uniq } from "lodash-es";
import { NButton } from "naive-ui";
import { computed, ref } from "vue";
import { useConnectionOfCurrentSQLEditorTab } from "@/store";
@@ -32,6 +32,7 @@ import { Engine } from "@/types/proto/v1/common";
import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";
import { extractSQLRowValue, getHighlightHTMLByRegExp } from "@/utils";
import { useSQLResultViewContext } from "../../context";
+import { useSelectionContext } from "../common/selection-logic";
const props = defineProps<{
table: Table<QueryRow>;
@@ -43,6 +44,12 @@ const props = defineProps<{
const { dark, disallowCopyingData, detail, keyword } =
useSQLResultViewContext();
+
+const {
+ state: selectionState,
+ disabled: selectionDisabled,
+ selectRow,
+} = useSelectionContext();
const wrapperRef = ref<HTMLDivElement>();
const truncated = ref(false);
@@ -76,11 +83,23 @@ const classes = computed(() => {
if (disallowCopyingData.value) {
classes.push("select-none");
}
+ if (!selectionDisabled.value) {
+ if (props.colIndex === 0) {
+ classes.push("cursor-pointer");
+ classes.push("hover:bg-accent/10");
+ }
+ if (
+ selectionState.value.column === props.colIndex ||
+ selectionState.value.row === props.rowIndex
+ ) {
+ classes.push("bg-accent/10");
+ }
+ }
if (clickable.value) {
classes.push("cursor-pointer");
classes.push(dark.value ? "hover:!bg-white/20" : "hover:!bg-black/5");
}
- return classes;
+ return uniq(classes);
});
const html = computed(() => {
@@ -108,7 +127,12 @@ const html = computed(() => {
);
});
-const handleClick = () => {
+const handleClick = (e: MouseEvent) => {
+ if (props.colIndex === 0 && !selectionDisabled.value) {
+ selectRow(props.rowIndex);
+ e.stopPropagation();
+ }
+
if (!clickable.value) return;
showDetail();
};
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/ColumnHeader.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/ColumnHeader.vue
index aa29e0f582..1586320203 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/ColumnHeader.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/ColumnHeader.vue
@@ -1,11 +1,17 @@
<template>
<div
- class="relative px-2 h-[32px] min-w-[2rem] whitespace-nowrap text-left bg-gray-50 dark:bg-gray-700 text-xs font-medium text-gray-500 dark:text-gray-300 tracking-wider"
+ class="group relative px-2 h-[32px] min-w-[2rem] whitespace-nowrap text-left bg-gray-50 dark:bg-gray-700 text-xs font-medium text-gray-500 dark:text-gray-300 tracking-wider"
+ :class="{
+ 'cursor-pointer': !selectionDisabled,
+ }"
+ @click.stop="selectColumn(header.index)"
>
<div
- class="h-full flex items-center overflow-hidden cursor-pointer"
- @click="header.column.getToggleSortingHandler()?.($event)"
- >
+ v-if="!selectionDisabled"
+ class="absolute inset-0 group-hover:bg-accent/10 pointer-events-none"
+ :class="selectionState.column === header.index && 'bg-accent/10'"
+ />
+ <div class="h-full flex items-center overflow-hidden">
<span class="flex flex-row items-center select-none">
<template v-if="String(header.column.columnDef.header).length > 0">
{{ header.column.columnDef.header }}
@@ -31,7 +37,10 @@
/>
</template>
- <ColumnSortedIcon :is-sorted="header.column.getIsSorted()" />
+ <ColumnSortedIcon
+ :is-sorted="header.column.getIsSorted()"
+ @click="header.column.getToggleSortingHandler()?.($event)"
+ />
</div>
<!-- The drag-to-resize handler -->
@@ -55,6 +64,7 @@ import { featureToRef } from "@/store";
import type { QueryRow } from "@/types/proto/v1/sql_service";
import ColumnSortedIcon from "../common/ColumnSortedIcon.vue";
import SensitiveDataIcon from "../common/SensitiveDataIcon.vue";
+import { useSelectionContext } from "../common/selection-logic";
defineProps<{
header: Header<QueryRow, any>;
@@ -67,5 +77,10 @@ defineEmits<{
(event: "auto-resize"): void;
}>();
+const {
+ state: selectionState,
+ disabled: selectionDisabled,
+ selectColumn,
+} = useSelectionContext();
const hasSensitiveFeature = featureToRef("bb.feature.sensitive-data");
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/DataTableLite.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/DataTableLite.vue
index 9360e86b2a..a40b037cb1 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/DataTableLite.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/DataTableLite.vue
@@ -57,6 +57,7 @@ import { computed, h, nextTick, ref, toRef, watch, type StyleValue } from "vue";
import { QueryRow, type RowValue } from "@/types/proto/v1/sql_service";
import { nextAnimationFrame, usePreventBackAndForward } from "@/utils";
import { useSQLResultViewContext } from "../../context";
+import { provideSelectionContext } from "../common/selection-logic";
import ColumnHeader from "./ColumnHeader.vue";
import TableCell from "./TableCell.vue";
import useTableColumnWidthLogic from "./useTableResize";
@@ -81,6 +82,7 @@ const props = defineProps<{
const { keyword } = useSQLResultViewContext();
+provideSelectionContext(toRef(props, "table"));
const headers = computed(() => {
return props.table.getFlatHeaders() as Header<QueryRow, RowValue>[];
});
@@ -195,6 +197,7 @@ const columns = computed(() => {
value,
width: tableResize.getColumnWidth(colIndex),
keyword: keyword.value,
+ offset: props.offset,
setIndex: props.setIndex,
rowIndex: props.offset + rowIndex,
colIndex,
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/TableCell.vue
index 5c865c49b0..3ac8f35ec6 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite/TableCell.vue
@@ -27,7 +27,7 @@
<script setup lang="ts">
import { type Table } from "@tanstack/vue-table";
-import { escape } from "lodash-es";
+import { escape, uniq } from "lodash-es";
import { NButton } from "naive-ui";
import stringWidth from "string-width";
import { computed, ref } from "vue";
@@ -36,6 +36,7 @@ import { Engine } from "@/types/proto/v1/common";
import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";
import { extractSQLRowValue, getHighlightHTMLByRegExp } from "@/utils";
import { useSQLResultViewContext } from "../../context";
+import { useSelectionContext } from "../common/selection-logic";
const props = defineProps<{
table: Table<QueryRow>;
@@ -46,6 +47,11 @@ const props = defineProps<{
colIndex: number;
}>();
+const {
+ state: selectionState,
+ disabled: selectionDisabled,
+ selectRow,
+} = useSelectionContext();
const { dark, disallowCopyingData, detail, keyword } =
useSQLResultViewContext();
const wrapperRef = ref<HTMLDivElement>();
@@ -82,11 +88,23 @@ const classes = computed(() => {
if (disallowCopyingData.value) {
classes.push("select-none");
}
+ if (!selectionDisabled.value) {
+ if (props.colIndex === 0) {
+ classes.push("cursor-pointer");
+ classes.push("hover:bg-accent/10");
+ }
+ if (
+ selectionState.value.column === props.colIndex ||
+ selectionState.value.row === props.rowIndex
+ ) {
+ classes.push("bg-accent/10");
+ }
+ }
if (clickable.value) {
classes.push("cursor-pointer");
classes.push(dark.value ? "hover:!bg-white/20" : "hover:!bg-black/5");
}
- return classes;
+ return uniq(classes);
});
const html = computed(() => {
@@ -114,7 +132,12 @@ const html = computed(() => {
);
});
-const handleClick = () => {
+const handleClick = (e: MouseEvent) => {
+ if (props.colIndex === 0 && !selectionDisabled.value) {
+ selectRow(props.rowIndex);
+ e.stopPropagation();
+ }
+
if (!clickable.value) return;
showDetail();
};
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/common/selection-logic.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/common/selection-logic.ts
new file mode 100644
index 0000000000..c917e0611d
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/common/selection-logic.ts
@@ -0,0 +1,168 @@
+import type { Cell, Table } from "@tanstack/vue-table";
+import { useEventListener } from "@vueuse/core";
+import {
+ computed,
+ inject,
+ nextTick,
+ provide,
+ ref,
+ watch,
+ type InjectionKey,
+ type Ref,
+} from "vue";
+import { useI18n } from "vue-i18n";
+import { pushNotification } from "@/store";
+import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";
+import { extractSQLRowValue, isDescendantOf, toClipboard } from "@/utils";
+import { useSQLResultViewContext } from "../../context";
+
+export const PREVENT_DISMISS_SELECTION = "bb-prevent-dismiss-selection";
+
+export type SelectionState = {
+ row: number;
+ column: number;
+};
+
+export type SelectionContext = {
+ state: Ref<SelectionState>;
+ disabled: Ref<boolean>;
+ selectRow: (row: number) => void;
+ selectColumn: (column: number) => void;
+ deselect: () => void;
+ copy: () => void;
+};
+
+export const KEY = Symbol(
+ "bb.sql-editor.result-view.selection"
+) as InjectionKey<SelectionContext>;
+
+export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {
+ const { t } = useI18n();
+
+ const copying = ref(false);
+ const state = ref<SelectionState>({
+ row: -1,
+ column: -1,
+ });
+ const resultViewContext = useSQLResultViewContext();
+ const disabled = computed(() => {
+ return resultViewContext.disallowCopyingData.value;
+ });
+ const selectRow = (row: number) => {
+ if (disabled.value) return;
+ state.value = { row, column: -1 };
+ };
+ const selectColumn = (column: number) => {
+ if (disabled.value) return;
+ state.value = { row: -1, column };
+ };
+ const deselect = () => {
+ state.value = {
+ row: -1,
+ column: -1,
+ };
+ };
+
+ useEventListener("click", (e) => {
+ if (copying.value) return;
+ if (isDescendantOf(e.target as Element, `.${PREVENT_DISMISS_SELECTION}`)) {
+ return;
+ }
+ deselect();
+ });
+
+ watch(table, deselect, {
+ immediate: true,
+ deep: false,
+ });
+
+ const getValues = () => {
+ if (state.value.row >= 0) {
+ const row = table.value.getPrePaginationRowModel().rows[state.value.row];
+ if (!row) {
+ return "";
+ }
+ const cells = row.getVisibleCells();
+ return cells
+ .map((cell) =>
+ String(extractSQLRowValue(cell.getValue() as RowValue).plain)
+ )
+ .join("\t");
+ } else if (state.value.column >= 0) {
+ const rows = table.value.getRowModel().rows;
+ const cells: Cell<QueryRow, unknown>[] = [];
+ for (let i = 0; i < rows.length; i++) {
+ const row = rows[i];
+ const cell = row.getVisibleCells()[state.value.column];
+ if (!cell) continue;
+ cells.push(cell);
+ }
+ if (cells.length === 0) {
+ return "";
+ }
+ return cells
+ .map((cell) =>
+ String(extractSQLRowValue(cell.getValue() as RowValue).plain)
+ )
+ .join("\n");
+ }
+
+ return "";
+ };
+
+ const copy = () => {
+ if (disabled.value) {
+ return false;
+ }
+ const values = getValues();
+ if (!values) {
+ return false;
+ }
+ copying.value = true;
+ toClipboard(values)
+ .catch((err: any) => {
+ const errors = [t("common.failed")];
+ if (err && err instanceof Error) {
+ errors.push(err.message);
+ }
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: errors.join(": "),
+ });
+ })
+ .finally(() => {
+ nextTick(() => {
+ copying.value = false;
+ });
+ });
+ return true;
+ };
+
+ useEventListener("keydown", (e) => {
+ if (disabled.value) return;
+ if (state.value.row < 0 && state.value.column < 0) return;
+ if ((e.key === "c" || e.key === "C") && (e.metaKey || e.ctrlKey)) {
+ const copied = copy();
+ if (copied) {
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ }
+ }
+ });
+
+ const context: SelectionContext = {
+ state,
+ disabled,
+ selectRow,
+ selectColumn,
+ deselect,
+ copy,
+ };
+ provide(KEY, context);
+ return context;
+};
+
+export const useSelectionContext = () => {
+ return inject(KEY)!;
+};