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: sql editor store (#395)
STEVEN976ea9c9aa05
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vueindex 878460b7c4..99e35b9777 100644--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue@@ -84,9 +84,8 @@ const init = async () => {// @ts-expect-error?.getValueInRange(editorInstance.getSelection()) as string;- const queryStatement = selectedValue || typedValue;-- emit("run-query", queryStatement);+ const query = selectedValue || typedValue;+ emit("run-query", query);},});diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.tsindex 63c5ee6143..84d438d274 100644--- a/frontend/src/composables/useExecuteSQL.ts+++ b/frontend/src/composables/useExecuteSQL.ts@@ -31,8 +31,9 @@ const useExecuteSQL = () => {notify("INFO", t("common.tips"), t("sql-editor.can-not-execute-query"));}- const queryStatement = store.state.sqlEditor.queryStatement;- const selectedStatement = store.state.sqlEditor.selectedStatement;+ const currentTab = store.getters["editorSelector/currentTab"];+ const queryStatement = currentTab.queryStatement;+ const selectedStatement = currentTab.selectedStatement;const sqlStatement = selectedStatement || queryStatement;const { data } = parseSQL(sqlStatement);diff --git a/frontend/src/store/modules/editorSelector.ts b/frontend/src/store/modules/editorSelector.tsindex 053a023d56..d379d019ba 100644--- a/frontend/src/store/modules/editorSelector.ts+++ b/frontend/src/store/modules/editorSelector.ts@@ -4,25 +4,32 @@ import { TabInfo, AnyTabInfo, EditorSelectorState } from "../../types";import * as types from "../mutation-types";import { makeActions } from "../actions";-const state: () => EditorSelectorState = () => ({- queryTabList: [],- activeTab: {+const getDefaultTab = () => {+ return {id: uuidv1(),- idx: 0,label: "Untitled Queries",isSaved: true,savedAt: dayjs().format("YYYY-MM-DD HH:mm:ss"),- queries: "",- },- activeTabIdx: 1,-});+ queryStatement: "",+ selectedStatement: "",+ };+};++const state: () => EditorSelectorState = () => {+ const defaultTab = getDefaultTab();++ return {+ queryTabList: [defaultTab],+ activeTabId: defaultTab.id,+ };+};const getters = {currentTab(state: EditorSelectorState) {const idx = state.queryTabList.findIndex(- (tab: TabInfo) => tab.idx === state.activeTabIdx+ (tab: TabInfo) => tab.id === state.activeTabId);- return idx === -1 ? {} : state.queryTabList[idx];+ return (idx === -1 ? {} : state.queryTabList[idx]) as TabInfo;},hasTabs(state: EditorSelectorState) {return state.queryTabList.length > 0;@@ -48,6 +55,18 @@ const mutations = {);Object.assign(state.queryTabList[idx], payload);},+ [types.UPDATE_ACTIVE_TAB](state: EditorSelectorState, payload: AnyTabInfo) {+ const idx = state.queryTabList.findIndex(+ (tab: TabInfo) => tab.id === state.activeTabId+ );+ Object.assign(state.queryTabList[idx], {+ ...state.queryTabList[idx],+ ...payload,+ });+ },+ [types.SET_ACTIVE_TAB_ID](state: EditorSelectorState, payload: string) {+ state.activeTabId = payload;+ },};type ActionsMap = {@@ -58,24 +77,18 @@ const actions = {...makeActions<ActionsMap>({setEditorSelectorState: types.SET_EDITOR_SELECTOR_STATE,}),- addTab({ commit, state }: any, payload: AnyTabInfo) {- const id = uuidv1();- const idx =- state.queryTabList.length === 0- ? 0- : Math.max(...state.queryTabList.map((tab: TabInfo) => tab.idx)) + 1;+ addTab({ commit }: any, payload: AnyTabInfo) {const newTab = {- id,- idx,+ ...getDefaultTab(),label: payload.label ? payload.label : `Untitled Queries`,isSaved: true,savedAt: dayjs().format("YYYY-MM-DD HH:mm:ss"),- queries: payload.queries || "",+ queryStatement: payload.queryStatement || "",+ selectedStatement: payload.selectedStatement || "",};commit(types.SET_EDITOR_SELECTOR_STATE, {- activeTab: newTab,- activeTabIdx: idx,+ activeTabId: newTab.id,});commit(types.ADD_TAB, newTab);},@@ -84,31 +97,14 @@ const actions = {const tabsLength = state.queryTabList.length;if (tabsLength > 0) {- dispatch("setActiveTab", state.queryTabList[tabsLength - 1]);+ dispatch("setActiveTabId", state.queryTabList[tabsLength - 1].id);}},- updateTab({ commit, state }: any, payload: AnyTabInfo) {- const { idx } = payload;- const tab = state.queryTabList.find((tab: TabInfo) => tab.idx === idx);-- if (tab) {- commit(types.SET_EDITOR_SELECTOR_STATE, {- activeTab: tab,- activeTabIdx: idx,- });- commit(types.UPDATE_TAB, payload);- }+ updateActiveTab({ commit }: any, payload: AnyTabInfo) {+ commit(types.UPDATE_ACTIVE_TAB, payload);},- setActiveTab({ commit, state }: any, payload: TabInfo) {- const { idx } = payload;- const tab = state.queryTabList.find((tab: TabInfo) => tab.idx === idx);-- if (tab) {- commit(types.SET_EDITOR_SELECTOR_STATE, {- activeTab: tab,- activeTabIdx: idx,- });- }+ setActiveTabId({ commit, state }: any, payload: string) {+ commit(types.SET_ACTIVE_TAB_ID, payload);},};diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.tsindex 78afbc9fd0..1b53bfa2ad 100644--- a/frontend/src/store/modules/sqlEditor.ts+++ b/frontend/src/store/modules/sqlEditor.ts@@ -11,10 +11,6 @@ import {} from "../../types";import * as types from "../mutation-types";import { makeActions } from "../actions";-import {- parseSQL,- transformSQL,-} from "../../components/MonacoEditor/sqlParser";const state: () => SqlEditorState = () => ({connectionTree: [],@@ -30,10 +26,7 @@ const state: () => SqlEditorState = () => ({selectedDatabaseId: 0,selectedTableName: "",},- queryStatement: "",- selectedStatement: "",isExecuting: false,- queryResult: null,isShowExecutingHint: false,});@@ -109,14 +102,6 @@ const getters = {const connectionContext = state.connectionContext;return `${connectionContext.instanceId}/${connectionContext.databaseId}/${connectionContext.tableId}`;},- isEmptyStatement(state: SqlEditorState) {- return isEmpty(state.queryStatement);- },- parsedStatement(state: SqlEditorState) {- const sqlStatement = state.selectedStatement || state.queryStatement;- const { data } = parseSQL(sqlStatement);- return data !== null ? transformSQL(data) : sqlStatement;- },};const mutations = {@@ -132,9 +117,6 @@ const mutations = {) {state.connectionTree = payload;},- [types.SET_QUERY_RESULT](state: SqlEditorState, payload: Array<any>) {- state.queryResult = payload;- },[types.SET_CONNECTION_CONTEXT](state: SqlEditorState,payload: Partial<ConnectionContext>@@ -149,7 +131,6 @@ const mutations = {type SqlEditorActionsMap = {setSqlEditorState: typeof mutations.SET_SQL_EDITOR_STATE;setConnectionTree: typeof mutations.SET_CONNECTION_TREE;- setQueryResult: typeof mutations.SET_QUERY_RESULT;setConnectionContext: typeof mutations.SET_CONNECTION_CONTEXT;setIsExecuting: typeof mutations.SET_IS_EXECUTING;};@@ -158,27 +139,31 @@ const actions = {...makeActions<SqlEditorActionsMap>({setSqlEditorState: types.SET_SQL_EDITOR_STATE,setConnectionTree: types.SET_CONNECTION_TREE,- setQueryResult: types.SET_QUERY_RESULT,setConnectionContext: types.SET_CONNECTION_CONTEXT,setIsExecuting: types.SET_IS_EXECUTING,}),async executeQuery(- { commit, dispatch, state }: any,+ { dispatch, state, rootGetters }: any,payload: Partial<QueryInfo> = {}) {+ const currentTab = rootGetters["editorSelector/currentTab"];const res = await dispatch("sql/query",{instanceId: state.connectionContext.instanceId,databaseName: state.connectionContext.databaseName,- statement: !isEmpty(state.selectedStatement)- ? state.selectedStatement- : state.queryStatement,+ statement: currentTab.selectedStatement || currentTab.queryStatement,...payload,},{ root: true });- commit(types.SET_QUERY_RESULT, res.data);+ dispatch(+ "editorSelector/updateActiveTab",+ {+ queryResult: res.data,+ },+ { root: true }+ );return res;},async fetchConnectionByInstanceIdAndDatabaseId(@@ -195,9 +180,6 @@ const actions = {{ databaseId },{ root: true });- commit(types.SET_SQL_EDITOR_STATE, {- queryResult: null,- });commit(types.SET_CONNECTION_CONTEXT, {hasSlug: true,instanceId,diff --git a/frontend/src/store/mutation-types.ts b/frontend/src/store/mutation-types.tsindex ada47e2cd0..d0da0f1e50 100644--- a/frontend/src/store/mutation-types.ts+++ b/frontend/src/store/mutation-types.ts@@ -1,7 +1,6 @@// SQL Editor mutation typesexport const SET_SQL_EDITOR_STATE = "SET_SQL_EDITOR_STATE";export const SET_CONNECTION_TREE = "SET_CONNECTION_TREE";-export const SET_QUERY_RESULT = "SET_QUERY_RESULT";export const SET_CONNECTION_CONTEXT = "SET_CONNECTION_CONTEXT";export const SET_IS_EXECUTING = "SET_IS_EXECUTING";@@ -10,4 +9,5 @@ export const SET_EDITOR_SELECTOR_STATE = "SET_EDITOR_SELECTOR_STATE";export const ADD_TAB = "ADD_TAB";export const REMOVE_TAB = "REMOVE_TAB";export const UPDATE_TAB = "UPDATE_TAB";-export const SET_ACTIVE_TAB = "SET_ACTIVE_TAB";+export const UPDATE_ACTIVE_TAB = "UPDATE_ACTIVE_TAB";+export const SET_ACTIVE_TAB_ID = "SET_ACTIVE_TAB_ID";diff --git a/frontend/src/types/editorSelector.ts b/frontend/src/types/editorSelector.tsindex 74b2712676..275b19af01 100644--- a/frontend/src/types/editorSelector.ts+++ b/frontend/src/types/editorSelector.ts@@ -1,10 +1,12 @@export interface TabInfo {id: string;- idx: number;label: string;isSaved: boolean;savedAt: string;- queries: string;+ queryStatement: string;+ selectedStatement: string;+ queryResult?: Record<string, any>[];+ currentQueryId?: string;}export type AnyTabInfo = Partial<TabInfo>;diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.tsindex 170c91bb4c..c8078f086b 100644--- a/frontend/src/types/store.ts+++ b/frontend/src/types/store.ts@@ -190,10 +190,7 @@ export interface LabelState {export interface SqlEditorState {connectionTree: ConnectionAtom[];connectionContext: ConnectionContext;- queryStatement: string;- selectedStatement: string;isExecuting: boolean;- queryResult: Record<string, any>[] | null;isShowExecutingHint: boolean;}export type SqlEditorGetters = typeof sqlEditorStore.getters;@@ -202,8 +199,7 @@ export type SqlEditorMutations = typeof sqlEditorStore.mutations;export interface EditorSelectorState {queryTabList: TabInfo[];- activeTab: TabInfo;- activeTabIdx: number;+ activeTabId: string;}export type EditorSelectorGetters = typeof editorSelectorStore.getters;export type EditorSelectorActions = typeof editorSelectorStore.actions;diff --git a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vueindex f2db7c51b5..b5b56cdeb6 100644--- a/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/EditorAction.vue@@ -38,7 +38,7 @@</template><script lang="ts" setup>-import { ref, watch } from "vue";+import { computed, ref, watch } from "vue";import { CascaderOption } from "naive-ui";import { cloneDeep } from "lodash-es";@@ -49,9 +49,9 @@ import {} from "vuex-composition-helpers";import {SqlEditorState,- SqlEditorGetters,SqlEditorActions,ConnectionContext,+ EditorSelectorGetters,} from "../../../types";import { useExecuteSQL } from "../../../composables/useExecuteSQL";import { isDev } from "../../../utils";@@ -61,15 +61,19 @@ const { connectionTree, connectionContext } ="connectionTree","connectionContext",]);-const { isEmptyStatement } = useNamespacedGetters<SqlEditorGetters>(- "sqlEditor",- ["isEmptyStatement", "connectionInfo"]+const { currentTab } = useNamespacedGetters<EditorSelectorGetters>(+ "editorSelector",+ ["currentTab"]);const { setConnectionContext } = useNamespacedActions<SqlEditorActions>("sqlEditor",["setConnectionContext"]);+const isEmptyStatement = computed(+ () => !currentTab.value || currentTab.value.queryStatement === ""+);+const selectedConnection = ref();const isSeletedDatabase = ref(false);const { execute, state: executeState } = useExecuteSQL();diff --git a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vueindex 5efd9763a3..40dc0224c7 100644--- a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue@@ -38,7 +38,7 @@</template><script lang="ts" setup>-import { defineEmits } from "vue";+import { computed, defineEmits } from "vue";import { useRouter } from "vue-router";import { useStore } from "vuex";import { useI18n } from "vue-i18n";@@ -48,7 +48,15 @@ import {useNamespacedState,} from "vuex-composition-helpers";-import type { SqlEditorState, SqlEditorGetters } from "../../../types";+import type {+ SqlEditorState,+ SqlEditorGetters,+ EditorSelectorGetters,+} from "../../../types";+import {+ parseSQL,+ transformSQL,+} from "../../../components/MonacoEditor/sqlParser";const emit = defineEmits<{(e: "close"): void;@@ -58,14 +66,24 @@ const router = useRouter();const store = useStore();const { t } = useI18n();-const { findProjectIdByDatabaseId, parsedStatement } =- useNamespacedGetters<SqlEditorGetters>("sqlEditor", [- "findProjectIdByDatabaseId",- "parsedStatement",- ]);+const { findProjectIdByDatabaseId } = useNamespacedGetters<SqlEditorGetters>(+ "sqlEditor",+ ["findProjectIdByDatabaseId"]+);const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", ["connectionContext",]);+const { currentTab } = useNamespacedGetters<EditorSelectorGetters>(+ "editorSelector",+ ["currentTab"]+);++const parsedStatement = computed(() => {+ const sqlStatement =+ currentTab.value.selectedStatement || currentTab.value.queryStatement;+ const { data } = parseSQL(sqlStatement);+ return data !== null ? transformSQL(data) : sqlStatement;+});const ctx = connectionContext.value;diff --git a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vueindex 648ca25d04..1b69ff6979 100644--- a/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue+++ b/frontend/src/views/SqlEditor/EditorPanel/QueryEditor.vue@@ -10,35 +10,33 @@<script lang="ts" setup>import { computed } from "vue";import { debounce } from "lodash-es";-import {- useNamespacedActions,- useNamespacedState,-} from "vuex-composition-helpers";-import { SqlEditorActions, EditorSelectorState } from "../../../types";+import { EditorSelectorActions, TabInfo } from "../../../types";import { useExecuteSQL } from "../../../composables/useExecuteSQL";+import { useStore } from "vuex";+import { useNamespacedActions } from "vuex-composition-helpers";-const { activeTab } = useNamespacedState<EditorSelectorState>(- "editorSelector",- ["activeTab"]+const store = useStore();+const activeTab = computed<TabInfo>(+ () => store.getters["editorSelector/currentTab"]);-const { setSqlEditorState } = useNamespacedActions<SqlEditorActions>(- "sqlEditor",- ["setSqlEditorState"]+const { updateActiveTab } = useNamespacedActions<EditorSelectorActions>(+ "editorSelector",+ ["updateActiveTab"]);const { execute } = useExecuteSQL();-const sqlCode = computed(() => activeTab.value.queries);+const sqlCode = computed(() => activeTab.value.queryStatement);const handleChange = debounce((value: string) => {- setSqlEditorState({+ updateActiveTab({queryStatement: value,});}, 300);const handleChangeSelection = debounce((value: string) => {- setSqlEditorState({+ updateActiveTab({selectedStatement: value,});}, 300);diff --git a/frontend/src/views/SqlEditor/QuerySelector.vue b/frontend/src/views/SqlEditor/QuerySelector.vueindex 58107d81c9..947982f302 100644--- a/frontend/src/views/SqlEditor/QuerySelector.vue+++ b/frontend/src/views/SqlEditor/QuerySelector.vue@@ -4,10 +4,10 @@v-for="tab in queryTabList":key="tab.id"class="query-selector--tab"- :class="{ active: tab.id === activeTab.id }"+ :class="{ active: tab.id === activeTabId }"@click="handleSelectTab(tab)"- @mouseover="enterTabIdx = tab.idx"- @mouseleave="enterTabIdx = -1"+ @mouseover="enterTabId = tab.id"+ @mouseleave="enterTabId = ''"><span class="prefix"><carbon:code class="h-4 w-4" />@@ -15,7 +15,7 @@<span class="label">{{ tab.label }}</span>- <template v-if="enterTabIdx === tab.idx && queryTabList.length > 1">+ <template v-if="enterTabId === tab.id && queryTabList.length > 1"><spanclass="suffix close hover:bg-gray-200 rounded-sm"@click.prevent="handleRemoveTab(tab)"@@ -29,7 +29,7 @@<carbon:dot-mark class="h-4 w-4" /></span></template> -->- <template v-if="tab.id === activeTab.id && queryTabList.length > 1">+ <template v-if="tab.id === activeTabId && queryTabList.length > 1"><spanclass="suffix close hover:bg-gray-200 rounded-sm"@click.prevent="handleRemoveTab(tab)"@@ -53,50 +53,35 @@</template><script lang="ts" setup>-import { ref, watch } from "vue";+import { ref } from "vue";import {useNamespacedState,useNamespacedActions,} from "vuex-composition-helpers";-import { debouncedWatch } from "@vueuse/core";import {EditorSelectorState,TabInfo,AnyTabInfo,EditorSelectorActions,- SqlEditorState,- SqlEditorActions,} from "../../types";import { isDev } from "../../utils";-const { queryTabList, activeTab } = useNamespacedState<EditorSelectorState>(+const { activeTabId, queryTabList } = useNamespacedState<EditorSelectorState>("editorSelector",- ["queryTabList", "activeTab"]+ ["activeTabId", "queryTabList"]);-const { addTab, setActiveTab, removeTab, updateTab } =+const { addTab, removeTab, setActiveTabId } =useNamespacedActions<EditorSelectorActions>("editorSelector", ["addTab",- "setActiveTab","removeTab",- "updateTab",+ "setActiveTabId",]);-const { queryStatement } = useNamespacedState<SqlEditorState>("sqlEditor", [- "queryStatement",-]);-const { setSqlEditorState } = useNamespacedActions<SqlEditorActions>(- "sqlEditor",- ["setSqlEditorState"]-);--const enterTabIdx = ref(-1);-if (queryTabList.value.length === 0) {- addTab({});-}+const enterTabId = ref("");const handleSelectTab = (tab: TabInfo) => {- setActiveTab(tab);+ setActiveTabId(tab.id);};const handleAddTab = (tab: AnyTabInfo) => {addTab(tab);@@ -104,34 +89,6 @@ const handleAddTab = (tab: AnyTabInfo) => {const handleRemoveTab = (tab: TabInfo) => {removeTab(tab);};--debouncedWatch(- () => queryStatement.value,- (newVal, oldVal) => {- if (newVal !== oldVal && newVal !== "") {- updateTab({- ...activeTab.value,- queries: newVal,- isSaved: false,- });- }- },- {- deep: true,- debounce: 333,- }-);--// initial tab state-watch(- () => activeTab.value,- (tab) => {- setSqlEditorState({- queryStatement: tab.queries,- queryResult: null,- });- }-);</script><style scoped>diff --git a/frontend/src/views/SqlEditor/SqlEditor.vue b/frontend/src/views/SqlEditor/SqlEditor.vueindex 091c66fb30..c640b6b332 100644--- a/frontend/src/views/SqlEditor/SqlEditor.vue+++ b/frontend/src/views/SqlEditor/SqlEditor.vue@@ -7,9 +7,9 @@</Pane><Pane size="80"><template v-if="hasTabs">- <Splitpanes horizontal class="default-theme">+ <Splitpanes :key="paneKey" horizontal class="default-theme"><Pane size="60">- <EditorPanel :key="paneKey" />+ <EditorPanel /></Pane><Pane size="40"><TablePanel />diff --git a/frontend/src/views/SqlEditor/TablePanel/TableView.vue b/frontend/src/views/SqlEditor/TablePanel/TableView.vueindex 0a519b1dc5..70041ad01d 100644--- a/frontend/src/views/SqlEditor/TablePanel/TableView.vue+++ b/frontend/src/views/SqlEditor/TablePanel/TableView.vue@@ -63,9 +63,13 @@<script lang="ts" setup>import { computed, reactive, ref } from "vue";import { useResizeObserver } from "@vueuse/core";-import { useNamespacedState } from "vuex-composition-helpers";+import {+ useNamespacedGetters,+ useNamespacedState,+} from "vuex-composition-helpers";import { useI18n } from "vue-i18n";+import { EditorSelectorGetters, SqlEditorState } from "../../../types";interface State {search: string;@@ -73,10 +77,16 @@ interface State {const { t } = useI18n();-const { queryResult, isExecuting } = useNamespacedState<{- queryResult: Record<string, any>[] | null;- isExecuting: boolean;-}>("sqlEditor", ["queryResult", "isExecuting"]);+const { isExecuting } = useNamespacedState<SqlEditorState>("sqlEditor", [+ "isExecuting",+]);++const { currentTab } = useNamespacedGetters<EditorSelectorGetters>(+ "editorSelector",+ ["currentTab"]+);++const queryResult = computed(() => currentTab.value.queryResult || []);const state = reactive<State>({search: "",