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(parser): remove legacy antlr fallbacks (#20682)
rebelicef3f6b2624957
diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex 9636d187c2..cee439fc79 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -8,8 +8,7 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/parser/base"-- _ "github.com/bytebase/bytebase/backend/plugin/parser/plsql"+ oracleparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql")func TestOracleRules(t *testing.T) {@@ -161,8 +160,8 @@ func assertOracleStmtsDidNotUseANTLRFallback(t *testing.T, stmts []base.ParsedStif stmt.AST == nil {continue}- if _, ok := base.GetANTLRAST(stmt.AST); ok {- t.Fatalf("Oracle advisor used ANTLR fallback for %T", stmt.AST)+ if _, ok := stmt.AST.(*oracleparser.OmniAST); !ok {+ t.Fatalf("Oracle advisor used non-omni AST %T", stmt.AST)}}}diff --git a/backend/plugin/parser/base/ast.go b/backend/plugin/parser/base/ast.goindex 1e233597fc..e365f40ee0 100644--- a/backend/plugin/parser/base/ast.go+++ b/backend/plugin/parser/base/ast.go@@ -1,8 +1,6 @@package baseimport (- "github.com/antlr4-go/antlr/v4"-storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -15,43 +13,3 @@ type AST interface {// Named to avoid collision with protobuf-generated GetStartPosition methods.ASTStartPosition() *storepb.Position}--// ANTLRAST is the AST implementation for ANTLR-based parsers.-// Supported engines: PostgreSQL, MySQL, MariaDB, OceanBase, MSSQL, Oracle, Redshift,-// Snowflake, BigQuery, Spanner, Doris, Cassandra, Trino, PartiQL.-//-// Parser packages can use this directly or embed it to add engine-specific fields.-type ANTLRAST struct {- // StartPosition is the 1-based position where this statement starts.- StartPosition *storepb.Position- Tree antlr.Tree- Tokens *antlr.CommonTokenStream-}--// ASTStartPosition implements AST interface.-func (a *ANTLRAST) ASTStartPosition() *storepb.Position {- return a.StartPosition-}--// AntlrASTProvider is an optional interface that AST implementations can provide-// to expose an underlying ANTLR AST. This is used during parser migrations when-// an AST wraps both a new AST format and a legacy ANTLR tree for backward compatibility.-type AntlrASTProvider interface {- AsANTLRAST() (*ANTLRAST, bool)-}--// GetANTLRAST extracts the ANTLRAST from an AST interface.-// Returns the ANTLRAST and true if it is an ANTLR-based AST, nil and false otherwise.-// Also checks for AntlrASTProvider implementations (e.g. OmniAST during migration).-func GetANTLRAST(a AST) (*ANTLRAST, bool) {- if a == nil {- return nil, false- }- if antlrAST, ok := a.(*ANTLRAST); ok {- return antlrAST, ok- }- if provider, ok := a.(AntlrASTProvider); ok {- return provider.AsANTLRAST()- }- return nil, false-}diff --git a/backend/plugin/parser/base/base.go b/backend/plugin/parser/base/base.goindex b706316421..d2490ced50 100644--- a/backend/plugin/parser/base/base.go+++ b/backend/plugin/parser/base/base.go@@ -1,12 +1,6 @@package base-import (- "fmt"-- "github.com/antlr4-go/antlr/v4"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-)+import storepb "github.com/bytebase/bytebase/backend/generated-go/store"// SyntaxError is a syntax error.type SyntaxError struct {@@ -20,74 +14,6 @@ func (e *SyntaxError) Error() string {return e.Message}-// ParseErrorListener is a custom error listener for PLSQL parser.-type ParseErrorListener struct {- // StartPosition is the 1-based position where this statement starts in the original multi-statement input.- // Used to calculate error positions relative to the original script.- StartPosition *storepb.Position- Err *SyntaxError- Statement string-}--// SyntaxError returns the errors.-func (l *ParseErrorListener) SyntaxError(_ antlr.Recognizer, token any, line, column int, message string, _ antlr.RecognitionException) {- if l.Err != nil {- return- }-- // Get 0-based line offset from StartPosition (1-based) for calculations- lineOffset := int32(0)- columnOffset := int32(0)- if l.StartPosition != nil {- lineOffset = l.StartPosition.Line - 1- if line == 1 && l.StartPosition.Column > 0 {- columnOffset = l.StartPosition.Column - 1- }- }-- errMessage := ""- if token, ok := token.(*antlr.CommonToken); ok {- stream := token.GetInputStream()- start := token.GetStart() - 40- if start < 0 {- start = 0- }- stop := token.GetStop()- if stop >= stream.Size() {- stop = stream.Size() - 1- }- errMessage = fmt.Sprintf("related text: %s", stream.GetTextFromInterval(antlr.NewInterval(start, stop)))- }- // ANTLR provides 1-based line and 0-based column- // Store as 1-based line and 1-based column in Position- posLine := int32(line) + lineOffset- posColumn := int32(column+1) + columnOffset- l.Err = &SyntaxError{- Position: &storepb.Position{- Line: posLine,- Column: posColumn,- },- RawMessage: message,- // Display directly (already 1-based)- Message: fmt.Sprintf("Syntax error at line %d:%d \n%s", posLine, posColumn, errMessage),- }-}--// ReportAmbiguity reports an ambiguity.-func (*ParseErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {- antlr.ConsoleErrorListenerINSTANCE.ReportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs)-}--// ReportAttemptingFullContext reports an attempting full context.-func (*ParseErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs *antlr.ATNConfigSet) {- antlr.ConsoleErrorListenerINSTANCE.ReportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs)-}--// ReportContextSensitivity reports a context sensitivity.-func (*ParseErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs *antlr.ATNConfigSet) {- antlr.ConsoleErrorListenerINSTANCE.ReportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs)-}-func GetOffsetLength(total int) int {length := 1for {diff --git a/backend/plugin/parser/base/c3.go b/backend/plugin/parser/base/c3.godeleted file mode 100644index 765083b59f..0000000000--- a/backend/plugin/parser/base/c3.go+++ /dev/null@@ -1,654 +0,0 @@-package base--import (- "context"- "sync"-- "github.com/antlr4-go/antlr/v4"-)--const maxRecursionDepth = 66--// CodeCompletionCore is the core of code completion.-// It only relies on the ANTLR runtime and does not depend on any specific language.-type CodeCompletionCore struct {- IgnoredTokens map[int]bool- PreferredRules map[int]bool-- // QueryRule and SelectItemAliasRule are used to determine the select item alias- // for the completion candidates.- // They can be used in GROUP BY, ORDER BY and HAVING clauses.- QueryRule int- ShadowQueryRule int- SelectItemAliasRule int- // CTERule is used to determine the CTE alias for the completion candidates.- // Currently, it always uses in ShadowQueryRule for MySQL.- CTERule int-- parser antlr.Parser- atn *antlr.ATN- candidates *CandidatesCollection- followSetsByState *FollowSetsByState- // shortcutMap map[int]map[int]RuleEndStatus- statesProcessed int- tokenStartIndex int- tokens []*Token-- callStack *RuleList- lastQueryRuleContext *RuleContext- lastShadowQueryRuleContext *RuleContext-- // Context for cancellation support- ctx context.Context-}--type Token struct {- Type int- StartPosition int-}--// NewCodeCompletionCore creates a new CodeCompletionCore.-func NewCodeCompletionCore(- ctx context.Context,- parser antlr.Parser,- ignoredTokens,- preferredRules map[int]bool,- followSets *FollowSetsByState,- queryRule int,- shadowQueryRule int,- selectItemAliasRule int,- cteRule int,-) *CodeCompletionCore {- return &CodeCompletionCore{- IgnoredTokens: ignoredTokens,- PreferredRules: preferredRules,- QueryRule: queryRule,- ShadowQueryRule: shadowQueryRule,- SelectItemAliasRule: selectItemAliasRule,- CTERule: cteRule,- parser: parser,- atn: parser.GetATN(),- followSetsByState: followSets,- ctx: ctx,- }-}--// PipelineEntry is the entry of the pipeline.-type PipelineEntry struct {- State antlr.ATNState- TokenIndex int-}--// RuleContext is the context of a rule.-type RuleContext struct {- ID int- // SelectItemAliases is the map of token index of select item aliases.- // Only the QueryRule has the SelectItemAliases.- SelectItemAliases map[int]bool- CTEList []int-}--// RuleList is the list of rules.-// Use a bitset to check existence of a rule in the list efficiently.-type RuleList struct {- rules []*RuleContext- bitSet *antlr.BitSet-}--// NewRuleList creates a new RuleList.-func NewRuleList() *RuleList {- return &RuleList{- rules: []*RuleContext{},- bitSet: antlr.NewBitSet(),- }-}--// Copy copies the RuleList.-func (l *RuleList) Copy() *RuleList {- rules := make([]*RuleContext, len(l.rules))- for i, rule := range l.rules {- var cteList []int- cteList = append(cteList, rule.CTEList...)- rules[i] = &RuleContext{- ID: rule.ID,- SelectItemAliases: rule.SelectItemAliases,- CTEList: cteList,- }- }- bitSet := antlr.NewBitSet()- bitSet.Or(l.bitSet)- return &RuleList{- rules: rules,- bitSet: bitSet,- }-}--// Append appends the rules from the given RuleList.-func (l *RuleList) Append(r *RuleList) {- for _, rule := range r.rules {- l.Push(rule)- }-}--// Contains checks if the rule exists in the list.-// Use the bitset to check the existence efficiently.-func (l *RuleList) Contains(rule int) bool {- return l.bitSet.Contains(rule)-}--// Push appends the rule to the list.-func (l *RuleList) Push(rule *RuleContext) {- l.rules = append(l.rules, rule)- l.bitSet.Add(rule.ID)-}--// Pop pops the last rule from the list.-// HINT: Each Push should not push the existing rule, otherwise Pop will destroy the bitSet.-func (l *RuleList) Pop() *RuleContext {- result := l.rules[len(l.rules)-1]- l.bitSet.Remove(result.ID)- l.rules = l.rules[:len(l.rules)-1]- return result-}--// CandidatesCollection is the collection of candidates.-// There are two types of candidates: tokens and rules.-// Tokens are the tokens that can follow the caret position.-// Rules are the parser rules that can be reduced at the caret position.-type CandidatesCollection struct {- Tokens map[int][]int- Rules map[int][]*RuleContext-}--type FollowSetWithPath struct {- intervals antlr.IntervalSet- path *RuleList- following []int-}--type FollowSetsList []FollowSetWithPath--func (l *FollowSetsList) Append(f FollowSetWithPath) {- *l = append(*l, f)-}--// FollowSetsHolder is the holder of follow sets.-type FollowSetsHolder struct {- sets FollowSetsList- combined antlr.IntervalSet-}--// FollowSetsByState is the map of follow sets by state.-// It is used to cache the follow sets.-// The FollowSetsByState is only dependent on the grammar,-// so that we can reuse it in multiple CodeCompletionCore calls for the same grammar.-// On the other hand, in single CodeCompletionCore call, the FollowSetsByState is also useful.-//-// For thread safety and performance, we use RWMutex to protect the map.-// The map is read frequently and written rarely.-type FollowSetsByState struct {- rw sync.RWMutex- m map[int]FollowSetsHolder-}--// NewFollowSetsByState creates a new FollowSetsByState.-func NewFollowSetsByState() FollowSetsByState {- return FollowSetsByState{- m: map[int]FollowSetsHolder{},- }-}--// Get thread safety gets the follow sets by state.-func (f *FollowSetsByState) Get(state int) FollowSetsHolder {- f.rw.RLock()- defer f.rw.RUnlock()-- return f.m[state]-}--// Set thread safety sets the follow sets by state.-func (f *FollowSetsByState) Set(state int, holder FollowSetsHolder) {- f.rw.Lock()- defer f.rw.Unlock()-- f.m[state] = holder-}--// CollectFollowSets collects the follow sets if needed.-func (f *FollowSetsByState) CollectFollowSets(parser antlr.Parser, startState antlr.ATNState, ignoredTokens, _ map[int]bool) {- state := startState.GetStateNumber()- f.rw.Lock()- defer f.rw.Unlock()-- if _, ok := f.m[state]; ok {- return- }-- stop := parser.GetATN().GetRuleToStopState(startState.GetRuleIndex())- followSets := determineFollowSets(parser, startState, stop, ignoredTokens)-- combined := antlr.NewIntervalSet()- for _, set := range followSets {- combined.AddAll(&set.intervals)- }-- f.m[state] = FollowSetsHolder{- sets: followSets,- combined: *combined,- }-}--// determineFollowSets collects tokens that can follow the given ATN state.-func determineFollowSets(parser antlr.Parser, start, stop antlr.ATNState, ignoredTokens map[int]bool) FollowSetsList {- seen := make(map[antlr.ATNState]bool)- ruleStack := NewRuleList()- result := FollowSetsList{}- collectFollowSets(parser, start, stop, &result, seen, ruleStack, ignoredTokens)- return result-}--func collectFollowSets(- parser antlr.Parser,- s antlr.ATNState,- stopState antlr.ATNState,- followSets *FollowSetsList,- seen map[antlr.ATNState]bool,- ruleStack *RuleList,- ignoredTokens map[int]bool,-) {- if _, exists := seen[s]; exists {- return- }-- seen[s] = true-- if s == stopState || s.GetStateType() == antlr.ATNStateRuleStop {- intervals := antlr.NewIntervalSet()- intervals.AddOne(antlr.TokenEpsilon)- followSets.Append(FollowSetWithPath{- intervals: *intervals,- path: ruleStack.Copy(),- following: []int{},- })- return- }-- for _, transition := range s.GetTransitions() {- if ruleTransition, ok := transition.(*antlr.RuleTransition); ok {- if ruleStack.Contains(ruleTransition.GetTarget().GetRuleIndex()) {- continue- }-- ruleStack.Push(&RuleContext{ID: ruleTransition.GetTarget().GetRuleIndex()})- collectFollowSets(parser, transition.GetTarget(), stopState, followSets, seen, ruleStack, ignoredTokens)- ruleStack.Pop()- } else if predicateTransition, ok := transition.(*antlr.PredicateTransition); ok {- if checkPredicate(parser, predicateTransition) {- collectFollowSets(parser, transition.GetTarget(), stopState, followSets, seen, ruleStack, ignoredTokens)- }- } else if transition.GetIsEpsilon() {- collectFollowSets(parser, transition.GetTarget(), stopState, followSets, seen, ruleStack, ignoredTokens)- } else if _, ok := transition.(*antlr.WildcardTransition); ok {- intervals := antlr.NewIntervalSet()- intervals.AddRange(antlr.TokenMinUserTokenType, parser.GetATN().GetMaxTokenType())- followSets.Append(FollowSetWithPath{- intervals: *intervals,- path: ruleStack.Copy(),- following: []int{},- })- } else {- set := transition.GetLabel()- if set != nil && set.Length() > 0 {- if _, ok := transition.(*antlr.NotSetTransition); ok {- set = set.Complement(antlr.TokenMinUserTokenType, parser.GetATN().GetMaxTokenType())- }- followSets.Append(FollowSetWithPath{- intervals: *set,- path: ruleStack.Copy(),- following: getFollowingTokens(transition, ignoredTokens),- })- }- }- }-}--// getFollowingTokens collects the tokens that can follow the given transition and only if there is a single token.-// It will not collect the ignored tokens.-func getFollowingTokens(transition antlr.Transition, ignoredTokens map[int]bool) []int {- result := []int{}- pipeline := []antlr.ATNState{transition.GetTarget()}-- for len(pipeline) > 0 {- state := pipeline[len(pipeline)-1]- pipeline = pipeline[:len(pipeline)-1]-- for _, transition := range state.GetTransitions() {- if _, ok := transition.(*antlr.AtomTransition); ok {- if !transition.GetIsEpsilon() {- list := transition.GetLabel().ToList()- if len(list) == 1 {- if _, exists := ignoredTokens[list[0]]; !exists {- result = append(result, list[0])- pipeline = append(pipeline, transition.GetTarget())- }- }- } else {- pipeline = append(pipeline, transition.GetTarget())- }- }- }- }-- return result-}--func checkPredicate(parser antlr.Parser, predicateTransition *antlr.PredicateTransition) bool {- return predicateTransition.GetPredicate().Evaluate(parser, antlr.ParserRuleContextEmpty)-}--type RuleEndStatus map[int]bool--// CollectCandidates collects the candidates.-func (c *CodeCompletionCore) CollectCandidates(caretTokenIndex int, context antlr.ParserRuleContext) *CandidatesCollection {- // Reset the fields.-- c.candidates = &CandidatesCollection{- Tokens: make(map[int][]int),- Rules: make(map[int][]*RuleContext),- }- c.statesProcessed = 0-- if context == nil {- c.tokenStartIndex = 0- } else {- c.tokenStartIndex = context.GetStart().GetTokenIndex()- }-- // Initialize the c.tokens:- // Set to the token types of tokenStream[ruleStartIndex, caretTokenIndex].- c.tokens = []*Token{}- tokenStream := c.parser.GetTokenStream()- currentOffset := tokenStream.Index()- tokenStream.Seek(c.tokenStartIndex)- offset := 1- for {- token := tokenStream.LT(offset)- offset++- c.tokens = append(c.tokens, &Token{- Type: token.GetTokenType(),- StartPosition: token.GetStart(),- })-- if token.GetTokenIndex() >= caretTokenIndex || token.GetTokenType() == antlr.TokenEOF {- break- }- }- // Seek back to the original index.- tokenStream.Seek(currentOffset)-- var startRule int- if context == nil {- startRule = 0- } else {- startRule = context.GetRuleIndex()- }-- c.callStack = NewRuleList()- c.fetchEndStatus(c.atn.GetRuleToStartState(startRule), 0 /* tokenIndex */, "" /* indentation */)- return c.candidates-}--func (c *CodeCompletionCore) fetchEndStatus(startState antlr.ATNState, tokenIndex int, indentation string) RuleEndStatus {- // Check for context cancellation- select {- case <-c.ctx.Done():- return RuleEndStatus{}- default:- }-- if len(c.callStack.rules) > maxRecursionDepth {- return RuleEndStatus{}- }- result := make(RuleEndStatus)- c.followSetsByState.CollectFollowSets(c.parser, startState, c.IgnoredTokens, c.PreferredRules)-- followSets := c.followSetsByState.Get(startState.GetStateNumber())- ruleContext := &RuleContext{ID: startState.GetRuleIndex()}- c.callStack.Push(ruleContext)- if startState.GetRuleIndex() == c.QueryRule {- oldContext := c.lastQueryRuleContext- c.lastQueryRuleContext = ruleContext- defer func() {- c.lastQueryRuleContext = oldContext- }()- }-- if startState.GetRuleIndex() == c.ShadowQueryRule {- oldContext := c.lastShadowQueryRuleContext- c.lastShadowQueryRuleContext = ruleContext- defer func() {- c.lastShadowQueryRuleContext = oldContext- }()- }-- if tokenIndex >= len(c.tokens)-1 {- if _, exists := c.PreferredRules[startState.GetRuleIndex()]; exists {- // If the rule is preferred, we should add it to the candidates.- c.translateToRuleIndex(c.callStack)- } else {- for _, set := range followSets.sets {- fullPath := c.callStack.Copy()- fullPath.Append(set.path)- // translateToRuleIndex will add the rule to the candidates if it is preferred.- if !c.translateToRuleIndex(fullPath) {- // If the rule is not preferred, we should add the following tokens to the candidates.- for _, symbol := range set.intervals.ToList() {- if _, exists := c.IgnoredTokens[symbol]; !exists {- if _, exists := c.candidates.Tokens[symbol]; !exists {- c.candidates.Tokens[symbol] = set.following- } else {- equal := len(c.candidates.Tokens[symbol]) == len(set.following)- if equal {- for i, item := range c.candidates.Tokens[symbol] {- if item != set.following[i] {- equal = false- break- }- }- }- if !equal {- // If the token is already in the candidates, and the following tokens are different,- // we use an empty list to indicate that.- c.candidates.Tokens[symbol] = []int{}- }- }- }- }- }- }- }-- c.callStack.Pop()- return RuleEndStatus{}- }-- // If the current token and Epsilon are not in the follow sets, we should stop.- currentSymbol := c.tokens[tokenIndex]- if !followSets.combined.Contains(antlr.TokenEpsilon) && !followSets.combined.Contains(currentSymbol.Type) {- c.callStack.Pop()- return RuleEndStatus{}- }-- var statePipeline []PipelineEntry- var currentEntry PipelineEntry-- statePipeline = append(statePipeline, PipelineEntry{- State: startState,- TokenIndex: tokenIndex,- })-- for len(statePipeline) != 0 {- // Check for context cancellation in the main loop- select {- case <-c.ctx.Done():- c.callStack.Pop()- return RuleEndStatus{}- default:- }-- currentEntry = statePipeline[len(statePipeline)-1]- statePipeline = statePipeline[:len(statePipeline)-1]- c.statesProcessed++-- atCaret := currentEntry.TokenIndex >= len(c.tokens)-1-- switch currentEntry.State.GetStateType() {- case antlr.ATNStateRuleStart:- indentation += " "- case antlr.ATNStateRuleStop:- result[currentEntry.TokenIndex] = true- continue- default:- // Other state types don't require special handling- }-- for _, t := range currentEntry.State.GetTransitions() {- switch transition := t.(type) {- case *antlr.RuleTransition:- endStatus := c.fetchEndStatus(transition.GetTarget(), currentEntry.TokenIndex, indentation)- for status := range endStatus {- statePipeline = append(statePipeline, PipelineEntry{- State: transition.GetFollowState(),- TokenIndex: status,- })- }- switch startState.GetRuleIndex() {- case c.SelectItemAliasRule:- if c.lastQueryRuleContext != nil {- if c.lastQueryRuleContext.SelectItemAliases == nil {- c.lastQueryRuleContext.SelectItemAliases = make(map[int]bool)- }- c.lastQueryRuleContext.SelectItemAliases[c.tokens[currentEntry.TokenIndex].StartPosition] = true- }- if c.lastShadowQueryRuleContext != nil {- if c.lastShadowQueryRuleContext.SelectItemAliases == nil {- c.lastShadowQueryRuleContext.SelectItemAliases = make(map[int]bool)- }- c.lastShadowQueryRuleContext.SelectItemAliases[c.tokens[currentEntry.TokenIndex].StartPosition] = true- }- case c.CTERule:- if c.lastShadowQueryRuleContext != nil {- c.lastShadowQueryRuleContext.CTEList = append(c.lastShadowQueryRuleContext.CTEList, c.tokens[currentEntry.TokenIndex].StartPosition)- }- default:- // Other rule indices don't require special handling- }- case *antlr.PredicateTransition:- if checkPredicate(c.parser, transition) {- statePipeline = append(statePipeline, PipelineEntry{- State: transition.GetTarget(),- TokenIndex: currentEntry.TokenIndex,- })- }- case *antlr.WildcardTransition:- if atCaret {- if !c.translateToRuleIndex(c.callStack) {- interval := antlr.NewIntervalSet()- interval.AddRange(antlr.TokenMinUserTokenType, c.parser.GetATN().GetMaxTokenType())- for _, symbol := range interval.ToList() {- if _, exists := c.IgnoredTokens[symbol]; !exists {- if _, exists := c.candidates.Tokens[symbol]; !exists {- c.candidates.Tokens[symbol] = []int{}- }- }- }- }- } else {- statePipeline = append(statePipeline, PipelineEntry{- State: transition.GetTarget(),- TokenIndex: currentEntry.TokenIndex + 1,- })- }- default:- if transition.GetIsEpsilon() {- if atCaret {- c.translateToRuleIndex(c.callStack)- }-- statePipeline = append(statePipeline, PipelineEntry{- State: transition.GetTarget(),- TokenIndex: currentEntry.TokenIndex,- })- }-- set := transition.GetLabel()- if set != nil && set.Length() > 0 {- if transition.GetSerializationType() == antlr.TransitionNOTSET {- set = set.Complement(antlr.TokenMinUserTokenType, c.parser.GetATN().GetMaxTokenType())- }- if atCaret {- if !c.translateToRuleIndex(c.callStack) {- list := set.ToList()- addFollowing := len(list) == 1- for _, symbol := range list {- if _, exists := c.IgnoredTokens[symbol]; !exists {- if addFollowing {- c.candidates.Tokens[symbol] = getFollowingTokens(transition, c.IgnoredTokens)- } else {- c.candidates.Tokens[symbol] = []int{}- }- }- }- }- } else {- currentSymbol := c.tokens[currentEntry.TokenIndex]- if set.Contains(currentSymbol.Type) {- statePipeline = append(statePipeline, PipelineEntry{- State: transition.GetTarget(),- TokenIndex: currentEntry.TokenIndex + 1,- })- }- }- }- }- }- }-- c.callStack.Pop()- return result-}--func (c *CodeCompletionCore) translateToRuleIndex(ruleStack *RuleList) bool {- if len(c.PreferredRules) == 0 {- return false- }-- for i, rule := range ruleStack.rules {- if _, exists := c.PreferredRules[rule.ID]; exists {- var path []*RuleContext- path = append(path, ruleStack.rules[:i]...)- addNew := true-- if candidates, exists := c.candidates.Rules[rule.ID]; exists && len(candidates) == len(path) {- equal := true- for j, item := range candidates {- if item.ID != path[j].ID {- equal = false- break- }- }-- if !equal {- addNew = false- }- }-- if addNew {- c.candidates.Rules[ruleStack.rules[i].ID] = path- }- return true- }- }-- return false-}diff --git a/backend/plugin/parser/base/lexer.go b/backend/plugin/parser/base/lexer.goindex 3aaba8660a..25ca5e414a 100644--- a/backend/plugin/parser/base/lexer.go+++ b/backend/plugin/parser/base/lexer.go@@ -1,143 +1,5 @@package base-import (- "strings"-- "github.com/antlr4-go/antlr/v4"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-)--func GetDefaultChannelTokenType(tokens []antlr.Token, base int, offset int) int {- current := base- step := 1- remaining := offset- if offset < 0 {- step = -1- remaining = -offset- }- for remaining != 0 {- current += step- if current < 0 || current >= len(tokens) {- return antlr.TokenEOF- }-- if tokens[current].GetChannel() == antlr.TokenDefaultChannel {- remaining--- }- }-- return tokens[current].GetTokenType()-}--// FirstDefaultChannelTokenPosition returns the first token position of the default channel.-// Both line and column are ZERO based.-func FirstDefaultChannelTokenPosition(tokens []antlr.Token) *common.ANTLRPosition {- for _, token := range tokens {- if token.GetChannel() == antlr.TokenDefaultChannel {- return &common.ANTLRPosition{- Line: int32(token.GetLine()),- Column: int32(token.GetColumn()),- }- }- }- return &common.ANTLRPosition{- Line: int32(tokens[len(tokens)-1].GetLine()),- Column: int32(tokens[len(tokens)-1].GetColumn()),- }-}--func IsEmpty(tokens []antlr.Token, semi int) bool {- for _, token := range tokens {- if token.GetChannel() == antlr.TokenDefaultChannel && token.GetTokenType() != semi && token.GetTokenType() != antlr.TokenEOF {- return false- }- }- return true-}--// SplitSQLByLexer is a grammar-free helper function that splits SQL statements using an ANTLR lexer.-// It works with any ANTLR-based lexer by accepting the token stream and semicolon token type as parameters.-//-// IMPORTANT: This function requires that the lexer sends whitespace to the hidden channel-// (using `-> channel(HIDDEN)` in the grammar), NOT skipping it. This ensures GetAllTokens()-// returns all tokens including whitespace, allowing us to use token positions directly.-//-// Parameters:-// - stream: The ANTLR token stream (must be already filled with stream.Fill())-// - semiTokenType: The token type value for semicolon in the specific grammar-//-// Returns:-// - A slice of Statement, each representing one statement with its text, position, and metadata-//-// Example usage:-//-// lexer := parser.NewSnowflakeLexer(antlr.NewInputStream(statement))-// stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)-// stream.Fill()-// return SplitSQLByLexer(stream, parser.SnowflakeLexerSEMI)-func SplitSQLByLexer(stream *antlr.CommonTokenStream, semiTokenType int) ([]Statement, error) {- tokens := stream.GetAllTokens()- var buf []antlr.Token- var sqls []Statement- byteOffset := 0-- for i, token := range tokens {- // Collect all tokens except the last one (EOF)- if i < len(tokens)-1 {- buf = append(buf, token)- }-- // Split on semicolon or at the end of tokens- if (token.GetTokenType() == semiTokenType || i == len(tokens)-1) && len(buf) > 0 {- // Reconstruct the text from tokens- bufStr := new(strings.Builder)- empty := true-- for _, b := range buf {- if _, err := bufStr.WriteString(b.GetText()); err != nil {- return nil, err- }- // Check if there's any non-hidden token (actual code, not just comments/whitespace)- if b.GetChannel() != antlr.TokenHiddenChannel {- empty = false- }- }-- stmtText := bufStr.String()- stmtByteLength := len(stmtText)-- // Use the last token in the buffer for End position (not EOF token when at end of stream)- lastToken := buf[len(buf)-1]- // Use buf[0]'s position directly since GetAllTokens() includes hidden channel tokens (whitespace/comments)- sqls = append(sqls, Statement{- Text: stmtText,- Range: &storepb.Range{- Start: int32(byteOffset),- End: int32(byteOffset + stmtByteLength),- },- End: common.ConvertANTLRTokenToExclusiveEndPosition(- int32(lastToken.GetLine()),- int32(lastToken.GetColumn()),- lastToken.GetText(),- ),- Start: &storepb.Position{- Line: int32(buf[0].GetLine()),- Column: int32(buf[0].GetColumn() + 1),- },- Empty: empty,- })-- byteOffset += stmtByteLength- buf = nil- continue- }- }-- return sqls, nil-}-// CalculateLineAndColumn calculates the 0-based line number and 0-based column (character offset)// for a given byte offset in the statement.func CalculateLineAndColumn(statement string, byteOffset int) (line, column int) {diff --git a/backend/plugin/parser/base/scanner.go b/backend/plugin/parser/base/scanner.godeleted file mode 100644index a8f22ad007..0000000000--- a/backend/plugin/parser/base/scanner.go+++ /dev/null@@ -1,218 +0,0 @@-package base--import "github.com/antlr4-go/antlr/v4"--// Scanner is the wrapper of antlr.CommonTokenStream.-// It provides a common scanner for all parsers-// and more useful methods.-type Scanner struct {- input *antlr.CommonTokenStream- index int- tokens []antlr.Token- // tokenStack is used to store the index of the token for saving and restoring.- tokenStack []int-}--// NewScanner creates a new Scanner.-func NewScanner(input *antlr.CommonTokenStream, fillInput bool) *Scanner {- if fillInput {- input.Fill()- }- return &Scanner{- input: input,- index: 0,- tokens: input.GetAllTokens(),- }-}--// GetIndex returns the current index of the scanner.-func (s *Scanner) GetIndex() int {- return s.index-}--// GetTokenChannel returns the channel of the current token.-func (s *Scanner) GetTokenChannel() int {- return s.tokens[s.index].GetChannel()-}--// GetTokenText returns the text of the current token.-func (s *Scanner) GetTokenText() string {- return s.tokens[s.index].GetText()-}--// GetPreviousTokenType returns the type of the previous token.-// If skipHidden is true, it will skip the hidden tokens.-// It does not change the current index of the scanner.-func (s *Scanner) GetPreviousTokenType(skipHidden bool) int {- index := s.index- for index > 0 {- index--- if s.tokens[index].GetChannel() == antlr.TokenDefaultChannel || !skipHidden {- return s.tokens[index].GetTokenType()- }- }-- return antlr.TokenInvalidType-}--// GetPreviousTokenText returns the text of the previous tok-// If skipHidden is true, it will skip the hidden tokens.-// It does not change the current index of the scanner.-func (s *Scanner) GetPreviousTokenText(skipHidden bool) string {- index := s.index- for index > 0 {- index--- if s.tokens[index].GetChannel() == antlr.TokenDefaultChannel || !skipHidden {- return s.tokens[index].GetText()- }- }-- return ""-}--// Push pushes the current index to the stack.-func (s *Scanner) Push() {- s.tokenStack = append(s.tokenStack, s.index)-}--// PopAndRestore pops the index from the stack and restores the index.-func (s *Scanner) PopAndRestore() bool {- if len(s.tokenStack) == 0 {- return false- }-- s.index = s.tokenStack[len(s.tokenStack)-1]- s.tokenStack = s.tokenStack[:len(s.tokenStack)-1]- return true-}--// Backward moves the index backward.-// If skipHidden is true, it will skip the hidden tokens.-func (s *Scanner) Backward(skipHidden bool) bool {- for s.index > 0 {- s.index--- if s.tokens[s.index].GetChannel() == antlr.TokenDefaultChannel || !skipHidden {- return true- }- }- return false-}--// Forward moves the index forward.-// If skipHidden is true, it will skip the hidden tokens.-func (s *Scanner) Forward(skipHidden bool) bool {- for s.index < len(s.tokens)-1 {- s.index++- if s.tokens[s.index].GetChannel() == antlr.TokenDefaultChannel || !skipHidden {- return true- }- }- return false-}--// GetTokenType returns the type of the current token.-func (s *Scanner) GetTokenType() int {- return s.tokens[s.index].GetTokenType()-}--// SkipTokenSequence skips the token sequence.-// This method will ignore the hidden tokens for scanner, but not check for given token list.-// If the given token list is not fully matched:-// 1. It will skip the common prefix of the given token list and the current token list in the scanner.-// 2. Return false.-//-// If the given token list is fully matched:-// 1. It will skip the matched tokens in the scanner.-// 2. Return true.-func (s *Scanner) SkipTokenSequence(list []int) bool {- if s.index >= len(s.tokens) {- return false- }-- for _, token := range list {- if s.tokens[s.index].GetTokenType() != token {- return false- }-- // Skip to the next unhidden token.- s.index++- for s.index < len(s.tokens) && s.tokens[s.index].GetChannel() != antlr.TokenDefaultChannel {- s.index++- }-- if s.index >= len(s.tokens) {- return false- }- }-- // Fully matched.- return true-}--// IsTokenType returns whether the current token type is the given token type.-func (s *Scanner) IsTokenType(tokenType int) bool {- return s.tokens[s.index].GetTokenType() == tokenType-}--// SeekIndex seeks the index of the scanner.-// If the index is out of range, it will do nothing.-func (s *Scanner) SeekIndex(index int) {- if index >= 0 && index < len(s.tokens) {- s.index = index- }-}--// SeekPosition seeks the position of the scanner.-// 1. If the position is before the first token, it will do nothing and return false.-//-// The following cases will move the index to the token and return true:-// 2. If the position is in one of the tokens, it will move the index to the token.-// 3. If the position is between two tokens, it will move the index to the previous token.-// 4. If the position is after the last token, it will move the index to the last token.-func (s *Scanner) SeekPosition(line, column int) bool {- if len(s.tokens) == 0 {- return false- }-- index := 0- for index < len(s.tokens) {- token := s.tokens[index]- tokenLine := token.GetLine()- if tokenLine >= line {- tokenColumn := token.GetColumn()- tokenLength := token.GetStop() - token.GetStart() + 1- if tokenLine == line && tokenColumn <= column && column < tokenColumn+tokenLength {- s.index = index- break- }-- if tokenLine > line || tokenColumn > column {- if index == 0 {- return false- }-- s.index = index - 1- break- }- }- index++- }-- if index == len(s.tokens) {- s.index = index - 1- }-- return true-}--// GetFollowingText returns the following text of the current token.-func (s *Scanner) GetFollowingText() string {- stream := s.tokens[s.index].GetInputStream()- return stream.GetText(s.tokens[s.index].GetStart(), stream.Size()-1)-}--// GetFollowingTextAfter returns the following text after the given position.-func (s *Scanner) GetFollowingTextAfter(pos int) string {- stream := s.tokens[s.index].GetInputStream()- return stream.GetText(pos, stream.Size()-1)-}diff --git a/backend/plugin/parser/base/statement_ranges.go b/backend/plugin/parser/base/statement_ranges.godeleted file mode 100644index eff37323ee..0000000000--- a/backend/plugin/parser/base/statement_ranges.go+++ /dev/null@@ -1,119 +0,0 @@-package base--import (- "strings"- "unicode"-- "github.com/antlr4-go/antlr/v4"-- protocol "github.com/bytebase/lsp-protocol"-)--// GetANTLRStatementRangesUTF16Position is a generic function to extract statement ranges from ANTLR token streams.-// It returns the ranges of statements in UTF-16 positions suitable for LSP protocol.-//-// Parameters:-// - tokenStream: The ANTLR token stream to process-// - eofTokenType: The EOF token type (e.g., parser.SnowflakeParserEOF)-// - terminatorTokenType: The statement terminator token type (e.g., parser.SnowflakeParserSEMI)-//-// The function handles:-// - UTF-16 surrogate pairs for characters outside the Basic Multilingual Plane (BMP)-// - Trimming leading whitespace from statements for more accurate ranges-// - Ignoring single terminator statements-// - Handling statements that don't end with a terminator-func GetANTLRStatementRangesUTF16Position(tokenStream *antlr.CommonTokenStream, eofTokenType, terminatorTokenType int) []Range {- var ranges []Range- var buf []antlr.Token- beginLine, beginUTF16CodePointOffset := 0, 0- endLine, endUTF16CodePointOffset := 0, 0-- for _, token := range tokenStream.GetAllTokens() {- if token.GetTokenType() == eofTokenType {- break- }- tokenText := token.GetText()- for _, r := range tokenText {- if r == '\n' {- endLine++- endUTF16CodePointOffset = 0- } else {- if r <= 0xFFFF {- endUTF16CodePointOffset++- } else {- // For non-BMP characters, we count as 2 UTF-16 code units.- endUTF16CodePointOffset += 2- }- }- }-- // Ignore heading spaces to provide more accurate range.- if len(buf) == 0 && len(strings.TrimSpace(tokenText)) == 0 {- beginLine = endLine- beginUTF16CodePointOffset = endUTF16CodePointOffset- continue- }-- buf = append(buf, token)-- // Check if this is a statement terminator.- if token.GetTokenType() != terminatorTokenType {- continue- }- if len(buf) == 1 {- // Ignore single terminator statement.- buf = nil- continue- }-- ranges = append(ranges, Range{- Start: protocol.Position{- Line: uint32(beginLine),- Character: uint32(beginUTF16CodePointOffset),- },- End: protocol.Position{- Line: uint32(endLine),- Character: uint32(endUTF16CodePointOffset),- },- })- buf = nil-- // Set the next begin position to the next token after terminator.- beginLine = endLine- beginUTF16CodePointOffset = endUTF16CodePointOffset- }-- // If there are remaining tokens in the buffer, it means the last statement does not end with terminator.- if len(buf) > 0 {- ranges = append(ranges, Range{- Start: protocol.Position{- Line: uint32(beginLine),- Character: uint32(beginUTF16CodePointOffset),- },- End: protocol.Position{- Line: uint32(endLine),- Character: uint32(endUTF16CodePointOffset),- },- })- }-- return ranges-}--// PrepareANTLRTokenStream is a helper function to prepare an ANTLR token stream from a statement.-// It trims trailing whitespace and creates the necessary lexer and token stream.-//-// Parameters:-// - statement: The SQL statement to process-// - createLexer: A function that creates an ANTLR lexer from an input stream-//-// Returns:-// - A filled CommonTokenStream ready for processing-func PrepareANTLRTokenStream(statement string, createLexer func(antlr.CharStream) antlr.Lexer) *antlr.CommonTokenStream {- trimmedStatement := strings.TrimRightFunc(statement, unicode.IsSpace)- inputStream := antlr.NewInputStream(trimmedStatement)- lexer := createLexer(inputStream)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()- return stream-}diff --git a/backend/plugin/parser/base/statement_test.go b/backend/plugin/parser/base/statement_test.goindex 53e04235ea..32ad11175f 100644--- a/backend/plugin/parser/base/statement_test.go+++ b/backend/plugin/parser/base/statement_test.go@@ -8,6 +8,14 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store")+type testAST struct {+ start *storepb.Position+}++func (a *testAST) ASTStartPosition() *storepb.Position {+ return a.start+}+func TestFilterEmptyStatements(t *testing.T) {statements := []Statement{{Text: "SELECT 1", Empty: false},@@ -24,8 +32,8 @@ func TestFilterEmptyStatements(t *testing.T) {}func TestExtractASTs(t *testing.T) {- ast1 := &ANTLRAST{StartPosition: &storepb.Position{Line: 1}}- ast2 := &ANTLRAST{StartPosition: &storepb.Position{Line: 2}}+ ast1 := &testAST{start: &storepb.Position{Line: 1}}+ ast2 := &testAST{start: &storepb.Position{Line: 2}}statements := []ParsedStatement{{Statement: Statement{Text: "SELECT 1"}, AST: ast1},@@ -47,7 +55,7 @@ func TestParsedStatementEmbedding(t *testing.T) {Text: "SELECT 1",Start: &storepb.Position{Line: 6, Column: 1},},- AST: &ANTLRAST{StartPosition: &storepb.Position{Line: 6}},+ AST: &testAST{start: &storepb.Position{Line: 6}},}// Direct access to embedded fieldsdiff --git a/backend/plugin/parser/mysql/antlr_dependency_test.go b/backend/plugin/parser/mysql/antlr_dependency_test.goindex 4497be3d99..2dda560100 100644--- a/backend/plugin/parser/mysql/antlr_dependency_test.go+++ b/backend/plugin/parser/mysql/antlr_dependency_test.go@@ -24,6 +24,11 @@ func TestMySQLDelimiterHandlingDoesNotUseLegacyTokenizer(t *testing.T) {require.NoError(t, err)source := string(content)+ require.NotContains(t, source, "github.com/antlr4-go/antlr/v4")+ require.NotContains(t, source, "github.com/bytebase/parser/mysql")+ require.NotContains(t, source, "base.ANTLRAST")+ require.NotContains(t, source, "ParseMySQL(")+ require.NotContains(t, source, "parseSingleStatement")require.NotContains(t, source, "plugin/parser/tokenizer")require.NotContains(t, source, "SplitTiDBMultiSQL")require.NotContains(t, source, "mysqlutil")diff --git a/backend/plugin/parser/mysql/mysql.go b/backend/plugin/parser/mysql/mysql.goindex 32f6fc3663..33627387d2 100644--- a/backend/plugin/parser/mysql/mysql.go+++ b/backend/plugin/parser/mysql/mysql.go@@ -3,11 +3,8 @@ package mysqlimport ("errors""fmt"- "strings"- "github.com/antlr4-go/antlr/v4"mysqlomniparser "github.com/bytebase/omni/mysql/parser"- parser "github.com/bytebase/parser/mysql"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"@@ -86,148 +83,3 @@ func convertOmniError(err error, stmt base.Statement) error {RawMessage: parseErr.Message,}}--// ParseMySQL parses the given SQL statement and returns the AST.-func ParseMySQL(statement string) ([]*base.ANTLRAST, error) {- stmts, err := SplitSQL(statement)- if err != nil {- return nil, err- }- return parseMySQLStatementsInternal(stmts)-}--// parseMySQLStatementsInternal parses pre-split statements without re-splitting.-// This is the internal implementation used by both ParseMySQL and parseMySQLStatements.-func parseMySQLStatementsInternal(stmts []base.Statement) ([]*base.ANTLRAST, error) {- var result []*base.ANTLRAST-- for _, s := range stmts {- if s.Empty {- continue- }- s.Text = mysqlAddSemicolonIfNeeded(s.Text)-- tree, tokens, err := parseSingleStatement(s.BaseLine(), s.Text)- if err != nil {- return nil, err- }-- if isEmptyStatement(tokens) {- continue- }-- result = append(result, &base.ANTLRAST{- StartPosition: &storepb.Position{Line: int32(s.BaseLine()) + 1},- Tree: tree,- Tokens: tokens,- })- }-- return result, nil-}--func parseSingleStatement(baseLine int, statement string) (antlr.Tree, *antlr.CommonTokenStream, error) {- input := antlr.NewInputStream(statement)- lexer := parser.NewMySQLLexer(input)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)-- p := parser.NewMySQLParser(stream)-- startPosition := &storepb.Position{Line: int32(baseLine) + 1}- lexerErrorListener := &base.ParseErrorListener{- StartPosition: startPosition,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)-- parserErrorListener := &base.ParseErrorListener{- StartPosition: startPosition,- }- p.RemoveErrorListeners()- p.AddErrorListener(parserErrorListener)-- p.BuildParseTrees = true-- tree := p.Script()-- if lexerErrorListener.Err != nil {- return nil, nil, lexerErrorListener.Err- }-- if parserErrorListener.Err != nil {- return nil, nil, parserErrorListener.Err- }-- return tree, stream, nil-}--func mysqlAddSemicolonIfNeeded(sql string) string {- lexer := parser.NewMySQLLexer(antlr.NewInputStream(sql))- lexerErrorListener := &base.ParseErrorListener{- Statement: sql,- }- lexer.RemoveErrorListeners()- lexer.AddErrorListener(lexerErrorListener)- stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)- stream.Fill()- if lexerErrorListener.Err != nil {- // If the lexer fails, we cannot add semicolon.- return sql- }- tokens := stream.GetAllTokens()- for i := len(tokens) - 1; i >= 0; i-- {- if tokens[i].GetChannel() != antlr.TokenDefaultChannel || tokens[i].GetTokenType() == parser.MySQLParserEOF {- continue- }-- // The last default channel token is a semicolon.- if tokens[i].GetTokenType() == parser.MySQLParserSEMICOLON_SYMBOL {- return sql- }-- var result []string- result = append(result, stream.GetTextFromInterval(antlr.NewInterval(0, tokens[i].GetTokenIndex())))- result = append(result, ";")- result = append(result, stream.GetTextFromInterval(antlr.NewInterval(tokens[i].GetTokenIndex()+1, tokens[len(tokens)-1].GetTokenIndex())))- return strings.Join(result, "")- }- return sql-}--func isEmptyStatement(tokens *antlr.CommonTokenStream) bool {- for _, token := range tokens.GetAllTokens() {- if token.GetChannel() == antlr.TokenDefaultChannel && token.GetTokenType() != parser.MySQLParserSEMICOLON_SYMBOL && token.GetTokenType() != parser.MySQLParserEOF {- return false- }- }- return true-}--// IsTopMySQLRule returns true if the given context is a top-level MySQL rule.-func IsTopMySQLRule(ctx *antlr.BaseParserRuleContext) bool {- if ctx.GetParent() == nil {- return false- }- switch ctx.GetParent().(type) {- case *parser.SimpleStatementContext:- if ctx.GetParent().GetParent() == nil {- return false- }- if _, ok := ctx.GetParent().GetParent().(*parser.QueryContext); !ok {- return false- }- case *parser.CreateStatementContext, *parser.DropStatementContext, *parser.TransactionOrLockingStatementContext, *parser.AlterStatementContext:- if ctx.GetParent().GetParent() == nil {- return false- }- if ctx.GetParent().GetParent().GetParent() == nil {- return false- }- if _, ok := ctx.GetParent().GetParent().GetParent().(*parser.QueryContext); !ok {- return false- }- default:- return false- }- return true-}diff --git a/backend/plugin/parser/mysql/mysql_test.go b/backend/plugin/parser/mysql/mysql_test.goindex 6e4f5084b5..94a8522d11 100644--- a/backend/plugin/parser/mysql/mysql_test.go+++ b/backend/plugin/parser/mysql/mysql_test.go@@ -9,106 +9,6 @@ import ("github.com/bytebase/bytebase/backend/plugin/parser/base")-func TestMySQLParser(t *testing.T) {- tests := []struct {- statement string- errorMessage string- total int- }{- {- statement: "INSERT INTO tbl (id, name, gender, height) VALUES(1, 'Alice', B'0', B'01111111');",- total: 1,- },- {- statement: "aaa",- errorMessage: "Syntax error at line 1:1 \nrelated text: aaa",- },- {- statement: "select 1;\n selec 5;\nselect 6;",- errorMessage: "Syntax error at line 2:4 \nrelated text: \n selec",- },- {- statement: "select * from t;\n -- comments",- total: 1,- },- {- statement: "SELECT count(t.a) as TID from t1 as t;",- total: 1,- },- {- statement: "SELECT * FROM t1 WHERE c1 = 1; SELECT * FROM t2; ",- total: 2,- },- {- statement: "CREATE TABLE t1 (c1 INT);",- total: 1,- },- {- statement: `- -- Drop the procedure if it already exists- DROP PROCEDURE IF EXISTS complex_procedure;-- -- Create the procedure- CREATE PROCEDURE complex_procedure(IN input VARCHAR(255), OUT output VARCHAR(255))- BEGIN- DECLARE var1 VARCHAR(255);- DECLARE var2 VARCHAR(255);- DECLARE var3 INT;-- -- Setting initial values- SET var1 = 'Hello, ';- SET var2 = 'World!';- SET var3 = 1;-- -- If statement- IF var3 = 1 THEN- -- String concatenation- SET output = CONCAT(var1, input, ' and ', var2);- ELSE- -- Use a SELECT statement to get data from a table- SELECT column_name INTO output FROM table_name WHERE condition_expression;- END IF;- END;-- -- Call the procedure- CALL complex_procedure('MySQL', @output_value);- SELECT @output_value;- `,- total: 4,- },- {- statement: `CREATE TABLE IF NOT EXISTS test_table (- id INT PRIMARY KEY,- name VARCHAR(255),- description VARCHAR(255)- );-- REPLACE INTO test_table (id, name, description)- VALUES (1, 'Test', 'This is a test.');- `,- total: 2,- },- }-- for i, test := range tests {- list, err := ParseMySQL(test.statement)- if test.errorMessage == "" {- require.NoError(t, err, i)- require.Equal(t, test.total, len(list), i)- } else {- require.EqualError(t, err, test.errorMessage)- }- }-}--func TestParseMySQLAddsSemicolonToEachDelimiterSegment(t *testing.T) {- statement := "DELIMITER //\nCREATE PROCEDURE p()\nBEGIN\n SELECT 1;\nEND//\nDELIMITER ;\nCALL p();"-- list, err := ParseMySQL(statement)- require.NoError(t, err)- require.Len(t, list, 2)-}-func TestParseMySQLStatements(t *testing.T) {statement := "SELECT 1; SELECT 2;"diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.godeleted file mode 100644index 3d7b6f2bc5..0000000000--- a/backend/plugin/parser/mysql/normalize.go+++ /dev/null@@ -1,385 +0,0 @@-package mysql--import (- "strings"-- parser "github.com/bytebase/parser/mysql"-)--// NormalizeMySQLTableName normalizes the given table name.-func NormalizeMySQLTableName(ctx parser.ITableNameContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- if ctx.DotIdentifier() != nil {- return "", NormalizeMySQLIdentifier(ctx.DotIdentifier().Identifier())- }- return "", ""-}--// NormalizeMySQLTableRef normalizes the given table reference.-func NormalizeMySQLTableRef(ctx parser.ITableRefContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- if ctx.DotIdentifier() != nil {- return "", NormalizeMySQLIdentifier(ctx.DotIdentifier().Identifier())- }- return "", ""-}--func NormalizeMySQLTableRefWithWildcard(ctx parser.ITableRefWithWildcardContext) (string, string) {- identifier := NormalizeMySQLIdentifier(ctx.Identifier())- if ctx.DotIdentifier() == nil {- return "", identifier- }-- return NormalizeMySQLIdentifier(ctx.DotIdentifier().Identifier()), identifier-}--// NormalizeMySQLColumnName normalizes the given column name.-func NormalizeMySQLColumnName(ctx parser.IColumnNameContext) (string, string, string) {- if ctx.Identifier() != nil {- return "", "", NormalizeMySQLIdentifier(ctx.Identifier())- }- return NormalizeMySQLFieldIdentifier(ctx.FieldIdentifier())-}--// NormalizeMySQLFieldIdentifier normalizes the given field identifier.-func NormalizeMySQLFieldIdentifier(ctx parser.IFieldIdentifierContext) (string, string, string) {- list := []string{}- if ctx.QualifiedIdentifier() != nil {- id1, id2 := normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- list = append(list, id1, id2)- }-- if ctx.DotIdentifier() != nil {- list = append(list, NormalizeMySQLIdentifier(ctx.DotIdentifier().Identifier()))- }-- for len(list) < 3 {- list = append([]string{""}, list...)- }-- return list[0], list[1], list[2]-}--func normalizeMySQLQualifiedIdentifier(qualifiedIdentifier parser.IQualifiedIdentifierContext) (string, string) {- list := []string{NormalizeMySQLIdentifier(qualifiedIdentifier.Identifier())}- if qualifiedIdentifier.DotIdentifier() != nil {- list = append(list, NormalizeMySQLIdentifier(qualifiedIdentifier.DotIdentifier().Identifier()))- }-- if len(list) == 1 {- list = append([]string{""}, list...)- }-- return list[0], list[1]-}--// NormalizeMySQLIdentifier normalizes the given identifier.-func NormalizeMySQLIdentifier(identifier parser.IIdentifierContext) string {- if identifier.PureIdentifier() != nil {- if identifier.PureIdentifier().IDENTIFIER() != nil {- return identifier.PureIdentifier().IDENTIFIER().GetText()- }- // For back tick quoted identifier, we need to remove the back tick.- text := identifier.PureIdentifier().BACK_TICK_QUOTED_ID().GetText()- return text[1 : len(text)-1]- }- return identifier.GetText()-}--func NormalizeMySQLPureIdentifier(pureIdentifier parser.IPureIdentifierContext) string {- if pureIdentifier.IDENTIFIER() != nil {- return pureIdentifier.IDENTIFIER().GetText()- }- if pureIdentifier.BACK_TICK_QUOTED_ID() == nil {- // For back tick quoted identifier, we need to remove the back tick.- text := pureIdentifier.BACK_TICK_QUOTED_ID().GetText()- return text[1 : len(text)-1]- }- return pureIdentifier.GetText()-}--// NormalizeMySQLTextOrIdentifier normalizes the given TextOrIdentifier.-func NormalizeMySQLTextOrIdentifier(ctx parser.ITextOrIdentifierContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- textString := ctx.TextStringLiteral().GetText()- // remove the quotations.- return textString[1 : len(textString)-1]-}--// NormalizeMySQLTextLiteral normalize the given TextStringLiteral.-func NormalizeMySQLTextLiteral(ctx parser.ITextLiteralContext) string {- textString := ctx.GetText()- // remove the quotations.- return textString[1 : len(textString)-1]-}--// NormalizeMySQLTextStringLiteral normalize the given TextStringLiteral.-func NormalizeMySQLTextStringLiteral(ctx parser.ITextStringLiteralContext) string {- textString := ctx.GetText()- // remove the quotations.- return textString[1 : len(textString)-1]-}--// NormalizeMySQLSignedLiteral normalize the given SignedLiteral.-func NormalizeMySQLSignedLiteral(ctx parser.ISignedLiteralContext) string {- textString := ctx.GetText()- if (strings.HasPrefix(textString, "'") && strings.HasSuffix(textString, "'")) || (strings.HasPrefix(textString, "\"") && strings.HasSuffix(textString, "\"")) {- textString = textString[1 : len(textString)-1]- }- return textString-}--// NormalizeMySQLSelectAlias normalizes the given select alias.-func NormalizeMySQLSelectAlias(selectAlias parser.ISelectAliasContext) string {- if selectAlias.Identifier() != nil {- return NormalizeMySQLIdentifier(selectAlias.Identifier())- }- textString := selectAlias.TextStringLiteral().GetText()- return textString[1 : len(textString)-1]-}--// NormalizeMySQLIdentifierList normalizes the given identifier list.-func NormalizeMySQLIdentifierList(ctx parser.IIdentifierListContext) []string {- if ctx == nil {- return []string{}- }-- var result []string- for _, identifier := range ctx.AllIdentifier() {- result = append(result, NormalizeMySQLIdentifier(identifier))- }- return result-}--// NormalizeMySQLViewName normalizes the given view name.-func NormalizeMySQLViewName(ctx parser.IViewNameContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- if ctx.DotIdentifier() != nil {- return "", NormalizeMySQLIdentifier(ctx.DotIdentifier().Identifier())- }- return "", ""-}--// NormalizeMySQLTriggerName normalizes the given trigger name.-func NormalizeMySQLTriggerName(ctx parser.ITriggerNameContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- return "", ""-}--// NormalizeMySQLFunctionName normalizes the given function name.-func NormalizeMySQLFunctionName(ctx parser.IFunctionNameContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- return "", ""-}--// NormalizeMySQLProcedureName normalizes the given procedure name.-func NormalizeMySQLProcedureName(ctx parser.IProcedureNameContext) (string, string) {- if ctx.QualifiedIdentifier() != nil {- return normalizeMySQLQualifiedIdentifier(ctx.QualifiedIdentifier())- }- return "", ""-}--// NormalizeMySQLSchemaRef normalize the given schemaRef.-func NormalizeMySQLSchemaRef(ctx parser.ISchemaRefContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- return ""-}--// NormalizeMySQLSchemaName normalize the given schemaName.-func NormalizeMySQLSchemaName(ctx parser.ISchemaNameContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- return ""-}--// NormalizeMySQLKeyListVariants normalize the given keyListVariants.-func NormalizeKeyListVariants(ctx parser.IKeyListVariantsContext) []string {- if ctx.KeyList() != nil {- return NormalizeKeyList(ctx.KeyList())- }- if ctx.KeyListWithExpression() != nil {- return NormalizeKeyListWithExpression(ctx.KeyListWithExpression())- }- return nil-}--// NormalizeMySQLKeyList normalize the given keyList.-func NormalizeKeyList(ctx parser.IKeyListContext) []string {- var result []string- for _, key := range ctx.AllKeyPart() {- keyText := NormalizeMySQLIdentifier(key.Identifier())- result = append(result, keyText)- }- return result-}--// NormalizeMySQLKeyListWithExpression normalizes the given keyListWithExpression.-func NormalizeKeyListWithExpression(ctx parser.IKeyListWithExpressionContext) []string {- var result []string- for _, expression := range ctx.AllKeyPartOrExpression() {- if expression.KeyPart() != nil {- keyText := NormalizeMySQLIdentifier(expression.KeyPart().Identifier())- result = append(result, keyText)- } else if expression.ExprWithParentheses() != nil {- keyText := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(expression.ExprWithParentheses().Expr())- result = append(result, keyText)- }- }- return result-}--// NormalizeMySQLIndexName normalize the given IndexName.-func NormalizeIndexName(ctx parser.IIndexNameContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- return ""-}--// NormalizeMySQLIndexName normalize the given IndeRef.-func NormalizeIndexRef(ctx parser.IIndexRefContext) (string, string, string) {- if ctx.FieldIdentifier() != nil {- return NormalizeMySQLFieldIdentifier(ctx.FieldIdentifier())- }- return "", "", ""-}--// NormalizeMySQLIdentifierListWithParentheses normalize the given IdentififerListWithparentheses.-func NormalizeIdentifierListWithParentheses(ctx parser.IIdentifierListWithParenthesesContext) []string {- if ctx.IdentifierList() != nil {- return NormalizeMySQLIdentifierList(ctx.IdentifierList())- }- return nil-}--// NormalizeMySQLConstraintName normalize the given IConstraintName.-func NormalizeConstraintName(ctx parser.IConstraintNameContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- return ""-}--// NormalizeMySQLColumnInternalRef noamalizes the given columnInternalRef.-func NormalizeMySQLColumnInternalRef(ctx parser.IColumnInternalRefContext) string {- if ctx.Identifier() != nil {- return NormalizeMySQLIdentifier(ctx.Identifier())- }- return ""-}--func NormalizeMySQLColumnRef(ctx parser.IColumnRefContext) (string, string, string) {- return NormalizeMySQLFieldIdentifier(ctx.FieldIdentifier())-}--// NormalizeMySQLCharsetName noamalizes the given charset name.-func NormalizeMySQLCharsetName(ctx parser.ICharsetNameContext) string {- switch {- case ctx.TextOrIdentifier() != nil:- return NormalizeMySQLTextOrIdentifier(ctx.TextOrIdentifier())- case ctx.DEFAULT_SYMBOL() != nil:- return "DEFAULT"- case ctx.BINARY_SYMBOL() != nil:- return "BINARY"- default:- return ""- }-}--// NormalizeMySQLCollationName noamalizes the given collation name.-func NormalizeMySQLCollationName(ctx parser.ICollationNameContext) string {- switch {- case ctx.TextOrIdentifier() != nil:- return NormalizeMySQLTextOrIdentifier(ctx.TextOrIdentifier())- case ctx.DEFAULT_SYMBOL() != nil:- return "DEFAULT"- case ctx.BINARY_SYMBOL() != nil:- return "BINARY"- default:- return ""- }-}--// NormalizeMySQLDataType noamalizes the given dataType.-// campact for tidb parser compatibility.-// eg: varchar(5).-// compact is true, return varchar.-// compact is false, return varchar(5).-func NormalizeMySQLDataType(ctx parser.IDataTypeContext, compact bool) string {- if !compact {- return ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)- }- switch ctx.GetType_().GetTokenType() {- case parser.MySQLParserDOUBLE_SYMBOL:- if ctx.PRECISION_SYMBOL() != nil {- return "double precision"- }- return "double"- case parser.MySQLParserCHAR_SYMBOL:- if ctx.VARYING_SYMBOL() != nil {- return "char varying"- }- return "char"- default:- return strings.ToLower(ctx.GetType_().GetText())- }-}--// GetCharSetName get charset name from DataTypeContext.-func GetCharSetName(ctx parser.IDataTypeContext) string {- if ctx.CharsetWithOptBinary() == nil {- return ""- }- charset := NormalizeMySQLCharsetName(ctx.CharsetWithOptBinary().CharsetName())- return charset-}--// GetCollationName get collation name from FieldDefinitionContext.-func GetCollationName(ctx parser.IFieldDefinitionContext) string {- for _, attr := range ctx.AllColumnAttribute() {- if attr != nil && attr.Collate() != nil && attr.Collate().CollationName() != nil {- return NormalizeMySQLCollationName(attr.Collate().CollationName())- }- }- return ""-}--// IsTimeType check if the dataType is time type.-func IsTimeType(ctx parser.IDataTypeContext) bool {- if ctx.GetType_() == nil {- return false- }-- switch ctx.GetType_().GetTokenType() {- case parser.MySQLParserDATETIME_SYMBOL, parser.MySQLParserTIMESTAMP_SYMBOL:- return true- default:- return false- }-}--// IsAutoIncrement check if this column is auto_increment.-func IsAutoIncrement(ctx parser.IFieldDefinitionContext) bool {- for _, attr := range ctx.AllColumnAttribute() {- if attr.AUTO_INCREMENT_SYMBOL() != nil {- return true- }- }- return false-}diff --git a/backend/plugin/parser/plsql/omni_test.go b/backend/plugin/parser/plsql/omni_test.goindex 36db374196..3e733e6044 100644--- a/backend/plugin/parser/plsql/omni_test.go+++ b/backend/plugin/parser/plsql/omni_test.go@@ -175,7 +175,7 @@ func TestOracleOmniASTWrapper(t *testing.T) {require.Nil(t, got)}-func TestOracleOmniASTDoesNotProvideANTLRFallback(t *testing.T) {+func TestOracleOmniASTUsesOmniNode(t *testing.T) {start := &storepb.Position{Line: 4, Column: 1}omniAST := &OmniAST{Node: &ast.SelectStmt{},@@ -183,9 +183,9 @@ func TestOracleOmniASTDoesNotProvideANTLRFallback(t *testing.T) {StartPosition: start,}- antlrAST, ok := base.GetANTLRAST(omniAST)- require.False(t, ok)- require.Nil(t, antlrAST)+ node, ok := GetOmniNode(omniAST)+ require.True(t, ok)+ require.Same(t, omniAST.Node, node)}func TestOracleByteOffsetToRunePosition(t *testing.T) {diff --git a/backend/plugin/parser/redshift/omni_migration_test.go b/backend/plugin/parser/redshift/omni_migration_test.goindex c04dc3d246..408ffe86c8 100644--- a/backend/plugin/parser/redshift/omni_migration_test.go+++ b/backend/plugin/parser/redshift/omni_migration_test.go@@ -18,9 +18,6 @@ func TestParseStatementsUsesOmniAST(t *testing.T) {_, ok := stmts[0].AST.(*OmniAST)require.True(t, ok)-- _, ok = base.GetANTLRAST(stmts[0].AST)- require.False(t, ok)}func TestParseStatementsUsesOmniASTForMultipleStatements(t *testing.T) {diff --git a/backend/plugin/parser/snowflake/omni_ast_test.go b/backend/plugin/parser/snowflake/omni_ast_test.goindex 5efc27363b..11ed5e5b32 100644--- a/backend/plugin/parser/snowflake/omni_ast_test.go+++ b/backend/plugin/parser/snowflake/omni_ast_test.go@@ -6,13 +6,18 @@ import (omniast "github.com/bytebase/omni/snowflake/ast""github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/plugin/parser/base"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store")+type testAST struct{}++func (testAST) ASTStartPosition() *storepb.Position {+ return nil+}+// TestParseSnowflakeStatementsOmniAST locks the omni-only contract: every// non-empty statement carries an OmniAST wrapping a non-nil omni node-// (reachable via GetOmniNode); the legacy ANTLR tree is gone, so-// base.GetANTLRAST reports false.+// (reachable via GetOmniNode); the legacy ANTLR tree is gone.func TestParseSnowflakeStatementsOmniAST(t *testing.T) {parsed, err := parseSnowflakeStatements("CREATE TABLE t1(id INT);\nSELECT id FROM t1;")require.NoError(t, err)@@ -28,10 +33,6 @@ func TestParseSnowflakeStatementsOmniAST(t *testing.T) {require.True(t, ok, "stmt %d: GetOmniNode must work", i)require.NotNil(t, node, "stmt %d", i)- // No legacy ANTLR tree rides along anymore.- _, ok = base.GetANTLRAST(ps.AST)- require.False(t, ok, "stmt %d: GetANTLRAST must report false", i)-// ASTStartPosition keeps the legacy shape (line-based, BaseLine()+1),// so position reporting is unchanged by the cutover.require.EqualValues(t, ps.BaseLine()+1, ps.AST.ASTStartPosition().GetLine(), "stmt %d", i)@@ -73,8 +74,7 @@ func TestGetOmniNodeEdgeCases(t *testing.T) {require.False(t, ok)require.Nil(t, node)- // A plain legacy ANTLRAST is not an OmniAST.- node, ok = GetOmniNode(&base.ANTLRAST{})+ node, ok = GetOmniNode(testAST{})require.False(t, ok)require.Nil(t, node)}diff --git a/backend/plugin/parser/tidb/ast.go b/backend/plugin/parser/tidb/ast.goindex 2f5b69d8a1..49de7d95a3 100644--- a/backend/plugin/parser/tidb/ast.go+++ b/backend/plugin/parser/tidb/ast.go@@ -26,7 +26,7 @@ func (a *AST) ASTStartPosition() *storepb.Position {// flipped to omni, *OmniAST values reach un-migrated callers of GetTiDBAST,// and those callers fall through to AsPingCapAST() to keep working.//-// Mirrors backend/plugin/parser/base/AntlrASTProvider for the mysql migration.+// Mirrors the native-AST bridge pattern used during parser migrations.// See plans/2026-04-23-omni-tidb-completion-plan.md §1.5.0 invariant #4.type PingCapASTProvider interface {AsPingCapAST() (*AST, bool)diff --git a/backend/plugin/parser/tidb/omni_test.go b/backend/plugin/parser/tidb/omni_test.goindex f40b39dad4..9d1d92bee7 100644--- a/backend/plugin/parser/tidb/omni_test.go+++ b/backend/plugin/parser/tidb/omni_test.go@@ -168,8 +168,7 @@ func TestAsPingCapASTReturnsNativeAST(t *testing.T) {// TestAsPingCapASTCachesAcrossCalls pins the lazy+cached contract: the// native parse runs once per OmniAST instance regardless of how many-// advisors call the bridge. Mirrors mysql/OmniAST.AsANTLRAST's antlrParsed-// flag pattern.+// advisors call the bridge.func TestAsPingCapASTCachesAcrossCalls(t *testing.T) {a := require.New(t)o := &OmniAST{diff --git a/backend/plugin/schema/mysql/antlr_dependency_test.go b/backend/plugin/schema/mysql/antlr_dependency_test.gonew file mode 100644index 0000000000..a864a4536a--- /dev/null+++ b/backend/plugin/schema/mysql/antlr_dependency_test.go@@ -0,0 +1,38 @@+package mysql++import (+ "os"+ "path/filepath"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestMySQLSchemaDoesNotKeepLegacyANTLRFallbacks(t *testing.T) {+ for _, name := range []string{+ "get_database_metadata.go",+ "walk_through.go",+ } {+ _, err := os.Stat(name)+ require.ErrorIs(t, err, os.ErrNotExist)+ }++ files, err := filepath.Glob("*.go")+ require.NoError(t, err)+ for _, file := range files {+ if filepath.Base(file) == "antlr_dependency_test.go" {+ continue+ }+ content, err := os.ReadFile(file)+ require.NoError(t, err)+ source := string(content)++ require.NotContains(t, source, "github.com/antlr4-go/antlr/v4", file)+ require.NotContains(t, source, "github.com/bytebase/parser/mysql", file)+ require.NotContains(t, source, "base.ANTLRAST", file)+ require.NotContains(t, source, "GetANTLRAST", file)+ require.NotContains(t, source, "ParseMySQL(", file)+ require.NotContains(t, source, "NormalizeMySQL", file)+ require.NotContains(t, source, "IsTopMySQLRule", file)+ }+}diff --git a/backend/plugin/schema/mysql/database.go b/backend/plugin/schema/mysql/database.gonew file mode 100644index 0000000000..38fcf621df--- /dev/null+++ b/backend/plugin/schema/mysql/database.go@@ -0,0 +1,15 @@+package mysql++import (+ "strings"++ "github.com/bytebase/bytebase/backend/store/model"+)++// isCurrentDatabase returns true if the given database is the current database.+func isCurrentDatabase(d *model.DatabaseMetadata, database string) bool {+ if !d.GetIsObjectCaseSensitive() {+ return strings.EqualFold(d.DatabaseName(), database)+ }+ return d.DatabaseName() == database+}diff --git a/backend/plugin/schema/mysql/get_database_metadata.go b/backend/plugin/schema/mysql/get_database_metadata.godeleted file mode 100644index a6b68d9d1b..0000000000--- a/backend/plugin/schema/mysql/get_database_metadata.go+++ /dev/null@@ -1,1721 +0,0 @@-package mysql--import (- "fmt"- "slices"- "strconv"- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/bytebase/parser/mysql"- "github.com/pkg/errors"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"-)--// Old ANTLR-based GetDatabaseMetadata registrations removed.-// GetDatabaseMetadataOmni is now registered in get_database_metadata_omni.go.--// GetDatabaseMetadata parses the MySQL schema text and returns the database metadata.-func GetDatabaseMetadata(schemaText string) (*storepb.DatabaseSchemaMetadata, error) {- parseResult, err := mysqlparser.ParseMySQL(schemaText)- if err != nil {- return nil, errors.Wrapf(err, "failed to parse MySQL schema")- }-- extractor := &metadataExtractor{- currentDatabase: "",- currentSchema: "",- tables: make(map[string]*storepb.TableMetadata),- views: make(map[string]*storepb.ViewMetadata),- functions: make(map[string]*storepb.FunctionMetadata),- procedures: make(map[string]*storepb.ProcedureMetadata),- triggers: make(map[string]*storepb.TriggerMetadata),- columnPrimaryKeys: make(map[string][]string),- columnUniqueKeys: make(map[string]map[string]bool),- pendingForeignKeyIndexes: make(map[string][]*storepb.ForeignKeyMetadata),- }-- // Walk each parse tree- for _, result := range parseResult {- if result.Tree != nil {- antlr.ParseTreeWalkerDefault.Walk(extractor, result.Tree)- }- }-- if extractor.err != nil {- return nil, extractor.err- }-- // Create primary key indexes from column-level definitions- extractor.createColumnPrimaryKeyIndexes()- // Create unique indexes from column-level definitions- extractor.createColumnUniqueKeyIndexes()- // Create indexes for foreign keys that don't have existing suitable indexes- extractor.createPendingForeignKeyIndexes()-- // Build the final metadata structure- schemaMetadata := &storepb.DatabaseSchemaMetadata{- Name: extractor.currentDatabase,- }-- // MySQL doesn't have schemas in the same way as PostgreSQL- // All objects are in a single schema- schema := &storepb.SchemaMetadata{- Name: "",- }-- // Sort and add tables- var tableNames []string- for name := range extractor.tables {- tableNames = append(tableNames, name)- }- slices.Sort(tableNames)- for _, name := range tableNames {- schema.Tables = append(schema.Tables, extractor.tables[name])- }-- // Sort and add views- var viewNames []string- for name := range extractor.views {- viewNames = append(viewNames, name)- }- slices.Sort(viewNames)- for _, name := range viewNames {- schema.Views = append(schema.Views, extractor.views[name])- }-- // Sort and add functions- var functionNames []string- for name := range extractor.functions {- functionNames = append(functionNames, name)- }- slices.Sort(functionNames)- for _, name := range functionNames {- schema.Functions = append(schema.Functions, extractor.functions[name])- }-- // Sort and add procedures- var procedureNames []string- for name := range extractor.procedures {- procedureNames = append(procedureNames, name)- }- slices.Sort(procedureNames)- for _, name := range procedureNames {- schema.Procedures = append(schema.Procedures, extractor.procedures[name])- }-- schemaMetadata.Schemas = []*storepb.SchemaMetadata{schema}-- return schemaMetadata, nil-}--// metadataExtractor walks the parse tree and extracts metadata-type metadataExtractor struct {- *mysql.BaseMySQLParserListener-- currentDatabase string- currentSchema string- tables map[string]*storepb.TableMetadata- views map[string]*storepb.ViewMetadata- functions map[string]*storepb.FunctionMetadata- procedures map[string]*storepb.ProcedureMetadata- triggers map[string]*storepb.TriggerMetadata- // Track column-level primary keys to create index entries later- columnPrimaryKeys map[string][]string // tableName -> []columnNames- // Track column-level unique constraints to create index entries later- columnUniqueKeys map[string]map[string]bool // tableName -> columnName -> true- // Track foreign keys to create index entries after all table elements are processed- pendingForeignKeyIndexes map[string][]*storepb.ForeignKeyMetadata // tableName -> []foreignKeys- err error-}--// Helper function to get or create table-func (e *metadataExtractor) getOrCreateTable(tableName string) *storepb.TableMetadata {- if table, exists := e.tables[tableName]; exists {- return table- }-- table := &storepb.TableMetadata{- Name: tableName,- Columns: []*storepb.ColumnMetadata{},- Indexes: []*storepb.IndexMetadata{},- ForeignKeys: []*storepb.ForeignKeyMetadata{},- CheckConstraints: []*storepb.CheckConstraintMetadata{},- Triggers: []*storepb.TriggerMetadata{},- Partitions: []*storepb.TablePartitionMetadata{},- }- e.tables[tableName] = table- return table-}--// EnterCreateDatabase is called when entering a CREATE DATABASE statement-func (e *metadataExtractor) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext) {- if e.err != nil {- return- }-- if ctx.SchemaName() != nil {- e.currentDatabase = mysqlparser.NormalizeMySQLSchemaName(ctx.SchemaName())- }-}--// EnterCreateTable is called when entering a CREATE TABLE statement-func (e *metadataExtractor) EnterCreateTable(ctx *mysql.CreateTableContext) {- if e.err != nil {- return- }-- if ctx.TableName() == nil {- return- }-- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())- table := e.getOrCreateTable(tableName)-- // Extract table elements- if ctx.TableElementList() != nil {- e.extractTableElements(ctx.TableElementList(), table)- }-- // Extract partitioning- if ctx.PartitionClause() != nil {- e.extractPartitions(ctx.PartitionClause(), table)- }-- // Extract table comment- e.extractTableComment(ctx, table)-}--// extractTableElements extracts columns and constraints from table elements-func (e *metadataExtractor) extractTableElements(ctx mysql.ITableElementListContext, table *storepb.TableMetadata) {- if ctx == nil {- return- }-- for _, element := range ctx.AllTableElement() {- if element == nil {- continue- }-- // Handle column definitions- if columnDef := element.ColumnDefinition(); columnDef != nil {- e.extractColumnDefinition(columnDef, table)- }-- // Handle table constraints- if constraint := element.TableConstraintDef(); constraint != nil {- e.extractTableConstraint(constraint, table)- }- }-}--// extractColumnDefinition extracts column metadata-func (e *metadataExtractor) extractColumnDefinition(ctx mysql.IColumnDefinitionContext, table *storepb.TableMetadata) {- if ctx == nil || ctx.FieldDefinition() == nil {- return- }-- if ctx.ColumnName() == nil {- return- }-- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(ctx.ColumnName())- column := &storepb.ColumnMetadata{- Name: columnName,- Position: int32(len(table.Columns) + 1),- }-- // Extract data type- if ctx.FieldDefinition() != nil && ctx.FieldDefinition().DataType() != nil {- column.Type = e.extractDataType(ctx.FieldDefinition().DataType())- }-- // Extract column attributes (NULL/NOT NULL, DEFAULT, etc.)- if ctx.FieldDefinition() != nil {- e.extractFieldAttributes(ctx.FieldDefinition(), column, table.Name)- }-- // Extract comment- if ctx.FieldDefinition() != nil {- if comment := e.extractColumnComment(ctx.FieldDefinition()); comment != "" {- column.Comment = comment- }- }-- table.Columns = append(table.Columns, column)-}--// extractDataType extracts the data type as a string-func (*metadataExtractor) extractDataType(ctx mysql.IDataTypeContext) string {- if ctx == nil {- return ""- }-- // Get the text representation of the data type and normalize to lowercase- dataType := strings.ToLower(ctx.GetText())-- // MySQL normalizations: BOOLEAN is stored as TINYINT(1)- if dataType == "boolean" || dataType == "bool" {- dataType = "tinyint(1)"- }-- // MySQL spatial type normalizations- if dataType == "geometrycollection" {- dataType = "geomcollection"- }-- // Handle UNSIGNED and ZEROFILL attributes that are part of the data type- // MySQL parser concatenates these without spaces, so we need to add them back- dataType = normalizeDataTypeAttributes(dataType)-- return dataType-}--// normalizeDataTypeAttributes adds proper spacing for MySQL data type attributes-func normalizeDataTypeAttributes(dataType string) string {- // Handle combined UNSIGNED ZEROFILL first- if strings.Contains(dataType, "unsignedzerofill") {- dataType = strings.ReplaceAll(dataType, "unsignedzerofill", "unsigned zerofill")- }-- // Handle UNSIGNED attribute- if strings.Contains(dataType, "unsigned") {- // Replace patterns like "intunsigned" with "int unsigned"- // Find where "unsigned" starts- if idx := strings.Index(dataType, "unsigned"); idx > 0 {- // Check if there's a character before "unsigned" that's not a space- if dataType[idx-1] != ' ' {- // Insert space before "unsigned"- dataType = dataType[:idx] + " " + dataType[idx:]- }- }- }-- // Handle BINARY attribute- if strings.Contains(dataType, "binary") && !strings.Contains(dataType, "varbinary") {- // Replace patterns like "char(10)binary" with "char(10)"- // MySQL stores BINARY as part of the collation, not the data type- if idx := strings.Index(dataType, "binary"); idx > 0 {- if dataType[idx-1] != ' ' && dataType[idx-1] != ')' {- // Insert space before "binary"- dataType = dataType[:idx] + " " + dataType[idx:]- } else if dataType[idx-1] == ')' {- // For patterns like "char(10)binary", MySQL normalizes to just "char(10)"- dataType = dataType[:idx]- }- }- }-- // Handle ZEROFILL attribute- if strings.Contains(dataType, "zerofill") {- // Replace patterns like "intzerofill" with "int zerofill"- if idx := strings.Index(dataType, "zerofill"); idx > 0 {- if dataType[idx-1] != ' ' {- dataType = dataType[:idx] + " " + dataType[idx:]- }- }-- // MySQL implicitly adds UNSIGNED when ZEROFILL is specified- // If we have zerofill but not unsigned, add unsigned- if strings.Contains(dataType, "zerofill") && !strings.Contains(dataType, "unsigned") {- // Insert "unsigned " before "zerofill"- dataType = strings.ReplaceAll(dataType, "zerofill", "unsigned zerofill")- }-- // MySQL adds default display width for INT UNSIGNED ZEROFILL when none specified- if strings.HasPrefix(dataType, "int ") && strings.Contains(dataType, "unsigned zerofill") && !strings.Contains(dataType, "(") {- dataType = strings.ReplaceAll(dataType, "int ", "int(10) ")- }- }-- return dataType-}--// extractFieldAttributes extracts field attributes like NULL/NOT NULL, DEFAULT, AUTO_INCREMENT, PRIMARY KEY-func (e *metadataExtractor) extractFieldAttributes(ctx mysql.IFieldDefinitionContext, column *storepb.ColumnMetadata, tableName string) {- if ctx == nil {- return- }-- // Default to nullable- column.Nullable = true- hasExplicitDefault := false-- // Check for GENERATED columns- // For generated columns, MySQL stores them as having NULL default, not the generation expression- if ctx.GENERATED_SYMBOL() != nil && ctx.ALWAYS_SYMBOL() != nil && ctx.AS_SYMBOL() != nil && ctx.ExprWithParentheses() != nil {- // Generated columns have no default value in the traditional sense- column.Default = "NULL"- hasExplicitDefault = true- }-- for _, attr := range ctx.AllColumnAttribute() {- if attr == nil {- continue- }-- // Check for PRIMARY KEY - inline definition makes column NOT NULL- if attr.PRIMARY_SYMBOL() != nil && attr.KEY_SYMBOL() != nil {- column.Nullable = false- // Track this column as part of a primary key (we'll create the index later)- e.addColumnPrimaryKey(tableName, column.Name)- }-- // Check for UNIQUE - inline definition creates unique constraint- if attr.UNIQUE_SYMBOL() != nil {- // Track this column as having a unique constraint (we'll create the index later)- e.addColumnUniqueKey(tableName, column.Name)- }-- // Check for NULL/NOT NULL- if attr.NullLiteral() != nil {- column.Nullable = attr.NOT_SYMBOL() == nil- }-- // Check for DEFAULT value- if attr.DEFAULT_SYMBOL() != nil {- hasExplicitDefault = true- if attr.SignedLiteral() != nil {- defaultValue := mysqlparser.NormalizeMySQLSignedLiteral(attr.SignedLiteral())- normalizedValue := normalizeDefaultValue(defaultValue)- // For literal values, add quotes to match database sync format- if isLiteralValue(normalizedValue) {- column.Default = fmt.Sprintf("'%s'", normalizedValue)- } else {- column.Default = normalizedValue- }- } else if attr.ExprWithParentheses() != nil {- // Expression - wrap in parentheses like sync logic does for DEFAULT_GENERATED- column.Default = fmt.Sprintf("(%s)", attr.GetParser().GetTokenStream().GetTextFromRuleContext(attr.ExprWithParentheses()))- } else {- // Check for special keywords like CURRENT_TIMESTAMP- // Parse the entire attribute text to find DEFAULT keyword and what follows- attrText := attr.GetText()- attrTextUpper := strings.ToUpper(attrText)-- defaultIdx := strings.Index(attrTextUpper, "DEFAULT")- if defaultIdx >= 0 && len(attrTextUpper) > defaultIdx+7 {- remaining := attrTextUpper[defaultIdx+7:]- if strings.HasPrefix(remaining, "CURRENT_TIMESTAMP") || strings.HasPrefix(remaining, "NOW()") {- column.Default = "CURRENT_TIMESTAMP"- }- }- }- }-- // Check for AUTO_INCREMENT- if attr.AUTO_INCREMENT_SYMBOL() != nil {- hasExplicitDefault = true- if column.Default == "" {- column.Default = "AUTO_INCREMENT"- }- }- }-- // If column is nullable and has no explicit default, set Default to NULL- if column.Nullable && !hasExplicitDefault && column.Default == "" {- column.Default = "NULL"- }-}--// extractColumnComment extracts column comment-func (*metadataExtractor) extractColumnComment(ctx mysql.IFieldDefinitionContext) string {- if ctx == nil {- return ""- }-- for _, attr := range ctx.AllColumnAttribute() {- if attr == nil || attr.COMMENT_SYMBOL() == nil {- continue- }-- if attr.TextLiteral() != nil {- return mysqlparser.NormalizeMySQLTextLiteral(attr.TextLiteral())- }- }-- return ""-}--// extractTableComment extracts table comment-func (*metadataExtractor) extractTableComment(ctx *mysql.CreateTableContext, table *storepb.TableMetadata) {- if ctx == nil || ctx.CreateTableOptions() == nil {- return- }-- // Look for COMMENT option in the CREATE TABLE statement- for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {- if option == nil {- continue- }-- if option.COMMENT_SYMBOL() != nil && option.TextStringLiteral() != nil {- comment := mysqlparser.NormalizeMySQLTextStringLiteral(option.TextStringLiteral())- table.Comment = comment- break- }- }-}--// extractTableConstraint extracts table-level constraints-func (e *metadataExtractor) extractTableConstraint(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- if ctx == nil {- return- }-- // Extract PRIMARY KEY- if ctx.GetType_() != nil && ctx.GetType_().GetTokenType() == mysql.MySQLParserPRIMARY_SYMBOL {- e.extractPrimaryKey(ctx, table)- }-- // Extract FOREIGN KEY- if ctx.FOREIGN_SYMBOL() != nil && ctx.KEY_SYMBOL() != nil {- e.extractForeignKey(ctx, table)- }-- // Extract INDEX/KEY- if (ctx.INDEX_SYMBOL() != nil || ctx.KEY_SYMBOL() != nil) && ctx.FOREIGN_SYMBOL() == nil && ctx.PRIMARY_SYMBOL() == nil {- e.extractIndex(ctx, table)- }-- // Extract UNIQUE constraint- if ctx.UNIQUE_SYMBOL() != nil {- e.extractUniqueIndex(ctx, table)- }-- // Extract FULLTEXT indexes- if ctx.FULLTEXT_SYMBOL() != nil {- e.extractFulltextIndex(ctx, table)- }-- // Extract SPATIAL indexes- if ctx.SPATIAL_SYMBOL() != nil {- e.extractSpatialIndex(ctx, table)- }-- // Extract CHECK constraint- if ctx.CheckConstraint() != nil {- // Extract constraint name if present- constraintName := ""- if ctx.ConstraintName() != nil {- constraintName = mysqlparser.NormalizeConstraintName(ctx.ConstraintName())- }- e.extractCheckConstraint(ctx.CheckConstraint(), table, constraintName)- }-}--// extractPrimaryKey extracts primary key constraint-func (*metadataExtractor) extractPrimaryKey(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- if ctx.KeyListVariants() == nil {- return- }-- columnInfo := extractIndexColumnsFromVariants(ctx.KeyListVariants())-- if len(columnInfo) > 0 {- // Extract expressions and flags- expressions := make([]string, len(columnInfo))- keyLength := make([]int64, len(columnInfo))- descending := make([]bool, len(columnInfo))-- for i, col := range columnInfo {- expressions[i] = col.Expression- keyLength[i] = col.KeyLength- descending[i] = col.Descending- }-- // Mark primary key columns as NOT NULL- for _, column := range table.Columns {- for _, pkExpr := range expressions {- if column.Name == pkExpr {- column.Nullable = false- break- }- }- }-- index := &storepb.IndexMetadata{- Name: "PRIMARY",- Type: "BTREE",- Expressions: expressions,- Primary: true,- Unique: true,- Visible: true,- Comment: "", // Primary keys don't have comments in MySQL- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}--// extractForeignKey extracts foreign key constraint-func (e *metadataExtractor) extractForeignKey(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- if ctx.KeyList() == nil || ctx.References() == nil {- return- }-- // Extract constraint name- constraintName := ""- if ctx.ConstraintName() != nil {- constraintName = mysqlparser.NormalizeConstraintName(ctx.ConstraintName())- }-- // Extract columns- var columns []string- for _, keyPart := range ctx.KeyList().AllKeyPart() {- if keyPart.Identifier() != nil {- columns = append(columns, mysqlparser.NormalizeMySQLIdentifier(keyPart.Identifier()))- }- }-- // Extract referenced table and columns- references := ctx.References()- if references.TableRef() == nil {- return- }-- _, referencedTable := mysqlparser.NormalizeMySQLTableRef(references.TableRef())-- var referencedColumns []string- if references.IdentifierListWithParentheses() != nil {- referencedColumns = mysqlparser.NormalizeIdentifierListWithParentheses(references.IdentifierListWithParentheses())- }-- // Extract ON DELETE and ON UPDATE actions- // MySQL defaults to "NO ACTION" if not specified- onDelete := "NO ACTION"- onUpdate := "NO ACTION"-- // Parse the full text of the references clause to find ON DELETE/UPDATE actions- if references != nil {- refText := references.GetText()- refTextUpper := strings.ToUpper(refText)-- // Extract ON DELETE action- onDeleteIdx := strings.Index(refTextUpper, "ONDELETE")- if onDeleteIdx >= 0 {- remaining := refTextUpper[onDeleteIdx+8:] // Skip "ONDELETE"- if strings.HasPrefix(remaining, "CASCADE") {- onDelete = "CASCADE"- } else if strings.HasPrefix(remaining, "SETNULL") {- onDelete = "SET NULL"- } else if strings.HasPrefix(remaining, "RESTRICT") {- onDelete = "RESTRICT"- } else if strings.HasPrefix(remaining, "NOACTION") {- onDelete = "NO ACTION"- }- }-- // Extract ON UPDATE action- onUpdateIdx := strings.Index(refTextUpper, "ONUPDATE")- if onUpdateIdx >= 0 {- remaining := refTextUpper[onUpdateIdx+8:] // Skip "ONUPDATE"- if strings.HasPrefix(remaining, "CASCADE") {- onUpdate = "CASCADE"- } else if strings.HasPrefix(remaining, "SETNULL") {- onUpdate = "SET NULL"- } else if strings.HasPrefix(remaining, "RESTRICT") {- onUpdate = "RESTRICT"- } else if strings.HasPrefix(remaining, "NOACTION") {- onUpdate = "NO ACTION"- }- }- }-- fk := &storepb.ForeignKeyMetadata{- Name: constraintName,- Columns: columns,- ReferencedTable: referencedTable,- ReferencedColumns: referencedColumns,- OnDelete: onDelete,- OnUpdate: onUpdate,- }-- table.ForeignKeys = append(table.ForeignKeys, fk)-- // Store the foreign key for later index creation (after all table elements are processed)- if e.pendingForeignKeyIndexes[table.Name] == nil {- e.pendingForeignKeyIndexes[table.Name] = []*storepb.ForeignKeyMetadata{}- }- e.pendingForeignKeyIndexes[table.Name] = append(e.pendingForeignKeyIndexes[table.Name], fk)-}--// extractIndex extracts regular index-func (e *metadataExtractor) extractIndex(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- // Extract index name- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }-- // Fallback: try to get name from IndexNameAndType if IndexName is empty- if indexName == "" && ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())- }-- // Extract columns/expressions with detailed information- var columnInfo []IndexColumnInfo-- // First try KeyList (for simple KEY definitions)- if ctx.KeyList() != nil {- columnInfo = extractIndexColumns(ctx.KeyList())- }-- // If KeyList is empty, try KeyListVariants (for INDEX definitions)- if len(columnInfo) == 0 && ctx.KeyListVariants() != nil {- columnInfo = extractIndexColumnsFromVariants(ctx.KeyListVariants())- }-- if len(columnInfo) > 0 {- // Extract expressions and flags- expressions := make([]string, len(columnInfo))- keyLength := make([]int64, len(columnInfo))- descending := make([]bool, len(columnInfo))-- for i, col := range columnInfo {- expressions[i] = col.Expression- keyLength[i] = col.KeyLength- descending[i] = col.Descending- }-- indexType := e.getIndexType(ctx)-- // Check for visibility - MySQL indexes are visible by default unless marked INVISIBLE- visible := !detectInvisibleKeyword(ctx)-- // Extract comment- comment := extractIndexComment(ctx)-- index := &storepb.IndexMetadata{- Name: indexName,- Type: indexType,- Expressions: expressions,- Primary: false,- Unique: false,- Visible: visible,- Comment: comment,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}--// extractUniqueIndex extracts unique index-func (*metadataExtractor) extractUniqueIndex(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- // Extract index name- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }-- // Fallback: try to get name from IndexNameAndType if IndexName is empty- if indexName == "" && ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())- }-- // Extract columns/expressions with detailed information- var columnInfo []IndexColumnInfo-- // First try KeyList (for simple KEY definitions)- if ctx.KeyList() != nil {- columnInfo = extractIndexColumns(ctx.KeyList())- }-- // If KeyList is empty, try KeyListVariants (for UNIQUE INDEX definitions)- if len(columnInfo) == 0 && ctx.KeyListVariants() != nil {- columnInfo = extractIndexColumnsFromVariants(ctx.KeyListVariants())- }-- if len(columnInfo) > 0 {- // Extract expressions and flags- expressions := make([]string, len(columnInfo))- keyLength := make([]int64, len(columnInfo))- descending := make([]bool, len(columnInfo))-- for i, col := range columnInfo {- expressions[i] = col.Expression- keyLength[i] = col.KeyLength- descending[i] = col.Descending- }-- // Check for visibility - MySQL indexes are visible by default unless marked INVISIBLE- visible := !detectInvisibleKeyword(ctx)-- // Extract comment- comment := extractIndexComment(ctx)-- index := &storepb.IndexMetadata{- Name: indexName,- Type: "BTREE",- Expressions: expressions,- Primary: false,- Unique: true,- Visible: visible,- Comment: comment,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}--// extractCheckConstraint extracts check constraint-func (*metadataExtractor) extractCheckConstraint(ctx mysql.ICheckConstraintContext, table *storepb.TableMetadata, constraintName string) {- if ctx == nil || ctx.ExprWithParentheses() == nil {- return- }-- // Extract expression with proper spacing- expression := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.ExprWithParentheses())-- // If no constraint name provided, generate one like MySQL does- if constraintName == "" {- constraintName = fmt.Sprintf("%s_chk_%d", table.Name, len(table.CheckConstraints)+1)- }-- check := &storepb.CheckConstraintMetadata{- Name: constraintName,- Expression: expression,- }-- table.CheckConstraints = append(table.CheckConstraints, check)-}--// extractPartitions extracts partition information-func (*metadataExtractor) extractPartitions(ctx mysql.IPartitionClauseContext, table *storepb.TableMetadata) {- if ctx == nil || ctx.PartitionTypeDef() == nil {- return- }-- // Extract partition expression based on partition type- partitionExpr := ""- partitionType := storepb.TablePartitionMetadata_TYPE_UNSPECIFIED-- // Determine partition type and extract expression- typeDef := ctx.PartitionTypeDef()- switch def := typeDef.(type) {- case *mysql.PartitionDefKeyContext:- // PARTITION BY KEY or LINEAR KEY- if def.LINEAR_SYMBOL() != nil {- partitionType = storepb.TablePartitionMetadata_LINEAR_KEY- } else {- partitionType = storepb.TablePartitionMetadata_KEY- }- if def.IdentifierList() != nil {- columns := mysqlparser.NormalizeMySQLIdentifierList(def.IdentifierList())- partitionExpr = strings.Join(columns, ",")- }- case *mysql.PartitionDefHashContext:- // PARTITION BY HASH or LINEAR HASH- if def.LINEAR_SYMBOL() != nil {- partitionType = storepb.TablePartitionMetadata_LINEAR_HASH- } else {- partitionType = storepb.TablePartitionMetadata_HASH- }- if def.BitExpr() != nil {- partitionExpr = def.BitExpr().GetText()- }- case *mysql.PartitionDefRangeListContext:- // PARTITION BY RANGE/LIST [COLUMNS]- if def.RANGE_SYMBOL() != nil {- if def.COLUMNS_SYMBOL() != nil {- partitionType = storepb.TablePartitionMetadata_RANGE_COLUMNS- } else {- partitionType = storepb.TablePartitionMetadata_RANGE- }- } else if def.LIST_SYMBOL() != nil {- if def.COLUMNS_SYMBOL() != nil {- partitionType = storepb.TablePartitionMetadata_LIST_COLUMNS- } else {- partitionType = storepb.TablePartitionMetadata_LIST- }- }-- // Extract expression- if def.COLUMNS_SYMBOL() != nil && def.IdentifierList() != nil {- columns := mysqlparser.NormalizeMySQLIdentifierList(def.IdentifierList())- partitionExpr = strings.Join(columns, ",")- } else if def.BitExpr() != nil {- partitionExpr = def.BitExpr().GetText()- }- default:- }-- // For KEY partitions without explicit columns, MySQL uses all primary key columns- // We'll set the expression to empty string in this case-- // Extract default partition count for KEY/HASH partitions- useDefault := ""- if ctx.Real_ulong_number() != nil {- // This indicates PARTITIONS n syntax- useDefault = ctx.Real_ulong_number().GetText()- }-- // Extract partition definitions- if ctx.PartitionDefinitions() != nil {- for _, partDef := range ctx.PartitionDefinitions().AllPartitionDefinition() {- if partDef == nil || partDef.Identifier() == nil {- continue- }-- partition := &storepb.TablePartitionMetadata{- Name: mysqlparser.NormalizeMySQLIdentifier(partDef.Identifier()),- Type: partitionType,- Expression: partitionExpr,- UseDefault: useDefault,- }-- // Extract partition value (for RANGE/LIST partitions)- if partDef.PartitionValueItemListParen() != nil {- // This contains VALUES LESS THAN or VALUES IN- partition.Value = partDef.PartitionValueItemListParen().GetText()- }-- table.Partitions = append(table.Partitions, partition)- }- } else if useDefault != "" {- // For KEY/HASH partitions with only PARTITIONS n syntax- // MySQL creates default partitions named p0, p1, p2, etc.- // Parse the partition count and create the appropriate number of partitions- if partitionCount, err := strconv.Atoi(useDefault); err == nil {- for i := 0; i < partitionCount; i++ {- partition := &storepb.TablePartitionMetadata{- Name: fmt.Sprintf("p%d", i),- Type: partitionType,- Expression: partitionExpr,- UseDefault: useDefault,- }- table.Partitions = append(table.Partitions, partition)- }- }- }-}--// EnterCreateView is called when entering a CREATE VIEW statement-func (e *metadataExtractor) EnterCreateView(ctx *mysql.CreateViewContext) {- if e.err != nil {- return- }-- if ctx.ViewName() == nil {- return- }-- _, viewName := mysqlparser.NormalizeMySQLViewName(ctx.ViewName())-- view := &storepb.ViewMetadata{- Name: viewName,- }-- // Extract view definition- // For MySQL, the view definition is the entire CREATE VIEW statement- if ctx.GetStart() != nil && ctx.GetStop() != nil {- startIndex := ctx.GetStart().GetTokenIndex()- stopIndex := ctx.GetStop().GetTokenIndex()- tokens := ctx.GetParser().GetTokenStream()-- var definitionParts []string- for i := startIndex; i <= stopIndex; i++ {- token := tokens.Get(i)- if token != nil {- definitionParts = append(definitionParts, token.GetText())- }- }- view.Definition = strings.Join(definitionParts, " ")- }-- e.views[viewName] = view-}--// EnterCreateFunction is called when entering a CREATE FUNCTION statement-func (e *metadataExtractor) EnterCreateFunction(ctx *mysql.CreateFunctionContext) {- if e.err != nil {- return- }-- if ctx.FunctionName() == nil {- return- }-- _, functionName := mysqlparser.NormalizeMySQLFunctionName(ctx.FunctionName())-- function := &storepb.FunctionMetadata{- Name: functionName,- }-- // Extract function definition- // Get the full text from the start of CREATE to the end of the statement- if ctx.GetStart() != nil && ctx.GetStop() != nil {- startIndex := ctx.GetStart().GetTokenIndex()- stopIndex := ctx.GetStop().GetTokenIndex()- tokens := ctx.GetParser().GetTokenStream()-- var definitionParts []string- for i := startIndex; i <= stopIndex; i++ {- token := tokens.Get(i)- if token != nil {- definitionParts = append(definitionParts, token.GetText())- }- }- function.Definition = strings.Join(definitionParts, " ")- }-- e.functions[functionName] = function-}--// EnterCreateProcedure is called when entering a CREATE PROCEDURE statement-func (e *metadataExtractor) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {- if e.err != nil {- return- }-- if ctx.ProcedureName() == nil {- return- }-- _, procedureName := mysqlparser.NormalizeMySQLProcedureName(ctx.ProcedureName())-- procedure := &storepb.ProcedureMetadata{- Name: procedureName,- }-- // Extract procedure definition- // Get the full text from the start of CREATE to the end of the statement- if ctx.GetStart() != nil && ctx.GetStop() != nil {- startIndex := ctx.GetStart().GetTokenIndex()- stopIndex := ctx.GetStop().GetTokenIndex()- tokens := ctx.GetParser().GetTokenStream()-- var definitionParts []string- for i := startIndex; i <= stopIndex; i++ {- token := tokens.Get(i)- if token != nil {- definitionParts = append(definitionParts, token.GetText())- }- }- procedure.Definition = strings.Join(definitionParts, " ")- }-- e.procedures[procedureName] = procedure-}--// EnterCreateTrigger is called when entering a CREATE TRIGGER statement-func (e *metadataExtractor) EnterCreateTrigger(ctx *mysql.CreateTriggerContext) {- if e.err != nil {- return- }-- if ctx.TriggerName() == nil || ctx.TableRef() == nil {- return- }-- _, triggerName := mysqlparser.NormalizeMySQLTriggerName(ctx.TriggerName())- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())-- trigger := &storepb.TriggerMetadata{- Name: triggerName,- }-- // Add trigger to the appropriate table- table := e.getOrCreateTable(tableName)- table.Triggers = append(table.Triggers, trigger)-}--// EnterAlterTable is called when entering an ALTER TABLE statement-func (e *metadataExtractor) EnterAlterTable(ctx *mysql.AlterTableContext) {- if e.err != nil {- return- }-- if ctx.TableRef() == nil {- return- }-- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())- table := e.getOrCreateTable(tableName)-- // Extract alter specifications- if ctx.AlterTableActions() != nil && ctx.AlterTableActions().AlterCommandList() != nil {- if ctx.AlterTableActions().AlterCommandList().AlterList() != nil {- for _, alterCmd := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {- if alterCmd == nil {- continue- }-- // Handle ADD COLUMN- if alterCmd.ADD_SYMBOL() != nil && alterCmd.Identifier() != nil && alterCmd.FieldDefinition() != nil {- columnName := mysqlparser.NormalizeMySQLIdentifier(alterCmd.Identifier())- e.extractFieldDefinitionForAlter(columnName, alterCmd.FieldDefinition(), table)- }-- // Handle ADD INDEX- if alterCmd.ADD_SYMBOL() != nil && alterCmd.TableConstraintDef() != nil {- e.extractTableConstraint(alterCmd.TableConstraintDef(), table)- }- }- }- }-}--// extractFieldDefinitionForAlter extracts field definition from ALTER TABLE ADD COLUMN-func (e *metadataExtractor) extractFieldDefinitionForAlter(columnName string, ctx mysql.IFieldDefinitionContext, table *storepb.TableMetadata) {- if ctx == nil {- return- }-- // Check if column already exists- for _, col := range table.Columns {- if col.Name == columnName {- return- }- }-- column := &storepb.ColumnMetadata{- Name: columnName,- Position: int32(len(table.Columns) + 1),- }-- // Extract data type- if ctx.DataType() != nil {- column.Type = e.extractDataType(ctx.DataType())- }-- // Extract column attributes- e.extractFieldAttributes(ctx, column, table.Name)-- // Extract comment- if comment := e.extractColumnComment(ctx); comment != "" {- column.Comment = comment- }-- table.Columns = append(table.Columns, column)-}--// EnterCreateIndex is called when entering a CREATE INDEX statement-func (e *metadataExtractor) EnterCreateIndex(ctx *mysql.CreateIndexContext) {- if e.err != nil {- return- }-- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {- return- }-- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())- table := e.getOrCreateTable(tableName)-- // Extract index name- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }-- // Extract columns/expressions with detailed information- var columnInfo []IndexColumnInfo- if ctx.CreateIndexTarget().KeyListVariants() != nil {- columnInfo = extractIndexColumnsFromVariants(ctx.CreateIndexTarget().KeyListVariants())- }-- if len(columnInfo) > 0 {- // Extract expressions and flags- expressions := make([]string, len(columnInfo))- keyLength := make([]int64, len(columnInfo))- descending := make([]bool, len(columnInfo))-- for i, col := range columnInfo {- expressions[i] = col.Expression- keyLength[i] = col.KeyLength- descending[i] = col.Descending- }-- indexType := "BTREE"- // Check for index type- if ctx.FULLTEXT_SYMBOL() != nil {- indexType = "FULLTEXT"- } else if ctx.SPATIAL_SYMBOL() != nil {- indexType = "SPATIAL"- }-- // Check if it's a unique index- unique := ctx.UNIQUE_SYMBOL() != nil-- // Check for visibility - MySQL indexes are visible by default unless marked INVISIBLE- visible := !detectInvisibleKeyword(ctx)-- // Extract comment- comment := extractIndexComment(ctx)-- index := &storepb.IndexMetadata{- Name: indexName,- Type: indexType,- Expressions: expressions,- Primary: false,- Unique: unique,- Visible: visible,- Comment: comment,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}--// getIndexType extracts the index type from the table constraint definition-func (*metadataExtractor) getIndexType(_ mysql.ITableConstraintDefContext) string {- // Default to BTREE if no type specified- indexType := "BTREE"-- // Try to extract index type from various possible locations in the parse tree- // This depends on the specific MySQL parser implementation- // For now, we'll return the default BTREE- // TODO: Implement proper index type extraction when parser methods are available-- return indexType-}--// IndexColumnInfo holds information about a single index column-type IndexColumnInfo struct {- Expression string- KeyLength int64- Descending bool-}--// extractIndexColumns extracts detailed information about index columns including DESC/ASC and key lengths-func extractIndexColumns(keyList mysql.IKeyListContext) []IndexColumnInfo {- var columns []IndexColumnInfo-- if keyList == nil {- return columns- }-- for _, keyPart := range keyList.AllKeyPart() {- if keyPart.Identifier() == nil {- continue- }-- columnName := mysqlparser.NormalizeMySQLIdentifier(keyPart.Identifier())-- // Initialize column info- colInfo := IndexColumnInfo{- Expression: columnName,- KeyLength: -1, // Default: no key length specified- Descending: false, // Default: ASC- }-- // Check for DESC/ASC direction- if keyPart.Direction() != nil {- if keyPart.Direction().DESC_SYMBOL() != nil {- colInfo.Descending = true- }- // ASC is default, so we don't need to explicitly check for it- }-- // Try to extract key length from FieldLength- if keyPart.FieldLength() != nil {- lengthText := keyPart.FieldLength().GetText()- // Remove parentheses- if strings.HasPrefix(lengthText, "(") && strings.HasSuffix(lengthText, ")") {- lengthText = lengthText[1 : len(lengthText)-1]- }- if length, err := strconv.ParseInt(lengthText, 10, 64); err == nil {- colInfo.KeyLength = length- }- }-- columns = append(columns, colInfo)- }-- return columns-}--// extractIndexColumnsFromVariants extracts detailed information from KeyListVariants-func extractIndexColumnsFromVariants(keyListVariants mysql.IKeyListVariantsContext) []IndexColumnInfo {- if keyListVariants == nil {- return nil- }-- // First try KeyList- if keyListVariants.KeyList() != nil {- return extractIndexColumns(keyListVariants.KeyList())- }-- // Then try KeyListWithExpression- if keyListVariants.KeyListWithExpression() != nil {- return extractIndexColumnsFromKeyListWithExpression(keyListVariants.KeyListWithExpression())- }-- return nil-}--// detectInvisibleKeyword tries to detect INVISIBLE keyword in the given parser context-func detectInvisibleKeyword(ctx interface {- GetStart() antlr.Token- GetStop() antlr.Token- GetParser() antlr.Parser-}) bool {- if ctx.GetStart() == nil || ctx.GetStop() == nil {- return false- }-- tokens := ctx.GetParser().GetTokenStream()- contextText := ""- for i := ctx.GetStart().GetTokenIndex(); i <= ctx.GetStop().GetTokenIndex(); i++ {- token := tokens.Get(i)- if token != nil {- contextText += token.GetText() + " "- }- }- contextTextUpper := strings.ToUpper(contextText)- return strings.Contains(contextTextUpper, "INVISIBLE")-}--// extractIndexComment tries to extract COMMENT from the given parser context-func extractIndexComment(ctx interface {- GetStart() antlr.Token- GetStop() antlr.Token- GetParser() antlr.Parser-}) string {- if ctx.GetStart() == nil || ctx.GetStop() == nil {- return ""- }-- tokens := ctx.GetParser().GetTokenStream()- contextText := ""- for i := ctx.GetStart().GetTokenIndex(); i <= ctx.GetStop().GetTokenIndex(); i++ {- token := tokens.Get(i)- if token != nil {- contextText += token.GetText() + " "- }- }- contextTextUpper := strings.ToUpper(contextText)-- // Look for COMMENT 'text' pattern- commentIdx := strings.Index(contextTextUpper, "COMMENT")- if commentIdx == -1 {- return ""- }-- // Find the start of the comment string (after COMMENT keyword)- commentStart := commentIdx + 7 // len("COMMENT")- remaining := contextText[commentStart:]-- // Skip whitespace- remaining = strings.TrimLeft(remaining, " \t\n\r")-- // Extract quoted string- if len(remaining) >= 2 && (remaining[0] == '\'' || remaining[0] == '"') {- quote := remaining[0]- endIdx := strings.IndexByte(remaining[1:], byte(quote))- if endIdx != -1 {- return remaining[1 : endIdx+1]- }- }-- return ""-}--// extractIndexColumnsFromKeyListWithExpression handles KeyListWithExpression-func extractIndexColumnsFromKeyListWithExpression(keyListWithExpr mysql.IKeyListWithExpressionContext) []IndexColumnInfo {- var columns []IndexColumnInfo-- if keyListWithExpr == nil {- return columns- }-- for _, expression := range keyListWithExpr.AllKeyPartOrExpression() {- colInfo := IndexColumnInfo{- KeyLength: -1, // Default: no key length specified- Descending: false, // Default: ASC- }-- if expression.KeyPart() != nil {- // Regular column reference- keyPart := expression.KeyPart()- if keyPart.Identifier() != nil {- colInfo.Expression = mysqlparser.NormalizeMySQLIdentifier(keyPart.Identifier())-- // Check for DESC/ASC direction- if keyPart.Direction() != nil {- if keyPart.Direction().DESC_SYMBOL() != nil {- colInfo.Descending = true- }- }-- // Try to extract key length from FieldLength- if keyPart.FieldLength() != nil {- lengthText := keyPart.FieldLength().GetText()- // Remove parentheses- if strings.HasPrefix(lengthText, "(") && strings.HasSuffix(lengthText, ")") {- lengthText = lengthText[1 : len(lengthText)-1]- }- if length, err := strconv.ParseInt(lengthText, 10, 64); err == nil {- colInfo.KeyLength = length- }- }- }- } else if expression.ExprWithParentheses() != nil {- // Expression like (YEAR(created_at))- colInfo.Expression = keyListWithExpr.GetParser().GetTokenStream().GetTextFromRuleContext(expression.ExprWithParentheses().Expr())- // Expressions don't support DESC, so keep default false- }-- columns = append(columns, colInfo)- }-- return columns-}--// normalizeDefaultValue normalizes default values to match MySQL's internal representation-func normalizeDefaultValue(defaultValue string) string {- if defaultValue == "" {- return ""- }-- // Handle boolean values - MySQL stores them as 0/1- switch strings.ToLower(defaultValue) {- case "true":- return "1"- case "false":- return "0"- default:- // Return as-is for non-boolean values- }-- return defaultValue-}--// isLiteralValue determines if a default value is a literal that needs quotes-func isLiteralValue(value string) bool {- if value == "" {- return false- }-- upper := strings.ToUpper(value)-- // These are keywords/functions that don't need quotes- if strings.HasPrefix(upper, "CURRENT_TIMESTAMP") ||- strings.HasPrefix(upper, "CURRENT_DATE") ||- strings.HasPrefix(upper, "NOW") ||- upper == "NULL" ||- upper == "AUTO_INCREMENT" {- return false- }-- // Check if it's a numeric value (don't quote numbers)- if _, err := strconv.ParseFloat(value, 64); err == nil {- return true // Numbers are literals but need quotes for consistency with sync- }-- // Everything else (strings, etc.) is a literal- return true-}--// addColumnPrimaryKey tracks a column-level primary key definition-func (e *metadataExtractor) addColumnPrimaryKey(tableName, columnName string) {- if e.columnPrimaryKeys[tableName] == nil {- e.columnPrimaryKeys[tableName] = []string{}- }- e.columnPrimaryKeys[tableName] = append(e.columnPrimaryKeys[tableName], columnName)-}--// addColumnUniqueKey tracks a column-level unique constraint definition-func (e *metadataExtractor) addColumnUniqueKey(tableName, columnName string) {- if e.columnUniqueKeys[tableName] == nil {- e.columnUniqueKeys[tableName] = make(map[string]bool)- }- e.columnUniqueKeys[tableName][columnName] = true-}--// createColumnPrimaryKeyIndexes creates index metadata for column-level primary key definitions-func (e *metadataExtractor) createColumnPrimaryKeyIndexes() {- for tableName, columnNames := range e.columnPrimaryKeys {- if len(columnNames) == 0 {- continue- }-- table, exists := e.tables[tableName]- if !exists {- continue- }-- // Check if a PRIMARY key index already exists (from table-level constraint)- hasPrimaryIndex := false- for _, index := range table.Indexes {- if index.Primary {- hasPrimaryIndex = true- break- }- }-- // Only create primary key index if one doesn't already exist- if !hasPrimaryIndex {- // Create default key lengths and descending flags- keyLength := make([]int64, len(columnNames))- descending := make([]bool, len(columnNames))- for i := range columnNames {- keyLength[i] = -1 // -1 indicates unspecified key length- descending[i] = false // Primary keys default to ASC- }-- index := &storepb.IndexMetadata{- Name: "PRIMARY",- Type: "BTREE",- Expressions: columnNames,- Primary: true,- Unique: true,- Visible: true,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }- }-}--// createColumnUniqueKeyIndexes creates index metadata for column-level unique constraint definitions-func (e *metadataExtractor) createColumnUniqueKeyIndexes() {- for tableName, columnMap := range e.columnUniqueKeys {- table, exists := e.tables[tableName]- if !exists {- continue- }-- // Create a unique index for each column with UNIQUE constraint- for columnName := range columnMap {- // Generate a unique index name (MySQL auto-generates these)- indexName := columnName-- // Check if an index with this name already exists- indexExists := false- for _, index := range table.Indexes {- if index.Name == indexName {- indexExists = true- break- }- }-- if !indexExists {- // Create default key lengths and descending flags- keyLength := []int64{-1} // Single column, unspecified length- descending := []bool{false} // Default to ASC-- index := &storepb.IndexMetadata{- Name: indexName,- Type: "BTREE",- Expressions: []string{columnName},- Primary: false,- Unique: true,- Visible: true,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }- }- }-}--// createPendingForeignKeyIndexes creates index metadata for foreign keys that don't have existing suitable indexes-func (e *metadataExtractor) createPendingForeignKeyIndexes() {- for tableName, foreignKeys := range e.pendingForeignKeyIndexes {- table, exists := e.tables[tableName]- if !exists {- continue- }-- for _, fk := range foreignKeys {- // MySQL automatically creates indexes on foreign key columns if they don't already exist.- // Check if an index already exists on these columns- hasMatchingIndex := false- for _, existingIndex := range table.Indexes {- // Check if an index already covers the foreign key columns- if len(existingIndex.Expressions) >= len(fk.Columns) {- matches := true- for i, col := range fk.Columns {- if i >= len(existingIndex.Expressions) || existingIndex.Expressions[i] != col {- matches = false- break- }- }- if matches {- hasMatchingIndex = true- break- }- }- }-- // If no matching index exists, create one- if !hasMatchingIndex {- // MySQL creates indexes using the constraint name for foreign keys- // If no constraint name is provided, MySQL would generate one automatically- indexName := fk.Name- if indexName == "" {- // Fallback to column names if no constraint name (shouldn't happen in practice)- indexName = fk.Columns[0]- if len(fk.Columns) > 1 {- indexName = strings.Join(fk.Columns, "_")- }- }-- // Create default key lengths and descending flags for FK index- keyLength := make([]int64, len(fk.Columns))- descending := make([]bool, len(fk.Columns))- for i := range fk.Columns {- keyLength[i] = -1 // -1 indicates unspecified key length- descending[i] = false // Default to ASC- }-- index := &storepb.IndexMetadata{- Name: indexName,- Type: "BTREE",- Expressions: fk.Columns,- Primary: false,- Unique: false,- Visible: true,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }- }- }-}--// extractFulltextIndex extracts FULLTEXT index-func (*metadataExtractor) extractFulltextIndex(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- // Extract index name- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }-- // Fallback: try to get name from IndexNameAndType if IndexName is empty- if indexName == "" && ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())- }-- // Extract columns/expressions - try both KeyList and KeyListVariants- var expressions []string-- // First try KeyList (for simple KEY definitions)- if ctx.KeyList() != nil {- for _, keyPart := range ctx.KeyList().AllKeyPart() {- if keyPart.Identifier() != nil {- columnName := mysqlparser.NormalizeMySQLIdentifier(keyPart.Identifier())- expressions = append(expressions, columnName)- }- }- }-- // If KeyList is empty, try KeyListVariants (for FULLTEXT INDEX definitions)- if len(expressions) == 0 && ctx.KeyListVariants() != nil {- expressions = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())- }-- if len(expressions) > 0 {- // Create default key lengths and descending flags- keyLength := make([]int64, len(expressions))- descending := make([]bool, len(expressions))- for i := range expressions {- keyLength[i] = -1 // -1 indicates unspecified key length- descending[i] = false // FULLTEXT indexes don't support DESC- }-- index := &storepb.IndexMetadata{- Name: indexName,- Type: "FULLTEXT",- Expressions: expressions,- Primary: false,- Unique: false,- Visible: true,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}--// extractSpatialIndex extracts SPATIAL index-func (*metadataExtractor) extractSpatialIndex(ctx mysql.ITableConstraintDefContext, table *storepb.TableMetadata) {- // Extract index name- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }-- // Fallback: try to get name from IndexNameAndType if IndexName is empty- if indexName == "" && ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())- }-- // Extract columns/expressions with detailed information- var columnInfo []IndexColumnInfo-- // First try KeyList (for simple KEY definitions)- if ctx.KeyList() != nil {- columnInfo = extractIndexColumns(ctx.KeyList())- }-- // If KeyList is empty, try KeyListVariants (for SPATIAL INDEX definitions)- if len(columnInfo) == 0 && ctx.KeyListVariants() != nil {- columnInfo = extractIndexColumnsFromVariants(ctx.KeyListVariants())- }-- if len(columnInfo) > 0 {- // Extract expressions and flags- expressions := make([]string, len(columnInfo))- keyLength := make([]int64, len(columnInfo))- descending := make([]bool, len(columnInfo))-- for i, col := range columnInfo {- expressions[i] = col.Expression- keyLength[i] = col.KeyLength- descending[i] = col.Descending-- // For SPATIAL indexes, MySQL uses default key length of 32 for GEOMETRY types- // if no explicit length is specified- if keyLength[i] == -1 {- // Find the column type to determine if we need default SPATIAL key length- for _, tableCol := range table.Columns {- if tableCol.Name == col.Expression {- colTypeUpper := strings.ToUpper(tableCol.Type)- if strings.Contains(colTypeUpper, "GEOMETRY") ||- strings.Contains(colTypeUpper, "POINT") ||- strings.Contains(colTypeUpper, "LINESTRING") ||- strings.Contains(colTypeUpper, "POLYGON") ||- strings.Contains(colTypeUpper, "MULTIPOINT") ||- strings.Contains(colTypeUpper, "MULTILINESTRING") ||- strings.Contains(colTypeUpper, "MULTIPOLYGON") ||- strings.Contains(colTypeUpper, "GEOMETRYCOLLECTION") {- keyLength[i] = 32 // MySQL default for spatial types- }- break- }- }- }- }-- // Check for visibility - MySQL indexes are visible by default unless marked INVISIBLE- visible := !detectInvisibleKeyword(ctx)-- // Extract comment- comment := extractIndexComment(ctx)-- index := &storepb.IndexMetadata{- Name: indexName,- Type: "SPATIAL",- Expressions: expressions,- Primary: false,- Unique: false,- Visible: visible,- Comment: comment,- KeyLength: keyLength,- Descending: descending,- }- table.Indexes = append(table.Indexes, index)- }-}diff --git a/backend/plugin/schema/mysql/walk_through.go b/backend/plugin/schema/mysql/walk_through.godeleted file mode 100644index f977458bc1..0000000000--- a/backend/plugin/schema/mysql/walk_through.go+++ /dev/null@@ -1,1880 +0,0 @@-package mysql--import (- "fmt"- "strings"-- "github.com/antlr4-go/antlr/v4"- "github.com/bytebase/parser/mysql"- tidbast "github.com/pingcap/tidb/pkg/parser/ast"- mysqldriver "github.com/pingcap/tidb/pkg/parser/mysql"- "github.com/pingcap/tidb/pkg/types"- "google.golang.org/protobuf/proto"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/advisor/code"- "github.com/bytebase/bytebase/backend/plugin/parser/base"- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"- "github.com/bytebase/bytebase/backend/plugin/parser/tidb"- "github.com/bytebase/bytebase/backend/store/model"-)--const (- // PrimaryKeyName is the string for PK.- PrimaryKeyName string = "PRIMARY"- // FullTextName is the string for FULLTEXT.- FullTextName string = "FULLTEXT"- // SpatialName is the string for SPATIAL.- SpatialName string = "SPATIAL"-)--// Old ANTLR-based WalkThrough registrations removed.-// WalkThroughOmni is now registered via RegisterWalkThroughWithContext in walk_through_omni.go.--// WalkThrough walks through MySQL AST and updates the database metadata.-func WalkThrough(d *model.DatabaseMetadata, ast []base.AST) *storepb.Advice {- // We define the Catalog as Database -> Schema -> Table. The Schema is only for PostgreSQL.- // So we use a Schema whose name is empty for other engines, such as MySQL.- // If there is no empty-string-name schema, create it to avoid corner cases.- if d.GetSchemaMetadata("") == nil {- d.CreateSchema("")- }-- // Extract ANTLRAST from AST- var nodeList []*base.ANTLRAST- for _, unifiedAST := range ast {- antlrAST, ok := base.GetANTLRAST(unifiedAST)- if !ok {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: "MySQL walk-through expects ANTLR-based parser result",- Content: "MySQL walk-through expects ANTLR-based parser result",- StartPosition: &storepb.Position{- Line: 0,- },- }- }- nodeList = append(nodeList, antlrAST)- }-- for _, node := range nodeList {- if advice := mysqlChangeState(d, node); advice != nil {- return advice- }- }-- return nil-}--type mysqlListener struct {- *mysql.BaseMySQLParserListener-- baseLine int- lineNumber int- text string- databaseMetadata *model.DatabaseMetadata- advice *storepb.Advice-}--func (l *mysqlListener) EnterQuery(ctx *mysql.QueryContext) {- l.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)- l.lineNumber = l.baseLine + ctx.GetStart().GetLine()-}--func mysqlChangeState(d *model.DatabaseMetadata, in *base.ANTLRAST) *storepb.Advice {- listener := &mysqlListener{- baseLine: base.GetLineOffset(in.StartPosition),- databaseMetadata: d,- }- antlr.ParseTreeWalkerDefault.Walk(listener, in.Tree)- if listener.advice != nil {- if listener.advice.StartPosition.Line == 0 {- listener.advice.StartPosition.Line = int32(listener.lineNumber)- }- return listener.advice- }- return nil-}--// EnterCreateTable is called when production createTable is entered.-func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.TableName() == nil {- return- }- databaseName, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())- if databaseName != "" && !isCurrentDatabase(l.databaseMetadata, databaseName) {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- schema := l.databaseMetadata.GetSchemaMetadata("")- if schema == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- return- }- if schema.GetTable(tableName) != nil {- if ctx.IfNotExists() != nil {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: fmt.Sprintf("Table `%s` already exists", tableName),- Content: fmt.Sprintf("Table `%s` already exists", tableName),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- if ctx.DuplicateAsQueryExpression() != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.StatementCreateTableAs.Int32(),- Title: fmt.Sprintf("CREATE TABLE AS statement is used in \"%s\"", l.text),- Content: fmt.Sprintf("CREATE TABLE AS statement is used in \"%s\"", l.text),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- if ctx.LIKE_SYMBOL() != nil {- _, referTable := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())- l.advice = mysqlCopyTable(l.databaseMetadata, databaseName, tableName, referTable)- return- }-- table, err := schema.CreateTable(tableName)- if err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- if ctx.TableElementList() == nil {- return- }-- hasAutoIncrement := false- for _, tableElement := range ctx.TableElementList().AllTableElement() {- switch {- // handle column- case tableElement.ColumnDefinition() != nil:- if tableElement.ColumnDefinition().FieldDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil {- continue- }- if mysqlparser.IsAutoIncrement(tableElement.ColumnDefinition().FieldDefinition()) {- if hasAutoIncrement {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.AutoIncrementExists.Int32(),- Title: fmt.Sprintf("There can be only one auto column for table `%s`", table.GetProto().Name),- Content: fmt.Sprintf("There can be only one auto column for table `%s`", table.GetProto().Name),- StartPosition: &storepb.Position{- Line: int32(l.baseLine + tableElement.GetStart().GetLine()),- },- }- }- hasAutoIncrement = true- }- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())- if advice := mysqlCreateColumn(table, columnName, tableElement.ColumnDefinition().FieldDefinition(), nil /* position */); advice != nil {- if advice.StartPosition.Line == 0 {- advice.StartPosition.Line = int32(l.baseLine + tableElement.GetStart().GetLine())- }- l.advice = advice- return- }- case tableElement.TableConstraintDef() != nil:- if advice := mysqlCreateConstraint(table, tableElement.TableConstraintDef()); advice != nil {- if advice.StartPosition.Line == 0 {- advice.StartPosition.Line = int32(tableElement.GetStart().GetLine())- }- l.advice = advice- return- }- default:- // Ignore other table element types- }- }-}--// EnterDropTable is called when production dropTable is entered.-func (l *mysqlListener) EnterDropTable(ctx *mysql.DropTableContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.TableRefList() == nil {- return- }-- for _, tableRef := range ctx.TableRefList().AllTableRef() {- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)- if databaseName != "" && !isCurrentDatabase(l.databaseMetadata, databaseName) {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- }-- schema := l.databaseMetadata.GetSchemaMetadata("")- if schema == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- return- }-- table := schema.GetTable(tableName)- if table == nil {- if ctx.IfExists() != nil {- return- }- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- // MySQL doesn't check view dependencies for DROP TABLE- if err := schema.DropTable(tableName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- }-}--// EnterAlterTable is called when production alterTable is entered.-func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.TableRef() == nil {- // todo: maybe need to do error handle.- return- }-- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())- table, advice := mysqlFindTableState(l.databaseMetadata, databaseName, tableName)- if advice != nil {- l.advice = advice- return- }-- if ctx.AlterTableActions() == nil {- return- }- if ctx.AlterTableActions().AlterCommandList() == nil {- return- }- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {- return- }-- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {- for _, op := range option.AllCreateTableOption() {- switch {- // engine.- case op.ENGINE_SYMBOL() != nil:- if op.EngineRef() == nil {- continue- }- engine := op.EngineRef().GetText()- table.GetProto().Engine = engine- // table comment.- case op.COMMENT_SYMBOL() != nil && op.TextStringLiteral() != nil:- comment := mysqlparser.NormalizeMySQLTextStringLiteral(op.TextStringLiteral())- table.GetProto().Comment = comment- // table collation.- case op.DefaultCollation() != nil && op.DefaultCollation().CollationName() != nil:- collation := mysqlparser.NormalizeMySQLCollationName(op.DefaultCollation().CollationName())- table.GetProto().Collation = collation- default:- }- }- }-- // alter table add column, change column, modify column.- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {- if item == nil {- continue- }-- switch {- case item.ADD_SYMBOL() != nil:- switch {- // add single column.- case item.Identifier() != nil && item.FieldDefinition() != nil:- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())- if advice := mysqlCreateColumn(table, columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); advice != nil {- l.advice = advice- return- }- // add multi columns.- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:- for _, tableElement := range item.TableElementList().AllTableElement() {- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {- continue- }- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())- if advice := mysqlCreateColumn(table, columnName, tableElement.ColumnDefinition().FieldDefinition(), nil); advice != nil {- l.advice = advice- return- }- }- // add constraint.- case item.TableConstraintDef() != nil:- if advice := mysqlCreateConstraint(table, item.TableConstraintDef()); advice != nil {- l.advice = advice- return- }- default:- // Ignore other ADD variations- }- // drop column or key.- case item.DROP_SYMBOL() != nil && item.ALTER_SYMBOL() == nil:- switch {- // drop foreign key.- // we do not deal with DROP FOREIGN KEY statements.- case item.FOREIGN_SYMBOL() != nil && item.KEY_SYMBOL() != nil:- // drop column.- case item.ColumnInternalRef() != nil:- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())- // Validate column exists- if table.GetColumn(columnName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- return- }- if err := table.DropColumn(columnName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("failed to drop column: %v", err),- Content: fmt.Sprintf("failed to drop column: %v", err),- StartPosition: &storepb.Position{Line: 0},- }- return- }- // drop primary key.- case item.PRIMARY_SYMBOL() != nil && item.KEY_SYMBOL() != nil:- if err := table.DropIndex(PrimaryKeyName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- // drop key/index.- case item.KeyOrIndex() != nil && item.IndexRef() != nil:- _, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())- if err := table.DropIndex(indexName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- default:- // Ignore other DROP variations- }- // modify column.- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil:- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())- if advice := mysqlChangeColumn(table, columnName, columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); advice != nil {- l.advice = advice- return- }- // change column- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())- newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())- if advice := mysqlChangeColumn(table, oldColumnName, newColumnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); advice != nil {- l.advice = advice- return- }- // rename column- case item.RENAME_SYMBOL() != nil && item.COLUMN_SYMBOL() != nil:- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())- newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())- // Validate old column exists- if table.GetColumn(oldColumnName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldColumnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldColumnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- return- }- // Validate new column doesn't already exist- if table.GetColumn(newColumnName) != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("Column `%s` already exists in table `%s`", newColumnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` already exists in table `%s`", newColumnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- return- }- if err := table.RenameColumn(oldColumnName, newColumnName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("failed to rename column: %v", err),- Content: fmt.Sprintf("failed to rename column: %v", err),- StartPosition: &storepb.Position{Line: 0},- }- return- }- case item.ALTER_SYMBOL() != nil:- switch {- // alter column.- case item.ColumnInternalRef() != nil:- if advice := mysqlAlterColumn(table, item); advice != nil {- l.advice = advice- return- }- // alter index visibility.- case item.INDEX_SYMBOL() != nil && item.IndexRef() != nil && item.Visibility() != nil:- _, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())- if advice := mysqlChangeIndexVisibility(table, indexName, item.Visibility()); advice != nil {- l.advice = advice- return- }- default:- }- // rename table.- case item.RENAME_SYMBOL() != nil && item.TableName() != nil:- _, newTableName := mysqlparser.NormalizeMySQLTableName(item.TableName())- schema := l.databaseMetadata.GetSchemaMetadata("")- if schema == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- return- }- if err := schema.RenameTable(table.GetProto().Name, newTableName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- // rename index.- case item.RENAME_SYMBOL() != nil && item.KeyOrIndex() != nil && item.IndexRef() != nil && item.IndexName() != nil:- _, _, oldIndexName := mysqlparser.NormalizeIndexRef(item.IndexRef())- newIndexName := mysqlparser.NormalizeIndexName(item.IndexName())- if err := table.RenameIndex(oldIndexName, newIndexName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- default:- // Ignore other alter table actions- }- }-}--// EnterDropIndex is called when production dropIndex is entered.-func (l *mysqlListener) EnterDropIndex(ctx *mysql.DropIndexContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.TableRef() == nil {- return- }- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())- table, advice := mysqlFindTableState(l.databaseMetadata, databaseName, tableName)- if advice != nil {- l.advice = advice- return- }-- if ctx.IndexRef() == nil {- return- }-- _, _, indexName := mysqlparser.NormalizeIndexRef(ctx.IndexRef())- if err := table.DropIndex(indexName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }-}--func (l *mysqlListener) EnterCreateIndex(ctx *mysql.CreateIndexContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {- return- }- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())- table, advice := mysqlFindTableState(l.databaseMetadata, databaseName, tableName)- if advice != nil {- l.advice = advice- return- }-- unique := false- isSpatial := false- tp := "BTREE"-- if ctx.GetType_() == nil {- return- }- switch ctx.GetType_().GetTokenType() {- case mysql.MySQLParserFULLTEXT_SYMBOL:- tp = FullTextName- case mysql.MySQLParserSPATIAL_SYMBOL:- isSpatial = true- tp = SpatialName- case mysql.MySQLParserINDEX_SYMBOL:- default:- // Other index types- }- if ctx.UNIQUE_SYMBOL() != nil {- unique = true- }-- indexName := ""- if ctx.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())- }- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())- }-- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {- return- }- if advice := mysqlValidateKeyListVariants(table, ctx.CreateIndexTarget().KeyListVariants(), false /* primary */, isSpatial); advice != nil {- l.advice = advice- return- }-- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())- if advice := mysqlCreateIndex(table, indexName, columnList, unique, tp, mysql.NewEmptyTableConstraintDefContext(), ctx); advice != nil {- l.advice = advice- return- }-}--// EnterAlterDatabase is called when production alterDatabase is entered.-func (l *mysqlListener) EnterAlterDatabase(ctx *mysql.AlterDatabaseContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.SchemaRef() != nil {- databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())- if !isCurrentDatabase(l.databaseMetadata, databaseName) {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- return- }- }-- for _, option := range ctx.AllAlterDatabaseOption() {- if option.CreateDatabaseOption() == nil {- continue- }-- switch {- case option.CreateDatabaseOption().DefaultCharset() != nil && option.CreateDatabaseOption().DefaultCharset().CharsetName() != nil:- charset := mysqlparser.NormalizeMySQLCharsetName(option.CreateDatabaseOption().DefaultCharset().CharsetName())- l.databaseMetadata.GetProto().CharacterSet = charset- case option.CreateDatabaseOption().DefaultCollation() != nil && option.CreateDatabaseOption().DefaultCollation().CollationName() != nil:- collation := mysqlparser.NormalizeMySQLCollationName(option.CreateDatabaseOption().DefaultCollation().CollationName())- l.databaseMetadata.GetProto().Collation = collation- default:- // Other options- }- }-}--// EnterDropDatabase is called when production dropDatabase is entered.-func (l *mysqlListener) EnterDropDatabase(ctx *mysql.DropDatabaseContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.SchemaRef() == nil {- return- }-- databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())- if !isCurrentDatabase(l.databaseMetadata, databaseName) {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- return- }-- // DROP DATABASE not supported - would mark database as deleted-}--// EnterCreateDatabase is called when production createDatabase is entered.-func (l *mysqlListener) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.SchemaName() == nil {- return- }- databaseName := mysqlparser.NormalizeMySQLSchemaName(ctx.SchemaName())- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }-}--// EnterRenameTableStatement is called when production renameTableStatement is entered.-func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatementContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- for _, pair := range ctx.AllRenamePair() {- schema := l.databaseMetadata.GetSchemaMetadata("")- if schema == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- return- }-- _, oldTableName := mysqlparser.NormalizeMySQLTableRef(pair.TableRef())- _, newTableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())-- if mysqlTheCurrentDatabase(l.databaseMetadata, pair) {- if compareIdentifier(oldTableName, newTableName, !l.databaseMetadata.GetIsObjectCaseSensitive()) {- return- }- if schema.GetTable(oldTableName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", oldTableName),- Content: fmt.Sprintf("Table `%s` does not exist", oldTableName),- StartPosition: &storepb.Position{Line: 0},- }- return- }- if schema.GetTable(newTableName) != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: fmt.Sprintf("Table `%s` already exists", newTableName),- Content: fmt.Sprintf("Table `%s` already exists", newTableName),- StartPosition: &storepb.Position{Line: 0},- }- return- }- if err := schema.RenameTable(oldTableName, newTableName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- } else if mysqlMoveToOtherDatabase(l.databaseMetadata, pair) {- if schema.GetTable(oldTableName) == nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", oldTableName),- Content: fmt.Sprintf("Table `%s` does not exist", oldTableName),- StartPosition: &storepb.Position{Line: 0},- }- return- }- if err := schema.DropTable(oldTableName); err != nil {- l.advice = &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- return- }- } else {- l.advice = &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", mysqlTargetDatabase(l.databaseMetadata, pair), l.databaseMetadata.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", mysqlTargetDatabase(l.databaseMetadata, pair), l.databaseMetadata.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- return- }- }-}--func (l *mysqlListener) EnterCreateTrigger(ctx *mysql.CreateTriggerContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.TriggerName() == nil {- return- }-- // Check if related table exists.- if ctx.TableRef() == nil {- return- }- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())- _, advice := mysqlFindTableState(l.databaseMetadata, databaseName, tableName)- if advice != nil {- l.advice = advice- return- }-}--func (*mysqlListener) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.ProcedureName() == nil {- return- }- // Skip other checks for now.-}--func (*mysqlListener) EnterCreateEvent(ctx *mysql.CreateEventContext) {- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {- return- }- if ctx.EventName() == nil {- return- }- // Skip other checks for now.-}--func mysqlTargetDatabase(d *model.DatabaseMetadata, renamePair mysql.IRenamePairContext) string {- oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())- if oldDatabaseName != "" && !isCurrentDatabase(d, oldDatabaseName) {- return oldDatabaseName- }- newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())- return newDatabaseName-}--func mysqlMoveToOtherDatabase(d *model.DatabaseMetadata, renamePair mysql.IRenamePairContext) bool {- oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())- if oldDatabaseName != "" && !isCurrentDatabase(d, oldDatabaseName) {- return false- }- newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())- return oldDatabaseName != newDatabaseName-}--func mysqlTheCurrentDatabase(d *model.DatabaseMetadata, renamePair mysql.IRenamePairContext) bool {- newDatabaseName, _ := mysqlparser.NormalizeMySQLTableName(renamePair.TableName())- if newDatabaseName != "" && !isCurrentDatabase(d, newDatabaseName) {- return false- }- oldDatabaseName, _ := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())- if oldDatabaseName != "" && !isCurrentDatabase(d, oldDatabaseName) {- return false- }- return true-}--func mysqlChangeIndexVisibility(table *model.TableMetadata, indexName string, visibility mysql.IVisibilityContext) *storepb.Advice {- index := table.GetIndex(indexName)- if index == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexNotExists.Int32(),- Title: fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, table.GetProto().Name),- Content: fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- indexProto := index.GetProto()- switch {- case visibility.VISIBLE_SYMBOL() != nil:- indexProto.Visible = true- case visibility.INVISIBLE_SYMBOL() != nil:- indexProto.Visible = false- default:- // No visibility specified- }- return nil-}--func mysqlAlterColumn(table *model.TableMetadata, itemDef mysql.IAlterListItemContext) *storepb.Advice {- if itemDef.ColumnInternalRef() == nil {- // should not reach here.- return nil- }- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(itemDef.ColumnInternalRef())- col := table.GetColumn(columnName)- if col == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }-- colProto := col.GetProto()- switch {- case itemDef.SET_SYMBOL() != nil:- switch {- // SET DEFAULT.- case itemDef.DEFAULT_SYMBOL() != nil:- if itemDef.SignedLiteral() != nil && itemDef.SignedLiteral().Literal() != nil && itemDef.SignedLiteral().Literal().NullLiteral() == nil {- if colProto.Type != "" {- switch strings.ToLower(colProto.Type) {- case "blob", "tinyblob", "mediumblob", "longblob",- "text", "tinytext", "mediumtext", "longtext",- "json",- "geometry":- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.InvalidColumnDefault.Int32(),- Title: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- StartPosition: &storepb.Position{Line: 0},- }- default:- // Other column types allow default values- }- }-- var defaultValue string- switch {- case itemDef.ExprWithParentheses() != nil:- defaultValue = itemDef.ExprWithParentheses().GetText()- case itemDef.SignedLiteral() != nil:- defaultValue = itemDef.SignedLiteral().GetText()- default:- // No default value expression- }-- colProto.Default = defaultValue- } else {- if !colProto.Nullable {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SetNullDefaultForNotNullColumn.Int32(),- Title: fmt.Sprintf("Invalid default value for column `%s`", columnName),- Content: fmt.Sprintf("Invalid default value for column `%s`", columnName),- StartPosition: &storepb.Position{Line: 0},- }- }-- colProto.Default = ""- }- // SET VISIBLE/INVISIBLE.- default:- }- case itemDef.DROP_SYMBOL() != nil && itemDef.DEFAULT_SYMBOL() != nil:- // DROP DEFAULT.- colProto.Default = ""- default:- // Other ALTER operations- }- return nil-}--// mysqlChangeColumn changes column definition.-// It works as:-// 1. rename column if name changed-// 2. update column properties from fieldDef-// 3. handle position changes by reordering columns in the table-func mysqlChangeColumn(table *model.TableMetadata, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *storepb.Advice {- column := table.GetColumn(oldColumnName)- if column == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldColumnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", oldColumnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }-- // If renaming, validate and use RenameColumn- if oldColumnName != newColumnName {- // Validate new column doesn't already exist- if table.GetColumn(newColumnName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("Column `%s` already exists in table `%s`", newColumnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` already exists in table `%s`", newColumnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- if err := table.RenameColumn(oldColumnName, newColumnName); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: fmt.Sprintf("failed to rename column: %v", err),- Content: fmt.Sprintf("failed to rename column: %v", err),- StartPosition: &storepb.Position{Line: 0},- }- }- // Get the renamed column- column = table.GetColumn(newColumnName)- }-- columnProto := column.GetProto()- // Update column properties from fieldDef- if fieldDef.DataType() != nil {- columnProto.Type = mysqlparser.NormalizeMySQLDataType(fieldDef.DataType(), true /* compact */)- columnProto.CharacterSet = mysqlparser.GetCharSetName(fieldDef.DataType())- columnProto.Collation = mysqlparser.GetCollationName(fieldDef)- }-- // Reset nullable to true, then check for NOT NULL constraint- // This ensures that if the new definition doesn't have NOT NULL, the column becomes nullable- columnProto.Nullable = true- for _, attribute := range fieldDef.AllColumnAttribute() {- if attribute == nil {- continue- }- // Check for NOT NULL constraint- if attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil {- columnProto.Nullable = false- break- }- // Check for PRIMARY KEY or UNIQUE KEY (these imply NOT NULL for primary keys)- if attribute.GetValue() != nil && attribute.GetValue().GetTokenType() == mysql.MySQLParserKEY_SYMBOL {- columnProto.Nullable = false- break- }- }-- // Handle position changes by reordering columns in the proto- if position != nil && position.tp != ColumnPositionNone {- tableProto := table.GetProto()- columns := tableProto.Columns-- // Find the current position of the column- var currentIdx int- for i, col := range columns {- if col == column.GetProto() {- currentIdx = i- break- }- }-- // Remove from current position- columns = append(columns[:currentIdx], columns[currentIdx+1:]...)-- // Insert at new position- var newIdx int- switch position.tp {- case ColumnPositionFirst:- newIdx = 0- case ColumnPositionAfter:- // Find the position after the relative column- for i, col := range columns {- if col.Name == position.relativeColumn {- newIdx = i + 1- break- }- }- default:- // ColumnPositionNone should not reach here, but keep at end if it does- newIdx = len(columns)- }-- // Insert at newIdx- if newIdx >= len(columns) {- columns = append(columns, column.GetProto())- } else {- columns = append(columns[:newIdx], append([]*storepb.ColumnMetadata{column.GetProto()}, columns[newIdx:]...)...)- }-- // Update the proto's column list- tableProto.Columns = columns-- // Update position field for all columns- for i, col := range columns {- col.Position = int32(i + 1)- }- }-- return nil-}--type columnPositionType int--const (- ColumnPositionNone columnPositionType = iota- ColumnPositionFirst- ColumnPositionAfter-)--type mysqlColumnPosition struct {- tp columnPositionType- relativeColumn string-}--func positionFromPlaceContext(place mysql.IPlaceContext) *mysqlColumnPosition {- columnPosition := &mysqlColumnPosition{- tp: ColumnPositionNone,- }- if place, ok := place.(*mysql.PlaceContext); ok {- if place != nil {- switch {- case place.FIRST_SYMBOL() != nil:- columnPosition.tp = ColumnPositionFirst- case place.AFTER_SYMBOL() != nil:- columnPosition.tp = ColumnPositionAfter- columnName := mysqlparser.NormalizeMySQLIdentifier(place.Identifier())- columnPosition.relativeColumn = columnName- default:- // No position specified- }- }- }- return columnPosition-}--func mysqlCopyTable(d *model.DatabaseMetadata, databaseName, tableName, referTable string) *storepb.Advice {- targetTable, advice := mysqlFindTableState(d, databaseName, referTable)- if advice != nil {- return advice- }-- schema := d.GetSchemaMetadata("")- if schema == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- }-- // Create the new table- newTable, createErr := schema.CreateTable(tableName)- if createErr != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableExists.Int32(),- Title: createErr.Error(),- Content: createErr.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }-- // Copy table properties- targetProto := targetTable.GetProto()- newTableProto := newTable.GetProto()- newTableProto.Engine = targetProto.Engine- newTableProto.Collation = targetProto.Collation- newTableProto.Comment = targetProto.Comment-- // Copy columns- for _, col := range targetTable.GetProto().GetColumns() {- colCopy, ok := proto.Clone(col).(*storepb.ColumnMetadata)- if !ok {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: "failed to clone column metadata",- Content: "failed to clone column metadata",- StartPosition: &storepb.Position{Line: 0},- }- }- if err := newTable.CreateColumn(colCopy, nil /* columnCatalog */); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- }-- // Copy indexes- for _, idx := range targetProto.Indexes {- idxCopy, ok := proto.Clone(idx).(*storepb.IndexMetadata)- if !ok {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.Internal.Int32(),- Title: "failed to clone index metadata",- Content: "failed to clone index metadata",- StartPosition: &storepb.Position{Line: 0},- }- }- if err := newTable.CreateIndex(idxCopy); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- }-- return nil-}--func mysqlFindTableState(d *model.DatabaseMetadata, databaseName, tableName string) (*model.TableMetadata, *storepb.Advice) {- if databaseName != "" && !isCurrentDatabase(d, databaseName) {- return nil, &storepb.Advice{- Status: storepb.Advice_WARNING,- Code: code.NotCurrentDatabase.Int32(),- Title: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, d.DatabaseName()),- Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, d.DatabaseName()),- StartPosition: &storepb.Position{Line: 0},- }- }-- schema := d.GetSchemaMetadata("")- if schema == nil {- return nil, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SchemaNotExists.Int32(),- Title: "Schema does not exist",- Content: "Schema does not exist",- StartPosition: &storepb.Position{Line: 0},- }- }-- table := schema.GetTable(tableName)- if table == nil {- return nil, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.TableNotExists.Int32(),- Title: fmt.Sprintf("Table `%s` does not exist", tableName),- Content: fmt.Sprintf("Table `%s` does not exist", tableName),- StartPosition: &storepb.Position{Line: 0},- }- }-- return table, nil-}--func mysqlCreateConstraint(table *model.TableMetadata, constraintDef mysql.ITableConstraintDefContext) *storepb.Advice {- if constraintDef.GetType_() != nil {- switch constraintDef.GetType_().GetTokenType() {- // PRIMARY KEY.- case mysql.MySQLParserPRIMARY_SYMBOL:- if constraintDef.KeyListVariants() == nil {- // never reach here.- return nil- }- if err := mysqlValidateKeyListVariants(table, constraintDef.KeyListVariants(), true /* primary */, false /* isSpatial*/); err != nil {- return err- }- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())- if err := mysqlCreatePrimaryKey(table, keyList, mysqlGetIndexType(constraintDef)); err != nil {- return err- }- // normal KEY/INDEX.- case mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserINDEX_SYMBOL:- if constraintDef.KeyListVariants() == nil {- // never reach here.- return nil- }- if err := mysqlValidateKeyListVariants(table, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial */); err != nil {- return err- }-- indexName := ""- if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())- }- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())- if err := mysqlCreateIndex(table, indexName, keyList, false /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {- return err- }- // UNIQUE KEY.- case mysql.MySQLParserUNIQUE_SYMBOL:- if constraintDef.KeyListVariants() == nil {- // never reach here.- return nil- }- if err := mysqlValidateKeyListVariants(table, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {- return err- }-- indexName := ""- if constraintDef.ConstraintName() != nil {- indexName = mysqlparser.NormalizeConstraintName(constraintDef.ConstraintName())- }- if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())- }- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())- if err := mysqlCreateIndex(table, indexName, keyList, true /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {- return err- }- // FULLTEXT KEY.- case mysql.MySQLParserFULLTEXT_SYMBOL:- if constraintDef.KeyListVariants() == nil {- // never reach here.- return nil- }- if err := mysqlValidateKeyListVariants(table, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {- return err- }- indexName := ""- if constraintDef.IndexName() != nil {- indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexName())- }- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())- if err := mysqlCreateIndex(table, indexName, keyList, false /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {- return err- }- case mysql.MySQLParserFOREIGN_SYMBOL:- // we do not deal with FOREIGN KEY constraints.- default:- // Other constraint types- }- }-- // we do not deal with check constraints.- // if constraintDef.CheckConstraint() != nil {}- return nil-}--// mysqlValidateKeyListVariants validates the key list variants.-func mysqlValidateKeyListVariants(table *model.TableMetadata, keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) *storepb.Advice {- if keyList.KeyList() != nil {- columns := mysqlparser.NormalizeKeyList(keyList.KeyList())- if err := mysqlValidateColumnList(table, columns, primary, isSpatial); err != nil {- return err- }- }- if keyList.KeyListWithExpression() != nil {- expressions := mysqlparser.NormalizeKeyListWithExpression(keyList.KeyListWithExpression())- if err := mysqlValidateExpressionList(table, expressions, primary, isSpatial); err != nil {- return err- }- }- return nil-}--func mysqlValidateColumnList(table *model.TableMetadata, columnList []string, primary bool, isSpatial bool) *storepb.Advice {- for _, columnName := range columnList {- column := table.GetColumn(columnName)- if column == nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnNotExists.Int32(),- Title: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- if primary {- column.GetProto().Nullable = false- }- if isSpatial && column.GetProto().Nullable {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SpatialIndexKeyNullable.Int32(),- Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- }- return nil-}--// mysqlValidateExpressionList validates the expression list.-// TODO: update expression validation.-func mysqlValidateExpressionList(table *model.TableMetadata, expressionList []string, primary bool, isSpatial bool) *storepb.Advice {- for _, expression := range expressionList {- column := table.GetColumn(expression)- // If expression is not a column, we do not need to validate it.- if column == nil {- continue- }-- if primary {- column.GetProto().Nullable = false- }- if isSpatial && column.GetProto().Nullable {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SpatialIndexKeyNullable.Int32(),- Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- }- return nil-}--func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) string {- if tableConstraint.GetType_() == nil {- return "BTREE"- }-- // I still need to handle IndexNameAndType to get index type(algorithm).- switch tableConstraint.GetType_().GetTokenType() {- case mysql.MySQLParserPRIMARY_SYMBOL,- mysql.MySQLParserKEY_SYMBOL,- mysql.MySQLParserINDEX_SYMBOL,- mysql.MySQLParserUNIQUE_SYMBOL:-- if tableConstraint.IndexNameAndType() != nil {- if tableConstraint.IndexNameAndType().IndexType() != nil {- indexType := tableConstraint.IndexNameAndType().IndexType().GetText()- return strings.ToUpper(indexType)- }- }-- for _, option := range tableConstraint.AllIndexOption() {- if option == nil || option.IndexTypeClause() == nil {- continue- }-- indexType := option.IndexTypeClause().IndexType().GetText()- return strings.ToUpper(indexType)- }- case mysql.MySQLParserFULLTEXT_SYMBOL:- return "FULLTEXT"- case mysql.MySQLParserFOREIGN_SYMBOL:- // Foreign key - no specific index type- default:- // Other constraint types- }- // for mysql, we use BTREE as default index type.- return "BTREE"-}--func mysqlCreateColumn(table *model.TableMetadata, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *storepb.Advice {- if table.GetColumn(columnName) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: fmt.Sprintf("Column `%s` already exists in table `%s`", columnName, table.GetProto().Name),- Content: fmt.Sprintf("Column `%s` already exists in table `%s`", columnName, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }-- columnType := ""- characterSet := ""- collation := ""- if fieldDef.DataType() == nil {- // todo: add more error detail.- return nil- }- columnType = mysqlparser.NormalizeMySQLDataType(fieldDef.DataType(), true /* compact */)- characterSet = mysqlparser.GetCharSetName(fieldDef.DataType())- collation = mysqlparser.GetCollationName(fieldDef)-- col := &storepb.ColumnMetadata{- Name: columnName,- Position: int32(len(table.GetProto().GetColumns()) + 1),- Default: "",- Nullable: true,- Type: columnType,- CharacterSet: characterSet,- Collation: collation,- }- setNullDefault := false-- for _, attribute := range fieldDef.AllColumnAttribute() {- if attribute == nil {- continue- }- if attribute.CheckConstraint() != nil {- // we do not deal with CHECK constraint.- continue- }- // not null.- if attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil {- col.Nullable = false- }- if attribute.GetValue() != nil {- switch attribute.GetValue().GetTokenType() {- // default value.- case mysql.MySQLParserDEFAULT_SYMBOL:- if err := mysqlCheckDefault(table, columnName, fieldDef); err != nil {- return err- }- if attribute.SignedLiteral() == nil {- continue- }- // handle default null.- if attribute.SignedLiteral().Literal() != nil && attribute.SignedLiteral().Literal().NullLiteral() != nil {- setNullDefault = true- continue- }- // handle default 'null' etc.- defaultValue := mysqlparser.NormalizeMySQLSignedLiteral(attribute.SignedLiteral())- col.Default = defaultValue- // comment.- case mysql.MySQLParserCOMMENT_SYMBOL:- if attribute.TextLiteral() == nil {- continue- }- comment := mysqlparser.NormalizeMySQLTextLiteral(attribute.TextLiteral())- col.Comment = comment- // on update now().- case mysql.MySQLParserON_SYMBOL:- if attribute.UPDATE_SYMBOL() == nil || attribute.NOW_SYMBOL() == nil {- continue- }- if !mysqlparser.IsTimeType(fieldDef.DataType()) {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.OnUpdateColumnNotDatetimeOrTimestamp.Int32(),- Title: fmt.Sprintf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.Name),- Content: fmt.Sprintf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.Name),- StartPosition: &storepb.Position{Line: 0},- }- }- // primary key.- case mysql.MySQLParserKEY_SYMBOL:- // the key attribute for in a column meaning primary key.- col.Nullable = false- // we need to check the key type which generated by tidb parser.- if err := mysqlCreatePrimaryKey(table, []string{strings.ToLower(col.Name)}, "BTREE"); err != nil {- return err- }- // unique key.- case mysql.MySQLParserUNIQUE_SYMBOL:- // unique index.- if err := mysqlCreateIndex(table, "", []string{strings.ToLower(col.Name)}, true /* unique */, "BTREE", mysql.NewEmptyTableConstraintDefContext(), mysql.NewEmptyCreateIndexContext()); err != nil {- return err- }- // auto_increment.- case mysql.MySQLParserAUTO_INCREMENT_SYMBOL:- // we do not deal with AUTO_INCREMENT.- // column_format.- case mysql.MySQLParserCOLUMN_FORMAT_SYMBOL:- // we do not deal with COLUMN_FORMAT.- // storage.- case mysql.MySQLParserSTORAGE_SYMBOL:- // we do not deal with STORAGE.- default:- // Other column attributes- }- }- }-- if !col.Nullable && setNullDefault {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SetNullDefaultForNotNullColumn.Int32(),- Title: fmt.Sprintf("Invalid default value for column `%s`", col.Name),- Content: fmt.Sprintf("Invalid default value for column `%s`", col.Name),- StartPosition: &storepb.Position{Line: 0},- }- }-- if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.ColumnExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }-- // Handle position by reordering columns in the proto- if position != nil && position.tp != ColumnPositionNone {- tableProto := table.GetProto()- columns := tableProto.Columns-- // The new column was just appended, so it's at the end- lastIdx := len(columns) - 1- newColumn := columns[lastIdx]-- // Remove from the end- columns = columns[:lastIdx]-- // Insert at the desired position- var newIdx int- switch position.tp {- case ColumnPositionFirst:- newIdx = 0- case ColumnPositionAfter:- // Find the position after the relative column- for i, c := range columns {- if c.Name == position.relativeColumn {- newIdx = i + 1- break- }- }- default:- // ColumnPositionNone should not reach here, but keep at end if it does- newIdx = len(columns)- }-- // Insert at newIdx- if newIdx >= len(columns) {- columns = append(columns, newColumn)- } else {- columns = append(columns[:newIdx], append([]*storepb.ColumnMetadata{newColumn}, columns[newIdx:]...)...)- }-- // Update the proto's column list- tableProto.Columns = columns-- // Update position field for all columns- for i, c := range columns {- c.Position = int32(i + 1)- }- }-- return nil-}--func mysqlCreateIndex(table *model.TableMetadata, name string, keyList []string, unique bool, tp string, tableConstraint mysql.ITableConstraintDefContext, createIndexDef mysql.ICreateIndexContext) *storepb.Advice {- if len(keyList) == 0 {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexEmptyKeys.Int32(),- Title: fmt.Sprintf("Index `%s` in table `%s` has empty key", name, table.GetProto().Name),- Content: fmt.Sprintf("Index `%s` in table `%s` has empty key", name, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- // construct a index name if name is empty.- if name != "" {- if table.GetIndex(name) != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: fmt.Sprintf("Index `%s` already exists in table `%s`", name, table.GetProto().Name),- Content: fmt.Sprintf("Index `%s` already exists in table `%s`", name, table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }- } else {- suffix := 1- for {- name = keyList[0]- if suffix > 1 {- name = fmt.Sprintf("%s_%d", keyList[0], suffix)- }- if table.GetIndex(name) == nil {- break- }- suffix++- }- }-- visible := true-- // Check visibility from table constraint- for _, attribute := range tableConstraint.AllIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- // Check visibility from create index statement- for _, attribute := range createIndexDef.AllIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- // Check FULLTEXT visibility- for _, attribute := range tableConstraint.AllFulltextIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- for _, attribute := range createIndexDef.AllFulltextIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- // Check SPATIAL visibility- for _, attribute := range tableConstraint.AllSpatialIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- for _, attribute := range createIndexDef.AllSpatialIndexOption() {- if attribute == nil || attribute.CommonIndexOption() == nil {- continue- }- if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {- visible = false- }- }-- index := &storepb.IndexMetadata{- Name: name,- Expressions: keyList,- Type: tp,- Unique: unique,- Primary: false,- Visible: visible,- }-- if err := table.CreateIndex(index); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.IndexExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- return nil-}--func mysqlCreatePrimaryKey(table *model.TableMetadata, keys []string, tp string) *storepb.Advice {- if table.GetPrimaryKey() != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: fmt.Sprintf("Primary key exists in table `%s`", table.GetProto().Name),- Content: fmt.Sprintf("Primary key exists in table `%s`", table.GetProto().Name),- StartPosition: &storepb.Position{Line: 0},- }- }-- pk := &storepb.IndexMetadata{- Name: PrimaryKeyName,- Expressions: keys,- Type: tp,- Unique: true,- Primary: true,- Visible: true,- }- if err := table.CreateIndex(pk); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.PrimaryKeyExists.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- return nil-}--func mysqlCheckDefault(_ *model.TableMetadata, columnName string, fieldDefinition mysql.IFieldDefinitionContext) *storepb.Advice {- if fieldDefinition.DataType() == nil || fieldDefinition.DataType().GetType_() == nil {- return nil- }-- switch fieldDefinition.DataType().GetType_().GetTokenType() {- case mysql.MySQLParserTEXT_SYMBOL,- mysql.MySQLParserTINYTEXT_SYMBOL,- mysql.MySQLParserMEDIUMTEXT_SYMBOL,- mysql.MySQLParserLONGTEXT_SYMBOL,- mysql.MySQLParserBLOB_SYMBOL,- mysql.MySQLParserTINYBLOB_SYMBOL,- mysql.MySQLParserMEDIUMBLOB_SYMBOL,- mysql.MySQLParserLONGBLOB_SYMBOL,- mysql.MySQLParserLONG_SYMBOL,- mysql.MySQLParserSERIAL_SYMBOL,- mysql.MySQLParserJSON_SYMBOL,- mysql.MySQLParserGEOMETRY_SYMBOL,- mysql.MySQLParserGEOMETRYCOLLECTION_SYMBOL,- mysql.MySQLParserPOINT_SYMBOL,- mysql.MySQLParserMULTIPOINT_SYMBOL,- mysql.MySQLParserLINESTRING_SYMBOL,- mysql.MySQLParserMULTILINESTRING_SYMBOL,- mysql.MySQLParserPOLYGON_SYMBOL,- mysql.MySQLParserMULTIPOLYGON_SYMBOL:- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.InvalidColumnDefault.Int32(),- Title: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- StartPosition: &storepb.Position{Line: 0},- }- default:- // Other data types are allowed to have default values- }-- return checkDefaultConvert(columnName, fieldDefinition)-}--func checkDefaultConvert(columnName string, fieldDefinition mysql.IFieldDefinitionContext) *storepb.Advice {- if fieldDefinition == nil {- return nil- }- list, err := tidb.ParseTiDB(fmt.Sprintf("CREATE TABLE t(%s %s)", columnName, fieldDefinition.GetParser().GetTokenStream().GetTextFromRuleContext(fieldDefinition)), "", "")- if err != nil {- // For now, we do not handle this case.- // nolint:nilerr- return nil- }- if len(list) != 1 {- return nil- }- createTable, ok := list[0].(*tidbast.CreateTableStmt)- if !ok {- return nil- }- if len(createTable.Cols) != 1 {- return nil- }- col := createTable.Cols[0]- for _, option := range col.Options {- if option.Tp == tidbast.ColumnOptionDefaultValue {- return checkDefault(columnName, col.Tp, option.Expr)- }- }-- return nil-}--func checkDefault(columnName string, columnType *types.FieldType, value tidbast.ExprNode) *storepb.Advice {- if value.GetType().GetType() != mysqldriver.TypeNull {- switch columnType.GetType() {- case mysqldriver.TypeBlob, mysqldriver.TypeTinyBlob, mysqldriver.TypeMediumBlob, mysqldriver.TypeLongBlob, mysqldriver.TypeJSON, mysqldriver.TypeGeometry:- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.InvalidColumnDefault.Int32(),- Title: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),- StartPosition: &storepb.Position{Line: 0},- }- default:- // Other column types allow default values- }- }-- if valueExpr, yes := value.(tidbast.ValueExpr); yes {- datum := types.NewDatum(valueExpr.GetValue())- if _, err := datum.ConvertTo(types.Context{}, columnType); err != nil {- return &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.InvalidColumnDefault.Int32(),- Title: err.Error(),- Content: err.Error(),- StartPosition: &storepb.Position{Line: 0},- }- }- }- return nil-}--// compareIdentifier returns true if the engine will regard the two identifiers as the same one.-func compareIdentifier(a, b string, ignoreCaseSensitive bool) bool {- if ignoreCaseSensitive {- return strings.EqualFold(a, b)- }- return a == b-}--// isCurrentDatabase returns true if the given database is the current database.-func isCurrentDatabase(d *model.DatabaseMetadata, database string) bool {- return compareIdentifier(d.DatabaseName(), database, !d.GetIsObjectCaseSensitive())-}diff --git a/backend/plugin/schema/sdl_differ.go b/backend/plugin/schema/sdl_differ.godeleted file mode 100644index dbbb5a068b..0000000000--- a/backend/plugin/schema/sdl_differ.go+++ /dev/null@@ -1,145 +0,0 @@-package schema--import (- "github.com/antlr4-go/antlr/v4"-)--type SDLChunk struct {- Identifier string- ASTNode antlr.ParserRuleContext // The parsed AST node for this chunk (e.g., CREATE SEQUENCE)- AlterStatements []antlr.ParserRuleContext // Additional ALTER statements for this object (e.g., ALTER SEQUENCE OWNED BY)- CommentStatements []antlr.ParserRuleContext // COMMENT ON statements for this object-}--// GetText extracts text from the AST node using its own token stream-// For chunks with ALTER statements, it combines the main statement with all ALTER statements-// For chunks with COMMENT statements, it combines them as well-func (c *SDLChunk) GetText() string {- if c.ASTNode == nil {- return ""- }-- // Extract text from the main AST node (e.g., CREATE SEQUENCE)- mainText := extractTextFromNode(c.ASTNode)-- // Collect all parts: main, ALTER statements, and COMMENT statements- parts := []string{mainText}-- // Add ALTER statements- for _, alterNode := range c.AlterStatements {- alterText := extractTextFromNode(alterNode)- if alterText != "" {- parts = append(parts, alterText)- }- }-- // Add COMMENT statements- for _, commentNode := range c.CommentStatements {- commentText := extractTextFromNode(commentNode)- if commentText != "" {- parts = append(parts, commentText)- }- }-- // Join with double newline to match SDL format- result := ""- for i, part := range parts {- if i > 0 {- result += "\n\n"- }- result += part- }- return result-}--// GetTextWithoutComments extracts text excluding COMMENT ON statements-// This is used for comparing object definitions without considering comments-func (c *SDLChunk) GetTextWithoutComments() string {- if c.ASTNode == nil {- return ""- }-- // Extract text from the main AST node- mainText := extractTextFromNode(c.ASTNode)-- // If there are no ALTER statements, return just the main text- if len(c.AlterStatements) == 0 {- return mainText- }-- // Combine main statement with ALTER statements (excluding COMMENT statements)- parts := []string{mainText}- for _, alterNode := range c.AlterStatements {- alterText := extractTextFromNode(alterNode)- if alterText != "" {- parts = append(parts, alterText)- }- }-- // Join with double newline to match SDL format- result := ""- for i, part := range parts {- if i > 0 {- result += "\n\n"- }- result += part- }- return result-}--// extractTextFromNode is a helper function to extract text from a parser rule context-func extractTextFromNode(node antlr.ParserRuleContext) string {- if node == nil {- return ""- }-- // Check for interfaces that have the required methods- type parserContext interface {- GetParser() antlr.Parser- GetStart() antlr.Token- GetStop() antlr.Token- }-- if ruleContext, ok := node.(parserContext); ok {- if parser := ruleContext.GetParser(); parser != nil {- if tokenStream := parser.GetTokenStream(); tokenStream != nil {- start := ruleContext.GetStart()- stop := ruleContext.GetStop()- if start != nil && stop != nil {- return tokenStream.GetTextFromTokens(start, stop)- }- }- }- }-- // Fallback to node's GetText method- return node.GetText()-}--type SDLChunks struct {- Tables map[string]*SDLChunk // key: table name/identifier- Views map[string]*SDLChunk // key: view name/identifier- MaterializedViews map[string]*SDLChunk // key: materialized view name/identifier- Functions map[string]*SDLChunk // key: function name/identifier- Triggers map[string]*SDLChunk // key: schema.table.trigger_name (table-scoped)- Indexes map[string]*SDLChunk // key: index name/identifier- Sequences map[string]*SDLChunk // key: sequence name/identifier- Schemas map[string]*SDLChunk // key: schema name/identifier- EnumTypes map[string]*SDLChunk // key: enum type name/identifier- Extensions map[string]*SDLChunk // key: extension name/identifier- EventTriggers map[string]*SDLChunk // key: event trigger name (database-level, no schema)-- // Column comments: map[schemaName.tableName][columnName] -> COMMENT ON COLUMN AST node- ColumnComments map[string]map[string]antlr.ParserRuleContext-- // Index comments for table-level indexes: map[schemaName.tableName][indexName] -> COMMENT ON INDEX AST node- IndexComments map[string]map[string]antlr.ParserRuleContext-}--type SDLDiff struct {- CurrentChunks *SDLChunks- PreviousChunks *SDLChunks- Added []*SDLChunk- Removed []*SDLChunk- Modified []*SDLChunk-}diff --git a/go.mod b/go.modindex 7466bd1494..33d1213fce 100644--- a/go.mod+++ b/go.mod@@ -22,7 +22,6 @@ require (github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.4.0github.com/ClickHouse/clickhouse-go/v2 v2.46.0github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0- github.com/antlr4-go/antlr/v4 v4.13.1github.com/apache/cassandra-gocql-driver/v2 v2.1.1github.com/aws/aws-sdk-go-v2 v1.41.7github.com/aws/aws-sdk-go-v2/config v1.32.17@@ -128,6 +127,7 @@ require (github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.31.0 // indirectgithub.com/Masterminds/semver v1.5.0 // indirectgithub.com/Microsoft/go-winio v0.6.2 // indirect+ github.com/antlr4-go/antlr/v4 v4.13.1 // indirectgithub.com/apache/arrow-go/v18 v18.4.0 // indirectgithub.com/apache/arrow/go/v15 v15.0.2 // indirectgithub.com/apache/thrift v0.23.0 // indirect@@ -295,7 +295,6 @@ require (github.com/aws/aws-sdk-go-v2/service/sts v1.42.1github.com/aws/smithy-go v1.25.1 // indirectgithub.com/beorn7/perks v1.0.1 // indirect- github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640github.com/cespare/xxhash/v2 v2.3.0 // indirectgithub.com/cncf/xds/go v0.0.0-20260202195803-dba9d589def2 // indirectgithub.com/cockroachdb/cockroach-go/v2 v2.4.3diff --git a/go.sum b/go.sumindex 9eafe17213..ccef960061 100644--- a/go.sum+++ b/go.sum@@ -252,8 +252,6 @@ github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=github.com/bytebase/omni v0.0.0-20260626034734-62d4f668f054 h1:ZjmNACtTuRrUFCqlOhKnPbe9LpwbP18kWfW20mkyDsM=github.com/bytebase/omni v0.0.0-20260626034734-62d4f668f054/go.mod h1:Gp5RN3FM07f9/FFOTZCeu8duTAIMeuinkjjBCrs2Dw4=-github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640 h1:g4Jed1/Jv/DHBiQmEgN5ILOQDDBjFlG4N4Lp+3B4aYE=-github.com/bytebase/parser v0.0.0-20260417075056-57b6ef7a2640/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=