Skip to content

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

chore: support copy all result set (#19427)

ecmadao1c773b54edaa
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
index f3abff3fb3..80ee60cf7b 100644
--- 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
@@ -12,9 +12,8 @@ import {
} from "vue";
import { useI18n } from "vue-i18n";
import { pushNotification } from "@/store";
-import type { QueryRow, RowValue } from "@/types/proto-es/v1/sql_service_pb";
+import type { RowValue } from "@/types/proto-es/v1/sql_service_pb";
import { extractSQLRowValuePlain } from "@/utils";
-import { type SQLResultViewContext } from "../../context";
import {
type BinaryFormatContext,
detectBinaryFormat,
@@ -34,7 +33,8 @@ export type SelectionContext = {
toggleSelectColumn: (column: number) => void;
toggleSelectCell: (row: number, column: number) => void;
deselect: () => void;
- copy: () => boolean;
+ copySelected: () => void;
+ copyAll: () => void;
};
export const KEY = Symbol(
@@ -45,12 +45,12 @@ export const provideSelectionContext = ({
rows,
columns,
binaryFormatContext,
- resultViewContext,
+ disallowCopyingData,
}: {
rows: ComputedRef<ResultTableRow[]>;
columns: ComputedRef<ResultTableColumn[]>;
binaryFormatContext: BinaryFormatContext;
- resultViewContext: SQLResultViewContext;
+ disallowCopyingData: ComputedRef<boolean>;
}) => {
const { t } = useI18n();
const { getBinaryFormat } = binaryFormatContext;
@@ -65,11 +65,12 @@ export const provideSelectionContext = ({
columns: [],
});
const disabled = computed(() => {
- return resultViewContext.disallowCopyingData.value;
+ return disallowCopyingData.value;
});
- const isCellSelected = computed(
- () => state.value.rows.length === 1 && state.value.columns.length === 1
- );
+ const checkIsSingleCellSelected = (state: SelectionState) => {
+ return state.rows.length === 1 && state.columns.length === 1;
+ };
+ const isCellSelected = computed(() => checkIsSingleCellSelected(state.value));
const deselect = () => {
state.value = {
@@ -123,6 +124,15 @@ export const provideSelectionContext = ({
};
};
+ // Escape values that contain TSV delimiters (\t, \n) or double quotes
+ // by wrapping in double quotes (standard TSV/CSV quoting convention).
+ const escapeTSVValue = (val: string): string => {
+ if (val.includes("\t") || val.includes("\n") || val.includes('"')) {
+ return `"${val.replaceAll('"', '""')}"`;
+ }
+ return val;
+ };
+
const getFormattedValue = ({
value,
colIndex,
@@ -161,14 +171,14 @@ export const provideSelectionContext = ({
return String(extractSQLRowValuePlain(value));
};
- const getValues = () => {
- if (isCellSelected.value) {
+ const getValues = (state: SelectionState) => {
+ if (checkIsSingleCellSelected(state)) {
// single cell selected
- const row = rows.value[state.value.rows[0]];
+ const row = rows.value[state.rows[0]];
if (!row) {
return "";
}
- const cell = row.item.values[state.value.columns[0]];
+ const cell = row.item.values[state.columns[0]];
if (!cell) {
return "";
}
@@ -176,56 +186,50 @@ export const provideSelectionContext = ({
// Get the cell value
return getFormattedValue({
value: cell,
- colIndex: state.value.columns[0],
- rowIndex: state.value.rows[0],
+ colIndex: state.columns[0],
+ rowIndex: state.rows[0],
});
}
- // build column name
- let columnNames = ["index", ...columns.value.map((c) => c.name)];
- if (state.value.columns.length > 0) {
- columnNames = state.value.columns.map(
- (columnIndex) => columns.value[columnIndex]?.name ?? ""
- );
- }
-
- if (state.value.rows.length > 0) {
+ if (state.rows.length > 0) {
+ const columnNames = ["index", ...columns.value.map((c) => c.name)];
// multi-rows selected
- const data: QueryRow[] = [];
- for (const rowIndex of state.value.rows) {
- const d = rows.value[rowIndex].item;
- if (!d) {
+ const data: string[] = [];
+ for (const rowIndex of state.rows) {
+ const queryRow = rows.value[rowIndex].item;
+ if (!queryRow) {
continue;
}
- data.push(d);
- }
- if (data.length === 0) {
- return "";
- }
- const value = data
- .map((row, rowIdx) => {
- return row.values
- .map((cell, colIdx) => {
- return getFormattedValue({
+ const rowStr = queryRow.values
+ .map((cell, colIdx) => {
+ return escapeTSVValue(
+ getFormattedValue({
value: cell,
colIndex: colIdx,
- rowIndex: state.value.rows[rowIdx],
- });
- })
- .join("\t");
- })
- .join("\n");
+ rowIndex,
+ })
+ );
+ })
+ .join("\t");
- return `${columnNames.join("\t")}\n${value}`;
+ data.push(`${rowIndex}\t${rowStr}`);
+ }
+ if (data.length === 0) {
+ return "";
+ }
+ return `${columnNames.join("\t")}\n${data.join("\n")}`;
}
- if (state.value.columns.length > 0) {
+ if (state.columns.length > 0) {
+ const columnNames = state.columns.map(
+ (columnIndex) => columns.value[columnIndex]?.name ?? ""
+ );
// multi-columns selected
const values: RowValue[][] = [];
for (const row of rows.value) {
const cells: RowValue[] = [];
- for (const column of state.value.columns) {
+ for (const column of state.columns) {
const cell = row.item.values[column];
if (!cell) continue;
cells.push(cell);
@@ -239,11 +243,13 @@ export const provideSelectionContext = ({
.map((cells, rowIdx) =>
cells
.map((cell, colIdx) => {
- return getFormattedValue({
- value: cell,
- rowIndex: rowIdx,
- colIndex: state.value.columns[colIdx],
- });
+ return escapeTSVValue(
+ getFormattedValue({
+ value: cell,
+ rowIndex: rowIdx,
+ colIndex: state.columns[colIdx],
+ })
+ );
})
.join("\t")
)
@@ -255,16 +261,23 @@ export const provideSelectionContext = ({
return "";
};
- const copy = () => {
- if (disabled.value || !isSupported.value) {
- return false;
+ const copy = (state: SelectionState) => {
+ if (disabled.value || !isSupported.value || copying.value) {
+ return;
}
- const values = getValues();
+ const values = getValues(state);
if (!values) {
- return false;
+ return;
}
copying.value = true;
copyTextToClipboard(values)
+ .then(() => {
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("common.copied"),
+ });
+ })
.catch((err: unknown) => {
const errors = [t("common.failed")];
if (err instanceof Error) {
@@ -281,7 +294,17 @@ export const provideSelectionContext = ({
copying.value = false;
});
});
- return true;
+ };
+
+ const copyAll = () => {
+ copy({
+ rows: rows.value.map((_, i) => i),
+ columns: [],
+ });
+ };
+
+ const copySelected = () => {
+ copy(state.value);
};
useEventListener("keydown", (e) => {
@@ -294,16 +317,9 @@ export const provideSelectionContext = ({
}
// Copy when Cmd/Ctrl + C is pressed.
if ((e.key === "c" || e.key === "C") && (e.metaKey || e.ctrlKey)) {
- const copied = copy();
- if (copied) {
- e.preventDefault();
- e.stopImmediatePropagation();
- pushNotification({
- module: "bytebase",
- style: "SUCCESS",
- title: t("common.copied"),
- });
- }
+ e.preventDefault();
+ e.stopImmediatePropagation();
+ copySelected();
}
});
@@ -314,7 +330,8 @@ export const provideSelectionContext = ({
toggleSelectColumn,
toggleSelectCell,
deselect,
- copy,
+ copySelected,
+ copyAll,
};
provide(KEY, context);
return context;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SelectionCopyTooltips.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SelectionCopyTooltips.vue
index e9f8efa968..600b784ccf 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SelectionCopyTooltips.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SelectionCopyTooltips.vue
@@ -25,7 +25,7 @@
</NButton>
</template>
<template #button>
- <NButton size="tiny" @click="copySelection" type="primary" secondary>
+ <NButton size="tiny" @click="copySelected" type="primary" secondary>
<template #icon>
<CopyIcon />
</template>
@@ -45,12 +45,9 @@
import { CopyIcon, InfoIcon } from "lucide-vue-next";
import { NButton } from "naive-ui";
import { computed } from "vue";
-import { useI18n } from "vue-i18n";
-import { pushNotification } from "@/store";
import { useSelectionContext } from "./DataTable/common/selection-logic";
-const { t } = useI18n();
-const { state: selectionState, copy, deselect } = useSelectionContext();
+const { state: selectionState, copySelected, deselect } = useSelectionContext();
const shouldShow = computed(
() =>
@@ -59,16 +56,4 @@ const shouldShow = computed(
);
const isMac = navigator.platform.match(/mac/i);
-
-const copySelection = () => {
- const copied = copy();
- if (!copied) {
- return;
- }
- pushNotification({
- module: "bytebase",
- style: "SUCCESS",
- title: t("common.copied"),
- });
-};
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index 33efc11112..9e3b8c16d5 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -15,7 +15,7 @@
<div
class="result-toolbar relative w-full shrink-0 flex flex-row gap-x-4 justify-between items-center mb-2 hide-scrollbar"
>
- <div class="flex flex-row justify-start items-center mr-2 flex-1">
+ <div class="flex flex-row justify-start items-center gap-x-2 mr-2 flex-1">
<AdvancedSearch
v-model:params="state.searchParams"
placeholder=""
@@ -23,19 +23,15 @@
:cache-query="false"
@keyup:enter="scrollToNextCandidate"
/>
- <span class="ml-2 whitespace-nowrap text-sm text-gray-500">{{
- resultRowsText
- }}</span>
- <span
- v-if="
- currentTab?.mode !== 'ADMIN' &&
- rows.length === editorStore.resultRowsLimit
- "
- class="ml-2 whitespace-nowrap text-sm text-gray-500"
- >
- <span>-</span>
- <span class="ml-2">{{ $t("sql-editor.rows-upper-limit") }}</span>
- </span>
+ <NTooltip :disabled="!reachQueryLimit">
+ <template #trigger>
+ <div class="flex items-center gap-x-1 whitespace-nowrap text-sm text-gray-500">
+ <InfoIcon v-if="reachQueryLimit" class="w-4" />
+ {{ resultRowsText }}
+ </div>
+ </template>
+ {{ $t("sql-editor.rows-upper-limit") }}
+ </NTooltip>
</div>
<div class="flex justify-between items-center shrink-0 gap-x-2">
<div v-if="isESSearchResult" class="flex items-center">
@@ -66,6 +62,17 @@
{{ $t("plugin.ai.text2sql") }}
</template>
</NTooltip>
+ <NButton
+ v-if="!disallowCopyingData && rows.length > 0"
+ size="small"
+ style="--n-padding: 0 8px"
+ @click="copyAll"
+ >
+ <template #icon>
+ <CopyIcon class="w-4 h-4" />
+ </template>
+ {{ $t("common.copy") }}
+ </NButton>
<template v-if="showExport">
<DataExportButton
size="small"
@@ -256,7 +263,13 @@
import { create } from "@bufbuild/protobuf";
import { useLocalStorage } from "@vueuse/core";
import { isEmpty } from "lodash-es";
-import { ArrowDownIcon, ArrowUpIcon, XIcon } from "lucide-vue-next";
+import {
+ ArrowDownIcon,
+ ArrowUpIcon,
+ CopyIcon,
+ InfoIcon,
+ XIcon,
+} from "lucide-vue-next";
import { NButton, NFormItem, NSwitch, NTooltip } from "naive-ui";
import { v4 as uuidv4 } from "uuid";
import { computed, nextTick, reactive, ref, toRef, watch } from "vue";
@@ -364,7 +377,7 @@ const dataTableRef =
const binaryFormatContext = provideBinaryFormatContext();
const detail: SQLResultViewContext["detail"] = ref(undefined);
-const resultViewContext = provideSQLResultViewContext({
+provideSQLResultViewContext({
dark: toRef(props, "dark"),
disallowCopyingData: toRef(props, "disallowCopyingData"),
detail,
@@ -633,6 +646,13 @@ const rows = computed((): ResultTableRow[] => {
});
});
+const reachQueryLimit = computed(() => {
+ return (
+ currentTab.value?.mode !== "ADMIN" &&
+ rows.value.length === editorStore.resultRowsLimit
+ );
+});
+
// Computed property for result rows text to avoid accessing data.length twice
const resultRowsText = computed(() => {
const length = rows.value.length;
@@ -666,11 +686,11 @@ const getMaskingReason = (columnIndex: number) => {
return reason;
};
-provideSelectionContext({
+const { copyAll } = provideSelectionContext({
columns,
rows,
binaryFormatContext,
- resultViewContext,
+ disallowCopyingData: computed(() => props.disallowCopyingData),
});
const showVisualizeButton = computed((): boolean => {
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue
index 179e9ebaa5..493f8a8656 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue
@@ -36,15 +36,17 @@
:key="column.id"
class="flex items-start text-gray-500 dark:text-gray-300 text-sm"
>
- <div class="min-w-28 text-left flex items-start font-medium pt-1">
- {{ column.name }}
- <MaskingReasonPopover
- v-if="getMaskingReason && getMaskingReason(columnIndex)"
- :reason="getMaskingReason(columnIndex)!"
- :statement="statement"
- :database="database.name"
- class="ml-0.5 shrink-0"
- />
+ <div class="min-w-28 text-left flex items-center font-medium pt-1">
+ <div class="flex items-center gap-x-1">
+ {{ column.name }}
+ <MaskingReasonPopover
+ v-if="getMaskingReason && getMaskingReason(columnIndex)"
+ :reason="getMaskingReason(columnIndex)!"
+ :statement="statement"
+ :database="database.name"
+ class="shrink-0"
+ />
+ </div>
:
</div>
<div class="flex-1">