Skip to content

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

fix: sql parser for explain statement (#353)

STEVENfbf5d379152d
diff --git a/frontend/src/components/MonacoEditor/sqlParser.ts b/frontend/src/components/MonacoEditor/sqlParser.ts
index d09000e09e..3afa60e7cf 100644
--- a/frontend/src/components/MonacoEditor/sqlParser.ts
+++ b/frontend/src/components/MonacoEditor/sqlParser.ts
@@ -17,11 +17,6 @@ const parseSQL = (sql: string): ParseResult => {
}
};
-export const isValidStatement = (sql: string) => {
- const { data } = parseSQL(sql);
- return data !== null;
-};
-
export const isSelectStatement = (sql: string) => {
const { data } = parseSQL(sql);
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index ab8854d40f..db42353a9f 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -1,10 +1,7 @@
import { ref } from "vue";
import { isEmpty } from "lodash-es";
import { useStore } from "vuex";
-import {
- isSelectStatement,
- isValidStatement,
-} from "../components/MonacoEditor/sqlParser";
+import { isSelectStatement } from "../components/MonacoEditor/sqlParser";
const useExecuteSQL = () => {
const store = useStore();
@@ -24,15 +21,6 @@ const useExecuteSQL = () => {
return;
}
- if (!isValidStatement(sqlStatement)) {
- store.dispatch("notification/pushNotification", {
- module: "bytebase",
- style: "CRITICAL",
- title: "Please check if the statement is correct",
- });
- return;
- }
-
if (!isSelectStatement(sqlStatement)) {
store.dispatch("notification/pushNotification", {
module: "bytebase",
diff --git a/server/sql.go b/server/sql.go
index 2e93191fa6..d8c491baa8 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -568,13 +568,13 @@ func getLatestSchemaVersion(ctx context.Context, driver db.Driver, databaseName
}
func validateSQLSelectStatement(sqlStatement string) bool {
+ whiteListRegs := []string{`^SELECT$`, `^EXPLAIN\s+?SELECT$`, `^(EXPLAIN\s+?)?SELECT\s`}
formatedStr := strings.ToUpper(strings.TrimSpace(sqlStatement))
- if formatedStr == "SELECT" {
- return true
- }
- matchResult, err := regexp.MatchString(`^SELECT\s`, formatedStr)
- if err != nil {
- return false
+ for _, reg := range whiteListRegs {
+ matchResult, _ := regexp.MatchString(reg, formatedStr)
+ if matchResult {
+ return true
+ }
}
- return matchResult
+ return false
}
diff --git a/server/sql_test.go b/server/sql_test.go
index 5c7cba6245..7c87026443 100644
--- a/server/sql_test.go
+++ b/server/sql_test.go
@@ -37,6 +37,30 @@ func TestValidateSQLSelectStatement(t *testing.T) {
sqlStatement: "select",
want: true,
},
+ {
+ sqlStatement: "explain select",
+ want: true,
+ },
+ {
+ sqlStatement: "explain \n select",
+ want: true,
+ },
+ {
+ sqlStatement: "\n explain \n \r select",
+ want: true,
+ },
+ {
+ sqlStatement: "explain select *",
+ want: true,
+ },
+ {
+ sqlStatement: " explain select ",
+ want: true,
+ },
+ {
+ sqlStatement: "asd explain selectasd ",
+ want: false,
+ },
{
sqlStatement: "SELECTfoo",
want: false,
@@ -49,6 +73,10 @@ func TestValidateSQLSelectStatement(t *testing.T) {
sqlStatement: "SETEST * FROM test",
want: false,
},
+ {
+ sqlStatement: " asdexplain selectasd ",
+ want: false,
+ },
{
sqlStatement: "",
want: false,