Skip to content

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

fix: mysql/tidb validate (#13121)

Danny Xuc98cc70b91a2
diff --git a/backend/plugin/parser/mysql/query.go b/backend/plugin/parser/mysql/query.go
index 552d4fa814..4393a22e5a 100644
--- a/backend/plugin/parser/mysql/query.go
+++ b/backend/plugin/parser/mysql/query.go
@@ -87,16 +87,13 @@ func (l *queryValidateListener) EnterUtilityStatement(ctx *parser.UtilityStateme
}
if ctx.ExplainStatement() == nil && ctx.DescribeStatement() == nil {
l.valid = false
- }
-}
-
-// EnterExplainableStatement is called when production explainableStatement is entered.
-func (l *queryValidateListener) EnterExplainableStatement(ctx *parser.ExplainableStatementContext) {
- if !l.valid {
return
}
- if ctx.DeleteStatement() != nil || ctx.UpdateStatement() != nil || ctx.InsertStatement() != nil || ctx.ReplaceStatement() != nil {
- l.valid = false
+ if ctx.ExplainStatement() != nil {
+ if ctx.ExplainStatement().ANALYZE_SYMBOL() != nil {
+ l.valid = false
+ return
+ }
}
}
diff --git a/backend/plugin/parser/mysql/query_test.go b/backend/plugin/parser/mysql/query_test.go
index 13b7d3b778..f0c9fca077 100644
--- a/backend/plugin/parser/mysql/query_test.go
+++ b/backend/plugin/parser/mysql/query_test.go
@@ -47,8 +47,8 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
statement: "EXPLAIN FORMAT=JSON DELETE FROM t1;",
- valid: false,
- gotAllQuery: false,
+ valid: true,
+ gotAllQuery: true,
},
{
statement: `select* from t`,
@@ -62,8 +62,13 @@ func TestValidateSQLForEditor(t *testing.T) {
},
{
statement: `explain analyze select * from t`,
- valid: true,
- gotAllQuery: true,
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: `explain analyze update t set a = 5`,
+ valid: false,
+ gotAllQuery: false,
},
{
statement: `
diff --git a/backend/plugin/parser/tidb/query.go b/backend/plugin/parser/tidb/query.go
index 918d7b8ad5..803d0c276a 100644
--- a/backend/plugin/parser/tidb/query.go
+++ b/backend/plugin/parser/tidb/query.go
@@ -27,12 +27,15 @@ func validateQuery(statement string) (bool, bool, error) {
}
hasExecute := false
for _, stmt := range stmtList {
- switch stmt.(type) {
+ switch stmt := stmt.(type) {
case *tidbast.SelectStmt:
- case *tidbast.SetOprStmt:
+ case *tidbast.SetStmt:
hasExecute = true
case *tidbast.ShowStmt:
case *tidbast.ExplainStmt:
+ if stmt.Analyze {
+ return false, false, nil
+ }
default:
return false, false, nil
}
diff --git a/backend/plugin/parser/tidb/query_test.go b/backend/plugin/parser/tidb/query_test.go
new file mode 100644
index 0000000000..9f40e71a0d
--- /dev/null
+++ b/backend/plugin/parser/tidb/query_test.go
@@ -0,0 +1,134 @@
+package tidb
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestValidateSQLForEditor(t *testing.T) {
+ tests := []struct {
+ statement string
+ valid bool
+ gotAllQuery bool
+ err bool
+ }{
+ {
+ statement: "SHOW CREATE TABLE bytebase;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: "DESC bytebase;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: "SELECT * FROM t1 WHERE c1 = 1; SELECT * FROM t2;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: "CREATE TABLE t1 (c1 INT);",
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: "UPDATE t1 SET c1 = 1;",
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: "EXPLAIN SELECT * FROM t1;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: "EXPLAIN FORMAT=JSON DELETE FROM t1;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: `select* from t`,
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: `explain select * from t;`,
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: `explain analyze select * from t`,
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: `explain analyze update t set a = 5`,
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: `
+ With t as (
+ select * from t1
+ ), tx as (
+ select * from t1
+ )
+ update t set a = 1;
+ `,
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: `
+ With t as (
+ select * from t1
+ ), tx as (
+ select * from t1
+ )
+ insert into t values (1, 2, 3);
+ `,
+ valid: false,
+ gotAllQuery: false,
+ err: true,
+ },
+ {
+ statement: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: `
+ With t as (
+ select * from t1 where a = 'insert'
+ ), tx as (` +
+ " select * from `delete`" +
+ `) /* UPDATE */` +
+ "select `update` from t;",
+ valid: true,
+ gotAllQuery: true,
+ },
+ {
+ statement: `create table t (a int);`,
+ valid: false,
+ gotAllQuery: false,
+ },
+ {
+ statement: `SET max_execution_time = 1000; select * from t`,
+ valid: true,
+ gotAllQuery: false,
+ },
+ }
+
+ for _, test := range tests {
+ gotValid, gotAllQuery, err := validateQuery(test.statement)
+ if test.err {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ require.Equal(t, test.valid, gotValid, test.statement)
+ require.Equal(t, test.gotAllQuery, gotAllQuery, test.statement)
+ }
+ }
+}