Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: move validator
d-bytebasede996f148c5f
diff --git a/backend/plugin/parser/mysql/query.go b/backend/plugin/parser/mysql/query.goindex ccec3f9aeb..020a61d569 100644--- a/backend/plugin/parser/mysql/query.go+++ b/backend/plugin/parser/mysql/query.go@@ -6,8 +6,12 @@ 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")// ValidateForEditor validates the given SQL statement for editor.@@ -211,3 +215,18 @@ 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 23f67449ff..eb37dba2d5 100644--- a/backend/plugin/parser/mysql/query_test.go+++ b/backend/plugin/parser/mysql/query_test.go@@ -153,3 +153,70 @@ func TestExtractMySQLChangedResources(t *testing.T) {require.Equal(t, test.expected, resources, test.statement)}}++type testData struct {+ sql string+ ans bool+}++func TestValidateSQLForMySQL(t *testing.T) {+ tests := []testData{+ {+ sql: `select* from t`,+ ans: true,+ },+ {+ sql: `explain select * from t;`,+ ans: true,+ },+ {+ sql: `explain analyze select * from t`,+ ans: false,+ },+ {+ sql: `+ With t as (+ select * from t1+ ), tx as (+ select * from t1+ )+ update t set a = 1;+ `,+ ans: false,+ },+ {+ sql: `+ With t as (+ select * from t1+ ), tx as (+ select * from t1+ )+ insert into t values (1, 2, 3);+ `,+ ans: false,+ },+ {+ sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",+ ans: true,+ },+ {+ sql: `+ With t as (+ select * from t1 where a = 'insert'+ ), tx as (` ++ " select * from `delete`" ++ `) /* UPDATE */` ++ "select `update` from t;",+ ans: true,+ },+ {+ sql: `create table t (a int);`,+ ans: false,+ },+ }++ for _, test := range tests {+ ans := ValidateSQLForEditor(test.sql)+ require.Equal(t, test.ans, ans, test.sql)+ }+}diff --git a/backend/plugin/parser/sql/validate_sql_for_editor.go b/backend/plugin/parser/sql/validate_sql_for_editor.goindex 0fc8858fc5..bbe183774a 100644--- a/backend/plugin/parser/sql/validate_sql_for_editor.go+++ b/backend/plugin/parser/sql/validate_sql_for_editor.go@@ -1,13 +1,9 @@package parserimport (- "log/slog"- "regexp"- "strings"-- "github.com/bytebase/bytebase/backend/common/log"+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"- "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"+ standardparser "github.com/bytebase/bytebase/backend/plugin/parser/standard")// ValidateSQLForEditor validates the SQL statement for editor.@@ -21,69 +17,8 @@ func ValidateSQLForEditor(engine EngineType, statement string) bool {case Postgres, Redshift, RisingWave:return pgparser.ValidateSQLForEditor(statement)case MySQL, TiDB, MariaDB, OceanBase:- return mysqlValidateSQLForEditor(statement)+ return mysqlparser.ValidateSQLForEditor(statement)default:- return standardValidateSQLForEditor(statement)- }-}--// standardValidateSQLForEditor 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 standardValidateSQLForEditor(statement string) bool {- textWithoutQuotedAndComment, err := tokenizer.StandardRemoveQuotedTextAndComment(statement)- if err != nil {- slog.Debug("Failed to remove quoted text and comment", slog.String("statement", statement), log.BBError(err))- return false- }-- return checkStatementWithoutQuotedTextAndComment(textWithoutQuotedAndComment)-}--// mysqlValidateSQLForEditor 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 mysqlValidateSQLForEditor(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.ValidateSQLForEditor(statement)}-- return checkStatementWithoutQuotedTextAndComment(textWithoutQuotedAndComment)-}--func checkStatementWithoutQuotedTextAndComment(statement string) bool {- formattedStr := strings.ToUpper(strings.TrimSpace(statement))- if isSelect, _ := regexp.MatchString(`^SELECT\s+?`, formattedStr); isSelect {- return true- }-- if isSelect, _ := regexp.MatchString(`^SELECT\*\s+?`, formattedStr); isSelect {- return true- }-- if isExplain, _ := regexp.MatchString(`^EXPLAIN\s+?`, formattedStr); isExplain {- if isExplainAnalyze, _ := regexp.MatchString(`^EXPLAIN\s+ANALYZE\s+?`, formattedStr); isExplainAnalyze {- return false- }- return true- }-- cteRegex := regexp.MustCompile(`^WITH\s+?`)- if matchResult := cteRegex.MatchString(formattedStr); matchResult {- dmlRegs := []string{`\bINSERT\b`, `\bUPDATE\b`, `\bDELETE\b`}- for _, reg := range dmlRegs {- if matchResult, _ := regexp.MatchString(reg, formattedStr); matchResult {- return false- }- }- return true- }-- return false}diff --git a/backend/plugin/parser/sql/validate_sql_for_editor_test.go b/backend/plugin/parser/sql/validate_sql_for_editor_test.godeleted file mode 100644index 9513da533b..0000000000--- a/backend/plugin/parser/sql/validate_sql_for_editor_test.go+++ /dev/null@@ -1,151 +0,0 @@-package parser_test--import (- "testing"-- "github.com/stretchr/testify/require"-- parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"-)--type testData struct {- sql string- ans bool-}--func TestValidateSQLForMySQL(t *testing.T) {- tests := []testData{- {- sql: `select* from t`,- ans: true,- },- {- sql: `explain select * from t;`,- ans: true,- },- {- sql: `explain analyze select * from t`,- ans: false,- },- {- sql: `- With t as (- select * from t1- ), tx as (- select * from t1- )- update t set a = 1;- `,- ans: false,- },- {- sql: `- With t as (- select * from t1- ), tx as (- select * from t1- )- insert into t values (1, 2, 3);- `,- ans: false,- },- {- sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",- ans: true,- },- {- sql: `- With t as (- select * from t1 where a = 'insert'- ), tx as (` +- " select * from `delete`" +- `) /* UPDATE */` +- "select `update` from t;",- ans: true,- },- {- sql: `create table t (a int);`,- ans: false,- },- }-- for _, test := range tests {- ans := parser.ValidateSQLForEditor(parser.MySQL, test.sql)- require.Equal(t, test.ans, ans, test.sql)-- ans = parser.ValidateSQLForEditor(parser.TiDB, test.sql)- require.Equal(t, test.ans, ans, test.sql)-- ans = parser.ValidateSQLForEditor(parser.MariaDB, test.sql)- require.Equal(t, test.ans, ans, test.sql)- }-}--func TestValidateSQLForStandard(t *testing.T) {- tests := []testData{- {- sql: `select* from t`,- ans: true,- },- {- sql: `explain select * from t;`,- ans: true,- },- {- sql: `explain analyze select * from t`,- ans: false,- },- {- sql: `- With t as (- select * from t1- ), tx as (- select * from t1- )- update t set a = 1;- `,- ans: false,- },- {- sql: `- With t as (- select * from t1- ), tx as (- select * from t1- )- insert into t values (1, 2, 3);- `,- ans: false,- },- {- sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",- ans: true,- },- {- sql: `- With t as (- select * from t1 where a = 'insert'- ), tx as (- select * from "delete"- ) /* UPDATE */- select "update" from t;- `,- ans: true,- },- {- sql: `create table t (a int);`,- ans: false,- },- }-- for _, test := range tests {- ans := parser.ValidateSQLForEditor(parser.Standard, test.sql)- require.Equal(t, test.ans, ans, test.sql)-- ans = parser.ValidateSQLForEditor(parser.Oracle, test.sql)- require.Equal(t, test.ans, ans, test.sql)-- ans = parser.ValidateSQLForEditor(parser.MSSQL, test.sql)- require.Equal(t, test.ans, ans, test.sql)- }-}diff --git a/backend/plugin/parser/standard/query.go b/backend/plugin/parser/standard/query.gonew file mode 100644index 0000000000..f40c709b98--- /dev/null+++ b/backend/plugin/parser/standard/query.go@@ -0,0 +1,57 @@+package standard++import (+ "regexp"+ "strings"++ "golang.org/x/exp/slog"++ "github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/plugin/parser/tokenizer"+)++// 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.StandardRemoveQuotedTextAndComment(statement)+ if err != nil {+ slog.Debug("Failed to remove quoted text and comment", slog.String("statement", statement), log.BBError(err))+ return false+ }++ return CheckStatementWithoutQuotedTextAndComment(textWithoutQuotedAndComment)+}++func CheckStatementWithoutQuotedTextAndComment(statement string) bool {+ formattedStr := strings.ToUpper(strings.TrimSpace(statement))+ if isSelect, _ := regexp.MatchString(`^SELECT\s+?`, formattedStr); isSelect {+ return true+ }++ if isSelect, _ := regexp.MatchString(`^SELECT\*\s+?`, formattedStr); isSelect {+ return true+ }++ if isExplain, _ := regexp.MatchString(`^EXPLAIN\s+?`, formattedStr); isExplain {+ if isExplainAnalyze, _ := regexp.MatchString(`^EXPLAIN\s+ANALYZE\s+?`, formattedStr); isExplainAnalyze {+ return false+ }+ return true+ }++ cteRegex := regexp.MustCompile(`^WITH\s+?`)+ if matchResult := cteRegex.MatchString(formattedStr); matchResult {+ dmlRegs := []string{`\bINSERT\b`, `\bUPDATE\b`, `\bDELETE\b`}+ for _, reg := range dmlRegs {+ if matchResult, _ := regexp.MatchString(reg, formattedStr); matchResult {+ return false+ }+ }+ return true+ }++ return false+}diff --git a/backend/plugin/parser/standard/query_test.go b/backend/plugin/parser/standard/query_test.gonew file mode 100644index 0000000000..7a39ed9847--- /dev/null+++ b/backend/plugin/parser/standard/query_test.go@@ -0,0 +1,75 @@+package standard++import (+ "testing"++ "github.com/stretchr/testify/require"+)++type testData struct {+ sql string+ ans bool+}++func TestValidateSQLForStandard(t *testing.T) {+ tests := []testData{+ {+ sql: `select* from t`,+ ans: true,+ },+ {+ sql: `explain select * from t;`,+ ans: true,+ },+ {+ sql: `explain analyze select * from t`,+ ans: false,+ },+ {+ sql: `+ With t as (+ select * from t1+ ), tx as (+ select * from t1+ )+ update t set a = 1;+ `,+ ans: false,+ },+ {+ sql: `+ With t as (+ select * from t1+ ), tx as (+ select * from t1+ )+ insert into t values (1, 2, 3);+ `,+ ans: false,+ },+ {+ sql: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",+ ans: true,+ },+ {+ sql: `+ With t as (+ select * from t1 where a = 'insert'+ ), tx as (+ select * from "delete"+ ) /* UPDATE */+ select "update" from t;+ `,+ ans: true,+ },+ {+ sql: `create table t (a int);`,+ ans: false,+ },+ }++ for _, test := range tests {+ ans := ValidateSQLForEditor(test.sql)+ require.Equal(t, test.ans, ans, test.sql)+ }+}