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: update extract row value (#14933)
boojacka68f5178a329
diff --git a/frontend/src/utils/v1/sql.ts b/frontend/src/utils/v1/sql.tsindex ba790441be..f2d1da9dc7 100644--- a/frontend/src/utils/v1/sql.ts+++ b/frontend/src/utils/v1/sql.ts@@ -2,98 +2,68 @@ import dayjs from "dayjs";import Long from "long";import { getDateForPbTimestamp } from "@/types";import { NullValue } from "@/types/proto/google/protobuf/struct";+import type { Timestamp } from "@/types/proto/google/protobuf/timestamp";import { Engine } from "@/types/proto/v1/common";import { RowValue } from "@/types/proto/v1/sql_service";import { isNullOrUndefined } from "../util";-export const extractSQLRowValue = (- value: RowValue | undefined-): { plain: any; raw: any } => {- const fallback = (v: any) => {- return { plain: v, raw: v };- };- if (typeof value === "undefined") {- return fallback(null);- }- if (value.nullValue === NullValue.NULL_VALUE) {- return fallback(null);+// extractSQLRowValuePlain extracts a plain value from a RowValue.+export const extractSQLRowValuePlain = (value: RowValue | undefined) => {+ if (+ typeof value === "undefined" ||+ value.nullValue === NullValue.NULL_VALUE+ ) {+ return null;}const plainObject = RowValue.toJSON(value) as Record<string, any>;const keys = Object.keys(plainObject);if (keys.length === 0) {- return fallback(undefined); // Will bi displayed as "UNSET"+ return undefined; // Will bi displayed as "UNSET"}if (keys.length > 1) {console.debug("mixed type in row value", value);}if (value.bytesValue) {- // convert byte arrays to binary 10101001 strings- const byteArray = value.bytesValue;- const parts: string[] = [];- for (let i = 0; i < byteArray.length; i++) {- const byte = byteArray[i];- const part = byte.toString(2).padStart(8, "0");- parts.push(part);- }- const binaryString = parts.join("").replace(/^0+/g, "");- if (binaryString.length === 0) {- return {- plain: "0",- raw: byteArray,- };- }- return {- plain: binaryString,- raw: byteArray,- };+ const byteArray = Array.from(value.bytesValue);+ const binaryString = byteArray+ .map((byte) => byte.toString(2).padStart(8, "0"))+ .join("")+ .replace(/^0+/g, "");+ return binaryString.length === 0 ? "0" : binaryString;}if (value.timestampValue) {- // Timezone-less timestamps are converted using UTC but they are not UTC timestamps.- const fullDayjs = dayjs(getDateForPbTimestamp(value.timestampValue)).utc();- const microseconds = Math.floor(value.timestampValue.nanos / 1000);- // Format the timestamp into a human-readable string, including microseconds if present- const formattedTimestamp =- microseconds > 0- ? // If there are microseconds, append them to the formatted string with 6 digits.- // Example: 2021-01-01 00:00:00.123456- `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}`- : // Otherwise, just format the date and time without microseconds- // Example: 2021-01-01 00:00:00- `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}`;-- return {- plain: formattedTimestamp,- raw: value.timestampValue,- };+ return formatTimestamp(value.timestampValue);}if (value.timestampTzValue && value.timestampTzValue.timestamp) {- const fullDayjs = dayjs(getDateForPbTimestamp(value.timestampTzValue.timestamp));- const microseconds = Math.floor(value.timestampTzValue.timestamp.nanos / 1000);- let timezoneOffset = fullDayjs.format("Z");- if (timezoneOffset.endsWith(":00")) {- timezoneOffset = timezoneOffset.slice(0, -3);- }- // Format the timestamp into a human-readable string, including microseconds if present- const formattedTimestamp =- microseconds > 0- ? // If there are microseconds, append them to the formatted string with 6 digits.- // Example: 2021-01-01 00:00:00.123456-07- `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}${timezoneOffset}`- : // Otherwise, just format the date and time without microseconds- // Example: 2021-01-01 00:00:00-07- `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}${timezoneOffset}`;-- return {- plain: formattedTimestamp,- raw: value.timestampTzValue,- };+ return formatTimestampWithTz(value.timestampTzValue.timestamp);}const key = keys[0];- return {- plain: plainObject[key],- raw: (value as any)[key],- };+ return plainObject[key];+};++const formatTimestamp = (timestamp: Timestamp) => {+ const fullDayjs = dayjs(getDateForPbTimestamp(timestamp)).utc();+ const microseconds = Math.floor(timestamp.nanos / 1000);+ const formattedTimestamp =+ microseconds > 0+ ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}`+ : `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}`;+ return formattedTimestamp;+};++const formatTimestampWithTz = (timestamp: Timestamp) => {+ const fullDayjs = dayjs(getDateForPbTimestamp(timestamp));+ const microseconds = Math.floor(timestamp.nanos / 1000);+ let timezoneOffset = fullDayjs.format("Z");+ if (timezoneOffset.endsWith(":00")) {+ timezoneOffset = timezoneOffset.slice(0, -3);+ }+ const formattedTimestamp =+ microseconds > 0+ ? `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}.${microseconds.toString().padStart(6, "0")}${timezoneOffset}`+ : `${fullDayjs.format("YYYY-MM-DD HH:mm:ss")}${timezoneOffset}`;+ return formattedTimestamp;};export const wrapSQLIdentifier = (id: string, engine: Engine) => {@@ -216,13 +186,16 @@ export const compareQueryRowValues = (a: RowValue,b: RowValue): number => {- const { plain: valueA, raw: rawA } = extractSQLRowValue(a);- const { plain: valueB, raw: rawB } = extractSQLRowValue(b);+ const valueA = extractSQLRowValuePlain(a);+ const valueB = extractSQLRowValuePlain(b);// NULL or undefined values go behindif (isNullOrUndefined(valueA)) return 1;if (isNullOrUndefined(valueB)) return -1;+ // Check if the values are Longs and compare them.+ const rawA = extractSQLRowValueRaw(a);+ const rawB = extractSQLRowValueRaw(b);if (Long.isLong(rawA) && Long.isLong(rawB)) {return rawA.compare(rawB);}@@ -251,6 +224,21 @@ export const compareQueryRowValues = (return stringA < stringB ? -1 : stringA > stringB ? 1 : 0;};+// extractSQLRowValueRaw extracts a raw value from a RowValue.+const extractSQLRowValueRaw = (value: RowValue | undefined) => {+ if (+ typeof value === "undefined" ||+ value.nullValue === NullValue.NULL_VALUE+ ) {+ return null;+ }+ const keys = Object.keys(RowValue.toJSON(value) as Record<string, any>);+ if (keys.length === 0) {+ return undefined;+ }+ return (value as any)[keys[0]];+};+const toInt = (a: any) => {return typeof a === "number"? adiff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vueindex fb852ca0bd..c2917d1314 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue@@ -74,7 +74,9 @@<ColumnSortedIcon:is-sorted="header.column.getIsSorted()"- @click="header.column.getToggleSortingHandler()?.($event)"+ @click.stop.prevent="+ header.column.getToggleSortingHandler()?.($event)+ "/></div>diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite.vueindex a0b7eebe1d..67251c4ea9 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTableLite.vue@@ -53,7 +53,9 @@<ColumnSortedIcon:is-sorted="header.column.getIsSorted()"- @click="header.column.getToggleSortingHandler()?.($event)"+ @click.stop.prevent="+ header.column.getToggleSortingHandler()?.($event)+ "/></div></th>diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vueindex 5208b6176a..e2bd9305a3 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue@@ -35,7 +35,7 @@ import { computed, ref } from "vue";import { useConnectionOfCurrentSQLEditorTab } from "@/store";import { Engine } from "@/types/proto/v1/common";import type { QueryRow, RowValue } from "@/types/proto/v1/sql_service";-import { extractSQLRowValue, getHighlightHTMLByRegExp } from "@/utils";+import { extractSQLRowValuePlain, getHighlightHTMLByRegExp } from "@/utils";import { useSQLResultViewContext } from "../context";import { useSelectionContext } from "./common/selection-logic";@@ -108,7 +108,7 @@ const classes = computed(() => {});const html = computed(() => {- const value = extractSQLRowValue(props.value).plain;+ const value = extractSQLRowValuePlain(props.value);if (value === undefined) {return `<span class="text-gray-400 italic">UNSET</span>`;}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.tsindex 484c69a124..1c62dc32eb 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@@ -14,7 +14,7 @@ import {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 { extractSQLRowValuePlain, isDescendantOf, toClipboard } from "@/utils";import { useSQLResultViewContext } from "../../context";export const PREVENT_DISMISS_SELECTION = "bb-prevent-dismiss-selection";@@ -109,7 +109,7 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {const cells = row.getVisibleCells();return cells.map((cell) =>- String(extractSQLRowValue(cell.getValue() as RowValue).plain)+ String(extractSQLRowValuePlain(cell.getValue() as RowValue))).join("\t");})@@ -133,7 +133,7 @@ export const provideSelectionContext = (table: Ref<Table<QueryRow>>) => {.map((cells) =>cells.map((cell) =>- String(extractSQLRowValue(cell.getValue() as RowValue).plain)+ String(extractSQLRowValuePlain(cell.getValue() as RowValue))).join("\t"))diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel/DetailPanel.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel/DetailPanel.vueindex 7728e1eeef..b411d0eb6b 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel/DetailPanel.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DetailPanel/DetailPanel.vue@@ -139,7 +139,7 @@ import { useI18n } from "vue-i18n";import { DrawerContent } from "@/components/v2";import { pushNotification } from "@/store";import type { RowValue } from "@/types/proto/v1/sql_service";-import { extractSQLRowValue } from "@/utils";+import { extractSQLRowValuePlain } from "@/utils";import { useSQLResultViewContext } from "../context";import PrettyJSON from "./PrettyJSON.vue";@@ -164,7 +164,7 @@ const content = computed(() => {.rows[row]?.getVisibleCells()[col]?.getValue<RowValue>();- return String(extractSQLRowValue(value).plain);+ return String(extractSQLRowValuePlain(value));});const guessedIsJSON = computed(() => {diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vueindex bea4af470a..bc7e81a320 100644--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue@@ -164,7 +164,7 @@class="text-md font-normal flex items-center gap-x-1":class="[dark ? 'text-matrix-green-hover' : 'text-control-light']">- <span>{{ extractSQLRowValue(result.rows[0].values[0]).plain }}</span>+ <span>{{ extractSQLRowValuePlain(result.rows[0].values[0]) }}</span><span>rows affected</span></div></template>@@ -241,7 +241,7 @@ import {compareQueryRowValues,createExplainToken,extractProjectResourceName,- extractSQLRowValue,+ extractSQLRowValuePlain,generateIssueTitle,hasPermissionToCreateRequestGrantIssue,hasWorkspacePermissionV2,@@ -400,7 +400,7 @@ const data = computed(() => {if (search) {temp = data.filter((item) => {return item.values.some((col) => {- const value = extractSQLRowValue(col).plain;+ const value = extractSQLRowValuePlain(col);if (isNullOrUndefined(value)) {return false;}@@ -415,12 +415,9 @@ const useDataTableLite = computed(() => {// In admin mode, always use DataTableLite to keep consistentif (currentTab.value?.mode === "ADMIN") return true;- // Otherwise, use DataTableLite if the result set has too many columns- // or too many rows in a page.+ // Otherwise, use DataTableLite if the result set has too many columns in a page.const colCount = table.getFlatHeaders().length;- const rowCount = Math.min(pageSize.value, props.result.rows.length);-- return colCount >= 50 || rowCount >= 200;+ return colCount >= 50;});const isSensitiveColumn = (columnIndex: number): boolean => {@@ -463,7 +460,7 @@ const pageSize = computed({},});const pageSizeOptions = computed(() => {- return [20, 50, 100, 1000].map<SelectOption>((n) => ({+ return [20, 50, 100, 200].map<SelectOption>((n) => ({label: t("sql-editor.n-per-page", { n }),value: n,}));@@ -572,7 +569,7 @@ const handleChangePage = (page: number) => {const explainFromSQLResultSetV1 = (resultSet: SQLResultSetV1 | undefined) => {if (!resultSet) return "";const lines = resultSet.results[0].rows.map((row) =>- row.values.map((value) => String(extractSQLRowValue(value).plain))+ row.values.map((value) => String(extractSQLRowValuePlain(value))));const explain = lines.map((line) => line[0]).join("\n");return explain;diff --git a/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue b/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vueindex 81007ea3e0..00c3b23c77 100644--- a/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue+++ b/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue@@ -64,7 +64,7 @@<BBSpin /><divv-if="query === currentQuery && expired"- class="text-gray-400 cursor-pointer hover:underline select-none"+ class="text-gray-400 cursor-pointer hover:underline text-sm select-none"@click="handleCancelQuery">{{ $t("common.cancel") }}