Skip to content

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

feat(sql-editor): show table schema when selected table in connection tree (#298)

Yunwei Xiaocc831c664db8
diff --git a/frontend/src/components/ProvideSqlEditorContext.vue b/frontend/src/components/ProvideSqlEditorContext.vue
index 788a980e13..38854ac864 100644
--- a/frontend/src/components/ProvideSqlEditorContext.vue
+++ b/frontend/src/components/ProvideSqlEditorContext.vue
@@ -22,10 +22,12 @@ const prepareSqlEdtiorContext = async function () {
let connectionTree = [];
const mapConnectionAtom =
- (type: ConnectionAtomType) => (item: Instance | Database | Table) => {
+ (type: ConnectionAtomType, parentId: number) =>
+ (item: Instance | Database | Table) => {
const connectionAtom: ConnectionAtom = {
+ parentId,
id: item.id,
- key: item.id,
+ key: `${type}-${item.id}`,
label: item.name,
type,
};
@@ -34,7 +36,7 @@ const prepareSqlEdtiorContext = async function () {
};
const instanceList = await store.dispatch("instance/fetchInstanceList");
- connectionTree = instanceList.map(mapConnectionAtom("instance"));
+ connectionTree = instanceList.map(mapConnectionAtom("instance", 0));
for (const instance of instanceList) {
const databaseList = await store.dispatch(
@@ -45,7 +47,9 @@ const prepareSqlEdtiorContext = async function () {
const instanceItem = connectionTree.find(
(item: ConnectionAtom) => item.id === instance.id
);
- instanceItem.children = databaseList.map(mapConnectionAtom("database"));
+ instanceItem.children = databaseList.map(
+ mapConnectionAtom("database", instance.id)
+ );
for (const db of databaseList) {
const tableList = await store.dispatch(
@@ -57,7 +61,7 @@ const prepareSqlEdtiorContext = async function () {
(item: ConnectionAtom) => item.id === db.id
);
- databaseItem.children = tableList.map(mapConnectionAtom("table"));
+ databaseItem.children = tableList.map(mapConnectionAtom("table", db.id));
}
}
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 243aa7e77f..ab298a3681 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1024,6 +1024,9 @@ setting:
sql-editor:
self: SQL Editor
select-connection: Select Connections
+ search-databases: Search Databases
+ loading-databases: Loading Databases...
+ close-pane: Close Pane
label:
error:
key-necessary: Key is required
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index b4a4408ed5..a3ae2a60bf 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -895,6 +895,9 @@ setting:
sql-editor:
self: SQL 编辑器
select-connection: 请选择数据连接
+ search-databases: 搜索数据库
+ loading-databases: 加载数据库信息...
+ close-pane: 关闭面板
label:
error:
key-necessary: 需要指定 Key
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index f47fd0b0c4..ec9efb4e39 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -20,7 +20,8 @@ const state: () => SqlEditorState = () => ({
tableId: 0,
tableName: "",
isLoadingTree: false,
- selectedTableId: 0,
+ selectedDatabaseId: 0,
+ selectedTableName: "",
},
queryStatement: "",
selectedStatement: "",
diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.ts
index 419a4d65ba..825f7a1226 100644
--- a/frontend/src/types/sqlEditor.ts
+++ b/frontend/src/types/sqlEditor.ts
@@ -9,8 +9,9 @@ export type CompletionItems = monaco.languages.CompletionItem[];
export type ConnectionAtomType = "instance" | "database" | "table" | "view";
export interface ConnectionAtom {
+ parentId: InstanceId | DatabaseId | TableId | ViewId;
id: InstanceId | DatabaseId | TableId | ViewId;
- key: InstanceId | DatabaseId | TableId | ViewId;
+ key: string;
label: string;
type?: ConnectionAtomType;
children?: ConnectionAtom[];
@@ -33,5 +34,6 @@ export type ConnectionContext = {
tableId?: TableId;
tableName?: string;
isLoadingTree: boolean;
- selectedTableId: number;
+ selectedDatabaseId: number;
+ selectedTableName: string;
};
diff --git a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
index 2fd560d86e..c9ded0d7db 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/AsidePanel.vue
@@ -2,21 +2,59 @@
<div class="aside-panel h-full">
<n-tabs type="segment" default-value="databases" class="h-full">
<n-tab-pane name="databases" tab="Databases">
- <Splitpanes horizontal class="default-theme">
- <Pane size="100"><DatabasesTree /></Pane>
- <Pane size="0" max-size="40"><TableSchema /></Pane>
+ <Splitpanes
+ horizontal
+ class="default-theme"
+ :dbl-click-splitter="false"
+ @resized="handleResized"
+ >
+ <Pane :size="databasePaneSzie"><DatabaseTree /></Pane>
+ <Pane
+ :size="FULL_HEIGHT - databasePaneSzie"
+ :max-size="TABLE_SCHEMA_PANE_SIZE"
+ >
+ <TableSchema @close-pane="handleCloseTableSchemaPane" />
+ </Pane>
</Splitpanes>
</n-tab-pane>
- <n-tab-pane name="queries" tab="Queries">
- Queries Page
- </n-tab-pane>
+ <n-tab-pane name="queries" tab="Queries"> Queries Page </n-tab-pane>
</n-tabs>
</div>
</template>
<script lang="ts" setup>
-import DatabasesTree from "./DatabasesTree.vue";
+import { ref, watch } from "vue";
+import { useNamespacedState } from "vuex-composition-helpers";
+
+import type { SqlEditorState } from "../../../types";
+import DatabaseTree from "./DatabaseTree.vue";
import TableSchema from "./TableSchema.vue";
+
+const FULL_HEIGHT = 100;
+const DATABASE_PANE_SIZE = 60;
+const TABLE_SCHEMA_PANE_SIZE = FULL_HEIGHT - DATABASE_PANE_SIZE;
+
+const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
+ "connectionContext",
+]);
+
+const databasePaneSzie = ref(FULL_HEIGHT);
+
+const handleResized = (data: any) => {
+ const [top, bottom] = data;
+ databasePaneSzie.value = top.size;
+};
+
+const handleCloseTableSchemaPane = () => {
+ databasePaneSzie.value = FULL_HEIGHT;
+};
+
+watch(
+ () => connectionContext.value.selectedTableName,
+ () => {
+ databasePaneSzie.value = DATABASE_PANE_SIZE;
+ }
+);
</script>
<style scoped>
diff --git a/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue b/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
similarity index 52%
rename from frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue
rename to frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
index eb71a40d4f..4fc57e11ae 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/DatabasesTree.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/DatabaseTree.vue
@@ -1,20 +1,32 @@
<template>
<div
- class="databases-tree p-2 space-y-2 overflow-x-auto"
v-if="!connectionContext.isLoadingTree"
+ class="databases-tree p-2 space-y-2 h-full"
>
- <NInput v-model:value="searchPattern" placeholder="Search Databases" />
- <NTree
- block-line
- :data="connectionTree"
- :pattern="searchPattern"
- :default-expanded-keys="defaultExpanedKeys"
- :default-selected-keys="defaultSelectedKeys"
- :on-update:selected-keys="handleSelectedKeysChange"
- />
+ <div class="databases-tree--input">
+ <NInput
+ v-model:value="searchPattern"
+ :placeholder="$t('sql-editor.search-databases')"
+ >
+ <template #prefix>
+ <heroicons-outline:search class="h-5 w-5 text-gray-300" />
+ </template>
+ </NInput>
+ </div>
+ <div class="databases-tree--tree overflow-y-auto">
+ <NTree
+ block-line
+ leaf-only
+ :data="connectionTree"
+ :pattern="searchPattern"
+ :default-expanded-keys="defaultExpanedKeys"
+ :default-selected-keys="defaultSelectedKeys"
+ :on-update:selected-keys="handleSelectedKeysChange"
+ />
+ </div>
</div>
<div v-else class="flex justify-center items-center h-full">
- <BBSpin title="Loading Databases..." />
+ <BBSpin :title="$t('sql-editor.loading-databases')" />
</div>
</template>
@@ -45,18 +57,18 @@ const { setConnectionContext } = useNamespacedActions<SqlEditorActions>(
const defaultExpanedKeys = computed(() => {
const ctx = connectionContext.value;
if (ctx.hasSlug) {
- return [ctx.instanceId, ctx.databaseId];
+ return [`instance-${ctx.instanceId}`, `database-${ctx.databaseId}`];
} else {
- [];
+ return [];
}
});
const defaultSelectedKeys = computed(() => {
const ctx = connectionContext.value;
if (ctx.hasSlug) {
- return [ctx.databaseId];
+ return [`database-${ctx.databaseId}`];
} else {
- [];
+ return [];
}
});
@@ -67,8 +79,15 @@ const handleSelectedKeysChange = (
const [selectedItem] = options;
if (selectedItem.type === "table") {
setConnectionContext({
- selectedTableId: selectedItem.id,
+ selectedDatabaseId: selectedItem.parentId,
+ selectedTableName: selectedItem.label,
});
}
};
</script>
+
+<style scoped>
+.databases-tree--tree {
+ height: calc(100% - 40px);
+}
+</style>
diff --git a/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue b/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
index 24b6323d32..69c8283e68 100644
--- a/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
+++ b/frontend/src/views/SqlEditor/AsidePanel/TableSchema.vue
@@ -1 +1,128 @@
-<template>Table Schema</template>
+<template>
+ <div v-if="tableInfo" class="table-schema">
+ <div class="table-schema--header">
+ <div class="table-schema--header-title mr-1 flex items-center">
+ <heroicons-outline:table class="h-4 w-4 mr-1" />
+ <span class="font-semibold">{{ tableInfo.name }}</span>
+ </div>
+ <div
+ class="table-schema--header-actions flex-1 flex justify-end space-x-2"
+ >
+ <div class="action-edit flex items-center">
+ <NTooltip trigger="hover">
+ <template #trigger>
+ <NButton text @click="handleEditorSchema">
+ <heroicons-outline:pencil-alt class="w-4 h-4" />
+ </NButton>
+ </template>
+ {{ $t("quick-action.alter-schema") }}
+ </NTooltip>
+ </div>
+ <div class="action-close flex items-center">
+ <NTooltip trigger="hover">
+ <template #trigger>
+ <NButton text @click="handleClosePane">
+ <heroicons-outline:x class="w-4 h-4" />
+ </NButton>
+ </template>
+ {{ $t("sql-editor.close-pane") }}
+ </NTooltip>
+ </div>
+ </div>
+ </div>
+ <div class="table-schema--meta text-gray-500 text-sm">
+ <div class="pb-1">
+ <span>{{ tableInfo.rowCount }} rows</span>
+ </div>
+ <div class="flex justify-between items-center w-full text-xs py-2">
+ <div class="table-schema--content-column">
+ <span>Columns</span>
+ </div>
+ <div class="table-schema--content-column">
+ <span>Data Type</span>
+ </div>
+ </div>
+ </div>
+ <div class="table-schema--content text-sm text-gray-400 overflow-y-auto">
+ <div
+ v-for="(column, index) in tableInfo.columnList"
+ :key="index"
+ class="flex justify-between items-center w-full p-1 hover:bg-link-hover"
+ >
+ <div class="table-schema--content-column text-gray-600">
+ <span>{{ column.name }}</span>
+ </div>
+ <div class="table-schema--content-column">
+ <span>{{ column.type }}</span>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div v-else class="h-full flex justify-center items-center">
+ Select a table to see its schema
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { ref, watch } from "vue";
+import {
+ useNamespacedState,
+ useNamespacedActions,
+} from "vuex-composition-helpers";
+
+import type { SqlEditorState } from "../../../types";
+
+const emit = defineEmits<{
+ (e: "close-pane"): void;
+}>();
+
+const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
+ "connectionContext",
+]);
+const { fetchTableByDatabaseIdAndTableName } = useNamespacedActions("table", [
+ "fetchTableByDatabaseIdAndTableName",
+]);
+
+const tableInfo = ref();
+const ctx = connectionContext.value;
+
+const handleEditorSchema = () => {
+ // TODO goto main console, create an issue
+};
+
+const handleClosePane = () => {
+ emit("close-pane");
+};
+
+watch(
+ () => connectionContext.value.selectedTableName,
+ async (newVal, oldVal) => {
+ const res = await fetchTableByDatabaseIdAndTableName({
+ databaseId: ctx.selectedDatabaseId,
+ tableName: ctx.selectedTableName,
+ });
+
+ tableInfo.value = res;
+ }
+);
+</script>
+
+<style scoped>
+.table-schema {
+ @apply h-full space-y-2;
+}
+
+.table-schema--header {
+ @apply flex items-center p-2 border-b;
+}
+
+.table-schema--meta {
+ @apply px-2 py-1;
+ @apply border-b;
+}
+
+.table-schema--content {
+ @apply px-2 py-1;
+ height: calc(100% - 116px);
+}
+</style>
diff --git a/frontend/src/views/SqlEditor/EditorHeader.vue b/frontend/src/views/SqlEditor/EditorHeader.vue
index 5e4741364d..d16e3cfaa6 100644
--- a/frontend/src/views/SqlEditor/EditorHeader.vue
+++ b/frontend/src/views/SqlEditor/EditorHeader.vue
@@ -3,12 +3,18 @@
<div class="flex items-center">
<div class="flex-shrink-0 w-44">
<router-link to="/" class="select-none" active-class exact-active-class>
- <img class="h-12 w-auto" src="../../assets/logo-full.svg" alt="Bytebase" />
+ <img
+ class="h-12 w-auto"
+ src="../../assets/logo-full.svg"
+ alt="Bytebase"
+ />
</router-link>
</div>
<div class="hidden sm:block">
<div class="ml-6 flex items-baseline space-x-1">
- <router-link to="/sql-editor" class="bar-link px-2 py-2 rounded-md">SQL Editor</router-link>
+ <router-link to="/sql-editor" class="bar-link px-2 py-2 rounded-md">{{
+ $t("sql-editor.self")
+ }}</router-link>
</div>
</div>
</div>
@@ -21,8 +27,11 @@
></span>
<heroicons-outline:bell class="w-6 h-6" />
</router-link>
- <!-- TODO test for now, will delete -->
- <div v-if="showSwitchPlan" class="cursor-pointer" @click="toggleLocales">
+ <div
+ v-if="showSwitchPlan"
+ class="cursor-pointer"
+ @click="toggleLocales"
+ >
<heroicons-outline:translate class="w-6 h-6" />
</div>
<div class="ml-2">
@@ -53,28 +62,38 @@
Open: "block", closed: "hidden"
-->
<div v-if="state.showMobileMenu" class="block md:hidden">
- <router-link to="/project" class="bar-link rounded-md block px-3 py-2">Projects</router-link>
+ <router-link to="/project" class="bar-link rounded-md block px-3 py-2">{{
+ $t("common.projects")
+ }}</router-link>
- <router-link to="/db" class="bar-link rounded-md block px-3 py-2">Databases</router-link>
+ <router-link to="/db" class="bar-link rounded-md block px-3 py-2">{{
+ $t("common.databases")
+ }}</router-link>
<router-link
v-if="showDBAItem"
to="/instance"
class="bar-link rounded-md block px-3 py-2"
- >Instances</router-link>
+ >{{ $t("common.instances") }}</router-link
+ >
- <router-link to="/environment" class="bar-link rounded-md block px-3 py-2">Environments</router-link>
+ <router-link
+ to="/environment"
+ class="bar-link rounded-md block px-3 py-2"
+ >{{ $t("common.environments") }}</router-link
+ >
<router-link
to="/setting/member"
class="bar-link rounded-md block px-3 py-2"
- >{{ $t("common.settings") }}</router-link>
+ >{{ $t("common.settings") }}</router-link
+ >
</div>
</template>
<script lang="ts">
import { defineAction, useRegisterActions } from "@bytebase/vue-kbar";
-import { computed, reactive, watchEffect } from "vue";
+import { computed, reactive, watchEffect, defineComponent } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { useI18n } from "vue-i18n";
@@ -88,7 +107,7 @@ interface LocalState {
showMobileMenu: boolean;
}
-export default {
+export default defineComponent({
name: "EditorHeader",
components: { ProfileDropdown },
setup() {
@@ -128,39 +147,6 @@ export default {
return store.getters["inbox/inboxSummaryByUser"](currentUser.value.id);
});
- const switchToOwner = () => {
- store.dispatch("auth/login", {
- email: "[email protected]",
- password: "1024",
- });
- };
-
- const switchToDBA = () => {
- store.dispatch("auth/login", {
- email: "[email protected]",
- password: "2048",
- });
- };
-
- const switchToDeveloper = () => {
- store.dispatch("auth/login", {
- email: "[email protected]",
- password: "4096",
- });
- };
-
- const switchToFree = () => {
- store.dispatch("plan/changePlan", PlanType.FREE);
- };
-
- const switchToTeam = () => {
- store.dispatch("plan/changePlan", PlanType.TEAM);
- };
-
- const switchToEnterprise = () => {
- store.dispatch("plan/changePlan", PlanType.ENTERPRISE);
- };
-
const kbarActions = computed(() => [
defineAction({
id: "bb.navigation.projects",
@@ -259,19 +245,11 @@ export default {
return {
state,
- currentUser,
- currentPlan,
showDBAItem,
showSwitchPlan,
inboxSummary,
- switchToOwner,
- switchToDBA,
- switchToDeveloper,
- switchToFree,
- switchToTeam,
- switchToEnterprise,
toggleLocales,
};
},
-};
+});
</script>
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
index ddbe1566a3..9afe60aa66 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorActions.vue
@@ -1,6 +1,6 @@
<template>
<div class="sqleditor-editor-actions">
- <div class="actions-left w-3/5">
+ <div class="actions-left w-1/2">
<NButton
type="primary"
@click="handleExecuteQueries"
@@ -9,7 +9,7 @@
<mdi:play class="h-5 w-5" /> {{ $t("common.run") }} (⌘+⏎)
</NButton>
</div>
- <div class="actions-right space-x-2 flex w-2/5 justify-end">
+ <div class="actions-right space-x-2 flex w-1/2 justify-end">
<NCascader
v-model:value="selectedConnection"
expand-trigger="hover"
diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
index 825aea9436..6d34f72b65 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorPanel.vue
@@ -1,7 +1,9 @@
<template>
- <EditorActions />
+ <div class="editor-pane h-full">
+ <EditorActions />
- <QueryEditor />
+ <QueryEditor />
+ </div>
</template>
<script lang="ts" setup>
diff --git a/frontend/src/views/SqlEditor/SqlEditor.vue b/frontend/src/views/SqlEditor/SqlEditor.vue
index 44f00e4ea6..748955e734 100644
--- a/frontend/src/views/SqlEditor/SqlEditor.vue
+++ b/frontend/src/views/SqlEditor/SqlEditor.vue
@@ -40,32 +40,25 @@ const randomKey = Math.random();
.splitpanes.default-theme .splitpanes__pane {
@apply bg-transparent;
}
+
.splitpanes.default-theme .splitpanes__splitter {
@apply bg-gray-100;
- min-height: 16px;
- min-width: 16px;
+ min-height: 8px;
+ min-width: 8px;
+}
+
+.splitpanes.default-theme .splitpanes__splitter:hover {
+ @apply bg-indigo-400;
}
.splitpanes.default-theme .splitpanes__splitter::before,
.splitpanes.default-theme .splitpanes__splitter::after {
- @apply bg-gray-700 opacity-50;
+ @apply bg-gray-700 opacity-50 text-white;
}
.splitpanes.default-theme .splitpanes__splitter:hover::before,
.splitpanes.default-theme .splitpanes__splitter:hover::after {
- @apply bg-transparent;
-}
-
-.default-theme.splitpanes--vertical > .splitpanes__splitter,
-.default-theme .splitpanes--vertical > .splitpanes__splitter {
- @apply border-l border-r;
- border-color: var(--border-color);
-}
-
-.default-theme.splitpanes--horizontal > .splitpanes__splitter,
-.default-theme .splitpanes--horizontal > .splitpanes__splitter {
- @apply border-t border-b;
- border-color: var(--border-color);
+ @apply bg-white opacity-100;
}
</style>