Skip to content

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

feat(sql-editor): Keyword highlighting in query result table (#5746)

Liu Ji8639bcc94a16
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 447061b0cb..e2f567b7a6 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -3,7 +3,7 @@ import dayOfYear from "dayjs/plugin/dayOfYear";
import duration from "dayjs/plugin/duration";
import relativeTime from "dayjs/plugin/relativeTime";
import utc from "dayjs/plugin/utc";
-import { round } from "lodash-es";
+import { escapeRegExp, round } from "lodash-es";
import semver from "semver";
dayjs.extend(dayOfYear);
@@ -176,6 +176,16 @@ export function getHighlightHTMLByKeyWords(s: string, k: string) {
return s.replaceAll(k, `<b class="text-accent">${k}</b>`);
}
+export function getHighlightHTMLByRegExp(
+ s: string,
+ pattern: string,
+ caseSensitive = false
+) {
+ const flags = caseSensitive ? "g" : "gi";
+ const re = new RegExp(escapeRegExp(pattern), flags);
+ return s.replaceAll(re, (k) => `<b class="text-accent">${k}</b>`);
+}
+
export type Defer<T> = {
promise: Promise<T>;
resolve: (value: T | PromiseLike<T>) => void;
diff --git a/frontend/src/views/sql-editor/EditorCommon/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/DataTable.vue
deleted file mode 100644
index d787e355f5..0000000000
--- a/frontend/src/views/sql-editor/EditorCommon/DataTable.vue
+++ /dev/null
@@ -1,138 +0,0 @@
-<template>
- <div
- class="bb-data-table relative w-full flex-1 overflow-hidden flex flex-col"
- >
- <div class="w-full flex-1 flex flex-col overflow-hidden">
- <div
- class="header-track absolute z-0 left-0 top-0 right-0 h-[34px] border border-block-border bg-gray-50 dark:bg-gray-700"
- />
-
- <div
- ref="scrollerRef"
- class="inner-wrapper max-h-full w-full overflow-auto border-y border-r border-block-border fix-scrollbar-z-index"
- :class="data.length === 0 && 'border-b-0 border-r-0'"
- >
- <table
- ref="tableRef"
- class="relative border-collapse table-fixed z-[1]"
- v-bind="tableResize.getTableProps()"
- >
- <thead class="sticky top-0 z-[1] shadow">
- <tr>
- <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"
- v-bind="tableResize.getColumnProps(header.index)"
- >
- <div class="flex items-center overflow-hidden">
- <span> {{ header.column.columnDef.header }}</span>
- <SensitiveDataIcon
- v-if="isSensitiveColumn(header.index)"
- class="ml-0.5 shrink-0"
- />
- </div>
-
- <!-- The drag-to-resize handler -->
- <div
- class="absolute w-[8px] right-0 top-0 bottom-0 cursor-col-resize"
- @dblclick="tableResize.autoAdjustColumnWidth([header.index])"
- @pointerdown="tableResize.startResizing(header.index)"
- />
- </th>
- </tr>
- </thead>
- <tbody>
- <tr
- v-for="(row, rowIndex) of table.getRowModel().rows"
- :key="rowIndex"
- class="group"
- >
- <td
- v-for="(cell, cellIndex) of row.getVisibleCells()"
- :key="cellIndex"
- class="px-2 py-1 text-sm dark:text-gray-100 leading-5 whitespace-pre-wrap break-all border border-block-border group-last:border-b-0 group-even:bg-gray-50/50 dark:group-even:bg-gray-700/50"
- >
- <template v-if="cell.getValue()">{{
- cell.getValue()
- }}</template>
- <br v-else class="min-h-[1rem] inline-flex" />
- </td>
- </tr>
- </tbody>
- </table>
- <div
- v-if="data.length === 0"
- class="text-center w-full my-12 textinfolabel"
- >
- {{ $t("sql-editor.no-rows-found") }}
- </div>
- </div>
- </div>
- </div>
-</template>
-
-<script lang="ts" setup>
-import { computed, nextTick, PropType, ref, watch } from "vue";
-import { ColumnDef, Table } from "@tanstack/vue-table";
-import useTableColumnWidthLogic from "./useTableResize";
-import { SensitiveDataIcon } from "./DataTable";
-
-export type DataTableColumn = {
- key: string;
- title: string;
-};
-
-const props = defineProps({
- data: {
- type: Array as PropType<string[][]>,
- default: () => [],
- },
- columns: {
- type: Array as PropType<ColumnDef<string[]>[]>,
- default: () => [],
- },
- sensitive: {
- type: Array as PropType<boolean[]>,
- default: () => [],
- },
- table: {
- type: Object as PropType<Table<string[]>>,
- required: true,
- },
-});
-
-const scrollerRef = ref<HTMLDivElement>();
-const tableRef = ref<HTMLTableElement>();
-
-const tableResize = useTableColumnWidthLogic({
- tableRef,
- scrollerRef,
- minWidth: 64, // 4rem
- maxWidth: 640, // 40rem
-});
-
-const data = computed(() => props.data);
-
-const isSensitiveColumn = (index: number): boolean => {
- return props.sensitive[index] ?? false;
-};
-
-const scrollTo = (x: number, y: number) => {
- scrollerRef.value?.scroll(x, y);
-};
-
-watch(
- () => props.columns.map((col) => col.header).join("|"),
- () => {
- nextTick(() => {
- // Re-calculate the column widths once the column definition changed.
- scrollTo(0, 0);
- tableResize.reset();
- });
- },
- { immediate: true }
-);
-
-defineExpose({ scrollTo });
-</script>
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 b5f7f57caa..33baa1f01b 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
@@ -56,16 +56,13 @@
:key="rowIndex"
class="group"
>
+ <!-- eslint-disable vue/no-v-html -->
<td
v-for="(cell, cellIndex) of row.getVisibleCells()"
:key="cellIndex"
class="px-2 py-1 text-sm dark:text-gray-100 leading-5 whitespace-pre-wrap break-all border border-block-border group-last:border-b-0 group-even:bg-gray-50/50 dark:group-even:bg-gray-700/50"
- >
- <template v-if="String(cell.getValue()).length > 0">
- {{ cell.getValue() }}
- </template>
- <br v-else class="min-h-[1rem] inline-flex" />
- </td>
+ v-html="renderCellValue(cell.getValue())"
+ ></td>
</tr>
</tbody>
</table>
@@ -83,8 +80,11 @@
<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 { getHighlightHTMLByRegExp } from "@/utils";
export type DataTableColumn = {
key: string;
@@ -108,6 +108,10 @@ const props = defineProps({
type: Object as PropType<Table<string[]>>,
required: true,
},
+ keyword: {
+ type: String,
+ default: "",
+ },
});
const scrollerRef = ref<HTMLDivElement>();
@@ -126,6 +130,24 @@ const isSensitiveColumn = (index: number): boolean => {
return props.sensitive[index] ?? false;
};
+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/SingleResultView.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultView.vue
index 37ed526cab..e25a801c52 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultView.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultView.vue
@@ -68,6 +68,7 @@
:columns="columns"
:data="data"
:sensitive="sensitive"
+ :keyword="state.search"
/>
</div>
</template>
@@ -182,11 +183,11 @@ const data = computed(() => {
}
const data = props.result.data[2];
- const search = keyword.value;
+ const search = keyword.value.trim().toLowerCase();
let temp = data;
if (search) {
temp = data.filter((item) => {
- return item.some((col) => String(col).includes(search));
+ return item.some((col) => String(col).toLowerCase().includes(search));
});
}
return temp;
@@ -274,7 +275,7 @@ const visualizeExplain = () => {
const statement = executeParams.query || "";
if (!statement) return;
- const lines: string[][] = queryResult.data[2];
+ const lines: string[][] = queryResult.resultList[0].data[2];
const explain = lines.map((line) => line[0]).join("\n");
if (!explain) return;