Skip to content

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

feat: search query result table (#366)

STEVEN97ebcf1d1d35
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index db42353a9f..c467873607 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -31,10 +31,12 @@ const useExecuteSQL = () => {
}
try {
isLoadingData.value = true;
+ store.dispatch("sqlEditor/setIsExecuting", true);
const res = await store.dispatch("sqlEditor/executeQuery", {
statement: sqlStatement,
});
isLoadingData.value = false;
+ store.dispatch("sqlEditor/setIsExecuting", false);
if (res.error) {
store.dispatch("notification/pushNotification", {
@@ -46,6 +48,7 @@ const useExecuteSQL = () => {
}
} catch (error) {
isLoadingData.value = false;
+ store.dispatch("sqlEditor/setIsExecuting", false);
store.dispatch("notification/pushNotification", {
module: "bytebase",
style: "CRITICAL",
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 77c79ca4ba..32cc7a8f8e 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -156,6 +156,7 @@ common:
ok: OK
share: Share
or: or
+ export: Export
error-page:
go-back-home: Go back home
anomaly-center: Anomaly Center
@@ -1032,10 +1033,14 @@ sql-editor:
self: SQL Editor
select-connection: Select Connections
search-databases: Search Databases
+ search-results: Search Results
loading-databases: Loading Databases...
close-pane: Close Pane
loading-data: Loading Data...
table-empty-placehoder: Click Run to execute the query.
+ no-rows-found: No rows found
+ download-as-csv: Download as CSV
+ download-as-json: Download as JSON
label:
error:
key-necessary: Key is required
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index a0b8ff5d79..a61e1c23a3 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -156,6 +156,7 @@ common:
ok: OK
share: 分享
or: 或者
+ export: 导出
error-page:
go-back-home: 回到主页
anomaly-center: 异常中心
@@ -901,10 +902,14 @@ sql-editor:
self: SQL 编辑器
select-connection: 请选择数据连接
search-databases: 搜索数据库
+ search-results: 搜索结果
loading-databases: 加载数据库信息...
close-pane: 关闭面板
loading-data: 加载数据中...
table-empty-placehoder: 点击 ”运行“ 执行查询
+ no-rows-found: 无数据
+ download-as-csv: 下载为 CSV 格式
+ download-as-json: 下载为 JSON 格式
label:
error:
key-necessary: 需要指定 Key
diff --git a/frontend/src/plugins/naive-ui.ts b/frontend/src/plugins/naive-ui.ts
index 7c335aa170..87f1d6226e 100644
--- a/frontend/src/plugins/naive-ui.ts
+++ b/frontend/src/plugins/naive-ui.ts
@@ -5,6 +5,7 @@ import {
NButtonGroup,
NCascader,
NDataTable,
+ NDropdown,
NInput,
NMessageProvider,
NModal,
@@ -26,6 +27,7 @@ const naive = create({
NButtonGroup,
NCascader,
NDataTable,
+ NDropdown,
NInput,
NMessageProvider,
NModal,
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 8b7948b144..b6c3df8d9e 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -28,7 +28,8 @@ const state: () => SqlEditorState = () => ({
},
queryStatement: "",
selectedStatement: "",
- queryResult: [],
+ isExecuting: false,
+ queryResult: null,
});
const getters = {
@@ -130,6 +131,9 @@ const mutations = {
) {
Object.assign(state.connectionContext, payload);
},
+ [types.SET_IS_EXECUTING](state: SqlEditorState, payload: boolean) {
+ state.isExecuting = payload;
+ },
};
type SqlEditorActionsMap = {
@@ -137,6 +141,7 @@ type SqlEditorActionsMap = {
setConnectionTree: typeof mutations.SET_CONNECTION_TREE;
setQueryResult: typeof mutations.SET_QUERY_RESULT;
setConnectionContext: typeof mutations.SET_CONNECTION_CONTEXT;
+ setIsExecuting: typeof mutations.SET_IS_EXECUTING;
};
const actions = {
@@ -145,6 +150,7 @@ const actions = {
setConnectionTree: types.SET_CONNECTION_TREE,
setQueryResult: types.SET_QUERY_RESULT,
setConnectionContext: types.SET_CONNECTION_CONTEXT,
+ setIsExecuting: types.SET_IS_EXECUTING,
}),
async executeQuery(
{ commit, dispatch, state }: any,
@@ -180,7 +186,7 @@ const actions = {
{ root: true }
);
commit(types.SET_SQL_EDITOR_STATE, {
- queryResult: [],
+ queryResult: null,
});
commit(types.SET_CONNECTION_CONTEXT, {
hasSlug: true,
diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.ts
index ef932d36ec..ada47e2cd0 100644
--- a/frontend/src/store/mutation-types.ts
+++ b/frontend/src/store/mutation-types.ts
@@ -3,6 +3,7 @@ export const SET_SQL_EDITOR_STATE = "SET_SQL_EDITOR_STATE";
export const SET_CONNECTION_TREE = "SET_CONNECTION_TREE";
export const SET_QUERY_RESULT = "SET_QUERY_RESULT";
export const SET_CONNECTION_CONTEXT = "SET_CONNECTION_CONTEXT";
+export const SET_IS_EXECUTING = "SET_IS_EXECUTING";
// Editor Selector mutation types
export const SET_EDITOR_SELECTOR_STATE = "SET_EDITOR_SELECTOR_STATE";
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 6e52d49dca..7bdd4079c7 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -41,7 +41,7 @@ import { Table } from "./table";
import { VCS } from "./vcs";
import { Label } from "./label";
import { ConnectionAtom, ConnectionContext } from "./sqlEditor";
-import { TabInfo, AnyTabInfo } from "./editorSelector";
+import { TabInfo } from "./editorSelector";
import instanceStore from "../store/modules/instance";
import sqlEditorStore from "../store/modules/sqlEditor";
import editorSelectorStore from "../store/modules/editorSelector";
@@ -192,7 +192,8 @@ export interface SqlEditorState {
connectionContext: ConnectionContext;
queryStatement: string;
selectedStatement: string;
- queryResult: Array<Record<any, any>>;
+ isExecuting: boolean;
+ queryResult: Record<string, any>[] | null;
}
export type SqlEditorGetters = typeof sqlEditorStore.getters;
export type SqlEditorActions = typeof sqlEditorStore.actions;
diff --git a/frontend/src/views/SqlEditor/TablePanel/TableView.vue b/frontend/src/views/SqlEditor/TablePanel/TableView.vue
index ac1a002ea9..40750282ce 100644
--- a/frontend/src/views/SqlEditor/TablePanel/TableView.vue
+++ b/frontend/src/views/SqlEditor/TablePanel/TableView.vue
@@ -1,41 +1,88 @@
<template>
- <div ref="tableViewRef" class="table-view h-full p-2">
+ <div
+ ref="tableViewRef"
+ class="relative flex flex-col justify-start items-start h-full p-2"
+ >
+ <div
+ v-show="queryResult !== null"
+ class="w-full flex justify-between items-center mb-2"
+ >
+ <n-input
+ v-model:value="state.search"
+ class="max-w-xs"
+ type="text"
+ :placeholder="t('sql-editor.search-results')"
+ >
+ <template #prefix>
+ <heroicons-outline:search class="h-5 w-5 text-gray-300" />
+ </template>
+ </n-input>
+ <div class="flex justify-between items-center">
+ <n-dropdown
+ trigger="hover"
+ :options="exportDropdownOptions"
+ @select="handleExportBtnClick"
+ >
+ <n-button>
+ <template #icon>
+ <n-icon>
+ <heroicons-solid:download class="h-5 w-5" />
+ </n-icon>
+ </template>
+ {{ t("common.export") }}
+ </n-button>
+ </n-dropdown>
+ </div>
+ </div>
<NDataTable
- v-if="!isLoadingData"
- size="mini"
+ v-show="data.length > 0"
+ size="small"
+ :bordered="false"
:columns="columns"
:data="data"
- :bordered="false"
- flex-height
- :style="{ height: `${tableMaxHeight}px` }"
>
<template #empty>
- <div class="p-20 h-full flex justify-center items-center">
- {{ $t("sql-editor.table-empty-placehoder") }}
- </div>
+ <span>
+ <!-- hide n-data-table default empty content -->
+ </span>
</template>
</NDataTable>
- <div v-else class="flex justify-center items-center h-full">
- <BBSpin :title="$t('sql-editor.loading-data')" />
+ <div
+ v-show="notifyMessage"
+ class="absolute w-full h-full flex justify-center items-center"
+ >
+ {{ notifyMessage }}
</div>
</div>
</template>
<script lang="ts" setup>
-import { computed, ref } from "vue";
+import { computed, reactive, ref } from "vue";
import { useResizeObserver } from "@vueuse/core";
import { useNamespacedState } from "vuex-composition-helpers";
-import { useExecuteSQL } from "../../../composables/useExecuteSQL";
+import { useI18n } from "vue-i18n";
+
+interface State {
+ search: string;
+}
-const { isLoadingData } = useExecuteSQL();
-const { queryResult } = useNamespacedState("sqlEditor", ["queryResult"]);
+const { t } = useI18n();
+
+const { queryResult, isExecuting } = useNamespacedState<{
+ queryResult: Record<string, any>[] | null;
+ isExecuting: boolean;
+}>("sqlEditor", ["queryResult", "isExecuting"]);
+
+const state = reactive<State>({
+ search: "",
+});
const tableViewRef = ref<HTMLDivElement>();
const tableMaxHeight = ref(0);
const columns = computed(() => {
- return queryResult.value.length > 0
+ return queryResult.value && queryResult.value.length > 0
? Object.keys(queryResult.value[0]).map((item) => {
return {
title: item.toUpperCase(),
@@ -44,7 +91,79 @@ const columns = computed(() => {
})
: [];
});
-const data = computed(() => queryResult.value);
+const data = computed(() => {
+ const temp =
+ queryResult.value && queryResult.value.length > 0
+ ? queryResult.value.filter((d) => {
+ let t = false;
+
+ for (const k in d) {
+ if (String(d[k]).includes(state.search)) {
+ t = true;
+ break;
+ }
+ }
+
+ return t;
+ })
+ : [];
+ return temp;
+});
+const notifyMessage = computed(() => {
+ if (!queryResult.value) {
+ return t("sql-editor.table-empty-placehoder");
+ }
+ if (isExecuting.value) {
+ return t("loading-data");
+ }
+ if (data.value.length === 0) {
+ return t("sql-editor.no-rows-found");
+ }
+
+ return "";
+});
+
+const exportDropdownOptions = [
+ {
+ label: t("sql-editor.download-as-csv"),
+ key: "csv",
+ },
+ {
+ label: t("sql-editor.download-as-json"),
+ key: "json",
+ },
+];
+
+const handleExportBtnClick = (format: "csv" | "json") => {
+ let rawText = "";
+
+ if (format === "csv") {
+ let CSVContent = "";
+ CSVContent += columns.value.map((item) => String(item.key)).join(",");
+ CSVContent += "\n";
+
+ for (const d of data.value) {
+ const temp: any[] = [];
+ for (const k in d) {
+ temp.push(d[k]);
+ }
+ CSVContent += temp.map((item) => String(item)).join(",");
+ CSVContent += "\n";
+ }
+
+ rawText = CSVContent;
+ } else {
+ rawText = JSON.stringify(data.value);
+ }
+
+ const encodedUri = encodeURI(`data:text/${format};charset=utf-8,${rawText}`);
+ const link = document.createElement("a");
+
+ // TODO: file name should be the current query name
+ link.download = `${Date.now()}.${format}`;
+ link.href = encodedUri;
+ link.click();
+};
// make sure the table view is always full of the pane
useResizeObserver(tableViewRef, (entries) => {