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: parse SQL before execute (#6147)
rebelice2da45826c641
diff --git a/backend/plugin/parser/sql/mysql.go b/backend/plugin/parser/sql/mysql.gonew file mode 100644index 0000000000..6881ec1d05--- /dev/null+++ b/backend/plugin/parser/sql/mysql.go@@ -0,0 +1,56 @@+// Package parser is the parser for SQL statement.+package parser++import (+ "strings"++ tidbparser "github.com/pingcap/tidb/parser"+ "github.com/pingcap/tidb/parser/ast"+ "github.com/pkg/errors"+)++// ParseMySQL parses the given SQL statement and returns the AST.+func ParseMySQL(statement string, charset string, collation string) ([]ast.StmtNode, error) {+ p := tidbparser.New()++ // To support MySQL8 window function syntax.+ // See https://github.com/bytebase/bytebase/issues/175.+ p.EnableWindowFunc(true)+ list, err := SplitMultiSQLAndNormalize(MySQL, statement)+ if err != nil {+ return nil, err+ }++ var result []ast.StmtNode+ for _, sql := range list {+ if sql.Empty {+ continue+ }+ if IsTiDBUnsupportDDLStmt(sql.Text) {+ // skip the TiDB parser unsupported DDL statement+ continue+ }++ nodes, _, err := p.Parse(sql.Text, charset, collation)+ if err != nil {+ return nil, err+ }+ if len(nodes) == 0 {+ continue+ }+ if len(nodes) > 1 {+ return nil, errors.Errorf("get more than one sql after split: %s", sql.Text)+ }+ node := nodes[0]+ node.SetText(nil, strings.TrimSpace(node.Text()))+ node.SetOriginTextPosition(sql.LastLine)+ if n, ok := node.(*ast.CreateTableStmt); ok {+ if err := SetLineForMySQLCreateTableStmt(n); err != nil {+ return nil, err+ }+ }+ result = append(result, node)+ }++ return result, err+}diff --git a/backend/plugin/parser/sql/tidb.go b/backend/plugin/parser/sql/tidb.gonew file mode 100644index 0000000000..ed8424764e--- /dev/null+++ b/backend/plugin/parser/sql/tidb.go@@ -0,0 +1,19 @@+// Package parser is the parser for SQL statement.+package parser++import (+ tidbparser "github.com/pingcap/tidb/parser"+ "github.com/pingcap/tidb/parser/ast"+)++// ParseTiDB parses the given SQL statement and returns the AST.+func ParseTiDB(sql string, charset string, collation string) ([]ast.StmtNode, error) {+ p := tidbparser.New()++ // To support MySQL8 window function syntax.+ // See https://github.com/bytebase/bytebase/issues/175.+ p.EnableWindowFunc(true)++ nodes, _, err := p.Parse(sql, charset, collation)+ return nodes, err+}diff --git a/backend/server/sql.go b/backend/server/sql.goindex 8d7ee7b412..ceaf2845d2 100644--- a/backend/server/sql.go+++ b/backend/server/sql.go@@ -243,6 +243,25 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", exec.InstanceID))}+ switch instance.Engine {+ case db.Postgres:+ if _, err := parser.Parse(parser.Postgres, parser.ParseContext{}, exec.Statement); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid sql statement: %v", err))+ }+ case db.Oracle:+ if _, err := parser.ParsePLSQL(exec.Statement); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid sql statement: %v", err))+ }+ case db.TiDB:+ if _, err := parser.ParseTiDB(exec.Statement, "", ""); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid sql statement: %v", err))+ }+ case db.MySQL:+ if _, err := parser.ParseMySQL(exec.Statement, "", ""); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid sql statement: %v", err))+ }+ }+if !parser.ValidateSQLForEditor(convertToParserEngine(instance.Engine), exec.Statement) {return echo.NewHTTPError(http.StatusBadRequest, "Malformed sql execute request, only support SELECT sql statement")}