Skip to content

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

feat(sql-editor): display views in SQL Editor (#9457)

Liu Jie9a51d1c8dd0
diff --git a/frontend/src/components/Icon/TableIcon.vue b/frontend/src/components/Icon/TableIcon.vue
new file mode 100644
index 0000000000..2a84f51754
--- /dev/null
+++ b/frontend/src/components/Icon/TableIcon.vue
@@ -0,0 +1,8 @@
+<template>
+ <div class="relative">
+ <TableIcon v-bind="$attrs" />
+ </div>
+</template>
+<script setup lang="ts">
+import TableIcon from "~icons/heroicons-outline/table-cells";
+</script>
diff --git a/frontend/src/components/Icon/ViewIcon.vue b/frontend/src/components/Icon/ViewIcon.vue
new file mode 100644
index 0000000000..7a84a79d1e
--- /dev/null
+++ b/frontend/src/components/Icon/ViewIcon.vue
@@ -0,0 +1,15 @@
+<template>
+ <div class="relative">
+ <TableIcon v-bind="$attrs" class="opacity-80" />
+ <GlassesIcon
+ class="absolute bottom-0 right-0 w-3/4 h-3/4"
+ stroke="rgb(var(--color-accent))"
+ fill="white"
+ :stroke-width="3.5"
+ />
+ </div>
+</template>
+<script setup lang="ts">
+import { GlassesIcon } from "lucide-vue-next";
+import TableIcon from "~icons/heroicons-outline/table-cells";
+</script>
diff --git a/frontend/src/components/Icon/index.ts b/frontend/src/components/Icon/index.ts
index 6a4ff0d401..85e301e576 100644
--- a/frontend/src/components/Icon/index.ts
+++ b/frontend/src/components/Icon/index.ts
@@ -1,4 +1,6 @@
import EngineIcon from "./EngineIcon.vue";
import SkipIcon from "./SkipIcon.vue";
+import TableIcon from "./TableIcon.vue";
+import ViewIcon from "./ViewIcon.vue";
-export { SkipIcon, EngineIcon };
+export { SkipIcon, EngineIcon, TableIcon, ViewIcon };
diff --git a/frontend/src/store/modules/sqlEditorTree.ts b/frontend/src/store/modules/sqlEditorTree.ts
index 195051e2a0..9f45aa963b 100644
--- a/frontend/src/store/modules/sqlEditorTree.ts
+++ b/frontend/src/store/modules/sqlEditorTree.ts
@@ -1,5 +1,5 @@
import { useLocalStorage } from "@vueuse/core";
-import { cloneDeep, head, orderBy, uniqBy } from "lodash-es";
+import { cloneDeep, head, isFunction, orderBy, uniqBy } from "lodash-es";
import { defineStore } from "pinia";
import { v4 as uuidv4 } from "uuid";
import { computed, reactive, ref, watch } from "vue";
@@ -25,6 +25,8 @@ import {
LeafTreeNodeTypes,
Connection,
DEFAULT_PROJECT_V1_NAME,
+ RichViewMetadata,
+ TextTarget,
} from "@/types";
import { Environment } from "@/types/proto/v1/environment_service";
import { Policy } from "@/types/proto/v1/org_policy_service";
@@ -346,10 +348,18 @@ export const idForSQLEditorTreeNodeTarget = <T extends NodeType>(
table.name
}`;
}
+ if (type === "view") {
+ const { database, schema, view } = target as RichViewMetadata;
+ return `${database.name}/schemas/${schema.name || "-"}/views/${view.name}`;
+ }
if (type === "label") {
const kv = target as NodeTarget<"label">;
return `labels/${kv.key}:${kv.value}`;
}
+ if (type === "expandable-text") {
+ const { text, type } = target as NodeTarget<"expandable-text">;
+ return `texts-${type}/${text}`;
+ }
if (type === "dummy") {
const dummyType = (target as NodeTarget<"dummy">).type;
return `dummy-${dummyType}`;
@@ -518,6 +528,14 @@ const readableTargetByType = <T extends NodeType>(
if (type === "table") {
return (target as RichTableMetadata).table.name;
}
+ if (type === "view") {
+ return (target as RichViewMetadata).view.name;
+ }
+ if (type === "expandable-text") {
+ const { text, searchable } = target as TextTarget<true>;
+ if (!searchable) return "";
+ return isFunction(text) ? text() : text;
+ }
if (type === "dummy") {
// Use empty strings for dummy nodes to make them unsearchable
return "";
diff --git a/frontend/src/types/sqlEditorTree.ts b/frontend/src/types/sqlEditorTree.ts
index 9bbba7e3db..f5d0c3d257 100644
--- a/frontend/src/types/sqlEditorTree.ts
+++ b/frontend/src/types/sqlEditorTree.ts
@@ -1,8 +1,13 @@
import { TreeOption } from "naive-ui";
+import { RenderFunction } from "vue";
import { t } from "@/plugins/i18n";
import { useSQLEditorTreeStore } from "@/store";
import { Engine } from "./proto/v1/common";
-import { SchemaMetadata, TableMetadata } from "./proto/v1/database_service";
+import {
+ SchemaMetadata,
+ TableMetadata,
+ ViewMetadata,
+} from "./proto/v1/database_service";
import { Environment } from "./proto/v1/environment_service";
import { ComposedDatabase, ComposedInstance, ComposedProject } from "./v1";
@@ -25,7 +30,9 @@ export type SQLEditorTreeNodeType =
| "schema"
| "table"
| "label"
- | "dummy"; // Dummy nodes to display "no tables" etc.
+ | "view"
+ | "expandable-text" // Text nodes to display "Tables / Views / Functions / Triggers" etc.
+ | "dummy"; // Dummy nodes to display "<Empty>" etc.
export type RichSchemaMetadata = {
database: ComposedDatabase;
@@ -36,6 +43,18 @@ export type RichTableMetadata = {
schema: SchemaMetadata;
table: TableMetadata;
};
+export type RichViewMetadata = {
+ database: ComposedDatabase;
+ schema: SchemaMetadata;
+ view: ViewMetadata;
+};
+export type TextTarget<E extends boolean> = {
+ expandable: E;
+ type: SQLEditorTreeNodeType;
+ text: string | (() => string);
+ render?: RenderFunction;
+ searchable?: boolean;
+};
export type SQLEditorTreeNodeTarget<T extends SQLEditorTreeNodeType = any> =
T extends "project"
@@ -50,8 +69,12 @@ export type SQLEditorTreeNodeTarget<T extends SQLEditorTreeNodeType = any> =
? RichSchemaMetadata
: T extends "table"
? RichTableMetadata
+ : T extends "view"
+ ? RichViewMetadata
: T extends "label"
? { key: string; value: string }
+ : T extends "expandable-text"
+ ? TextTarget<true>
: T extends "dummy"
? { type: SQLEditorTreeNodeType; error?: unknown }
: never;
@@ -85,6 +108,7 @@ export const ExpandableTreeNodeTypes: readonly SQLEditorTreeNodeType[] = [
"instance",
"environment",
"project",
+ "expandable-text",
"label",
] as const;
@@ -95,6 +119,7 @@ export const ConnectableTreeNodeTypes: readonly SQLEditorTreeNodeType[] = [
export const LeafTreeNodeTypes: readonly SQLEditorTreeNodeType[] = [
"table",
+ "view",
"dummy",
] as const;
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
index ac0f0acc18..5530a0caa7 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
@@ -17,6 +17,12 @@
<template v-if="type === 'table'">
<TableNode :node="node" :factors="factors" :keyword="keyword" />
</template>
+ <template v-if="type === 'view'">
+ <ViewNode :node="node" :factors="factors" :keyword="keyword" />
+ </template>
+ <template v-if="type === 'expandable-text'">
+ <TextNode :node="node" :factors="factors" :keyword="keyword" />
+ </template>
<template v-if="type === 'label'">
<LabelNode :node="node" :factors="factors" :keyword="keyword" />
</template>
@@ -39,6 +45,8 @@ import LabelNode from "./LabelNode.vue";
import ProjectNode from "./ProjectNode.vue";
import SchemaNode from "./SchemaNode.vue";
import TableNode from "./TableNode.vue";
+import TextNode from "./TextNode.vue";
+import ViewNode from "./ViewNode.vue";
const props = defineProps<{
node: TreeNode;
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/TableNode.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/TableNode.vue
index 2578d87960..c62183ef72 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TreeNode/TableNode.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/TableNode.vue
@@ -1,6 +1,6 @@
<template>
<div class="flex items-center max-w-full overflow-hidden gap-x-1">
- <TableIcon />
+ <TableIcon class="w-4 h-4" />
<HighlightLabelText
:text="target.table.name"
:keyword="keyword"
@@ -11,7 +11,7 @@
<script setup lang="ts">
import { computed } from "vue";
-import TableIcon from "~icons/heroicons-outline/table-cells";
+import { TableIcon } from "@/components/Icon";
import {
SQLEditorTreeNode as TreeNode,
SQLEditorTreeFactor as Factor,
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/TextNode.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/TextNode.vue
new file mode 100644
index 0000000000..17b8157f0e
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/TextNode.vue
@@ -0,0 +1,39 @@
+<template>
+ <div class="flex flex-row items-center gap-x-1">
+ <TableIcon v-if="type === 'table'" class="w-4" />
+ <ViewIcon v-if="type === 'view'" class="w-4 h-4" />
+ <component :is="render" v-if="render" />
+ <span v-else>{{ text }}</span>
+ </div>
+</template>
+
+<script setup lang="ts">
+import { isFunction } from "lodash-es";
+import { computed } from "vue";
+import { TableIcon, ViewIcon } from "@/components/Icon";
+import {
+ SQLEditorTreeNode as TreeNode,
+ SQLEditorTreeFactor as Factor,
+} from "@/types";
+
+const props = defineProps<{
+ node: TreeNode;
+ factors: Factor[];
+ keyword: string;
+}>();
+
+const target = computed(() => {
+ return (props.node as TreeNode<"expandable-text">).meta.target;
+});
+
+const type = computed(() => {
+ return target.value.type;
+});
+const text = computed(() => {
+ const { text } = target.value;
+ return isFunction(text) ? text() : text;
+});
+const render = computed(() => {
+ return target.value.render;
+});
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/ViewNode.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/ViewNode.vue
new file mode 100644
index 0000000000..d85df8610e
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/ViewNode.vue
@@ -0,0 +1,28 @@
+<template>
+ <div class="flex items-center max-w-full overflow-hidden gap-x-1">
+ <ViewIcon class="w-4 h-4" />
+ <HighlightLabelText
+ :text="target.view.name"
+ :keyword="keyword"
+ class="flex-1 truncate"
+ />
+ </div>
+</template>
+
+<script setup lang="ts">
+import { computed } from "vue";
+import { ViewIcon } from "@/components/Icon";
+import {
+ SQLEditorTreeNode as TreeNode,
+ SQLEditorTreeFactor as Factor,
+} from "@/types";
+import HighlightLabelText from "./HighlightLabelText.vue";
+
+const props = defineProps<{
+ node: TreeNode;
+ factors: Factor[];
+ keyword: string;
+}>();
+
+const target = computed(() => (props.node as TreeNode<"view">).meta.target);
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/common.ts b/frontend/src/views/sql-editor/AsidePanel/common.ts
index 818a5d8f0e..40180e137b 100644
--- a/frontend/src/views/sql-editor/AsidePanel/common.ts
+++ b/frontend/src/views/sql-editor/AsidePanel/common.ts
@@ -1,19 +1,27 @@
+import { RenderFunction } from "vue";
+import { t } from "@/plugins/i18n";
import { useDBSchemaV1Store } from "@/store";
import { mapTreeNodeByType } from "@/store/modules/sqlEditorTree";
-import { ComposedDatabase, SQLEditorTreeNode } from "@/types";
+import {
+ ComposedDatabase,
+ SQLEditorTreeNode,
+ SQLEditorTreeNodeType,
+ TextTarget,
+} from "@/types";
import {
SchemaMetadata,
DatabaseMetadataView,
} from "@/types/proto/v1/database_service";
-const createDummyTableNode = (
+const createDummyNode = (
+ type: "table" | "view",
parent: SQLEditorTreeNode,
error: unknown | undefined = undefined
) => {
return mapTreeNodeByType(
"dummy",
{
- type: "table",
+ type,
error,
},
parent,
@@ -22,6 +30,23 @@ const createDummyTableNode = (
}
);
};
+const createExpandableTextNode = (
+ type: SQLEditorTreeNodeType,
+ parent: SQLEditorTreeNode,
+ text: TextTarget<true>["text"],
+ render?: RenderFunction
+) => {
+ return mapTreeNodeByType(
+ "expandable-text",
+ {
+ type,
+ expandable: true,
+ text,
+ render,
+ },
+ parent
+ );
+};
const mapTableNodes = (
database: ComposedDatabase,
@@ -32,7 +57,58 @@ const mapTableNodes = (
mapTreeNodeByType("table", { database, schema, table }, parent)
);
if (children.length === 0) {
- return [createDummyTableNode(parent)];
+ return [createDummyNode("table", parent)];
+ }
+ return children;
+};
+const mapViewNodes = (
+ database: ComposedDatabase,
+ schema: SchemaMetadata,
+ parent: SQLEditorTreeNode
+) => {
+ const children = schema.views.map((view) =>
+ mapTreeNodeByType(
+ "view",
+ {
+ database,
+ schema,
+ view,
+ },
+ parent
+ )
+ );
+ if (children.length === 0) {
+ return [createDummyNode("view", parent)];
+ }
+ return children;
+};
+
+const buildSchemaNodeChildren = (
+ database: ComposedDatabase,
+ schema: SchemaMetadata,
+ parent: SQLEditorTreeNode
+) => {
+ if (schema.tables.length === 0 && schema.views.length === 0) {
+ return [createDummyNode("table", parent)];
+ }
+
+ const children: SQLEditorTreeNode[] = [];
+
+ // Always show "Tables" node
+ // If no tables, show "<Empty>"
+ const tablesNode = createExpandableTextNode("table", parent, () =>
+ t("db.tables")
+ );
+ tablesNode.children = mapTableNodes(database, schema, tablesNode);
+ children.push(tablesNode);
+
+ // 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(database, schema, viewsNode);
+ children.push(viewsNode);
}
return children;
};
@@ -46,21 +122,21 @@ export const fetchDatabaseSubTree = async (
await useDBSchemaV1Store().getOrFetchDatabaseMetadata({
database: database.name,
skipCache: false,
- view: DatabaseMetadataView.DATABASE_METADATA_VIEW_FULL,
+ view: DatabaseMetadataView.DATABASE_METADATA_VIEW_BASIC,
});
const { schemas } = databaseMetadata;
if (schemas.length === 0) {
- // Empty database
- node.children = [createDummyTableNode(node)];
+ // Empty database, show "<Empty>"
+ node.children = [createDummyNode("table", node)];
return;
}
if (schemas.length === 1 && schemas[0].name === "") {
const schema = schemas[0];
- // A single schema database, should render tables directly as a database
+ // A single schema database, should render tables as views directly as a database
// node's children
- node.children = mapTableNodes(database, schema, node);
+ node.children = buildSchemaNodeChildren(database, schema, node);
return;
} else {
// Multiple schema database
@@ -70,12 +146,14 @@ export const fetchDatabaseSubTree = async (
{ database, schema },
node
);
- schemaNode.children = mapTableNodes(database, schema, schemaNode);
+
+ schemaNode.children = buildSchemaNodeChildren(database, schema, node);
return schemaNode;
});
return;
}
} catch (error) {
- node.children = [createDummyTableNode(node, error)];
+ console.warn("[fetchDatabaseSubTree]", error);
+ node.children = [createDummyNode("table", node, error)];
}
};
diff --git a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/HoverPanel.vue b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/HoverPanel.vue
index 3269be6695..1873057f6d 100644
--- a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/HoverPanel.vue
+++ b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/HoverPanel.vue
@@ -15,16 +15,23 @@
:db="state.db"
:database="state.database"
:schema="state.schema"
- :table="state.table"
+ :table="state.table!"
:column="state.column"
/>
<TableInfo
- v-else
+ v-else-if="state.table"
:db="state.db"
:database="state.database"
:schema="state.schema"
:table="state.table"
/>
+ <ViewInfo
+ v-else-if="state.view"
+ :db="state.db"
+ :database="state.database"
+ :schema="state.schema"
+ :view="state.view"
+ />
</template>
</div>
</template>
@@ -35,6 +42,7 @@ import { zindexable as vZindexable } from "vdirs";
import { computed, ref } from "vue";
import ColumnInfo from "./ColumnInfo.vue";
import TableInfo from "./TableInfo.vue";
+import ViewInfo from "./ViewInfo.vue";
import { useHoverStateContext } from "./hover-state";
defineProps<{
diff --git a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/ViewInfo.vue b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/ViewInfo.vue
new file mode 100644
index 0000000000..00ebd258bf
--- /dev/null
+++ b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/ViewInfo.vue
@@ -0,0 +1,71 @@
+<template>
+ <div class="w-[32rem] h-[20rem] flex flex-col gap-y-2">
+ <div class="flex flex-row content-justify items-center gap-x-2">
+ <div class="textlabel flex-1 truncate">
+ {{ name }}
+ </div>
+ <div class="flex flex-row justify-end items-center">
+ <NCheckbox v-if="!formatted.error" v-model:checked="format">
+ Format
+ </NCheckbox>
+ </div>
+ </div>
+ <MonacoEditor
+ :value="format ? formatted.data : view.definition"
+ :readonly="true"
+ class="border w-full flex-1"
+ />
+ </div>
+</template>
+
+<script setup lang="ts">
+import { useLocalStorage } from "@vueuse/core";
+import { NCheckbox } from "naive-ui";
+import { computed } from "vue";
+import MonacoEditor from "@/components/MonacoEditor";
+import formatSQL from "@/components/MonacoEditor/sqlFormatter";
+import { ComposedDatabase, dialectOfEngineV1 } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
+import {
+ DatabaseMetadata,
+ SchemaMetadata,
+ ViewMetadata,
+} from "@/types/proto/v1/database_service";
+
+const props = defineProps<{
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ view: ViewMetadata;
+}>();
+const format = useLocalStorage<boolean>(
+ "bb.sql-editor.schema-hover-panel.view-info.format",
+ false
+);
+const instanceEngine = computed(() => props.db.instanceEntity.engine);
+
+const hasSchemaProperty = computed(() => {
+ return [Engine.POSTGRES, Engine.RISINGWAVE].includes(instanceEngine.value);
+});
+
+const name = computed(() => {
+ const { schema, view } = props;
+ if (hasSchemaProperty.value) {
+ return `${schema.name}.${view.name}`;
+ }
+ return view.name;
+});
+
+const formatted = computed(() => {
+ const sql = props.view.definition;
+ try {
+ const result = formatSQL(sql, dialectOfEngineV1(instanceEngine.value));
+ return result;
+ } catch (err) {
+ return {
+ error: err,
+ data: sql,
+ };
+ }
+});
+</script>
diff --git a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/hover-state.ts b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/hover-state.ts
index 2bce392ff4..66b4bfb140 100644
--- a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/hover-state.ts
+++ b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/HoverPanel/hover-state.ts
@@ -3,9 +3,10 @@ import { useDelayedValue } from "@/composables/useDelayedValue";
import { ComposedDatabase } from "@/types";
import {
ColumnMetadata,
- DatabaseMetaData,
+ DatabaseMetadata,
SchemaMetadata,
TableMetadata,
+ ViewMetadata,
} from "@/types/proto/v1/database_service";
type UpdateFn = ReturnType<typeof useDelayedValue<any>>["update"];
@@ -17,9 +18,10 @@ type Position = {
export type HoverState = {
db: ComposedDatabase;
- database: DatabaseMetaData;
+ database: DatabaseMetadata;
schema: SchemaMetadata;
- table: TableMetadata;
+ table?: TableMetadata;
+ view?: ViewMetadata;
column?: ColumnMetadata;
};
diff --git a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/TableList.vue b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/TableList.vue
index 8edb8f96eb..703dc66898 100644
--- a/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/TableList.vue
+++ b/frontend/src/views/sql-editor/SecondarySidebar/InfoTabPane/SchemaPanel/TableList.vue
@@ -1,31 +1,31 @@
<template>
<div class="flex flex-col overflow-hidden">
<VirtualList
- :items="filteredTableList"
+ :items="filteredTableOrViewList"
:key-field="`key`"
:item-resizable="true"
:item-size="24"
>
- <template
- #default="{ item: { key, schema, table }, index }: VirtualListItem"
- >
+ <template #default="{ item, index }: VirtualListItem">
<div
- class="bb-table-list--table-item text-sm leading-6 px-1"
- :data-key="key"
+ class="text-sm leading-6 px-1"
+ :class="`bb-table-list--item`"
+ :data-key="item.key"
:data-index="index"
- @mouseenter="handleMouseEnter($event, schema, table)"
- @mouseleave="handleMouseLeave($event, schema, table)"
+ @mouseenter="handleMouseEnter($event, item)"
+ @mouseleave="handleMouseLeave($event, item)"
>
<div
class="flex items-center text-gray-600 whitespace-pre-wrap break-words rounded-sm px-1"
:class="
rowClickable && ['hover:bg-control-bg-hover/50', 'cursor-pointer']
"
- @click="handleClickTable(schema, table)"
+ @click="handleClickItem(item)"
>
- <heroicons-outline:table class="h-4 w-4 mr-1 shrink-0" />
+ <TableIcon v-if="item.table" class="w-4 h-4 mr-1 shrink-0" />
+ <ViewIcon v-if="item.view" class="w-4 h-4 mr-1 shrink-0" />
<!-- eslint-disable-next-line vue/no-v-html -->
- <div v-html="renderTableName(schema, table)" />
+ <div v-html="renderItem(item)" />
</div>
</div>
</template>
@@ -37,24 +37,27 @@
import { escape } from "lodash-es";
import { computed, nextTick } from "vue";
import { VirtualList } from "vueuc";
+import { TableIcon, ViewIcon } from "@/components/Icon";
import { ComposedDatabase } from "@/types";
import {
DatabaseMetadata,
SchemaMetadata,
TableMetadata,
+ ViewMetadata,
} from "@/types/proto/v1/database_service";
import { findAncestor, getHighlightHTMLByRegExp } from "@/utils";
import { useHoverStateContext } from "./HoverPanel";
import { useSchemaPanelContext } from "./context";
-export type SchemaAndTable = {
+export type SchemaAndTableOrView = {
key: string;
schema: SchemaMetadata;
- table: TableMetadata;
+ table?: TableMetadata;
+ view?: ViewMetadata;
};
export type VirtualListItem = {
- item: SchemaAndTable;
+ item: SchemaAndTableOrView;
index: number;
};
@@ -76,42 +79,58 @@ const {
update: updateHoverState,
} = useHoverStateContext();
-const flattenTableList = computed(() => {
- return props.schemaList.flatMap((schema) => {
- return schema.tables.map<SchemaAndTable>((table) => ({
- key: `${schema.name}.${table.name}`,
+const flattenTableOrViewList = computed(() => {
+ const { schemaList } = props;
+ const tables = schemaList.flatMap((schema) => {
+ return schema.tables.map<SchemaAndTableOrView>((table) => ({
+ key: `schemas/${schema.name}/tables/${table.name}`,
schema,
table,
}));
});
+ const views = schemaList.flatMap((schema) => {
+ return schema.views.map<SchemaAndTableOrView>((view) => ({
+ key: `schemas/${schema.name}/views/${view.name}`,
+ schema,
+ view,
+ }));
+ });
+ return [...tables, ...views];
});
-const filteredTableList = computed(() => {
+const filteredTableOrViewList = computed(() => {
const kw = keyword.value.toLowerCase().trim();
if (!kw) {
- return flattenTableList.value;
+ return flattenTableOrViewList.value;
}
- return flattenTableList.value.filter(({ schema, table }) => {
+ return flattenTableOrViewList.value.filter(({ schema, table, view }) => {
return (
schema.name.toLowerCase().includes(kw) ||
- table.name.toLowerCase().includes(kw)
+ table?.name.toLowerCase().includes(kw) ||
+ view?.name.toLowerCase().includes(kw)
);
});
});
-const handleClickTable = (schema: SchemaMetadata, table: TableMetadata) => {
+const handleClickItem = (item: SchemaAndTableOrView) => {
if (!props.rowClickable) {
return;
}
- emit("select-table", schema, table);
+ const { schema, table } = item;
+ if (table) {
+ emit("select-table", schema, table);
+ }
};
-const renderTableName = (schema: SchemaMetadata, table: TableMetadata) => {
+const renderItem = (item: SchemaAndTableOrView) => {
const parts: string[] = [];
+ const { schema, table, view } = item;
+ const name = table?.name ?? view?.name ?? "";
+ if (!name) return null;
if (schema.name) {
parts.push(`${schema.name}.`);
}
- parts.push(table.name);
+ parts.push(name);
if (!keyword.value.trim()) {
return parts.join("");
}
@@ -123,27 +142,25 @@ const renderTableName = (schema: SchemaMetadata, table: TableMetadata) => {
);
};
-const handleMouseEnter = (
- e: MouseEvent,
- schema: SchemaMetadata,
- table: TableMetadata
-) => {
+const handleMouseEnter = (e: MouseEvent, item: SchemaAndTableOrView) => {
const { db, database } = props;
+ const { schema, table, view } = item;
+
if (hoverState.value) {
updateHoverState(
- { db, database, schema, table },
+ { db, database, schema, table, view },
"before",
0 /* overrideDelay */
);
} else {
- updateHoverState({ db, database, schema, table }, "before");
+ updateHoverState({ db, database, schema, table, view }, "before");
}
nextTick().then(() => {
// Find the node element and put the database panel to the top-left corner
// of the node
const wrapper = findAncestor(
e.target as HTMLElement,
- ".bb-table-list--table-item"
+ ".bb-table-list--item"
);
if (!wrapper) {
updateHoverState(undefined, "after", 0 /* overrideDelay */);
@@ -155,11 +172,7 @@ const handleMouseEnter = (
});
};
-const handleMouseLeave = (
- e: MouseEvent,
- schema: SchemaMetadata,
- table: TableMetadata
-) => {
+const handleMouseLeave = (e: MouseEvent, item: SchemaAndTableOrView) => {
updateHoverState(undefined, "after");
};
</script>