Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: improve tidb explain result style (#8480)
Danny Xua3b23e673d36
diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.goindex 8dd5b19a33..478b0d45dd 100644--- a/backend/plugin/db/mysql/mysql.go+++ b/backend/plugin/db/mysql/mysql.go@@ -9,6 +9,7 @@ import ("net""strings""time"+ "unicode""github.com/go-sql-driver/mysql""github.com/pkg/errors"@@ -226,9 +227,10 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, singlereturn nil, nil}statement := strings.TrimLeft(strings.TrimRight(singleSQL.Text, " \n\t;"), " \n\t")+ isExplain := strings.HasPrefix(statement, "EXPLAIN")stmt := statement- if !strings.HasPrefix(stmt, "EXPLAIN") && queryContext.Limit > 0 {+ if !isExplain && queryContext.Limit > 0 {var err errorstmt, err = driver.getStatementWithResultLimit(stmt, queryContext.Limit)if err != nil {@@ -249,9 +251,36 @@ func (driver *Driver) querySingleSQL(ctx context.Context, conn *sql.Conn, single}result.Latency = durationpb.New(time.Since(startTime))result.Statement = statement+ if isExplain && driver.dbType == db.TiDB {+ if err := updateTiDBExplainResult(result); err != nil {+ return nil, err+ }+ }return result, nil}+func updateTiDBExplainResult(result *v1pb.QueryResult) error {+ for _, row := range result.Rows {+ if len(row.Values) > 0 {+ str := row.Values[0].GetStringValue()+ var sb strings.Builder+ for i, char := range str {+ if unicode.IsLetter(char) {+ if _, err := sb.WriteString(str[i:]); err != nil {+ return err+ }+ break+ }+ if _, err := sb.WriteString("-"); err != nil {+ return err+ }+ }+ row.Values[0] = &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: sb.String()}}+ }+ }+ return nil+}+// RunStatement runs a SQL statement in a given connection.func (*Driver) RunStatement(ctx context.Context, conn *sql.Conn, statement string) ([]*v1pb.QueryResult, error) {return util.RunStatement(ctx, parser.MySQL, conn, statement)diff --git a/backend/plugin/db/mysql/mysql_test.go b/backend/plugin/db/mysql/mysql_test.gonew file mode 100644index 0000000000..f3b88149d9--- /dev/null+++ b/backend/plugin/db/mysql/mysql_test.go@@ -0,0 +1,48 @@+package mysql++import (+ "testing"++ "github.com/stretchr/testify/require"++ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"+)++func TestUpdateTiDBExplainResult(t *testing.T) {+ a := require.New(t)+ got := &v1pb.QueryResult{+ Rows: []*v1pb.QueryRow{+ convertExplainRow("Projection_9", "a"),+ convertExplainRow("└─IndexJoin_13", "b"),+ convertExplainRow("├─TableReader_26(Build)", "c"),+ convertExplainRow("│ └─Selection_25", "d"),+ convertExplainRow("│ └─TableFullScan_24", "e"),+ convertExplainRow("└─IndexLookUp_12(Probe)", "f"),+ convertExplainRow("├─IndexRangeScan_10(Build)", "g"),+ convertExplainRow("└─TableRowIDScan_11(Probe)", "h"),+ },+ }+ want := &v1pb.QueryResult{+ Rows: []*v1pb.QueryRow{+ convertExplainRow("Projection_9", "a"),+ convertExplainRow("--IndexJoin_13", "b"),+ convertExplainRow("--TableReader_26(Build)", "c"),+ convertExplainRow("----Selection_25", "d"),+ convertExplainRow("------TableFullScan_24", "e"),+ convertExplainRow("--IndexLookUp_12(Probe)", "f"),+ convertExplainRow("--IndexRangeScan_10(Build)", "g"),+ convertExplainRow("--TableRowIDScan_11(Probe)", "h"),+ },+ }+ err := updateTiDBExplainResult(got)+ a.NoError(err)+ a.Equal(want, got)+}++func convertExplainRow(values ...string) *v1pb.QueryRow {+ row := &v1pb.QueryRow{}+ for _, v := range values {+ row.Values = append(row.Values, &v1pb.RowValue{Kind: &v1pb.RowValue_StringValue{StringValue: v}})+ }+ return row+}