Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: fix copy binary values (#15653)
Vincent Huang609d3a396c60
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vueindex 7cde13ea96..10be542837 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue@@ -150,6 +150,8 @@ import SensitiveDataIcon from "./common/SensitiveDataIcon.vue";import BinaryFormatButton from "./common/BinaryFormatButton.vue";import { useSelectionContext } from "./common/selection-logic";import useTableColumnWidthLogic from "./useTableResize";+import { setColumnFormatOverride, setBinaryFormat, getBinaryFormat } from "./binary-format-store";+import { useConnectionOfCurrentSQLEditorTab } from "@/store";const props = defineProps<{table: Table<QueryRow>;@@ -331,6 +333,16 @@ const setColumnFormat = (columnIndex: number, format: string | null) => {columnFormatOverrides.value.set(columnIndex, format);}+ // Store the format in the binary format store for use during copy+ const { database } = useConnectionOfCurrentSQLEditorTab();+ const databaseName = database.value?.name || '';+ setColumnFormatOverride({+ colIndex: columnIndex,+ format,+ setIndex: props.setIndex,+ databaseName+ });+// Force a re-rendercolumnFormatOverrides.value = new Map(columnFormatOverrides.value);};@@ -338,6 +350,49 @@ const setColumnFormat = (columnIndex: number, format: string | null) => {onMounted(() => {nextTick(() => {tableResize.reset();++ // Store auto-detected formats for all binary columns+ // This ensures the format is available for copy operations+ const { database } = useConnectionOfCurrentSQLEditorTab();+ const databaseName = database.value?.name || '';++ // For each column with binary data, store its server-detected format+ for (let colIndex = 0; colIndex < props.table.getAllColumns().length; colIndex++) {+ if (isColumnWithBinaryData(colIndex)) {+ const serverFormat = getColumnServerFormat(colIndex);+ if (serverFormat) {+ // Important: Don't set column format override during initialization+ // This would override cell-specific formats for all cells in the column+ // We only want to set it when the user explicitly chooses a format+ // setColumnFormatOverride(colIndex, serverFormat, props.setIndex, databaseName);++ // Only set formats for cells that don't already have a format+ const rows = props.table.getPrePaginationRowModel().rows;+ rows.forEach((row, rowIndex) => {+ const cell = row.getVisibleCells()[colIndex];+ if (cell && cell.getValue<RowValue>()?.bytesValue) {+ // Check if this cell already has a format before overriding+ const existingFormat = getBinaryFormat({+ rowIndex,+ colIndex,+ setIndex: props.setIndex,+ databaseName+ });+ if (!existingFormat) {+ // Only set if no format exists yet+ setBinaryFormat({+ rowIndex,+ colIndex,+ format: serverFormat,+ setIndex: props.setIndex,+ databaseName+ });+ }+ }+ });+ }+ }+ }});});diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vueindex 6a84484f24..211d715f93 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue@@ -62,7 +62,8 @@ import { escape } from "lodash-es";import { Code as IconCode } from "lucide-vue-next";import { NButton, NDropdown } from "naive-ui";import { twMerge } from "tailwind-merge";-import { computed, nextTick, ref } from "vue";+import { computed, nextTick, ref, onMounted } from "vue";+import { getBinaryFormat, setBinaryFormat, setColumnType, detectBinaryFormat } from "./binary-format-store";import { useConnectionOfCurrentSQLEditorTab } from "@/store";import { Engine } from "@/types/proto/v1/common";import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";@@ -242,53 +243,21 @@ const formattedBinaryValue = computed(() => {}// Otherwise use auto-detected formatelse {- // Use column type directly from props if available- const columnType = props.columnType?.toLowerCase() || '';-- // Default format based on column type- let defaultFormat = "BINARY";-- // Detect BIT column types - for binary format display (0s and 1s)- const isBitColumn = (- columnType === 'bit' ||- columnType.startsWith('bit(') ||- (columnType.includes('bit') && !columnType.includes('binary')) ||- columnType === 'varbit' ||- columnType === 'bit varying'- );-- // Detect BINARY column types - for hex format display (0x...)- const isBinaryColumn = (- columnType === 'binary' ||- columnType.includes('binary') ||- columnType.startsWith('binary(') ||- columnType.startsWith('varbinary') ||- columnType.includes('blob') ||- columnType === 'bytea'- );-- // Set default format based on column type- if (isBitColumn) {- defaultFormat = "BINARY";- } else if (isBinaryColumn) {- defaultFormat = "HEX";- }-- // Now also consider content-based auto-detection- const byteArray = newValue.bytesValue ? Array.from(newValue.bytesValue) : [];+ // Auto-detect format based on content and column type+ actualFormat = detectBinaryFormat({+ bytesValue: newValue.bytesValue,+ columnType: props.columnType+ });- // For single bit values (could be boolean)- if (byteArray.length === 1 && (byteArray[0] === 0 || byteArray[0] === 1)) {- actualFormat = "BOOLEAN";- }- // Check if it's readable text- else if (byteArray.every(byte => byte >= 32 && byte <= 126)) {- actualFormat = "TEXT";- }- // Default to format based on column type- else {- actualFormat = defaultFormat;- }+ // Store the auto-detected format for use during copy operations+ const databaseName = database.value?.name || '';+ setBinaryFormat({+ rowIndex: props.rowIndex,+ colIndex: props.colIndex,+ format: actualFormat,+ setIndex: props.setIndex,+ databaseName+ });}// Skip formatting for DEFAULT (auto) format@@ -412,12 +381,31 @@ const handleContextMenu = (e: MouseEvent) => {// Handle format selection from dropdownconst handleFormatSelect = (key: string) => {+ // Get database name to scope the format to this database+ const databaseName = database.value?.name || '';+// If DEFAULT is selected, remove the overrideif (key === "DEFAULT") {formatOverride.value = null;+ // Also remove from the global store if it exists+ setBinaryFormat({+ rowIndex: props.rowIndex,+ colIndex: props.colIndex,+ format: "DEFAULT",+ setIndex: props.setIndex,+ databaseName+ });} else {// Otherwise set the overrideformatOverride.value = key;+ // Store the format in the global binary format store+ setBinaryFormat({+ rowIndex: props.rowIndex,+ colIndex: props.colIndex,+ format: key,+ setIndex: props.setIndex,+ databaseName+ });}showFormatMenu.value = false;@@ -425,4 +413,50 @@ const handleFormatSelect = (key: string) => {// Force recomputation of htmlnextTick();};++// On component mount, check for any stored formats and store column type+onMounted(() => {+ // Get database name to scope the format to this database+ const databaseName = database.value?.name || '';++ // Store column type information for later format detection+ if (props.columnType) {+ setColumnType({+ colIndex: props.colIndex,+ columnType: props.columnType,+ setIndex: props.setIndex,+ databaseName+ });+ }++ // Only proceed if we have binary data+ if (hasByteData.value) {+ // Check for a saved format and apply it+ const savedFormat = getBinaryFormat({+ rowIndex: props.rowIndex,+ colIndex: props.colIndex,+ setIndex: props.setIndex,+ databaseName+ });++ if (savedFormat && savedFormat !== "DEFAULT") {+ formatOverride.value = savedFormat;+ } else {+ // If no saved format yet, use our shared detection function+ const detectedFormat = detectBinaryFormat({+ bytesValue: props.value.bytesValue,+ columnType: props.columnType+ });++ // Store this format immediately+ setBinaryFormat({+ rowIndex: props.rowIndex,+ colIndex: props.colIndex,+ format: detectedFormat,+ setIndex: props.setIndex,+ databaseName+ });+ }+ }+});</script>diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/binary-format-store.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/binary-format-store.tsnew file mode 100644index 0000000000..a5583e0edd--- /dev/null+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/binary-format-store.ts@@ -0,0 +1,218 @@+import { ref } from 'vue';++// Store for binary data formatting+// This store maps row/column coordinates to their selected format+type FormatMap = Map<string, string>;+type ColumnTypeMap = Map<string, string>;+type ColumnFormatMap = Map<string, string>;++// Create a global format map using Vue's ref for reactivity+const formattedBinaryValues = ref<FormatMap>(new Map());++// Store column types to help with auto-detection+const columnTypes = ref<ColumnTypeMap>(new Map());++// Store column format overrides+const columnFormatOverrides = ref<ColumnFormatMap>(new Map());++// Parameter interfaces+interface CellKeyParams {+ rowIndex: number;+ colIndex: number;+ setIndex?: number;+ databaseName?: string;+}++interface ColumnKeyParams {+ colIndex: number;+ setIndex?: number;+ databaseName?: string;+}++// Get a unique key for a cell's row/column position+const getCellKey = (params: CellKeyParams): string => {+ const { rowIndex, colIndex, setIndex = 0, databaseName = '' } = params;+ return `${databaseName}:${setIndex}:${rowIndex}:${colIndex}`;+};++// Get a key for a column+const getColumnKey = (params: ColumnKeyParams): string => {+ const { colIndex, setIndex = 0, databaseName = '' } = params;+ return `${databaseName}:${setIndex}:col:${colIndex}`;+};++// Parameter interfaces for the public API functions+export interface BinaryFormatParams {+ rowIndex: number;+ colIndex: number;+ format: string;+ setIndex?: number;+ databaseName?: string;+}++export interface GetBinaryFormatParams {+ rowIndex: number;+ colIndex: number;+ setIndex?: number;+ databaseName?: string;+}++// Store a format for a specific cell+export const setBinaryFormat = (params: BinaryFormatParams): void => {+ const { rowIndex, colIndex, format, setIndex, databaseName } = params;+ const key = getCellKey({ rowIndex, colIndex, setIndex, databaseName });+ formattedBinaryValues.value.set(key, format);+};++// Get the format for a specific cell+export const getBinaryFormat = (params: GetBinaryFormatParams): string | undefined => {+ const { rowIndex, colIndex, setIndex, databaseName } = params;+ const key = getCellKey({ rowIndex, colIndex, setIndex, databaseName });+ return formattedBinaryValues.value.get(key);+};++// Column type parameters+export interface ColumnTypeParams {+ colIndex: number;+ columnType: string;+ setIndex?: number;+ databaseName?: string;+}++export interface GetColumnTypeParams {+ colIndex: number;+ setIndex?: number;+ databaseName?: string;+}++// Column format parameters+export interface ColumnFormatParams {+ colIndex: number;+ format: string | null;+ setIndex?: number;+ databaseName?: string;+}++export interface GetColumnFormatParams {+ colIndex: number;+ setIndex?: number;+ databaseName?: string;+}++// Store column type information+export const setColumnType = (params: ColumnTypeParams): void => {+ const { colIndex, columnType, setIndex, databaseName } = params;+ const key = getColumnKey({ colIndex, setIndex, databaseName });+ columnTypes.value.set(key, columnType.toLowerCase());+};++// Get column type information+export const getColumnType = (params: GetColumnTypeParams): string | undefined => {+ const { colIndex, setIndex, databaseName } = params;+ const key = getColumnKey({ colIndex, setIndex, databaseName });+ return columnTypes.value.get(key);+};++// Store a column format override+export const setColumnFormatOverride = (params: ColumnFormatParams): void => {+ const { colIndex, format, setIndex, databaseName } = params;+ const key = getColumnKey({ colIndex, setIndex, databaseName });+ if (format === null) {+ columnFormatOverrides.value.delete(key);+ } else {+ columnFormatOverrides.value.set(key, format);+ }+};++// Get a column format override+export const getColumnFormatOverride = (params: GetColumnFormatParams): string | undefined => {+ const { colIndex, setIndex, databaseName } = params;+ const key = getColumnKey({ colIndex, setIndex, databaseName });+ return columnFormatOverrides.value.get(key);+};++export interface DetectBinaryFormatParams {+ bytesValue: Uint8Array | undefined;+ columnType?: string;+}++// Detect the best format for binary data based on content+export const detectBinaryFormat = (params: DetectBinaryFormatParams): string => {+ const { bytesValue, columnType = '' } = params;+ if (!bytesValue || bytesValue.length === 0) {+ return 'HEX';+ }++ const byteArray = Array.from(bytesValue);++ // For single bit values (could be boolean)+ if (byteArray.length === 1 && (byteArray[0] === 0 || byteArray[0] === 1)) {+ return "BOOLEAN";+ }++ // Check if it's readable text+ const isReadableText = byteArray.every(byte => byte >= 32 && byte <= 126);+ if (isReadableText) {+ return "TEXT";+ }++ // Default format based on column type+ const lowerColumnType = columnType.toLowerCase();++ // Detect BIT column types - for binary format display (0s and 1s)+ const isBitColumn = (+ lowerColumnType === 'bit' ||+ lowerColumnType.startsWith('bit(') ||+ (lowerColumnType.includes('bit') && !lowerColumnType.includes('binary')) ||+ lowerColumnType === 'varbit' ||+ lowerColumnType === 'bit varying'+ );++ // BIT columns default to binary format+ if (isBitColumn) {+ return "BINARY";+ }++ // All other binary types default to HEX+ return "HEX";+};++export interface FormatBinaryValueParams {+ bytesValue: Uint8Array | undefined;+ format: string;+}++export const formatBinaryValue = (params: FormatBinaryValueParams): string => {+ const { bytesValue, format } = params;+ if (!bytesValue || bytesValue.length === 0) {+ return '';+ }++ const byteArray = Array.from(bytesValue);++ switch (format) {+ case "BINARY":+ return byteArray+ .map(byte => byte.toString(2).padStart(8, "0"))+ .join("");+ case "TEXT":+ try {+ return new TextDecoder().decode(new Uint8Array(byteArray));+ } catch {+ // Fallback to HEX if text decoding fails+ return "0x" + byteArray+ .map(byte => byte.toString(16).toUpperCase().padStart(2, "0"))+ .join("");+ }+ case "BOOLEAN":+ if (byteArray.length === 1 && (byteArray[0] === 0 || byteArray[0] === 1)) {+ return byteArray[0] === 1 ? "true" : "false";+ }+ // Fall through to HEX for non-boolean data+ case "HEX":+ default:+ return "0x" + byteArray+ .map(byte => byte.toString(16).toUpperCase().padStart(2, "0"))+ .join("");+ }+};\ No newline at end of filediff --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.tsindex 65511c8512..584e503820 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,10 @@ import {type Ref,} from "vue";import { useI18n } from "vue-i18n";-import { pushNotification } from "@/store";+import { pushNotification, useConnectionOfCurrentSQLEditorTab } from "@/store";import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";import { extractSQLRowValuePlain, isDescendantOf, toClipboard } from "@/utils";+import { getBinaryFormat, formatBinaryValue, getColumnFormatOverride, detectBinaryFormat } from "../binary-format-store";import { useSQLResultViewContext } from "../../context";export const PREVENT_DISMISS_SELECTION = "bb-prevent-dismiss-selection";@@ -124,6 +125,10 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {});const getValues = () => {+ // Get the current database name for scoping the binary format+ const { database } = useConnectionOfCurrentSQLEditorTab();+ const databaseName = database.value?.name || '';+if (state.value.rows.length === 1 && state.value.columns.length === 1) {const row =table.value.getPrePaginationRowModel().rows[state.value.rows[0]];@@ -134,7 +139,59 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {if (!cell) {return "";}- return String(extractSQLRowValuePlain(cell.getValue() as RowValue));++ // Get the cell value+ const value = cell.getValue() as RowValue;++ // Special handling for binary data+ if (value && value.bytesValue) {+ // Get the result set index if available+ const setIndex = (resultViewContext.detail?.value?.set) ?? 0;++ // First check if there's a column format override+ const columnFormatOverride = getColumnFormatOverride({+ colIndex: state.value.columns[0],+ setIndex,+ databaseName+ });+ if (columnFormatOverride) {+ // Column format overrides take precedence+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: columnFormatOverride+ });+ }++ // Then check for cell-specific format+ const cellFormat = getBinaryFormat({+ rowIndex: state.value.rows[0],+ colIndex: state.value.columns[0],+ setIndex,+ databaseName+ });+ if (cellFormat && cellFormat !== "DEFAULT") {+ // Use the stored format to format the binary data+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: cellFormat+ });+ }++ // If no stored format is found, detect based on content and column type+ // This ensures copy uses the same auto-detection logic as display+ const columnType = resultViewContext.columnTypeNames?.value?.[state.value.columns[0]] || '';+ const detectedFormat = detectBinaryFormat({+ bytesValue: value.bytesValue,+ columnType+ });+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: detectedFormat+ });+ }++ // Fall back to default formatting+ return String(extractSQLRowValuePlain(value));} else if (state.value.rows.length > 0) {const rows: Row<QueryRow>[] = [];for (const row of state.value.rows) {@@ -148,12 +205,61 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {return "";}return rows- .map((row) => {+ .map((row, rowIdx) => {const cells = row.getVisibleCells();return cells- .map((cell) =>- String(extractSQLRowValuePlain(cell.getValue() as RowValue))- )+ .map((cell, colIdx) => {+ const value = cell.getValue() as RowValue;++ // Special handling for binary data+ if (value && value.bytesValue) {+ // Get the result set index if available+ const setIndex = (resultViewContext.detail?.value?.set) ?? 0;++ // First check if there's a column format override+ const columnFormatOverride = getColumnFormatOverride({+ colIndex: colIdx,+ setIndex,+ databaseName+ });+ if (columnFormatOverride) {+ // Column format overrides take precedence+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: columnFormatOverride+ });+ }++ // Then check for cell-specific format+ const cellFormat = getBinaryFormat({+ rowIndex: state.value.rows[rowIdx],+ colIndex: colIdx,+ setIndex,+ databaseName+ });+ if (cellFormat && cellFormat !== "DEFAULT") {+ // Use the stored format to format the binary data+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: cellFormat+ });+ }++ // If no stored format is found, detect based on content and column type+ const columnType = resultViewContext.columnTypeNames?.value?.[colIdx] || '';+ const detectedFormat = detectBinaryFormat({+ bytesValue: value.bytesValue,+ columnType+ });+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: detectedFormat+ });+ }++ // Fall back to default formatting+ return String(extractSQLRowValuePlain(value));+ }).join("\t");}).join("\n");@@ -173,11 +279,60 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {return "";}return values- .map((cells) =>+ .map((cells, rowIdx) =>cells- .map((cell) =>- String(extractSQLRowValuePlain(cell.getValue() as RowValue))- )+ .map((cell, colIdx) => {+ const value = cell.getValue() as RowValue;++ // Special handling for binary data+ if (value && value.bytesValue) {+ // Get the result set index if available+ const setIndex = (resultViewContext.detail?.value?.set) ?? 0;++ // First check if there's a column format override+ const columnFormatOverride = getColumnFormatOverride({+ colIndex: state.value.columns[colIdx],+ setIndex,+ databaseName+ });+ if (columnFormatOverride) {+ // Column format overrides take precedence+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: columnFormatOverride+ });+ }++ // Then check for cell-specific format+ const cellFormat = getBinaryFormat({+ rowIndex: rowIdx,+ colIndex: state.value.columns[colIdx],+ setIndex,+ databaseName+ });+ if (cellFormat && cellFormat !== "DEFAULT") {+ // Use the stored format to format the binary data+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: cellFormat+ });+ }++ // If no stored format is found, detect based on content and column type+ const columnType = resultViewContext.columnTypeNames?.value?.[state.value.columns[colIdx]] || '';+ const detectedFormat = detectBinaryFormat({+ bytesValue: value.bytesValue,+ columnType+ });+ return formatBinaryValue({+ bytesValue: value.bytesValue,+ format: detectedFormat+ });+ }++ // Fall back to default formatting+ return String(extractSQLRowValuePlain(value));+ }).join("\t")).join("\n");@@ -253,4 +408,4 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {export const useSelectionContext = () => {return inject(KEY)!;-};+};\ No newline at end of file