Skip to content

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

feat(sql-editor): run non-SELECT SQL navigate to the different workflow (#622)

Yunwei Xiaofc405e619367
diff --git a/frontend/src/components/MonacoEditor/sqlParser.ts b/frontend/src/components/MonacoEditor/sqlParser.ts
index ad8b823133..f4460051d1 100644
--- a/frontend/src/components/MonacoEditor/sqlParser.ts
+++ b/frontend/src/components/MonacoEditor/sqlParser.ts
@@ -6,6 +6,10 @@ type ParseResult = {
error: Error | null;
};
+const DDL_TYPE = ["create", "alter", "drop"];
+const DML_TYPE = ["insert", "update", "delete"];
+const SELECT = "select";
+
export const parseSQL = (sql: string): ParseResult => {
if (sql === "") {
return { data: [], error: null };
@@ -29,11 +33,11 @@ export const isValidStatement = (data: ParseResult["data"]) => {
export const isSelectStatement = (data: ParseResult["data"]) => {
// if the sql statement is an object, it's a single sql statement
if (isObject(data) && !isArray(data)) {
- return data.type.toLowerCase() === "select";
+ return data.type.toLowerCase() === SELECT;
}
// if the sql statement is an array, it's a multiple sql statements
if (isArray(data)) {
- return data.every((statement) => statement.type.toLowerCase() === "select");
+ return data.every((statement) => statement.type.toLowerCase() === SELECT);
}
};
@@ -42,6 +46,32 @@ export const isMultipleStatements = (data: ParseResult["data"]) => {
return isArray(data) && data.length > 1;
};
+export const isDDLStatement = (data: ParseResult["data"]) => {
+ // if the sql statement is an object, it's a single sql statement
+ if (isObject(data) && !isArray(data)) {
+ return DDL_TYPE.includes(data.type.toLowerCase());
+ }
+ // if the sql statement is an array, it's a multiple sql statements
+ if (isArray(data)) {
+ return data.every((statement) =>
+ DDL_TYPE.includes(statement.type.toLowerCase())
+ );
+ }
+};
+
+export const isDMLStatement = (data: ParseResult["data"]) => {
+ // if the sql statement is an object, it's a single sql statement
+ if (isObject(data) && !isArray(data)) {
+ return DML_TYPE.includes(data.type.toLowerCase());
+ }
+ // if the sql statement is an array, it's a multiple sql statements
+ if (isArray(data)) {
+ return data.every((statement) =>
+ DML_TYPE.includes(statement.type.toLowerCase())
+ );
+ }
+};
+
export const transformSQL = (data: AST | AST[], database = "MySQL") => {
const parser = new Parser();
return parser.sqlify(data, { database });
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index a0d6484c68..ea567adee4 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -8,6 +8,8 @@ import {
transformSQL,
isSelectStatement,
isMultipleStatements,
+ isDDLStatement,
+ isDMLStatement,
} from "../components/MonacoEditor/sqlParser";
const useExecuteSQL = () => {
@@ -49,13 +51,29 @@ const useExecuteSQL = () => {
return;
}
- if (data !== null && !isSelectStatement(data)) {
- store.dispatch("sqlEditor/setSqlEditorState", {
- isShowExecutingHint: true,
- });
+ if (data === undefined) {
+ notify("CRITICAL", t("sql-editor.notify-invalid-sql-statement"));
return;
}
+ if (data !== null && !isSelectStatement(data)) {
+ if (isMultipleStatements(data)) {
+ notify(
+ "INFO",
+ t("common.tips"),
+ t("sql-editor.notify-multiple-statements")
+ );
+ return;
+ }
+ // only DDL and DML statements are allowed
+ if (isDDLStatement(data) || isDMLStatement(data)) {
+ store.dispatch("sqlEditor/setSqlEditorState", {
+ isShowExecutingHint: true,
+ });
+ return;
+ }
+ }
+
if (isMultipleStatements(data)) {
notify(
"INFO",
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 744cf38d64..fa3ca9c297 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -1101,6 +1101,7 @@ sql-editor:
Multiple SQL statements detected. SQL Editor only executes the first
statement. You can select another statement and execute it individually.
goto-alter-schema-hint: Please select a database connection from the top of the editor
+ notify-invalid-sql-statement: Invalid SQL statement.
can-not-execute-query: Can not execute query when loading data.
rows: row | rows
no-history-found: No history found
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index 700fe2ed3c..7307c95864 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -971,6 +971,7 @@ sql-editor:
table-schema-placeholder: 选择一个表进行查看 Schema
notify-empty-statement: 请在编辑器中输入 SQL 语句
notify-multiple-statements: 检测到您输入了多条 SQL 语句。SQL 编辑器只支持执行第一条语句。您可以选择其他语句单独执行。
+ notify-invalid-sql-statement: 请检查您的 SQL 语句。
goto-alter-schema-hint: 请从编辑器顶部选择一个数据库连接
can-not-execute-query: 加载数据时无法执行查询
rows: 条记录
diff --git a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
index eaf5162278..a214117a79 100644
--- a/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
+++ b/frontend/src/views/SqlEditor/EditorPanel/ExecuteHint.vue
@@ -21,7 +21,13 @@
<p>
<i18n-t keypath="sql-editor.go-to-alter-schema">
<template #alterschema>
- <strong>{{ $t("database.alter-schema") }}</strong>
+ <strong>
+ {{
+ isDDLSQLStatement
+ ? $t("database.alter-schema")
+ : $t("database.change-data")
+ }}
+ </strong>
</template>
</i18n-t>
</p>
@@ -31,7 +37,11 @@
<div class="execute-hint-content mt-4 flex justify-end space-x-2">
<NButton @click="handleColse">{{ $t("common.close") }}</NButton>
<NButton type="primary" @click="gotoAlterSchema">
- {{ $t("database.alter-schema") }}
+ {{
+ isDDLSQLStatement
+ ? $t("database.alter-schema")
+ : $t("database.change-data")
+ }}
</NButton>
</div>
</div>
@@ -57,6 +67,7 @@ import type {
import {
parseSQL,
transformSQL,
+ isDDLStatement,
} from "../../../components/MonacoEditor/sqlParser";
const emit = defineEmits<{
@@ -76,11 +87,20 @@ const { connectionContext } = useNamespacedState<SqlEditorState>("sqlEditor", [
]);
const { currentTab } = useNamespacedGetters<TabGetters>("tab", ["currentTab"]);
-const parsedStatement = computed(() => {
- const sqlStatement =
- currentTab.value.selectedStatement || currentTab.value.statement;
- const { data } = parseSQL(sqlStatement);
- return data !== null ? transformSQL(data) : sqlStatement;
+const sqlStatement = computed(
+ () => currentTab.value.selectedStatement || currentTab.value.statement
+);
+
+const getParsedStatement = () => {
+ const statement = sqlStatement.value;
+ const { data } = parseSQL(statement);
+ return data !== null ? transformSQL(data) : statement;
+};
+
+const isDDLSQLStatement = computed(() => {
+ const statement = getParsedStatement();
+ const { data } = parseSQL(statement);
+ return data !== null ? isDDLStatement(data) : false;
});
const ctx = connectionContext.value;
@@ -105,6 +125,8 @@ const gotoAlterSchema = () => {
emit("close");
const projectId = findProjectIdByDatabaseId.value(ctx.databaseId as number);
+ const DDLIssueTemplate = "bb.issue.database.schema.update";
+ const DMLIssueTemplate = "bb.issue.database.data.update";
router.push({
name: "workspace.issue.detail",
@@ -112,11 +134,13 @@ const gotoAlterSchema = () => {
issueSlug: "new",
},
query: {
- template: "bb.issue.database.schema.update",
- name: `[${ctx.databaseName}] Alter schema`,
+ template: isDDLSQLStatement.value ? DDLIssueTemplate : DMLIssueTemplate,
+ name: `[${ctx.databaseName}] ${
+ isDDLSQLStatement.value ? "Alter schema" : "Change Data"
+ }`,
project: projectId,
databaseList: ctx.databaseId,
- sql: parsedStatement.value,
+ sql: getParsedStatement(),
},
});
};