Skip to content

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

feat: allow desc and show commands in sql editor read only mode for tidb (#12392)

h3n4l0596086f9234
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.go
index c30b5d6b3e..796fd99f0e 100644
--- a/backend/plugin/db/mysql/mysql.go
+++ b/backend/plugin/db/mysql/mysql.go
@@ -42,6 +42,9 @@ var (
viewTableType = "VIEW"
_ db.Driver = (*Driver)(nil)
+
+ variableSetStmtRegexp = regexp.MustCompile(`(?i)^SET\s+?`)
+ variableShowStmtRegexp = regexp.MustCompile(`(?i)^SHOW\s+?`)
)
func init() {
@@ -501,8 +504,9 @@ func getConnectionID(ctx context.Context, conn *sql.Conn) (string, error) {
func (d *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimLeft(strings.TrimRight(singleSQL.Text, " \n\t;"), " \n\t")
- isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
- if !isSet {
+ isSet := variableSetStmtRegexp.MatchString(statement)
+ isShow := variableShowStmtRegexp.MatchString(statement)
+ if !isSet && !isShow {
if queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
} else if queryContext.Limit > 0 {
diff --git a/backend/plugin/db/tidb/query.go b/backend/plugin/db/tidb/query.go
index a14eafbfd5..3166787ff3 100644
--- a/backend/plugin/db/tidb/query.go
+++ b/backend/plugin/db/tidb/query.go
@@ -68,10 +68,9 @@ func getStatementWithResultLimitForTiDB(singleStatement string, limitCount int)
return "", err
}
return buffer.String(), nil
-
default:
continue
}
}
- return "", nil
+ return singleStatement, nil
}
diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.go
index ef8859932c..2154f30be0 100644
--- a/backend/plugin/db/tidb/tidb.go
+++ b/backend/plugin/db/tidb/tidb.go
@@ -36,6 +36,9 @@ var (
sequenceTableType = "SEQUENCE"
_ db.Driver = (*Driver)(nil)
+
+ variableSetStmtRegexp = regexp.MustCompile(`(?i)^SET\s+?`)
+ variableShowStmtRegexp = regexp.MustCompile(`(?i)^SHOW\s+?`)
)
func init() {
@@ -373,8 +376,9 @@ func getConnectionID(ctx context.Context, conn *sql.Conn) (string, error) {
func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singleSQL base.SingleSQL, queryContext *db.QueryContext) (*v1pb.QueryResult, error) {
statement := strings.TrimLeft(strings.TrimRight(singleSQL.Text, " \n\t;"), " \n\t")
- isSet, _ := regexp.MatchString(`(?i)^SET\s+?`, statement)
- if !isSet {
+ isSet := variableSetStmtRegexp.MatchString(statement)
+ isShow := variableShowStmtRegexp.MatchString(statement)
+ if !isSet && !isShow {
if queryContext.Explain {
statement = fmt.Sprintf("EXPLAIN %s", statement)
} else if queryContext.Limit > 0 {
diff --git a/backend/plugin/parser/tidb/query.go b/backend/plugin/parser/tidb/query.go
index 7563e475da..6a0cfc85fd 100644
--- a/backend/plugin/parser/tidb/query.go
+++ b/backend/plugin/parser/tidb/query.go
@@ -26,14 +26,11 @@ func validateQuery(statement string) (bool, error) {
return false, err
}
for _, stmt := range stmtList {
- switch stmt := stmt.(type) {
+ switch stmt.(type) {
case *tidbast.SelectStmt:
case *tidbast.SetOprStmt:
+ case *tidbast.ShowStmt:
case *tidbast.ExplainStmt:
- // Disable DESC command.
- if _, ok := stmt.Stmt.(*tidbast.ShowStmt); ok {
- return false, nil
- }
default:
return false, nil
}
diff --git a/backend/plugin/parser/tidb/query_span_extractor.go b/backend/plugin/parser/tidb/query_span_extractor.go
index 0d97f07477..b915883e80 100644
--- a/backend/plugin/parser/tidb/query_span_extractor.go
+++ b/backend/plugin/parser/tidb/query_span_extractor.go
@@ -56,8 +56,8 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, statement string)
case *tidbast.SelectStmt:
case *tidbast.SetOprStmt:
case *tidbast.CreateViewStmt:
- case *tidbast.ExplainStmt:
- // Skip the EXPLAIN statement.
+ case *tidbast.ExplainStmt, *tidbast.ShowStmt:
+ // Skip the EXPLAIN and SHOW statement.
return &base.QuerySpan{
Results: []base.QuerySpanResult{},
SourceColumns: base.SourceColumnSet{},