Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(structurer): restore delimiter (#10370)
h3n4lfd613af913a3
diff --git a/backend/plugin/parser/mysql/mysql.go b/backend/plugin/parser/mysql/mysql.goindex a4354f2e28..ca2d144d0f 100644--- a/backend/plugin/parser/mysql/mysql.go+++ b/backend/plugin/parser/mysql/mysql.go@@ -67,8 +67,14 @@ func DealWithDelimiter(statement string, options ...tokenizer.Option) (string, ereturn statement, nil}-// RestoreDelimiter trys to restore the delimiter statement which commentted by DealWithDelimiter, assumes the delimiter comment takes up a single line-// and follows the syntax `-- DELIMITER ?`, otherwise returns an error.+// RestoreDelimiter tries to restore the delimiter statement which is commented by DealWithDelimiter,+//+// Assumes the delimiter comment takes up a single line and matches the syntax `-- DELIMITER ?`,+// also, one delimiter comment MUST only affect one statement, in the other words, the following statement cause UB:+// -- DELIMITER ;+// SELECT 1;+// SELECT 2;+// -- DELIMITER ;.func RestoreDelimiter(statement string) (string, error) {delimiterRegexp := regexp.MustCompile(`(?i)^-- DELIMITER\s+(?P<DELIMITER>[^\s\\]+)\s*`)lines := strings.Split(statement, "\n")@@ -78,20 +84,17 @@ func RestoreDelimiter(statement string) (string, error) {flushBuf := func(delimiter string) error {bufStmt := buf.String()- t := tokenizer.NewTokenizer(bufStmt)- list, err := t.SplitTiDBMultiSQL()- if err != nil {- return errors.Errorf("failed to split multi sql %q: %v", bufStmt, err)- }- for _, sql := range list {- if IsDelimiter(sql.Text) {- return errors.Errorf("delimiter statement %q miexed with delimiter comments lap, previous delimiter %q", sql.Text, delimiter)- }- if _, err := result.WriteString(fmt.Sprintf("%s%s\n", strings.TrimSuffix(sql.Text, ";"), previousDelimiter)); err != nil {- return errors.Wrapf(err, "failed to write string %q into builder", sql.Text)+ buf.Reset()+ // Lookup reversely to modify the last delimiter.+ for i := len(bufStmt) - 1; i >= 0; i-- {+ if bufStmt[i] == ';' {+ bufStmt = bufStmt[:i] + delimiter + bufStmt[i+1:]+ break}}- buf.Reset()+ if _, err := result.WriteString(bufStmt); err != nil {+ return errors.Wrapf(err, "failed to write string %q into builder", bufStmt)+ }return nil}diff --git a/backend/plugin/parser/mysql/mysql_test.go b/backend/plugin/parser/mysql/mysql_test.goindex d9ba39b483..5e40486862 100644--- a/backend/plugin/parser/mysql/mysql_test.go+++ b/backend/plugin/parser/mysql/mysql_test.go@@ -183,6 +183,85 @@ CREATE TABLE u (`,},+ {+ // Semicolon in body after delimiter+ input: `+SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;+SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;+--+-- Table structure for u+--+CREATE TABLE u (+ b blob NOT NULL,+ t text NOT NULL,+ g geometry NOT NULL,+ j json NOT NULL,+ ti tinyint(1) NOT NULL,+ tb tinyblob+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;++-- DELIMITER ;;+CREATE DEFINER=root@localhost PROCEDURE GetEmployeeFullName(IN in_employee_id INT, OUT out_full_name VARCHAR(100))+BEGIN+ -- Declare variables+ DECLARE first_name VARCHAR(50);+ DECLARE last_name VARCHAR(50);++ -- Initialize variables+ SET out_full_name = NULL;++ -- Retrieve employee details based on employee_id+ SELECT first_name, last_name INTO first_name, last_name+ FROM employees+ WHERE employee_id = in_employee_id;++ -- Check if employee exists+ IF first_name IS NOT NULL THEN+ -- Concatenate first and last names+ SET out_full_name = CONCAT(first_name, ' ', last_name);+ END IF;+END ;+-- DELIMITER ;+`,+ want: `+SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;+SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;+--+-- Table structure for u+--+CREATE TABLE u (+ b blob NOT NULL,+ t text NOT NULL,+ g geometry NOT NULL,+ j json NOT NULL,+ ti tinyint(1) NOT NULL,+ tb tinyblob+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;++DELIMITER ;;+CREATE DEFINER=root@localhost PROCEDURE GetEmployeeFullName(IN in_employee_id INT, OUT out_full_name VARCHAR(100))+BEGIN+ -- Declare variables+ DECLARE first_name VARCHAR(50);+ DECLARE last_name VARCHAR(50);++ -- Initialize variables+ SET out_full_name = NULL;++ -- Retrieve employee details based on employee_id+ SELECT first_name, last_name INTO first_name, last_name+ FROM employees+ WHERE employee_id = in_employee_id;++ -- Check if employee exists+ IF first_name IS NOT NULL THEN+ -- Concatenate first and last names+ SET out_full_name = CONCAT(first_name, ' ', last_name);+ END IF;+END ;;+DELIMITER ;+`,+ },{// One delimiterinput: `@@ -245,6 +324,7 @@ CREATE TABLE u (CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4DETERMINISTICRETURN CONCAT('Hello, ',s,'!') ;+-- DELIMITER ;CREATE FUNCTION hello2(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4DETERMINISTICRETURN CONCAT('Hello, ',s,'!') ;@@ -273,9 +353,10 @@ DELIMITER ;;CREATE FUNCTION hello(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4DETERMINISTICRETURN CONCAT('Hello, ',s,'!') ;;+DELIMITER ;CREATE FUNCTION hello2(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4DETERMINISTIC-RETURN CONCAT('Hello, ',s,'!') ;;+RETURN CONCAT('Hello, ',s,'!') ;DELIMITER ??CREATE FUNCTION hello3(s CHAR(20)) RETURNS char(50) CHARSET utf8mb4DETERMINISTIC@@ -286,9 +367,9 @@ DROP FUNCTION hello3;},}- for _, tc := range testCases {+ for i, tc := range testCases {got, err := RestoreDelimiter(tc.input)require.NoError(t, err)- require.Equal(t, tc.want, got)+ require.Equalf(t, tc.want, got, "test cases: %d", i)}}