Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(Completion): skip heading sqls (#9617)
rebelice0d038b87c6b3
diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.goindex 100aa64542..373847fcb4 100644--- a/backend/plugin/parser/mysql/completion.go+++ b/backend/plugin/parser/mysql/completion.go@@ -878,6 +878,7 @@ func (l *TableRefListener) ExitSubquery(_ *mysql.SubqueryContext) {}func prepareParserAndScanner(statement string, caretLine int, caretOffset int) (*mysql.MySQLParser, *mysql.MySQLLexer, *base.Scanner) {+ statement, caretLine, caretOffset = skipHeadingSQLs(statement, caretLine, caretOffset)input := antlr.NewInputStream(statement)lexer := mysql.NewMySQLLexer(input)stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)@@ -890,6 +891,53 @@ func prepareParserAndScanner(statement string, caretLine int, caretOffset int) (return parser, lexer, scanner}+func notEmptySQLCount(list []base.SingleSQL) int {+ count := 0+ for _, sql := range list {+ if !sql.Empty {+ count+++ }+ }+ return count+}++// caretLine is 1-based and caretOffset is 0-based.+func skipHeadingSQLs(statement string, caretLine int, caretOffset int) (string, int, int) {+ newCaretLine, newCaretOffset := caretLine, caretOffset+ list, err := SplitSQL(statement)+ if err != nil || notEmptySQLCount(list) <= 1 {+ return statement, caretLine, caretOffset+ }++ caretLine-- // Convert to 0-based.++ start := 0+ for i, sql := range list {+ if sql.LastLine > caretLine || (sql.LastLine == caretLine && sql.LastColumn >= caretOffset) {+ start = i+ if i == 0 {+ // The caret is in the first SQL statement, so we don't need to skip any SQL statements.+ continue+ }+ newCaretLine = caretLine - list[i-1].LastLine + 1 // Convert to 1-based.+ if caretLine == list[i-1].LastLine {+ // The caret is in the same line as the last line of the previous SQL statement.+ // We need to adjust the caret offset.+ newCaretOffset = caretOffset - list[i-1].LastColumn - 1 // Convert to 0-based.+ }+ }+ }++ var buf strings.Builder+ for i := start; i < len(list); i++ {+ if _, err := buf.WriteString(list[i].Text); err != nil {+ return statement, caretLine, caretOffset+ }+ }++ return buf.String(), newCaretLine, newCaretOffset+}+func unquote(s string) string {if len(s) < 2 {return sdiff --git a/backend/plugin/parser/mysql/completion_test.go b/backend/plugin/parser/mysql/completion_test.goindex bf7b9b09f7..3c7fdc3137 100644--- a/backend/plugin/parser/mysql/completion_test.go+++ b/backend/plugin/parser/mysql/completion_test.go@@ -54,7 +54,7 @@ func TestCompletion(t *testing.T) {if record {tests[i].Want = filteredResult} else {- a.Equal(t.Want, filteredResult)+ a.Equal(t.Want, filteredResult, t.Input)}}diff --git a/backend/plugin/parser/mysql/test-data/test_completion.yaml b/backend/plugin/parser/mysql/test-data/test_completion.yamlindex 0dd8b9e84a..be49b4659c 100644--- a/backend/plugin/parser/mysql/test-data/test_completion.yaml+++ b/backend/plugin/parser/mysql/test-data/test_completion.yaml@@ -1,3 +1,15 @@+- input: SELECT FROM basdkfjasldf;SELECT | FROM t1+ want:+ - text: db+ type: DATABASE+ - text: t1+ type: TABLE+ - text: t2+ type: TABLE+ - text: c1+ type: COLUMN+ - text: v1+ type: VIEW- input: SELECT | FROM (SELECT c1 FROM t1) cc1(cc1c1)want:- text: db