Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: database tree in aside panel (#15799)
ecmadao6ab61b317ed8
diff --git a/frontend/src/components/Database/DatabaseOverviewPanel.vue b/frontend/src/components/Database/DatabaseOverviewPanel.vueindex 827d505870..0e677b02c2 100644--- a/frontend/src/components/Database/DatabaseOverviewPanel.vue+++ b/frontend/src/components/Database/DatabaseOverviewPanel.vue@@ -170,7 +170,6 @@<script lang="ts" setup>import { head } from "lodash-es";import { NSelect } from "naive-ui";-import type { PropType } from "vue";import { computed, reactive, watch } from "vue";import { useI18n } from "vue-i18n";import { useRoute, useRouter } from "vue-router";@@ -200,27 +199,20 @@ import { SearchBox } from "../v2";import DatabaseOverviewInfo from "./DatabaseOverviewInfo.vue";interface LocalState {- selectedSchemaName: string;+ selectedSchemaName?: string;tableNameSearchKeyword: string;externalTableNameSearchKeyword: string;}-const props = defineProps({- database: {- required: true,- type: Object as PropType<ComposedDatabase>,- },- anomalyList: {- required: true,- type: Object as PropType<Anomaly[]>,- },-});+const props = defineProps<{+ database: ComposedDatabase;+ anomalyList: Anomaly[];+}>();const { t } = useI18n();const route = useRoute();const router = useRouter();const state = reactive<LocalState>({- selectedSchemaName: "",tableNameSearchKeyword: "",externalTableNameSearchKeyword: "",});@@ -289,25 +281,17 @@ const schemaNameOptions = computed(() => {});const tableList = computed(() => {- if (hasSchemaPropertyV1.value) {- return (- schemaList.value.find(- (schema) => schema.name === state.selectedSchemaName- )?.tables || []- );- }- return dbSchemaStore.getTableList(props.database.name);+ return dbSchemaStore.getTableList({+ database: props.database.name,+ schema: state.selectedSchemaName,+ });});const viewList = computed(() => {- if (hasSchemaPropertyV1.value) {- return (- schemaList.value.find(- (schema) => schema.name === state.selectedSchemaName- )?.views || []- );- }- return dbSchemaStore.getViewList(props.database.name);+ return dbSchemaStore.getViewList({+ database: props.database.name,+ schema: state.selectedSchemaName,+ });});const dbExtensionList = computed(() => {@@ -315,21 +299,17 @@ const dbExtensionList = computed(() => {});const externalTableList = computed(() => {- return dbSchemaStore.getExternalTableList(- props.database.name,- state.selectedSchemaName- );+ return dbSchemaStore.getExternalTableList({+ database: props.database.name,+ schema: state.selectedSchemaName,+ });});const functionList = computed(() => {- if (hasSchemaPropertyV1.value) {- return (- schemaList.value.find(- (schema) => schema.name === state.selectedSchemaName- )?.functions || []- );- }- return dbSchemaStore.getFunctionList(props.database.name);+ return dbSchemaStore.getFunctionList({+ database: props.database.name,+ schema: state.selectedSchemaName,+ });});const streamList = computed(() => {diff --git a/frontend/src/store/modules/v1/dbSchema.ts b/frontend/src/store/modules/v1/dbSchema.tsindex 8e8497312c..635eb4de1f 100644--- a/frontend/src/store/modules/v1/dbSchema.ts+++ b/frontend/src/store/modules/v1/dbSchema.ts@@ -7,6 +7,9 @@ import { UNKNOWN_ID, EMPTY_ID, UNKNOWN_INSTANCE_NAME } from "@/types";import {TableMetadata,DatabaseMetadata,+ ExternalTableMetadata,+ ViewMetadata,+ SchemaMetadata,} from "@/types/proto/v1/database_service";import { extractDatabaseResourceName } from "@/utils";@@ -191,10 +194,31 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {return promise;};+const getSchemaList = (database: string) => {return getCache(database)?.schemas ?? [];};- const getTableList = (database: string, schema?: string) => {++ const getSchemaMetadata = ({+ database,+ schema,+ }: {+ database: string;+ schema: string;+ }) => {+ return (+ getSchemaList(database).find((s) => s.name === schema) ??+ SchemaMetadata.create()+ );+ };++ const getTableList = ({+ database,+ schema,+ }: {+ database: string;+ schema?: string;+ }) => {const databaseMetadata = getCache(database);if (!databaseMetadata) {return [];@@ -208,15 +232,20 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {return databaseMetadata.schemas.flatMap((s) => s.tables);};- const getTableByName = (database: string, table: string, schema?: string) => {- const databaseMetadata = getCache(database);- if (!databaseMetadata) {- return undefined;- }- const tableList = getTableList(database, schema);- return tableList.find((t) => t.name === table);+ const getTableMetadata = ({+ database,+ table,+ schema,+ }: {+ database: string;+ table: string;+ schema?: string;+ }) => {+ const tableList = getTableList({ database, schema });+ return tableList.find((t) => t.name === table) ?? TableMetadata.create();};+const getOrFetchTableMetadata = async ({database,schema,@@ -243,7 +272,7 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {const metadataResourceName = ensureDatabaseMetadataResourceName(database);if (!skipCache) {- const existedTable = getTableByName(database, table, schema);+ const existedTable = getTableMetadata({ database, table, schema });if (existedTable && existedTable.columns.length > 0) {return existedTable;}@@ -284,7 +313,14 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {);return promise;};- const getExternalTableList = (database: string, schema?: string) => {++ const getExternalTableList = ({+ database,+ schema,+ }: {+ database: string;+ schema?: string;+ }) => {const databaseMetadata = getCache(database);if (!databaseMetadata) {return [];@@ -299,6 +335,23 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {return databaseMetadata.schemas.flatMap((s) => s.externalTables);};++ const getExternalTableMetadata = ({+ database,+ schema,+ externalTable,+ }: {+ database: string;+ schema?: string;+ externalTable: string;+ }) => {+ return (+ getExternalTableList({ database, schema }).find(+ (metadata) => metadata.name === externalTable+ ) ?? ExternalTableMetadata.create()+ );+ };+const getOrFetchExternalTableList = async (database: string,schema?: string@@ -306,9 +359,16 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {if (!getCache(database)) {await getOrFetchDatabaseMetadata({ database });}- return getExternalTableList(database, schema);+ return getExternalTableList({ database, schema });};- const getViewList = (database: string, schema?: string) => {++ const getViewList = ({+ database,+ schema,+ }: {+ database: string;+ schema?: string;+ }) => {const databaseMetadata = getCache(database);if (!databaseMetadata) {return [];@@ -322,6 +382,22 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {return databaseMetadata.schemas.flatMap((s) => s.views);};++ const getViewMetadata = ({+ database,+ schema,+ view,+ }: {+ database: string;+ schema?: string;+ view: string;+ }) => {+ return (+ getViewList({ database, schema }).find((v) => v.name === view) ??+ ViewMetadata.create()+ );+ };+const getExtensionList = (database: string) => {const databaseMetadata = getCache(database);if (!databaseMetadata) {@@ -330,7 +406,14 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {return databaseMetadata.extensions;};- const getFunctionList = (database: string, schema?: string) => {++ const getFunctionList = ({+ database,+ schema,+ }: {+ database: string;+ schema?: string;+ }) => {const databaseMetadata = getCache(database);if (!databaseMetadata) {return [];@@ -361,11 +444,15 @@ export const useDBSchemaV1Store = defineStore("dbSchema_v1", () => {getDatabaseMetadata,getOrFetchDatabaseMetadata,getSchemaList,+ getSchemaMetadata,getTableList,+ getTableMetadata,getOrFetchTableMetadata,getExternalTableList,+ getExternalTableMetadata,getOrFetchExternalTableList,getViewList,+ getViewMetadata,getExtensionList,getFunctionList,removeCache,diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ColumnInfo.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ColumnInfo.vueindex c2e8d7952b..8e4b94eb9d 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ColumnInfo.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ColumnInfo.vue@@ -1,17 +1,17 @@<template><div class="min-w-[14rem] max-w-[18rem] gap-y-1"><InfoItem :title="$t('common.name')">- {{ column.name }}+ {{ columnMetadata.name }}</InfoItem><InfoItem :title="$t('common.type')">- {{ column.type }}+ {{ columnMetadata.type }}</InfoItem><InfoItem :title="$t('common.Default')">- {{ getColumnDefaultValuePlaceholder(column) }}+ {{ getColumnDefaultValuePlaceholder(columnMetadata) }}</InfoItem><InfoItem :title="$t('database.nullable')"><div class="inline-flex items-center justify-end">- <CheckIcon v-if="column.nullable" class="w-4 h-4" />+ <CheckIcon v-if="columnMetadata.nullable" class="w-4 h-4" /><XIcon v-else class="w-4 h-4" /></div></InfoItem>@@ -21,8 +21,8 @@<InfoItem v-if="collation" :title="$t('db.collation')">{{ collation }}</InfoItem>- <InfoItem v-if="column.userComment" :title="$t('database.comment')">- {{ column.userComment }}+ <InfoItem v-if="columnMetadata.userComment" :title="$t('database.comment')">+ {{ columnMetadata.userComment }}</InfoItem></div></template>@@ -31,25 +31,36 @@import { CheckIcon, XIcon } from "lucide-vue-next";import { computed } from "vue";import { getColumnDefaultValuePlaceholder } from "@/components/SchemaEditorLite";-import type { ComposedDatabase } from "@/types";+import { useDBSchemaV1Store, useDatabaseV1Store } from "@/store";import { Engine } from "@/types/proto/v1/common";-import type {- ColumnMetadata,- DatabaseMetadata,- SchemaMetadata,-} from "@/types/proto/v1/database_service";+import { ColumnMetadata } from "@/types/proto/v1/database_service";import InfoItem from "./InfoItem.vue";const props = defineProps<{- db: ComposedDatabase;- database: DatabaseMetadata;- schema: SchemaMetadata;- column: ColumnMetadata;+ database: string;+ schema?: string;+ table: string;+ column: string;}>();-const instanceEngine = computed(() => {- return props.db.instanceResource.engine;-});+const dbSchema = useDBSchemaV1Store();+const databaseStore = useDatabaseV1Store();++const columnMetadata = computed(+ () =>+ dbSchema+ .getTableMetadata({+ database: props.database,+ schema: props.schema,+ table: props.table,+ })+ .columns.find((col) => col.name === props.column) ??+ ColumnMetadata.create()+);++const instanceEngine = computed(+ () => databaseStore.getDatabaseByName(props.database).instanceResource.engine+);const characterSet = computed(() => {if (@@ -57,14 +68,14 @@ const characterSet = computed(() => {instanceEngine.value)) {- return props.column.characterSet;+ return columnMetadata.value.characterSet;}return "";});const collation = computed(() => {if ([Engine.CLICKHOUSE, Engine.SNOWFLAKE].includes(instanceEngine.value)) {- return props.column.collation;+ return columnMetadata.value.collation;}return "";});diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/CommonText.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/CommonText.vueindex e9abb492ad..5d9e176760 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/CommonText.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/CommonText.vue@@ -5,12 +5,7 @@</template><script setup lang="ts">-import type { ComposedDatabase } from "@/types";-import type { DatabaseMetadata } from "@/types/proto/v1/database_service";-defineProps<{- db: ComposedDatabase;- database: DatabaseMetadata;content: string;}>();</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ExternalTableInfo.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ExternalTableInfo.vueindex 7bd1e65413..9cc3d54149 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ExternalTableInfo.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ExternalTableInfo.vue@@ -1,42 +1,35 @@<template><div class="min-w-[14rem] max-w-[18rem] gap-y-1"><InfoItem :title="$t('common.name')">- {{ name }}+ {{ externalTableMetadata.name }}</InfoItem><InfoItem :title="$t('database.external-server-name')">- {{ externalTable.externalServerName }}+ {{ externalTableMetadata.externalServerName }}</InfoItem><InfoItem :title="$t('database.external-database-name')">- {{ externalTable.externalDatabaseName }}+ {{ externalTableMetadata.externalDatabaseName }}</InfoItem></div></template><script setup lang="ts">import { computed } from "vue";-import type { ComposedDatabase } from "@/types";-import type {- DatabaseMetadata,- SchemaMetadata,- ExternalTableMetadata,-} from "@/types/proto/v1/database_service";-import { hasSchemaProperty } from "@/utils";+import { useDBSchemaV1Store } from "@/store";import InfoItem from "./InfoItem.vue";const props = defineProps<{- db: ComposedDatabase;- database: DatabaseMetadata;- schema: SchemaMetadata;- externalTable: ExternalTableMetadata;+ database: string;+ schema?: string;+ externalTable: string;}>();-const instanceEngine = computed(() => props.db.instanceResource.engine);+const dbSchema = useDBSchemaV1Store();-const name = computed(() => {- const { schema, externalTable } = props;- if (hasSchemaProperty(instanceEngine.value)) {- return `${schema.name}.${externalTable.name}`;- }- return externalTable.name;-});+const externalTableMetadata = computed(() =>+ dbSchema.getExternalTableMetadata({+ database: props.database,+ schema: props.schema,+ externalTable: props.externalTable,+ })+);</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/HoverPanel.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/HoverPanel.vueindex 2187b7ed06..1b6b288d3b 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/HoverPanel.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/HoverPanel.vue@@ -11,15 +11,14 @@><template v-if="state"><ColumnInfo- v-if="state.column"- :db="state.db"+ v-if="state.column && state.table":database="state.database":schema="state.schema"+ :table="state.table":column="state.column"/><TablePartitionInfov-else-if="state.table && state.partition"- :db="state.db":database="state.database":schema="state.schema":table="state.table"@@ -27,23 +26,21 @@/><TableInfov-else-if="state.table"- :db="state.db":database="state.database":schema="state.schema":table="state.table"/><ExternalTableInfov-else-if="state.externalTable"- :db="state.db":database="state.database":schema="state.schema":external-table="state.externalTable"/>- <CommonText+ <ViewInfov-else-if="state.view"- :db="state.db":database="state.database"- :content="state.view.comment"+ :schema="state.schema"+ :view="state.view"/></template></div>@@ -56,10 +53,10 @@ import { computed, ref } from "vue";import type { Position } from "@/types";import { minmax } from "@/utils";import ColumnInfo from "./ColumnInfo.vue";-import CommonText from "./CommonText.vue";import ExternalTableInfo from "./ExternalTableInfo.vue";import TableInfo from "./TableInfo.vue";import TablePartitionInfo from "./TablePartitionInfo.vue";+import ViewInfo from "./ViewInfo.vue";import { useHoverStateContext } from "./hover-state";const props = defineProps<{@@ -87,10 +84,11 @@ const show = computed(() => {state.value !== undefined &&position.value.x !== 0 &&position.value.y !== 0;- if (show && state.value?.view) {- const comment = state.value.view.comment;- if (!comment) return false;- }+ // TODO(ed):+ // if (show && state.value?.view) {+ // const comment = state.value.view.comment;+ // if (!comment) return false;+ // }return show;});diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TableInfo.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TableInfo.vueindex 277200b042..34690b388e 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TableInfo.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TableInfo.vue@@ -1,16 +1,16 @@<template><div class="min-w-[14rem] max-w-[18rem] gap-y-1"><InfoItem :title="$t('common.name')">- {{ name }}+ {{ tableMetadata.name }}</InfoItem>- <InfoItem v-if="engine" :title="$t('database.engine')">- {{ engine }}+ <InfoItem :title="$t('database.engine')">+ <RichEngineName :engine="instanceEngine" /></InfoItem><InfoItem :title="$t('database.row-count-estimate')">- {{ table.rowCount }}+ {{ tableMetadata.rowCount }}</InfoItem><InfoItem :title="$t('database.data-size')">- {{ bytesToString(table.dataSize.toNumber()) }}+ {{ bytesToString(tableMetadata.dataSize.toNumber()) }}</InfoItem><InfoItem v-if="indexSize" :title="$t('database.index-size')">{{ indexSize }}@@ -26,45 +26,38 @@<script setup lang="ts">import { computed } from "vue";-import type { ComposedDatabase } from "@/types";+import { RichEngineName } from "@/components/v2";+import { useDBSchemaV1Store, useDatabaseV1Store } from "@/store";import { Engine } from "@/types/proto/v1/common";-import type {- DatabaseMetadata,- SchemaMetadata,- TableMetadata,-} from "@/types/proto/v1/database_service";-import { bytesToString, hasSchemaProperty } from "@/utils";+import { bytesToString } from "@/utils";import InfoItem from "./InfoItem.vue";const props = defineProps<{- db: ComposedDatabase;- database: DatabaseMetadata;- schema: SchemaMetadata;- table: TableMetadata;+ database: string;+ schema?: string;+ table: string;}>();-const instanceEngine = computed(() => props.db.instanceResource.engine);+const dbSchema = useDBSchemaV1Store();+const databaseStore = useDatabaseV1Store();-const name = computed(() => {- const { schema, table } = props;- if (hasSchemaProperty(instanceEngine.value)) {- return `${schema.name}.${table.name}`;- }- return table.name;-});+const instanceEngine = computed(+ () => databaseStore.getDatabaseByName(props.database).instanceResource.engine+);-const engine = computed(() => {- if ([Engine.POSTGRES, Engine.SNOWFLAKE].includes(instanceEngine.value)) {- return "";- }- return props.table.engine;-});+const tableMetadata = computed(() =>+ dbSchema.getTableMetadata({+ database: props.database,+ schema: props.schema,+ table: props.table,+ })+);const indexSize = computed(() => {if ([Engine.CLICKHOUSE, Engine.SNOWFLAKE].includes(instanceEngine.value)) {return "";}- return bytesToString(props.table.indexSize.toNumber());+ return bytesToString(tableMetadata.value.indexSize.toNumber());});const collation = computed(() => {@@ -75,10 +68,10 @@ const collation = computed(() => {) {return "";}- return props.table.collation;+ return tableMetadata.value.collation;});const comment = computed(() => {- return props.table.userComment;+ return tableMetadata.value.userComment;});</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TablePartitionInfo.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TablePartitionInfo.vueindex b3b1edea9a..04ba11bb89 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TablePartitionInfo.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/TablePartitionInfo.vue@@ -1,36 +1,47 @@<template><div class="min-w-[14rem] max-w-[18rem] gap-y-1"><InfoItem :title="$t('common.name')">- {{ partition.name }}+ {{ partitionMetadata.name }}</InfoItem><InfoItem :title="$t('schema-editor.table-partition.type')">- {{ tablePartitionMetadata_TypeToJSON(partition.type) }}+ {{ tablePartitionMetadata_TypeToJSON(partitionMetadata.type) }}</InfoItem><InfoItem :title="$t('schema-editor.table-partition.expression')">- <code>{{ partition.expression }}</code>+ <code>{{ partitionMetadata.expression }}</code></InfoItem><InfoItem :title="$t('schema-editor.table-partition.value')">- {{ partition.value }}+ {{ partitionMetadata.value }}</InfoItem></div></template><script setup lang="ts">-import type { ComposedDatabase } from "@/types";+import { computed } from "vue";+import { useDBSchemaV1Store } from "@/store";import {tablePartitionMetadata_TypeToJSON,- type DatabaseMetadata,- type SchemaMetadata,- type TableMetadata,- type TablePartitionMetadata,+ TablePartitionMetadata,} from "@/types/proto/v1/database_service";import InfoItem from "./InfoItem.vue";-defineProps<{- db: ComposedDatabase;- database: DatabaseMetadata;- schema: SchemaMetadata;- table: TableMetadata;- partition: TablePartitionMetadata;+const props = defineProps<{+ database: string;+ schema?: string;+ table: string;+ partition: string;}>();++const dbSchema = useDBSchemaV1Store();++const partitionMetadata = computed(+ () =>+ dbSchema+ .getTableMetadata({+ database: props.database,+ schema: props.schema,+ table: props.table,+ })+ .partitions.find((p) => p.name === props.partition) ??+ TablePartitionMetadata.create()+);</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ViewInfo.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ViewInfo.vuenew file mode 100644index 0000000000..2510a424dc--- /dev/null+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/ViewInfo.vue@@ -0,0 +1,25 @@+<template>+ <CommonText :content="viewMetadata.comment" />+</template>++<script setup lang="tsx">+import { computed } from "vue";+import { useDBSchemaV1Store } from "@/store";+import CommonText from "./CommonText.vue";++const props = defineProps<{+ database: string;+ schema?: string;+ view: string;+}>();++const dbSchema = useDBSchemaV1Store();++const viewMetadata = computed(() =>+ dbSchema.getViewMetadata({+ database: props.database,+ schema: props.schema,+ view: props.view,+ })+);+</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/hover-state.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/hover-state.tsindex a8cdba18bb..dcdb920a37 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/hover-state.ts+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/HoverPanel/hover-state.ts@@ -1,13 +1,3 @@-import type { ComposedDatabase } from "@/types";-import type {- ColumnMetadata,- DatabaseMetadata,- ExternalTableMetadata,- SchemaMetadata,- TableMetadata,- TablePartitionMetadata,- ViewMetadata,-} from "@/types/proto/v1/database_service";import {useHoverStateContext as _useHoverStateContext,provideHoverStateContext as _provideHoverStateContext,@@ -16,14 +6,13 @@ import {export const KEY = "schema-pane";export type HoverState = {- db: ComposedDatabase;- database: DatabaseMetadata;- schema: SchemaMetadata;- table?: TableMetadata;- externalTable?: ExternalTableMetadata;- view?: ViewMetadata;- column?: ColumnMetadata;- partition?: TablePartitionMetadata;+ database: string;+ schema?: string;+ table?: string;+ externalTable?: string;+ view?: string;+ column?: string;+ partition?: string;};export const useHoverStateContext = () => {diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vueindex e269f18dc4..327594a1a9 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue@@ -13,11 +13,7 @@/></div><div class="shrink-0 flex items-center">- <SyncSchemaButton- :database="database"- :metadata="metadata"- size="small"- />+ <SyncSchemaButton size="small" /></div></div>@@ -60,12 +56,13 @@<BBModal :show="!!schemaViewer" @close="schemaViewer = undefined"><template v-if="schemaViewer" #title>- <RichDatabaseName :database="schemaViewer.database" />+ <RichDatabaseName :database="database" /></template><TableSchemaViewerv-if="schemaViewer"style="width: calc(100vw - 12rem); height: calc(100vh - 12rem)"v-bind="schemaViewer"+ :database="database"/></BBModal>@@ -115,13 +112,13 @@ import HoverPanel, { provideHoverStateContext } from "./HoverPanel";import SyncSchemaButton from "./SyncSchemaButton.vue";import { Label } from "./TreeNode";import { useDropdown, useActions } from "./actions";+import { useClickEvents } from "./click";import {type NodeTarget,type TreeNode,buildDatabaseSchemaTree,ExpandableNodeTypes,- useClickEvents,-} from "./common";+} from "./tree";const mounted = useMounted();const searchBoxRef = ref<InstanceType<typeof SearchBox>>();@@ -373,14 +370,9 @@ watchEffect(() => {// bottom-up recursivelyconst expandNode = (node: TreeNode) => {const keysToExpand: string[] = [];- const walk = (node: TreeNode | undefined) => {- if (!node) return;- if (ExpandableNodeTypes.includes(node.meta.type)) {- keysToExpand.push(node.key);- }- walk(node.parent);- };- walk(node);+ if (ExpandableNodeTypes.includes(node.meta.type)) {+ keysToExpand.push(node.key);+ }upsertExpandedKeys(keysToExpand);};const collapseNode = (node: TreeNode) => {@@ -399,9 +391,7 @@ const singleClick = (node: TreeNode) => {if (node.meta.type === "schema") {const tab = currentTab.value;if (tab) {- tab.connection.schema = (- node.meta.target as NodeTarget<"schema">- ).schema.name;+ tab.connection.schema = (node.meta.target as NodeTarget<"schema">).schema;return;}}diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vueindex 2e8ec7df8b..7c48e58b97 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.column.name"+ :text="target.column":keyword="keyword":highlight="true":indent="0"@@ -19,7 +19,7 @@class="text-size-adjust-none"style="--n-height: 16px; --n-padding: 0 3px; --n-font-size: 10px">- {{ target.column.type }}+ {{ columnMetadata?.type }}</NTag></div></template>@@ -30,7 +30,8 @@import { NTag } from "naive-ui";import { computed } from "vue";import { ColumnIcon, IndexIcon, PrimaryKeyIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import { useDBSchemaV1Store } from "@/store";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{@@ -38,24 +39,56 @@ const props = defineProps<{keyword: string;}>();+const dbSchema = useDBSchemaV1Store();const target = computed(() => (props.node as TreeNode<"column">).meta.target);+const tableMetadata = computed(() => {+ if ("table" in target.value) {+ const { database, schema, table } = target.value;+ return dbSchema.getTableMetadata({ database, schema, table });+ }+ return undefined;+});++const columnMetadata = computed(() => {+ const { database, schema, column } = target.value;+ const schemaMetadata = dbSchema.getSchemaMetadata({ database, schema });++ if ("table" in target.value) {+ return tableMetadata.value?.columns.find((c) => c.name === column);+ } else if ("externalTable" in target.value) {+ const { externalTable } = target.value;+ return schemaMetadata.externalTables+ .find((t) => t.name === externalTable)+ ?.columns.find((c) => c.name === column);+ } else if ("view" in target.value) {+ const { view } = target.value;+ return schemaMetadata.views+ .find((v) => v.name === view)+ ?.columns.find((c) => c.name === column);+ }+ return undefined;+});+const isPrimaryKey = computed(() => {if ("table" in target.value) {- const { table, column } = target.value;- const pk = table.indexes.find((idx) => idx.primary);+ const { column } = target.value;+ const pk = tableMetadata.value?.indexes.find((idx) => idx.primary);if (!pk) return false;- return pk.expressions.includes(column.name);+ return pk.expressions.includes(column);}return false;});+const isIndex = computed(() => {if (isPrimaryKey.value) return false;if ("table" in target.value) {- const { table, column } = target.value;- return table.indexes.some((idx) => idx.expressions.includes(column.name));+ const { column } = target.value;+ return tableMetadata.value?.indexes.some((idx) =>+ idx.expressions.includes(column)+ );}return false;diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DatabaseNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DatabaseNode.vueindex 13e7e08753..09967f6222 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DatabaseNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DatabaseNode.vue@@ -13,7 +13,8 @@<script setup lang="ts">import { computed } from "vue";import { InstanceV1EngineIcon } from "@/components/v2";-import type { TreeNode } from "../common";+import { useDatabaseV1Store } from "@/store";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{@@ -21,7 +22,11 @@ const props = defineProps<{keyword: string;}>();-const database = computed(- () => (props.node as TreeNode<"database">).meta.target.db+const databaseStore = useDatabaseV1Store();++const database = computed(() =>+ databaseStore.getDatabaseByName(+ (props.node as TreeNode<"database">).meta.target.database+ ));</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DependencyColumnNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DependencyColumnNode.vueindex b9f8ddb42e..9cef4dbacd 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DependencyColumnNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DependencyColumnNode.vue@@ -9,7 +9,7 @@<script setup lang="ts">import { computed } from "vue";import { ColumnIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DummyNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DummyNode.vueindex ec592f07b1..5bde4e76b8 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DummyNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/DummyNode.vue@@ -15,7 +15,7 @@import { NPopover } from "naive-ui";import { computed } from "vue";import { useI18n } from "vue-i18n";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";const props = defineProps<{node: TreeNode;diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ExternalTableNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ExternalTableNode.vueindex 2401c85e2a..b2941b320b 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ExternalTableNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ExternalTableNode.vue@@ -1,9 +1,5 @@<template>- <CommonNode- :text="target.externalTable.name"- :keyword="keyword"- :highlight="true"- >+ <CommonNode :text="target.externalTable" :keyword="keyword" :highlight="true"><template #icon><ExternalTableIcon class="w-4 h-4" /></template>@@ -13,7 +9,7 @@<script setup lang="ts">import { computed } from "vue";import { ExternalTableIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ForeignKeyNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ForeignKeyNode.vueindex 618fe8cdd2..3852e8ef3a 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ForeignKeyNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ForeignKeyNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.foreignKey.name"+ :text="target.foreignKey":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { ForeignKeyIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/FunctionNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/FunctionNode.vueindex 57a7d5a30b..87978162b2 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/FunctionNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/FunctionNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.function.signature || target.function.name"+ :text="(functionMetadata?.signature || functionMetadata?.name) ?? ''":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,8 @@<script setup lang="ts">import { computed } from "vue";import { FunctionIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import { useDBSchemaV1Store } from "@/store";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{@@ -22,5 +23,16 @@ const props = defineProps<{keyword: string;}>();+const dbSchema = useDBSchemaV1Store();+const target = computed(() => (props.node as TreeNode<"function">).meta.target);++const functionMetadata = computed(() =>+ dbSchema+ .getSchemaMetadata({+ database: target.value.database,+ schema: target.value.schema,+ })+ .functions.find((f) => f.name === target.value.function)+);</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/IndexNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/IndexNode.vueindex 6dad4d28c1..5e313ef5e7 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/IndexNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/IndexNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.index.name"+ :text="target.index":keyword="keyword":highlight="true":indent="0"@@ -15,7 +15,8 @@<script setup lang="ts">import { computed } from "vue";import { IndexIcon, PrimaryKeyIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import { useDBSchemaV1Store } from "@/store";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{@@ -23,8 +24,21 @@ const props = defineProps<{keyword: string;}>();+const dbSchema = useDBSchemaV1Store();+const target = computed(() => (props.node as TreeNode<"index">).meta.target);++const indexMetadata = computed(() => {+ const { database, schema, table, index } = target.value;+ const tableMetadata = dbSchema.getTableMetadata({+ database,+ schema,+ table,+ });+ return tableMetadata.indexes.find((i) => i.name === index);+});+const isPrimaryKey = computed(() => {- return target.value.index.primary;+ return indexMetadata.value?.primary;});</script>diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/Label.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/Label.vueindex 78545904d2..9c87b8f40d 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/Label.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/Label.vue@@ -54,7 +54,7 @@<script lang="ts" setup>import { computed } from "vue";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import ColumnNode from "./ColumnNode.vue";import DatabaseNode from "./DatabaseNode.vue";import DependencyColumnNode from "./DependencyColumnNode.vue";diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PackageNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PackageNode.vueindex ed2ce22e36..29c8f68931 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PackageNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PackageNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.package.name"+ :text="target.package":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { PackageIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PartitionTableNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PartitionTableNode.vueindex 68fb5baafc..0f43591252 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PartitionTableNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/PartitionTableNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.partition.name"+ :text="target.partition":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { TablePartitionIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ProcedureNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ProcedureNode.vueindex e2f48a3e14..a0e58af597 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ProcedureNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ProcedureNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.procedure.name"+ :text="target.procedure":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { ProcedureIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SchemaNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SchemaNode.vueindex 94b12df597..8c99daf1a1 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SchemaNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SchemaNode.vue@@ -1,5 +1,5 @@<template>- <CommonNode :text="target.schema.name" :keyword="keyword" :highlight="true">+ <CommonNode :text="target.schema" :keyword="keyword" :highlight="true"><template #icon><SchemaIcon class="w-4 h-4" /></template>@@ -12,7 +12,7 @@<script setup lang="ts">import { computed } from "vue";import { SchemaIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SequenceNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SequenceNode.vueindex 335395d89c..813fba6ff9 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SequenceNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/SequenceNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.sequence.name"+ :text="target.sequence":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { SequenceIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TableNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TableNode.vueindex c990b3db8c..6a128e99f7 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TableNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TableNode.vue@@ -1,5 +1,5 @@<template>- <CommonNode :text="target.table.name" :keyword="keyword" :highlight="true">+ <CommonNode :text="target.table" :keyword="keyword" :highlight="true"><template #icon><TableIcon class="w-4 h-4" /></template>@@ -9,7 +9,7 @@<script setup lang="ts">import { computed } from "vue";import { TableIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TextNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TextNode.vueindex da45ada099..63030d2f15 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TextNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TextNode.vue@@ -14,7 +14,7 @@import { isFunction } from "lodash-es";import { FolderIcon } from "lucide-vue-next";import { computed } from "vue";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TriggerNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TriggerNode.vueindex 543137f77a..10cc404bf2 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TriggerNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/TriggerNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.trigger.name"+ :text="target.trigger":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { TriggerIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ViewNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ViewNode.vueindex 1f2b44ddfd..53f7cb1921 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ViewNode.vue+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ViewNode.vue@@ -1,6 +1,6 @@<template><CommonNode- :text="target.view.name"+ :text="target.view":keyword="keyword":highlight="true":indent="0"@@ -14,7 +14,7 @@<script setup lang="ts">import { computed } from "vue";import { ViewIcon } from "@/components/Icon";-import type { TreeNode } from "../common";+import type { TreeNode } from "../tree";import CommonNode from "./CommonNode.vue";const props = defineProps<{diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsxindex c33f16260d..00cd61b8b3 100644--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/actions.tsx@@ -21,7 +21,14 @@ import formatSQL from "@/components/MonacoEditor/sqlFormatter";import { useExecuteSQL } from "@/composables/useExecuteSQL";import { t } from "@/plugins/i18n";import { SQL_EDITOR_DATABASE_MODULE } from "@/router/sqlEditor";-import { pushNotification, useAppFeature, useSQLEditorTabStore, useTabViewStateStore } from "@/store";+import {+ pushNotification,+ useAppFeature,+ useSQLEditorTabStore,+ useTabViewStateStore,+ useDBSchemaV1Store,+ useDatabaseV1Store,+} from "@/store";import {DEFAULT_SQL_EDITOR_TAB_MODE,dialectOfEngineV1,@@ -58,7 +65,8 @@ import {useCurrentTabViewStateContext,} from "../../EditorPanel";import { useSQLEditorContext } from "../../context";-import type { NodeTarget, NodeType, TreeNode } from "./common";+import type { NodeTarget, NodeType, TreeNode } from "./tree";+import { readableTextForNodeTarget } from "./tree"type DropdownOptionWithTreeNode = DropdownOption & {onSelect?: () => void;@@ -125,20 +133,22 @@ const confirmOverrideStatement = async (export const useActions = () => {const { updateViewState } = useCurrentTabViewStateContext();+ const databaseStore = useDatabaseV1Store();const selectAllFromTableOrView = async (node: TreeNode) => {- const { target } = (node as TreeNode<"table" | "view">).meta;+ const { target, type } = (node as TreeNode<"table" | "view">).meta;if (!targetSupportsGenerateSQL(target)) {return;}- const schema = target.schema.name;- const tableOrViewName = tableOrViewNameForNode(node);+ const schema = target.schema;+ const tableOrViewName = readableTextForNodeTarget(type, target);if (!tableOrViewName) {return;}- const { db } = target;+ const { database } = target;+ const db = databaseStore.getDatabaseByName(database);const { engine } = db.instanceResource;const query = await formatCode(@@ -203,17 +213,11 @@ export const useActions = () => {return;}- const { schema } = target as NodeTarget<- | "table"- | "view"- | "procedure"- | "function"- >;-+ const { schema } = target as NodeTarget<"schema">;openNewTab({title: "View detail",view: typeToView(type),- schema: schema.name,+ schema: schema,});await nextTick();@@ -221,22 +225,22 @@ export const useActions = () => {let name = "";switch (type) {case "table":- name = (target as NodeTarget<"table">).table.name;+ name = (target as NodeTarget<"table">).table;detail.table = name;break;case "view":- name = (target as NodeTarget<"view">).view.name;+ name = (target as NodeTarget<"view">).view;detail.view = name;break;case "procedure":const { procedure, position } = target as NodeTarget<"procedure">;- name = procedure.name;- detail.procedure = keyWithPosition(procedure.name, position);+ name = procedure;+ detail.procedure = keyWithPosition(procedure, position);break;case "function":const { function: func, position: funcPosition } = target as NodeTarget<"function">;- name = func.name;- detail.func = keyWithPosition(func.name, funcPosition);+ name = func;+ detail.func = keyWithPosition(func, funcPosition);break}@@ -244,7 +248,7 @@ export const useActions = () => {detail,});if (name) {- useSQLEditorTabStore().updateCurrentTab({ title: `Detail for ${name}` })+ useSQLEditorTabStore().updateCurrentTab({ title: `Detail for ${type} ${name}` })}};@@ -253,6 +257,8 @@ export const useActions = () => {export const useDropdown = () => {const router = useRouter();+ const dbSchema = useDBSchemaV1Store();+ const databaseStore = useDatabaseV1Store();const { availableActions } = useCurrentTabViewStateContext();const { events: editorEvents, schemaViewer } = useSQLEditorContext();const { selectAllFromTableOrView, viewDetail, openNewTab } = useActions();@@ -277,26 +283,25 @@ export const useDropdown = () => {return [];}const { type, target } = node.meta;+ const { database, schema } = target as NodeTarget<"schema">;// Don't show any context menu actions for disabled nodes- if (node.disabled) {+ if (node.disabled || !database) {return [];}+ const db = databaseStore.getDatabaseByName(database);+const items: DropdownOptionWithTreeNode[] = [];- if (type === "database" || type === "schema") {+ if (type === "schema") {for (const action of availableActions.value) {items.push({key: action.view,label: action.title,icon: action.icon,onSelect: () => {- let schema: string | undefined;- if (type === "schema") {- schema = (node.meta.target as NodeTarget<"schema">).schema.name;- }openNewTab({- title: action.title,+ title: `[schema ${schema}] ${action.title}`,view: action.view,schema,})@@ -306,8 +311,7 @@ export const useDropdown = () => {}if (type === "table" || type === "view") {- const schema = (target as NodeTarget<"table" | "view">).schema.name;- const tableOrView = tableOrViewNameForNode(node);+ const tableOrView = readableTextForNodeTarget(type, target as NodeTarget<"table" | "view">);items.push({key: "copy-name",label: t("sql-editor.copy-name"),@@ -318,7 +322,7 @@ export const useDropdown = () => {},});if (type === 'view') {- const {db, schema, view} = target as NodeTarget<"view">;+ const { view } = target as NodeTarget<"view">;if (supportGetStringSchema(db.instanceResource.engine)) {items.push({key: "view-schema-text",@@ -326,9 +330,8 @@ export const useDropdown = () => {icon: () => <CodeIcon class="w-4 h-4" />,onSelect: () => {schemaViewer.value = {- database: db,- schema: schema.name,- object: view.name,+ schema,+ object: view,type: GetSchemaStringRequest_ObjectType.VIEW};},@@ -337,15 +340,16 @@ export const useDropdown = () => {}if (type === "table") {- const { db, schema, table } = target as NodeTarget<"table">;+ const { table } = target as NodeTarget<"table">;items.push({key: "copy-all-column-names",label: t("sql-editor.copy-all-column-names"),icon: () => <CopyIcon class="w-4 h-4" />,onSelect: () => {- const names = table.columns.map((col) => col.name).join(", ");- copyToClipboard(names);+ const tableMetadata = dbSchema.getTableMetadata({ database, schema, table })+ const name = tableMetadata.columns.map((col) => col.name).join(", ");+ copyToClipboard(name);},});@@ -356,9 +360,8 @@ export const useDropdown = () => {icon: () => <CodeIcon class="w-4 h-4" />,onSelect: () => {schemaViewer.value = {- database: db,- schema: schema.name,- object: table.name,+ schema,+ object: table,type: GetSchemaStringRequest_ObjectType.TABLE,};},@@ -374,8 +377,8 @@ export const useDropdown = () => {onSelect: () => {editorEvents.emit("alter-schema", {databaseName: db.name,- schema: schema.name,- table: table.name,+ schema: schema,+ table: table,});},});@@ -394,8 +397,8 @@ export const useDropdown = () => {database: db.databaseName,},query: {- table: table.name,- schema: schema.name,+ table: table,+ schema: schema,},});const url = new URL(route.href, window.location.origin).href;@@ -436,8 +439,10 @@ export const useDropdown = () => {},});if (type === "table") {- const { schema, table } = target as NodeTarget<"table">;- const columns = table.columns.map((column) => column.name);+ const { table } = target as NodeTarget<"table">;+ const tableMetadata = dbSchema.getTableMetadata({ database, schema, table })+ const columns = tableMetadata.columns.map((column) => column.name);+generateSQLChildren.push({key: "generate-sql--insert",label: "INSERT",@@ -446,8 +451,8 @@ export const useDropdown = () => {const statement = await formatCode(generateSimpleInsertStatement(engine,- schema.name,- table.name,+ schema,+ table,columns),engine@@ -463,8 +468,8 @@ export const useDropdown = () => {const statement = await formatCode(generateSimpleUpdateStatement(engine,- schema.name,- table.name,+ schema,+ table,columns),engine@@ -478,7 +483,7 @@ export const useDropdown = () => {icon: () => <FileMinusIcon class="w-4 h-4" />,onSelect: async () => {const statement = await formatCode(- generateSimpleDeleteStatement(engine, schema.name, table.name),+ generateSimpleDeleteStatement(engine, schema, table),engine);applyContentToCurrentTabOrCopyToClipboard(statement, $d);@@ -560,17 +565,9 @@ export const useDropdown = () => {};};-const tableOrViewNameForNode = (node: TreeNode) => {- const { type, target } = node.meta;- return type === "table"- ? (target as NodeTarget<"table">).table.name- : type === "view"- ? (target as NodeTarget<"view">).view.name- : "";-};-const engineForTarget = (target: NodeTarget) => {- return (target as NodeTarget<"database">).db.instanceResource.engine;+ const { database } = target as NodeTarget<"database">+ return useDatabaseV1Store().getDatabaseByName(database).instanceResource.engine;};const targetSupportsGenerateSQL = (target: NodeTarget) => {diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/click.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/click.tsnew file mode 100644index 0000000000..8598f2b0dc--- /dev/null+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/click.ts@@ -0,0 +1,42 @@+import Emittery from "emittery";+import { ref } from "vue";+import { type TreeNode } from "./tree";++export const useClickEvents = () => {+ const DELAY = 250;+ const state = ref<{+ timeout: ReturnType<typeof setTimeout>;+ node: TreeNode;+ }>();+ const events = new Emittery<{+ "single-click": { node: TreeNode };+ "double-click": { node: TreeNode };+ }>();++ const clear = () => {+ if (!state.value) return;+ clearTimeout(state.value.timeout);+ state.value = undefined;+ };+ const queue = (node: TreeNode) => {+ state.value = {+ timeout: setTimeout(() => {+ events.emit("single-click", { node });+ clear();+ }, DELAY),+ node,+ };+ };++ const handleClick = (node: TreeNode) => {+ if (state.value && state.value.node.key === node.key) {+ events.emit("double-click", { node });+ clear();+ return;+ }+ clear();+ queue(node);+ };++ return { events, handleClick };+};diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.tsdeleted file mode 100644index 43a7fbafd6..0000000000--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts+++ /dev/null@@ -1,923 +0,0 @@-import Emittery from "emittery";-import type { TreeOption } from "naive-ui";-import { ref, type RenderFunction } from "vue";-import { t } from "@/plugins/i18n";-import type { ComposedDatabase } from "@/types";-import type {- ColumnMetadata,- DatabaseMetadata,- DependencyColumn,- ExternalTableMetadata,- ForeignKeyMetadata,- FunctionMetadata,- IndexMetadata,- PackageMetadata,- ProcedureMetadata,- SchemaMetadata,- SequenceMetadata,- TableMetadata,- TablePartitionMetadata,- TriggerMetadata,- ViewMetadata,-} from "@/types/proto/v1/database_service";-import { keyForDependencyColumn } from "@/utils";-import { keyWithPosition } from "../../EditorCommon";--export type NodeType =- | "database"- | "schema"- | "table"- | "external-table"- | "view"- | "procedure"- | "function"- | "sequence"- | "trigger"- | "package"- | "partition-table"- | "column"- | "index"- | "foreign-key"- | "dependency-column"- | "expandable-text" // Text nodes to display "Tables / Views / Functions / Triggers" etc.- | "error"; // Error nodes to display "<Empty>" or "Cannot fetch ..." etc.--export type RichDatabaseMetadata = {- db: ComposedDatabase;- database: DatabaseMetadata;-};-export type RichSchemaMetadata = RichDatabaseMetadata & {- schema: SchemaMetadata;-};-export type RichTableMetadata = RichSchemaMetadata & {- table: TableMetadata;-};-export type RichExternalTableMetadata = RichSchemaMetadata & {- externalTable: ExternalTableMetadata;-};-export type RichColumnMetadata = (- | RichTableMetadata- | RichExternalTableMetadata- | RichViewMetadata-) & {- column: ColumnMetadata;-};-export type RichDependencyColumnMetadata = RichViewMetadata & {- dependencyColumn: DependencyColumn;-};-export type RichIndexMetadata = RichTableMetadata & {- index: IndexMetadata;-};-export type RichForeignKeyMetadata = RichTableMetadata & {- foreignKey: ForeignKeyMetadata;-};-export type RichPartitionTableMetadata = RichTableMetadata & {- parentPartition?: TablePartitionMetadata;- partition: TablePartitionMetadata;-};-export type RichTriggerMetadata = RichTableMetadata & {- trigger: TriggerMetadata;- position: number;-};-export type RichViewMetadata = RichSchemaMetadata & {- view: ViewMetadata;-};-export type RichSequenceMetadata = RichSchemaMetadata & {- sequence: SequenceMetadata;- position: number;-};-export type RichProcedureMetadata = RichSchemaMetadata & {- procedure: ProcedureMetadata;- position: number;-};-export type RichPackageMetadata = RichSchemaMetadata & {- package: PackageMetadata;- position: number;-};-export type RichFunctionMetadata = RichSchemaMetadata & {- function: FunctionMetadata;- position: number;-};-export type TextTarget<E extends boolean = any, S extends boolean = any> = {- expandable: E;- id: string;- mockType?: NodeType; // mock as a node type if needed- text: () => string;- render?: RenderFunction;- searchable?: S;-};-export type ErrorTarget = TextTarget<false, false> & {- error?: unknown;-};--export type NodeTarget<T extends NodeType = NodeType> = T extends "database"- ? RichDatabaseMetadata- : T extends "schema"- ? RichSchemaMetadata- : T extends "table"- ? RichTableMetadata- : T extends "external-table"- ? RichExternalTableMetadata- : T extends "column"- ? RichColumnMetadata- : T extends "dependency-column"- ? RichDependencyColumnMetadata- : T extends "index"- ? RichIndexMetadata- : T extends "foreign-key"- ? RichForeignKeyMetadata- : T extends "partition-table"- ? RichPartitionTableMetadata- : T extends "view"- ? RichViewMetadata- : T extends "procedure"- ? RichProcedureMetadata- : T extends "package"- ? RichPackageMetadata- : T extends "function"- ? RichFunctionMetadata- : T extends "sequence"- ? RichSequenceMetadata- : T extends "trigger"- ? RichTriggerMetadata- : T extends "expandable-text"- ? TextTarget<true, any>- : T extends "error"- ? ErrorTarget- : never;--export type TreeState = "UNSET" | "LOADING" | "READY";--export type NodeMeta<T extends NodeType = NodeType> = {- type: T;- target: NodeTarget<T>;-};--export type TreeNode<T extends NodeType = NodeType> = TreeOption & {- key: string;- meta: NodeMeta<T>;- parent?: TreeNode;- children?: TreeNode[];-};--export const ExpandableNodeTypes: readonly NodeType[] = [- "schema",- "table",- "view",- "external-table",- "partition-table",- "expandable-text",-] as const;-export const LeafNodeTypes: readonly NodeType[] = [- "column",- "index",- "foreign-key",- "procedure",- "package",- "function",- "sequence",- "trigger",- "dependency-column",- "error",-] as const;--export const keyForNodeTarget = <T extends NodeType>(- type: T,- target: NodeTarget<T>-): string => {- if (type === "database") {- const { db } = target as NodeTarget<"database">;- return db.name;- }- if (type === "schema") {- const { db, schema } = target as NodeTarget<"schema">;- return [db.name, `schemas/${schema.name}`].join("/");- }- if (type === "table") {- const { db, schema, table } = target as NodeTarget<"table">;- return [db.name, `schemas/${schema.name}`, `tables/${table.name}`].join(- "/"- );- }- if (type === "external-table") {- const { db, schema, externalTable } =- target as NodeTarget<"external-table">;- return [- db.name,- `schemas/${schema.name}`,- `externalTables/${externalTable.name}`,- ].join("/");- }- if (type === "column") {- const parentKey =- "table" in target- ? keyForNodeTarget("table", target as NodeTarget<"table">)- : "external-table" in target- ? keyForNodeTarget(- "external-table",- target as NodeTarget<"external-table">- )- : "view" in target- ? keyForNodeTarget("view", target as NodeTarget<"view">)- : ""; // Fall back to empty string.- const { column } = target as NodeTarget<"column">;- return [parentKey, `columns/${column.name}`].join("/");- }- if (type === "dependency-column") {- const { db, schema, view, dependencyColumn } =- target as NodeTarget<"dependency-column">;- return [- db.name,- `schemas/${schema.name}`,- `views/${view.name}`,- `dependencyColumns/${keyForDependencyColumn(dependencyColumn)}`,- ].join("/");- }- if (type === "index") {- const { db, schema, table, index } = target as NodeTarget<"index">;- return [- db.name,- `schemas/${schema.name}`,- `tables/${table.name}`,- `indexes/${index.name}`,- ].join("/");- }- if (type === "foreign-key") {- const { db, schema, table, foreignKey } =- target as NodeTarget<"foreign-key">;- return [- db.name,- `schemas/${schema.name}`,- `tables/${table}`,- `foreignKeys/${foreignKey.name}`,- ].join("/");- }- if (type === "partition-table") {- const { db, schema, table, partition } =- target as NodeTarget<"partition-table">;- return [- db.name,- `schemas/${schema.name}`,- `tables/${table.name}`,- `partitionTables/${partition.name}`,- ].join("/");- }- if (type === "view") {- const { db, schema, view } = target as NodeTarget<"view">;- return [db.name, `schemas/${schema.name}`, `views/${view.name}`].join("/");- }- if (type === "procedure") {- const { db, schema, procedure, position } =- target as NodeTarget<"procedure">;- return [- db.name,- `schemas/${schema.name}`,- `procedures/${keyWithPosition(procedure.name, position)}`,- ].join("/");- }- if (type === "package") {- const {- db,- schema,- package: pack,- position,- } = target as NodeTarget<"package">;- return [- db.name,- `schemas/${schema.name}`,- `packages/${keyWithPosition(pack.name, position)}`,- ].join("/");- }- if (type === "function") {- const {- db,- schema,- function: func,- position,- } = target as NodeTarget<"function">;- return [- db.name,- `schemas/${schema.name}`,- `functions/${keyWithPosition(func.name, position)}`,- ].join("/");- }- if (type === "sequence") {- const { db, schema, sequence, position } = target as NodeTarget<"sequence">;- return [- db.name,- `schemas/${schema.name}`,- `sequences/${keyWithPosition(sequence.name, position)}`,- ].join("/");- }- if (type === "trigger") {- const { db, schema, table, trigger, position } =- target as NodeTarget<"trigger">;- return [- db.name,- `schemas/${schema.name}`,- `tables/${table.name}`,- `triggers/${keyWithPosition(trigger.name, position)}`,- ].join("/");- }- if (type === "expandable-text") {- const { id } = target as NodeTarget<"expandable-text">;- return `expandableTexts/${id}`;- }- if (type === "error") {- const { id } = target as NodeTarget<"error">;- return `errors/${id}`;- }- console.assert(false, "should never reach this line");- return "";-};--const readableTextForNodeTarget = <T extends NodeType>(- type: T,- target: NodeTarget<T>-): string => {- if (type === "database") {- return (target as NodeTarget<"database">).db.databaseName;- }- if (type === "schema") {- return (target as RichSchemaMetadata).schema.name;- }- if (type === "table") {- return (target as RichTableMetadata).table.name;- }- if (type === "external-table") {- return (target as RichExternalTableMetadata).externalTable.name;- }- if (type === "column") {- return (target as RichColumnMetadata).column.name;- }- if (type === "index") {- return (target as RichIndexMetadata).index.name;- }- if (type === "foreign-key") {- return (target as RichForeignKeyMetadata).foreignKey.name;- }- if (type === "partition-table") {- return (target as RichPartitionTableMetadata).partition.name;- }- if (type === "view") {- return (target as RichViewMetadata).view.name;- }- if (type === "dependency-column") {- const dep = (target as RichDependencyColumnMetadata).dependencyColumn;- const parts = [dep.table, dep.column];- if (dep.schema) parts.unshift(dep.schema);- return parts.join(".");- }- if (type === "procedure") {- return (target as RichProcedureMetadata).procedure.name;- }- if (type === "package") {- return (target as RichPackageMetadata).package.name;- }- if (type === "function") {- return (target as RichFunctionMetadata).function.name;- }- if (type === "sequence") {- return (target as RichSequenceMetadata).sequence.name;- }- if (type === "trigger") {- return (target as RichTriggerMetadata).trigger.name;- }- if (type === "expandable-text") {- const { text, searchable } = target as TextTarget;- if (!searchable) return "";- return text();- }- if (type === "error") {- // Use empty strings for error nodes to make them unsearchable- return "";- }- console.assert(false, "should never reach this line");- return "";-};--const isLeafNodeType = (type: NodeType) => {- return LeafNodeTypes.includes(type);-};--export const mapTreeNodeByType = <T extends NodeType>(- type: T,- target: NodeTarget<T>,- parent: TreeNode | undefined,- overrides: Partial<TreeNode<T>> | undefined = undefined-): TreeNode<T> => {- const key = keyForNodeTarget(type, target);- const node: TreeNode<T> = {- key,- meta: { type, target },- parent,- label: readableTextForNodeTarget(type, target),- isLeaf: isLeafNodeType(type),- ...overrides,- };-- return node;-};--const createDummyNode = (- type:- | "column"- | "dependency-column"- | "index"- | "foreign-key"- | "table"- | "view"- | "procedure"- | "package"- | "function",- parent: TreeNode,- key: string | number = 0,- error: unknown | undefined = undefined-) => {- return mapTreeNodeByType(- "error",- {- id: `${parent.key}/dummy-${type}-${key}`,- expandable: false,- mockType: type,- error,- text: () => "",- },- parent,- {- disabled: true,- }- );-};-const createExpandableTextNode = (- type: NodeType,- parent: TreeNode,- text: () => string,- render?: RenderFunction-) => {- return mapTreeNodeByType(- "expandable-text",- {- id: `${parent.key}/${type}`,- mockType: type,- expandable: true,- text,- render,- },- parent- );-};-const mapColumnNodes = (- target:- | NodeTarget<"table">- | NodeTarget<"external-table">- | NodeTarget<"view">,- columns: ColumnMetadata[],- parent: TreeNode-) => {- if (columns.length === 0) {- // Create a "<Empty>" columns node placeholder- return [createDummyNode("column", parent)];- }-- const children = columns.map((column) => {- const node = mapTreeNodeByType("column", { ...target, column }, parent);- return node;- });- return children;-};-const mapIndexNodes = (- target: NodeTarget<"table">,- indexes: IndexMetadata[],- parent: TreeNode-) => {- if (indexes.length === 0) {- // Create a "<Empty>" index node placeholder- return [createDummyNode("index", parent)];- }-- const children = indexes.map((index) => {- const node = mapTreeNodeByType("index", { ...target, index }, parent);- return node;- });- return children;-};-const mapForeignKeyNodes = (- target: NodeTarget<"table">,- foreignKeys: ForeignKeyMetadata[],- parent: TreeNode-) => {- if (foreignKeys.length === 0) {- // Create a "<Empty>" foreignKey node placeholder- return [createDummyNode("foreign-key", parent)];- }-- const children = foreignKeys.map((foreignKey) => {- const node = mapTreeNodeByType(- "foreign-key",- { ...target, foreignKey },- parent- );- return node;- });- return children;-};-const mapTableNodes = (target: NodeTarget<"schema">, parent: TreeNode) => {- const { schema } = target;- const children = schema.tables.map((table) => {- const node = mapTreeNodeByType("table", { ...target, table }, parent);- const columnsFolderNode = createExpandableTextNode("column", node, () =>- t("database.columns")- );- node.children = [columnsFolderNode];- // Map column columns- columnsFolderNode.children = mapColumnNodes(- node.meta.target,- table.columns,- columnsFolderNode- );-- // Map indexes- if (table.indexes.length > 0) {- const indexesFolderNode = createExpandableTextNode("index", node, () =>- t("database.indexes")- );- indexesFolderNode.children = mapIndexNodes(- node.meta.target,- table.indexes,- indexesFolderNode- );- node.children.push(indexesFolderNode);- }-- // Map foreign keys- if (table.foreignKeys.length > 0) {- const foreignKeysFolderNode = createExpandableTextNode(- "foreign-key",- node,- () => t("database.foreign-keys")- );- foreignKeysFolderNode.children = mapForeignKeyNodes(- node.meta.target,- table.foreignKeys,- foreignKeysFolderNode- );- node.children.push(foreignKeysFolderNode);- }-- // Show "Triggers" if there's at least 1 function- if (table.triggers.length > 0) {- const triggerNode = createExpandableTextNode("trigger", parent, () =>- t("db.triggers")- );- triggerNode.children = mapTriggerNodes(node.meta.target, triggerNode);- node.children.push(triggerNode);- }-- // Map table-level partitions.- if (table.partitions.length > 0) {- const partitionsFolderNode = createExpandableTextNode(- "partition-table",- node,- () => t("db.partitions")- );- partitionsFolderNode.children = [];- for (const partition of table.partitions) {- const subnode = mapTreeNodeByType(- "partition-table",- { ...node.meta.target, partition },- node- );- if (partition.subpartitions.length > 0) {- subnode.isLeaf = false;- subnode.children = mapPartitionTableNodes(partition, subnode);- } else {- subnode.isLeaf = true;- }- partitionsFolderNode.children?.push(subnode);- }- node.children?.push(partitionsFolderNode);- }- return node;- });- if (children.length === 0) {- return [createDummyNode("table", parent)];- }- return children;-};-const mapExternalTableNodes = (- target: NodeTarget<"schema">,- parent: TreeNode-) => {- const { schema } = target;- const externalTableNodes = schema.externalTables.map((externalTable) => {- const node = mapTreeNodeByType(- "external-table",- { ...target, externalTable },- parent- );- const folderNode = createExpandableTextNode("column", node, () =>- t("database.columns")- );- node.children = [folderNode];-- // columns- folderNode.children = mapColumnNodes(- node.meta.target,- externalTable.columns,- folderNode- );-- return node;- });- return externalTableNodes;-};-// Map partition-table-level partitions.-const mapPartitionTableNodes = (- parentPartition: TablePartitionMetadata,- parent: TreeNode<"partition-table">-) => {- const children = parentPartition.subpartitions.map((partition) => {- const node = mapTreeNodeByType(- "partition-table",- {- ...parent.meta.target,- parentPartition,- partition,- },- parent- );- if (partition.subpartitions.length > 0) {- node.isLeaf = false;- node.children = mapPartitionTableNodes(partition, node);- } else {- node.isLeaf = true;- }- return node;- });- return children;-};-const mapViewNodes = (- target: NodeTarget<"schema">,- parent: TreeNode<"expandable-text">-) => {- const { schema } = target;- const children = schema.views.map((view) => {- const viewNode = mapTreeNodeByType("view", { ...target, view }, parent);- const columnsFolderNode = createExpandableTextNode("column", viewNode, () =>- t("database.columns")- );- viewNode.children = [columnsFolderNode];- // Map column columns- columnsFolderNode.children = mapColumnNodes(- viewNode.meta.target,- view.columns,- columnsFolderNode- );- return viewNode;- });- if (children.length === 0) {- return [createDummyNode("view", parent)];- }- return children;-};-const mapProcedureNodes = (- target: NodeTarget<"schema">,- parent: TreeNode<"expandable-text">-) => {- const { schema } = target;- const children = schema.procedures.map((procedure, position) =>- mapTreeNodeByType("procedure", { ...target, procedure, position }, parent)- );- if (children.length === 0) {- return [createDummyNode("procedure", parent)];- }- return children;-};-const mapPackageNodes = (- target: NodeTarget<"schema">,- parent: TreeNode<"expandable-text">-) => {- const { schema } = target;- const children = schema.packages.map((pack, position) =>- mapTreeNodeByType("package", { ...target, package: pack, position }, parent)- );- if (children.length === 0) {- return [createDummyNode("package", parent)];- }- return children;-};-const mapFunctionNodes = (- target: NodeTarget<"schema">,- parent: TreeNode<"expandable-text">-) => {- const { schema } = target;- const children = schema.functions.map((func, position) =>- mapTreeNodeByType(- "function",- { ...target, function: func, position },- parent- )- );- if (children.length === 0) {- return [createDummyNode("function", parent)];- }- return children;-};-const mapSequenceNodes = (- target: NodeTarget<"schema">,- parent: TreeNode<"expandable-text">-) => {- const { schema } = target;- const children = schema.sequences.map((sequence, position) =>- mapTreeNodeByType("sequence", { ...target, sequence, position }, parent)- );- if (children.length === 0) {- return [createDummyNode("function", parent)];- }- return children;-};-const mapTriggerNodes = (- target: NodeTarget<"table">,- parent: TreeNode<"expandable-text">-) => {- const { table } = target;- const children = table.triggers.map((trigger, position) =>- mapTreeNodeByType("trigger", { ...target, trigger, position }, parent)- );- if (children.length === 0) {- return [createDummyNode("function", parent)];- }- return children;-};-const buildSchemaNodeChildren = (- target: NodeTarget<"schema">,- parent: TreeNode<"schema"> | TreeNode<"database">-) => {- const { schema } = target;- if (- schema.tables.length === 0 &&- schema.externalTables.length === 0 &&- schema.views.length === 0 &&- schema.procedures.length === 0 &&- schema.functions.length === 0- ) {- return [createDummyNode("table", parent)];- }-- const children: TreeNode[] = [];-- // Always show "Tables" node- // If no tables, show "<Empty>"- const tablesNode = createExpandableTextNode("table", parent, () =>- t("db.tables")- );- tablesNode.children = mapTableNodes(target, tablesNode);- children.push(tablesNode);-- // Only show "External Tables" node if the schema do have external tables.- if (schema.externalTables.length > 0) {- const externalTablesNode = createExpandableTextNode(- "external-table",- parent,- () => t("db.external-tables")- );- externalTablesNode.children = mapExternalTableNodes(- target,- externalTablesNode- );- children.push(externalTablesNode);- }-- // Only show "Views" node if the schema do have views.- if (schema.views.length > 0) {- const viewsNode = createExpandableTextNode("view", parent, () =>- t("db.views")- );- viewsNode.children = mapViewNodes(target, viewsNode);- children.push(viewsNode);- }-- // Show "Procedures" if there's at least 1 procedure- if (schema.procedures.length > 0) {- const procedureNode = createExpandableTextNode("procedure", parent, () =>- t("db.procedures")- );- procedureNode.children = mapProcedureNodes(target, procedureNode);- children.push(procedureNode);- }-- // Show "Packages" if there's at least 1 package- if (schema.packages.length > 0) {- const packageNode = createExpandableTextNode("package", parent, () =>- t("db.packages")- );- packageNode.children = mapPackageNodes(target, packageNode);- children.push(packageNode);- }-- // Show "Functions" if there's at least 1 function- if (schema.functions.length > 0) {- const functionNode = createExpandableTextNode("function", parent, () =>- t("db.functions")- );- functionNode.children = mapFunctionNodes(target, functionNode);- children.push(functionNode);- }-- // Show "Sequences" if there's at least 1 function- if (schema.sequences.length > 0) {- const sequenceNode = createExpandableTextNode("sequence", parent, () =>- t("db.sequences")- );- sequenceNode.children = mapSequenceNodes(target, sequenceNode);- children.push(sequenceNode);- }-- return children;-};-export const buildDatabaseSchemaTree = (- database: ComposedDatabase,- metadata: DatabaseMetadata-) => {- const dummyRoot = mapTreeNodeByType(- "database",- {- db: database,- database: metadata,- },- /* parent */ undefined- );- const { schemas } = metadata;- if (schemas.length === 0) {- // Empty database, show "<Empty>"- return [createDummyNode("table", dummyRoot)];- }-- if (schemas.length === 1 && schemas[0].name === "") {- const schema = schemas[0];- // A single schema database, should render tables as views directly as a database- // node's children- return buildSchemaNodeChildren(- { ...dummyRoot.meta.target, schema },- dummyRoot- );- } else {- // Multiple schema database- return schemas.map((schema) => {- const schemaNode = mapTreeNodeByType(- "schema",- { ...dummyRoot.meta.target, schema },- dummyRoot- );-- schemaNode.children = buildSchemaNodeChildren(- schemaNode.meta.target,- schemaNode- );- return schemaNode;- });- }-- console.error("should never reach this line");- return [];-};--export const useClickEvents = () => {- const DELAY = 250;- const state = ref<{- timeout: ReturnType<typeof setTimeout>;- node: TreeNode;- }>();- const events = new Emittery<{- "single-click": { node: TreeNode };- "double-click": { node: TreeNode };- }>();-- const clear = () => {- if (!state.value) return;- clearTimeout(state.value.timeout);- state.value = undefined;- };- const queue = (node: TreeNode) => {- state.value = {- timeout: setTimeout(() => {- events.emit("single-click", { node });- clear();- }, DELAY),- node,- };- };-- const handleClick = (node: TreeNode) => {- if (state.value && state.value.node.key === node.key) {- events.emit("double-click", { node });- clear();- return;- }- clear();- queue(node);- };-- return { events, handleClick };-};diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/tree.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/tree.tsnew file mode 100644index 0000000000..dbe178cfd9--- /dev/null+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/tree.ts@@ -0,0 +1,814 @@+import type { TreeOption } from "naive-ui";+import { type RenderFunction } from "vue";+import { t } from "@/plugins/i18n";+import type { ComposedDatabase } from "@/types";+import type {+ ColumnMetadata,+ DatabaseMetadata,+ DependencyColumn,+ ForeignKeyMetadata,+ IndexMetadata,+ SchemaMetadata,+ TableMetadata,+ TablePartitionMetadata,+} from "@/types/proto/v1/database_service";+import { keyForDependencyColumn } from "@/utils";+import { keyWithPosition } from "../../EditorCommon";++export type NodeType =+ | "database"+ | "schema"+ | "table"+ | "external-table"+ | "view"+ | "procedure"+ | "function"+ | "sequence"+ | "trigger"+ | "package"+ | "partition-table"+ | "column"+ | "index"+ | "foreign-key"+ | "dependency-column"+ | "expandable-text" // Text nodes to display "Tables / Views / Functions / Triggers" etc.+ | "error"; // Error nodes to display "<Empty>" or "Cannot fetch ..." etc.++export type RichDatabaseMetadata = {+ database: string;+};+export type RichSchemaMetadata = RichDatabaseMetadata & {+ schema: string;+};+export type RichTableMetadata = RichSchemaMetadata & {+ table: string;+};+export type RichExternalTableMetadata = RichSchemaMetadata & {+ externalTable: string;+};+export type RichColumnMetadata = (+ | RichTableMetadata+ | RichExternalTableMetadata+ | RichViewMetadata+) & {+ column: string;+};+export type RichDependencyColumnMetadata = RichViewMetadata & {+ dependencyColumn: DependencyColumn;+};+export type RichIndexMetadata = RichTableMetadata & {+ index: string;+};+export type RichForeignKeyMetadata = RichTableMetadata & {+ foreignKey: string;+};+export type RichPartitionTableMetadata = RichTableMetadata & {+ partition: string;+};+export type RichTriggerMetadata = RichTableMetadata & {+ trigger: string;+ position: number;+};+export type RichViewMetadata = RichSchemaMetadata & {+ view: string;+};+export type RichSequenceMetadata = RichSchemaMetadata & {+ sequence: string;+ position: number;+};+export type RichProcedureMetadata = RichSchemaMetadata & {+ procedure: string;+ position: number;+};+export type RichPackageMetadata = RichSchemaMetadata & {+ package: string;+ position: number;+};+export type RichFunctionMetadata = RichSchemaMetadata & {+ function: string;+ position: number;+};+export type TextTarget<E extends boolean = any, S extends boolean = any> = {+ expandable: E;+ id: string;+ mockType?: NodeType; // mock as a node type if needed+ text: () => string;+ render?: RenderFunction;+ searchable?: S;+};+export type ErrorTarget = TextTarget<false, false> & {+ error?: unknown;+};++export type NodeTarget<T extends NodeType = NodeType> = T extends "database"+ ? RichDatabaseMetadata+ : T extends "schema"+ ? RichSchemaMetadata+ : T extends "table"+ ? RichTableMetadata+ : T extends "external-table"+ ? RichExternalTableMetadata+ : T extends "column"+ ? RichColumnMetadata+ : T extends "dependency-column"+ ? RichDependencyColumnMetadata+ : T extends "index"+ ? RichIndexMetadata+ : T extends "foreign-key"+ ? RichForeignKeyMetadata+ : T extends "partition-table"+ ? RichPartitionTableMetadata+ : T extends "view"+ ? RichViewMetadata+ : T extends "procedure"+ ? RichProcedureMetadata+ : T extends "package"+ ? RichPackageMetadata+ : T extends "function"+ ? RichFunctionMetadata+ : T extends "sequence"+ ? RichSequenceMetadata+ : T extends "trigger"+ ? RichTriggerMetadata+ : T extends "expandable-text"+ ? TextTarget<true, any>+ : T extends "error"+ ? ErrorTarget+ : never;++export type TreeState = "UNSET" | "LOADING" | "READY";++export type NodeMeta<T extends NodeType = NodeType> = {+ type: T;+ target: NodeTarget<T>;+};++export type TreeNode<T extends NodeType = NodeType> = TreeOption & {+ key: string;+ meta: NodeMeta<T>;+ children?: TreeNode[];+};++export const ExpandableNodeTypes: readonly NodeType[] = [+ "schema",+ "table",+ "view",+ "external-table",+ "partition-table",+ "expandable-text",+] as const;++export const LeafNodeTypes: readonly NodeType[] = [+ "column",+ "index",+ "foreign-key",+ "procedure",+ "package",+ "function",+ "sequence",+ "trigger",+ "dependency-column",+ "error",+] as const;++export const keyForNodeTarget = <T extends NodeType>(+ type: T,+ target: NodeTarget<T>+): string => {+ const { database } = target as NodeTarget<"database">;++ switch (type) {+ case "database":+ return database;+ case "schema":+ const { schema } = target as NodeTarget<"schema">;+ return [database, `schemas/${schema}`].join("/");+ case "table":+ const { table } = target as NodeTarget<"table">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `tables/${table}`,+ ].join("/");+ case "external-table":+ const { externalTable } = target as NodeTarget<"external-table">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `externalTables/${externalTable}`,+ ].join("/");+ case "view":+ const { view } = target as NodeTarget<"view">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `views/${view}`,+ ].join("/");+ case "procedure":+ const { procedure, position } = target as NodeTarget<"procedure">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `procedures/${keyWithPosition(procedure, position)}`,+ ].join("/");+ case "package":+ const { package: pack, position: pos } = target as NodeTarget<"package">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `packages/${keyWithPosition(pack, pos)}`,+ ].join("/");+ case "function":+ const { function: func, position: fp } = target as NodeTarget<"function">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `functions/${keyWithPosition(func, fp)}`,+ ].join("/");+ case "sequence":+ const { sequence, position: sp } = target as NodeTarget<"sequence">;+ return [+ keyForNodeTarget("schema", target as NodeTarget<"schema">),+ `sequences/${keyWithPosition(sequence, sp)}`,+ ].join("/");+ case "trigger":+ const { trigger, position: tp } = target as NodeTarget<"trigger">;+ return [+ keyForNodeTarget("table", target as NodeTarget<"table">),+ `triggers/${keyWithPosition(trigger, tp)}`,+ ].join("/");+ case "index":+ const { index } = target as NodeTarget<"index">;+ return [+ keyForNodeTarget("table", target as NodeTarget<"table">),+ `indexes/${index}`,+ ].join("/");+ case "foreign-key":+ const { foreignKey } = target as NodeTarget<"foreign-key">;+ return [+ keyForNodeTarget("table", target as NodeTarget<"table">),+ `foreignKeys/${foreignKey}`,+ ].join("/");+ case "partition-table":+ const { partition } = target as NodeTarget<"partition-table">;+ return [+ keyForNodeTarget("table", target as NodeTarget<"table">),+ `partitionTables/${partition}`,+ ].join("/");+ case "dependency-column":+ const { dependencyColumn } = target as NodeTarget<"dependency-column">;+ return [+ keyForNodeTarget("view", target as NodeTarget<"view">),+ `dependencyColumns/${keyForDependencyColumn(dependencyColumn)}`,+ ].join("/");+ case "column":+ const { column } = target as NodeTarget<"column">;+ const key = `columns/${column}`;+ if ("table" in target) {+ return [+ keyForNodeTarget("table", target as NodeTarget<"table">),+ key,+ ].join("/");+ } else if ("external-table" in target) {+ return [+ keyForNodeTarget(+ "external-table",+ target as NodeTarget<"external-table">+ ),+ key,+ ].join("/");+ } else if ("view" in target) {+ return [+ keyForNodeTarget("view", target as NodeTarget<"view">),+ key,+ ].join("/");+ }+ return "";+ default:+ const { id } = target as NodeTarget<"expandable-text" | "error">;+ return `${type}/${id}`;+ }+};++export const readableTextForNodeTarget = <T extends NodeType>(+ type: T,+ target: NodeTarget<T>+): string => {+ switch (type) {+ case "error":+ // Use empty strings for error nodes to make them unsearchable+ return "";+ case "expandable-text":+ const { text, searchable } = target as TextTarget;+ if (!searchable) return "";+ return text();+ case "dependency-column":+ const dep = (target as NodeTarget<"dependency-column">).dependencyColumn;+ const parts = [dep.table, dep.column];+ if (dep.schema) parts.unshift(dep.schema);+ return parts.join(".");+ default:+ const key = keyForNodeTarget(type, target);+ return key.split("/").slice(-1)[0];+ }+};++const isLeafNodeType = (type: NodeType) => {+ return LeafNodeTypes.includes(type);+};++export const mapTreeNodeByType = <T extends NodeType>(+ type: T,+ target: NodeTarget<T>,+ overrides: Partial<TreeNode<T>> | undefined = undefined+): TreeNode<T> => {+ const key = keyForNodeTarget(type, target);+ const node: TreeNode<T> = {+ key,+ meta: { type, target },+ label: readableTextForNodeTarget(type, target),+ isLeaf: isLeafNodeType(type),+ ...overrides,+ };++ return node;+};++const createDummyNode = <T extends NodeType>(+ type: T,+ parentKey: string,+ key: string | number = 0+) => {+ return mapTreeNodeByType(+ "error",+ {+ id: `${parentKey}/dummy-${type}-${key}`,+ expandable: false,+ mockType: type,+ text: () => "",+ },+ {+ disabled: true,+ }+ );+};++const createExpandableTextNode = (+ type: NodeType,+ parentKey: string,+ text: () => string,+ render?: RenderFunction+) => {+ return mapTreeNodeByType("expandable-text", {+ id: `${parentKey}/${type}`,+ mockType: type,+ expandable: true,+ text,+ render,+ });+};++const mapColumnNodes = (+ target:+ | NodeTarget<"table">+ | NodeTarget<"external-table">+ | NodeTarget<"view">,+ columns: ColumnMetadata[],+ parentKey: string+) => {+ if (columns.length === 0) {+ // Create a "<Empty>" columns node placeholder+ return [createDummyNode("column", parentKey)];+ }++ const children = columns.map((column) => {+ const node = mapTreeNodeByType("column", {+ ...target,+ column: column.name,+ });+ return node;+ });+ return children;+};++const mapIndexNodes = (+ target: NodeTarget<"table">,+ indexes: IndexMetadata[],+ parentKey: string+) => {+ if (indexes.length === 0) {+ // Create a "<Empty>" index node placeholder+ return [createDummyNode("index", parentKey)];+ }++ const children = indexes.map((index) => {+ const node = mapTreeNodeByType("index", { ...target, index: index.name });+ return node;+ });+ return children;+};++const mapForeignKeyNodes = (+ target: NodeTarget<"table">,+ foreignKeys: ForeignKeyMetadata[],+ parentKey: string+) => {+ if (foreignKeys.length === 0) {+ // Create a "<Empty>" foreignKey node placeholder+ return [createDummyNode("foreign-key", parentKey)];+ }++ const children = foreignKeys.map((foreignKey) => {+ const node = mapTreeNodeByType("foreign-key", {+ ...target,+ foreignKey: foreignKey.name,+ });+ return node;+ });+ return children;+};++const mapTableNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.tables.map((table) => {+ const node = mapTreeNodeByType("table", {+ ...target,+ table: table.name,+ });+ const columnsFolderNode = createExpandableTextNode(+ "column",+ parentKey,+ () => t("database.columns")+ );+ node.children = [columnsFolderNode];+ // Map column columns+ columnsFolderNode.children = mapColumnNodes(+ node.meta.target,+ table.columns,+ columnsFolderNode.key+ );++ // Map indexes+ if (table.indexes.length > 0) {+ const indexesFolderNode = createExpandableTextNode(+ "index",+ parentKey,+ () => t("database.indexes")+ );+ indexesFolderNode.children = mapIndexNodes(+ node.meta.target,+ table.indexes,+ indexesFolderNode.key+ );+ node.children.push(indexesFolderNode);+ }++ // Map foreign keys+ if (table.foreignKeys.length > 0) {+ const foreignKeysFolderNode = createExpandableTextNode(+ "foreign-key",+ parentKey,+ () => t("database.foreign-keys")+ );+ foreignKeysFolderNode.children = mapForeignKeyNodes(+ node.meta.target,+ table.foreignKeys,+ foreignKeysFolderNode.key+ );+ node.children.push(foreignKeysFolderNode);+ }++ // Show "Triggers" if there's at least 1 function+ if (table.triggers.length > 0) {+ const triggerNode = createExpandableTextNode("trigger", parentKey, () =>+ t("db.triggers")+ );+ triggerNode.children = mapTriggerNodes(+ table,+ node.meta.target,+ triggerNode.key+ );+ node.children.push(triggerNode);+ }++ // Map table-level partitions.+ if (table.partitions.length > 0) {+ const partitionsFolderNode = createExpandableTextNode(+ "partition-table",+ parentKey,+ () => t("db.partitions")+ );+ partitionsFolderNode.children = [];+ for (const partition of table.partitions) {+ const subnode = mapTreeNodeByType("partition-table", {+ ...node.meta.target,+ partition: partition.name,+ });+ if (partition.subpartitions.length > 0) {+ subnode.isLeaf = false;+ subnode.children = mapPartitionTableNodes(+ partition,+ subnode.meta.target+ );+ } else {+ subnode.isLeaf = true;+ }+ partitionsFolderNode.children?.push(subnode);+ }+ node.children?.push(partitionsFolderNode);+ }+ return node;+ });+ if (children.length === 0) {+ return [createDummyNode("table", parentKey)];+ }+ return children;+};++const mapExternalTableNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">+) => {+ const externalTableNodes = schema.externalTables.map((externalTable) => {+ const node = mapTreeNodeByType("external-table", {+ ...target,+ externalTable: externalTable.name,+ });+ const folderNode = createExpandableTextNode("column", node.key, () =>+ t("database.columns")+ );+ node.children = [folderNode];++ // columns+ folderNode.children = mapColumnNodes(+ node.meta.target,+ externalTable.columns,+ folderNode.key+ );++ return node;+ });+ return externalTableNodes;+};++// Map partition-table-level partitions.+const mapPartitionTableNodes = (+ parentPartition: TablePartitionMetadata,+ target: RichPartitionTableMetadata+) => {+ const children = parentPartition.subpartitions.map((partition) => {+ const node = mapTreeNodeByType("partition-table", {+ ...target,+ partition: partition.name,+ });+ if (partition.subpartitions.length > 0) {+ node.isLeaf = false;+ node.children = mapPartitionTableNodes(partition, node.meta.target);+ } else {+ node.isLeaf = true;+ }+ return node;+ });+ return children;+};++const mapViewNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.views.map((view) => {+ const viewNode = mapTreeNodeByType("view", {+ ...target,+ view: view.name,+ });+ const columnsFolderNode = createExpandableTextNode(+ "column",+ viewNode.key,+ () => t("database.columns")+ );+ viewNode.children = [columnsFolderNode];+ // Map column columns+ columnsFolderNode.children = mapColumnNodes(+ viewNode.meta.target,+ view.columns,+ columnsFolderNode.key+ );+ return viewNode;+ });+ if (children.length === 0) {+ return [createDummyNode("view", parentKey)];+ }+ return children;+};++const mapProcedureNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.procedures.map((procedure, position) =>+ mapTreeNodeByType("procedure", {+ ...target,+ procedure: procedure.name,+ position,+ })+ );+ if (children.length === 0) {+ return [createDummyNode("procedure", parentKey)];+ }+ return children;+};++const mapPackageNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.packages.map((pack, position) =>+ mapTreeNodeByType("package", {+ ...target,+ package: pack.name,+ position,+ })+ );+ if (children.length === 0) {+ return [createDummyNode("package", parentKey)];+ }+ return children;+};++const mapFunctionNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.functions.map((func, position) =>+ mapTreeNodeByType("function", {+ ...target,+ function: func.name,+ position,+ })+ );+ if (children.length === 0) {+ return [createDummyNode("function", parentKey)];+ }+ return children;+};++const mapSequenceNodes = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ const children = schema.sequences.map((sequence, position) =>+ mapTreeNodeByType("sequence", {+ ...target,+ sequence: sequence.name,+ position,+ })+ );+ if (children.length === 0) {+ return [createDummyNode("function", parentKey)];+ }+ return children;+};++const mapTriggerNodes = (+ table: TableMetadata,+ target: NodeTarget<"table">,+ parentKey: string+) => {+ const children = table.triggers.map((trigger, position) =>+ mapTreeNodeByType("trigger", { ...target, trigger: trigger.name, position })+ );+ if (children.length === 0) {+ return [createDummyNode("function", parentKey)];+ }+ return children;+};++const buildSchemaNodeChildren = (+ schema: SchemaMetadata,+ target: NodeTarget<"schema">,+ parentKey: string+) => {+ if (+ schema.tables.length === 0 &&+ schema.externalTables.length === 0 &&+ schema.views.length === 0 &&+ schema.procedures.length === 0 &&+ schema.functions.length === 0+ ) {+ return [createDummyNode("table", parentKey)];+ }++ const children: TreeNode[] = [];++ // Always show "Tables" node+ // If no tables, show "<Empty>"+ const tablesNode = createExpandableTextNode("table", parentKey, () =>+ t("db.tables")+ );+ tablesNode.children = mapTableNodes(schema, target, tablesNode.key);+ children.push(tablesNode);++ // Only show "External Tables" node if the schema do have external tables.+ if (schema.externalTables.length > 0) {+ const externalTablesNode = createExpandableTextNode(+ "external-table",+ parentKey,+ () => t("db.external-tables")+ );+ externalTablesNode.children = mapExternalTableNodes(schema, target);+ children.push(externalTablesNode);+ }++ // Only show "Views" node if the schema do have views.+ if (schema.views.length > 0) {+ const viewsNode = createExpandableTextNode("view", parentKey, () =>+ t("db.views")+ );+ viewsNode.children = mapViewNodes(schema, target, viewsNode.key);+ children.push(viewsNode);+ }++ // Show "Procedures" if there's at least 1 procedure+ if (schema.procedures.length > 0) {+ const procedureNode = createExpandableTextNode("procedure", parentKey, () =>+ t("db.procedures")+ );+ procedureNode.children = mapProcedureNodes(+ schema,+ target,+ procedureNode.key+ );+ children.push(procedureNode);+ }++ // Show "Packages" if there's at least 1 package+ if (schema.packages.length > 0) {+ const packageNode = createExpandableTextNode("package", parentKey, () =>+ t("db.packages")+ );+ packageNode.children = mapPackageNodes(schema, target, packageNode.key);+ children.push(packageNode);+ }++ // Show "Functions" if there's at least 1 function+ if (schema.functions.length > 0) {+ const functionNode = createExpandableTextNode("function", parentKey, () =>+ t("db.functions")+ );+ functionNode.children = mapFunctionNodes(schema, target, functionNode.key);+ children.push(functionNode);+ }++ // Show "Sequences" if there's at least 1 function+ if (schema.sequences.length > 0) {+ const sequenceNode = createExpandableTextNode("sequence", parentKey, () =>+ t("db.sequences")+ );+ sequenceNode.children = mapSequenceNodes(schema, target, sequenceNode.key);+ children.push(sequenceNode);+ }++ return children;+};++export const buildDatabaseSchemaTree = (+ database: ComposedDatabase,+ metadata: DatabaseMetadata+) => {+ const dummyRoot = mapTreeNodeByType("database", {+ database: database.name,+ });+ const { schemas } = metadata;+ if (schemas.length === 0) {+ // Empty database, show "<Empty>"+ return [createDummyNode("table", dummyRoot.key)];+ }++ if (schemas.length === 1 && schemas[0].name === "") {+ const schema = schemas[0];+ // A single schema database, should render tables as views directly as a database+ // node's children+ return buildSchemaNodeChildren(+ schema,+ { ...dummyRoot.meta.target, schema: schema.name },+ dummyRoot.key+ );+ } else {+ // Multiple schema database+ return schemas.map((schema) => {+ const schemaNode = mapTreeNodeByType("schema", {+ ...dummyRoot.meta.target,+ schema: schema.name,+ });++ schemaNode.children = buildSchemaNodeChildren(+ schema,+ schemaNode.meta.target,+ schemaNode.key+ );+ return schemaNode;+ });+ }+};diff --git a/frontend/src/views/sql-editor/context.ts b/frontend/src/views/sql-editor/context.tsindex bf885d9a58..0599de4df1 100644--- a/frontend/src/views/sql-editor/context.ts+++ b/frontend/src/views/sql-editor/context.ts@@ -3,9 +3,9 @@ import Emittery from "emittery";import type { InjectionKey, Ref } from "vue";import { inject, provide, ref } from "vue";import { useSQLEditorStore } from "@/store";-import type { ComposedDatabase, SQLEditorTab } from "@/types";-import type { Worksheet } from "@/types/proto/v1/worksheet_service";+import type { SQLEditorTab } from "@/types";import type { GetSchemaStringRequest_ObjectType } from "@/types/proto/v1/database_service";+import type { Worksheet } from "@/types/proto/v1/worksheet_service";export type AsidePanelTab = "SCHEMA" | "WORKSHEET" | "HISTORY";@@ -45,7 +45,6 @@ export type SQLEditorContext = {AIPanelSize: Ref<number>;schemaViewer: Ref<| {- database: ComposedDatabase;schema?: string;object?: string;type?: GetSchemaStringRequest_ObjectType;