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: migrate validator
d-bytebasedf93203d7e58
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 336dc0dffc..d019ca2270 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -19,7 +19,6 @@ import ("github.com/google/cel-go/cel""github.com/labstack/echo/v4""github.com/lib/pq"- tidbast "github.com/pingcap/tidb/parser/ast""github.com/pkg/errors""github.com/xuri/excelize/v2""google.golang.org/grpc/codes"@@ -38,11 +37,8 @@ import ("github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/pg""github.com/bytebase/bytebase/backend/plugin/parser/base"- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"- plsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql"parser "github.com/bytebase/bytebase/backend/plugin/parser/sql""github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"- tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb""github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"@@ -2001,53 +1997,9 @@ func validateQueryRequest(instance *store.InstanceMessage, databaseName string,return nil}- switch instance.Engine {- case storepb.Engine_MYSQL:- trees, err := mysqlparser.ParseMySQL(statement)- if err != nil {- return status.Errorf(codes.InvalidArgument, "failed to parse query: %s", err.Error())- }- for _, item := range trees {- tree := item.Tree- if err := mysqlparser.ValidateForEditor(tree); err != nil {- return nonSelectSQLError.Err()- }- }- case storepb.Engine_TIDB:- stmtList, err := tidbparser.ParseTiDB(statement, "", "")- if err != nil {- return status.Errorf(codes.InvalidArgument, "failed to parse query: %s", err.Error())- }- for _, stmt := range stmtList {- switch stmt := stmt.(type) {- case *tidbast.SelectStmt:- case *tidbast.ExplainStmt:- // Disable DESC command.- if _, ok := stmt.Stmt.(*tidbast.ShowStmt); ok {- return nonSelectSQLError.Err()- }- default:- return nonSelectSQLError.Err()- }- }- case storepb.Engine_ORACLE, storepb.Engine_DM:- if instance.Options != nil && instance.Options.SchemaTenantMode && databaseName == "" {- return status.Error(codes.InvalidArgument, "connection_database is required for oracle schema tenant mode instance")- }- tree, _, err := plsqlparser.ParsePLSQL(statement)- if err != nil {- return status.Errorf(codes.InvalidArgument, "failed to parse query: %s", err.Error())- }- if err := plsqlparser.ValidateForEditor(tree); err != nil {- return nonSelectSQLError.Err()- }- default:- // TODO(rebelice): support multiple statements here.- if !parser.ValidateSQLForEditor(instance.Engine, statement) {- return nonSelectSQLError.Err()- }+ if !parser.ValidateSQLForEditor(instance.Engine, statement) {+ return nonSelectSQLError.Err()}-return nil}diff --git a/backend/plugin/parser/mysql/query.go b/backend/plugin/parser/mysql/query.goindex 569fae76b7..e848ab5620 100644--- a/backend/plugin/parser/mysql/query.go+++ b/backend/plugin/parser/mysql/query.go@@ -5,13 +5,8 @@ import ("github.com/antlr4-go/antlr/v4"parser "github.com/bytebase/mysql-parser"- "github.com/pkg/errors"- "golang.org/x/exp/slog"- "github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/plugin/parser/base"- standardparser "github.com/bytebase/bytebase/backend/plugin/parser/standard"- "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"storepb "github.com/bytebase/bytebase/proto/generated-go/store")@@ -19,20 +14,28 @@ func init() {base.RegisterQueryValidator(storepb.Engine_MYSQL, ValidateSQLForEditor)base.RegisterQueryValidator(storepb.Engine_MARIADB, ValidateSQLForEditor)base.RegisterQueryValidator(storepb.Engine_OCEANBASE, ValidateSQLForEditor)- base.RegisterQueryValidator(storepb.Engine_TIDB, ValidateSQLForEditor)}-// ValidateForEditor validates the given SQL statement for editor.-func ValidateForEditor(tree antlr.Tree) error {- l := &mysqlValidateForEditorListener{- validate: true,+// ValidateSQLForEditor validates the SQL statement for SQL editor.+// We validate the statement by following steps:+// 1. Remove all quoted text(quoted identifier, string literal) and comments from the statement.+// 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement.+// 3. For CTE, use regexp to check if the statement has UPDATE, DELETE and INSERT statements.+func ValidateSQLForEditor(statement string) bool {+ trees, err := ParseMySQL(statement)+ if err != nil {+ return false}-- antlr.ParseTreeWalkerDefault.Walk(l, tree)- if !l.validate {- return errors.New("only support SELECT sql statement")+ for _, item := range trees {+ l := &mysqlValidateForEditorListener{+ validate: true,+ }+ antlr.ParseTreeWalkerDefault.Walk(l, item.Tree)+ if !l.validate {+ return false+ }}- return nil+ return true}type mysqlValidateForEditorListener struct {@@ -223,18 +226,3 @@ func (l *mysqlResourceExtractListener) EnterTableRef(ctx *parser.TableRefContextresource.Table = tablel.resourceMap[resource.String()] = resource}--// ValidateSQLForEditor validates the SQL statement for SQL editor.-// We validate the statement by following steps:-// 1. Remove all quoted text(quoted identifier, string literal) and comments from the statement.-// 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement.-// 3. For CTE, use regexp to check if the statement has UPDATE, DELETE and INSERT statements.-func ValidateSQLForEditor(statement string) bool {- textWithoutQuotedAndComment, err := tokenizer.MysqlRemoveQuotedTextAndComment(statement)- if err != nil {- slog.Debug("Failed to remove quoted text and comment", slog.String("statement", statement), log.BBError(err))- return false- }-- return standardparser.CheckStatementWithoutQuotedTextAndComment(textWithoutQuotedAndComment)-}diff --git a/backend/plugin/parser/mysql/query_test.go b/backend/plugin/parser/mysql/query_test.goindex 6abea8b669..80ec75fa96 100644--- a/backend/plugin/parser/mysql/query_test.go+++ b/backend/plugin/parser/mysql/query_test.go@@ -36,14 +36,8 @@ func TestMySQLValidateForEditor(t *testing.T) {}for _, test := range tests {- trees, err := ParseMySQL(test.statement)- require.NoError(t, err)- err = ValidateForEditor(trees[0].Tree)- if test.validate {- require.NoError(t, err)- } else {- require.Error(t, err)- }+ got := ValidateSQLForEditor(test.statement)+ require.Equal(t, test.validate, got)}}diff --git a/backend/plugin/parser/plsql/query.go b/backend/plugin/parser/plsql/query.goindex 02153c7a37..44f2b39247 100644--- a/backend/plugin/parser/plsql/query.go+++ b/backend/plugin/parser/plsql/query.go@@ -9,8 +9,26 @@ import (parser "github.com/bytebase/plsql-parser""github.com/bytebase/bytebase/backend/plugin/parser/base"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")+func init() {+ base.RegisterQueryValidator(storepb.Engine_ORACLE, ValidateSQLForEditor)+ base.RegisterQueryValidator(storepb.Engine_DM, ValidateSQLForEditor)+}++// ValidateSQLForEditor validates the SQL statement for SQL editor.+func ValidateSQLForEditor(statement string) bool {+ tree, _, err := ParsePLSQL(statement)+ if err != nil {+ return false+ }+ if err := ValidateForEditor(tree); err != nil {+ return false+ }+ return true+}+func ExtractChangedResources(currentDatabase string, currentSchema string, statement string) ([]base.SchemaResource, error) {tree, _, err := ParsePLSQL(statement)if err != nil {diff --git a/backend/plugin/parser/tidb/query.go b/backend/plugin/parser/tidb/query.goindex d4c802166d..3b31b173fb 100644--- a/backend/plugin/parser/tidb/query.go+++ b/backend/plugin/parser/tidb/query.go@@ -8,8 +8,38 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/plugin/parser/base"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")+func init() {+ base.RegisterQueryValidator(storepb.Engine_TIDB, ValidateSQLForEditor)+}++// ValidateSQLForEditor validates the SQL statement for SQL editor.+// We validate the statement by following steps:+// 1. Remove all quoted text(quoted identifier, string literal) and comments from the statement.+// 2. Use regexp to check if the statement is a normal SELECT statement and EXPLAIN statement.+// 3. For CTE, use regexp to check if the statement has UPDATE, DELETE and INSERT statements.+func ValidateSQLForEditor(statement string) bool {+ stmtList, err := ParseTiDB(statement, "", "")+ if err != nil {+ return false+ }+ for _, stmt := range stmtList {+ switch stmt := stmt.(type) {+ case *tidbast.SelectStmt:+ case *tidbast.ExplainStmt:+ // Disable DESC command.+ if _, ok := stmt.Stmt.(*tidbast.ShowStmt); ok {+ return false+ }+ default:+ return false+ }+ }+ return true+}+func ExtractTiDBResourceList(currentDatabase string, sql string) ([]base.SchemaResource, error) {nodes, err := ParseTiDB(sql, "", "")if err != nil {diff --git a/backend/plugin/parser/tokenizer/tokenizer.go b/backend/plugin/parser/tokenizer/tokenizer.goindex 69b6f555e6..4455b89921 100644--- a/backend/plugin/parser/tokenizer/tokenizer.go+++ b/backend/plugin/parser/tokenizer/tokenizer.go@@ -1051,84 +1051,3 @@ func StandardRemoveQuotedTextAndComment(statement string) (string, error) {}}}--// MysqlRemoveQuotedTextAndComment removes quoted text and comment.-func MysqlRemoveQuotedTextAndComment(statement string) (string, error) {- var buf bytes.Buffer- t := NewTokenizer(statement)-- t.skipBlank()- startPos := t.pos()- for {- switch {- case t.char(0) == eofRune:- text := t.getString(startPos, t.pos()-startPos)- if _, err := buf.WriteString(text); err != nil {- return "", err- }- return buf.String(), nil- case t.char(0) == '/' && t.char(1) == '*':- text := t.getString(startPos, t.pos()-startPos)- if err := t.scanComment(); err != nil {- return "", err- }- if _, err := buf.WriteString(text); err != nil {- return "", err- }- if err := buf.WriteByte(' '); err != nil {- return "", err- }- startPos = t.pos()- case t.char(0) == '-' && t.char(1) == '-':- text := t.getString(startPos, t.pos()-startPos)- if err := t.scanComment(); err != nil {- return "", err- }- if _, err := buf.WriteString(text); err != nil {- return "", err- }- if err := buf.WriteByte(' '); err != nil {- return "", err- }- startPos = t.pos()- case t.char(0) == '#':- text := t.getString(startPos, t.pos()-startPos)- if err := t.scanComment(); err != nil {- return "", err- }- if _, err := buf.WriteString(text); err != nil {- return "", err- }- if err := buf.WriteByte(' '); err != nil {- return "", err- }- startPos = t.pos()- case t.char(0) == '\'' || t.char(0) == '"':- text := t.getString(startPos, t.pos()-startPos)- if err := t.scanString(t.char(0)); err != nil {- return "", err- }- if _, err := buf.WriteString(text); err != nil {- return "", err- }- if err := buf.WriteByte(' '); err != nil {- return "", err- }- startPos = t.pos()- case t.char(0) == '`':- text := t.getString(startPos, t.pos()-startPos)- if err := t.scanIdentifier('`'); err != nil {- return "", err- }- if _, err := buf.WriteString(text); err != nil {- return "", err- }- if err := buf.WriteByte(' '); err != nil {- return "", err- }- startPos = t.pos()- default:- t.skip(1)- }- }-}