Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(advisor/mysql): migrate all 87 advisor rules to omni AST [BYT-9123] (#19826)

rebelice699ff60c2069
diff --git a/backend/plugin/advisor/mysql/generic_checker.go b/backend/plugin/advisor/mysql/generic_checker.go
index 6228249263..b9ab3f666d 100644
--- a/backend/plugin/advisor/mysql/generic_checker.go
+++ b/backend/plugin/advisor/mysql/generic_checker.go
@@ -151,8 +151,8 @@ func (*GenericChecker) getNodeType(ctx antlr.ParserRuleContext) string {
// BaseRule provides common functionality for rules.
// Other rules can embed this struct to get common behavior.
type BaseRule struct {
- level storepb.Advice_Status
- title string
+ level storepb.Advice_Status // nolint:unused
+ title string // nolint:unused
adviceList []*storepb.Advice
baseLine int
}
diff --git a/backend/plugin/advisor/mysql/generic_checker_omni.go b/backend/plugin/advisor/mysql/generic_checker_omni.go
new file mode 100644
index 0000000000..310064eefc
--- /dev/null
+++ b/backend/plugin/advisor/mysql/generic_checker_omni.go
@@ -0,0 +1,153 @@
+package mysql
+
+import (
+ "strings"
+
+ "github.com/bytebase/omni/mysql/ast"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+)
+
+// OmniRule defines the interface for omni-based SQL validation rules.
+type OmniRule interface {
+ // OnStatement is called for each top-level statement AST node.
+ OnStatement(node ast.Node)
+
+ // Name returns the rule name for logging/debugging.
+ Name() string
+
+ // GetAdviceList returns the accumulated advice from this rule.
+ GetAdviceList() []*storepb.Advice
+}
+
+// OmniBaseRule provides common functionality for omni-based rules.
+type OmniBaseRule struct {
+ Level storepb.Advice_Status
+ Title string
+ Advice []*storepb.Advice
+ BaseLine int
+ StmtText string
+}
+
+// SetStatement sets the statement context for position calculations.
+func (r *OmniBaseRule) SetStatement(baseLine int, stmtText string) {
+ r.BaseLine = baseLine
+ r.StmtText = stmtText
+}
+
+// GetAdviceList returns the accumulated advice.
+func (r *OmniBaseRule) GetAdviceList() []*storepb.Advice {
+ return r.Advice
+}
+
+// AddAdvice adds advice. The BaseLine offset is added automatically to StartPosition.Line.
+func (r *OmniBaseRule) AddAdvice(advice *storepb.Advice) {
+ if advice.StartPosition != nil {
+ advice.StartPosition.Line += int32(r.BaseLine)
+ }
+ r.Advice = append(r.Advice, advice)
+}
+
+// AddAdviceAbsolute adds advice with an already-absolute line number.
+func (r *OmniBaseRule) AddAdviceAbsolute(advice *storepb.Advice) {
+ r.Advice = append(r.Advice, advice)
+}
+
+// LocToLine converts an omni Loc byte offset to a 1-based line number
+// within the current statement text. The returned value includes any leading
+// newlines in StmtText, matching ANTLR's GetStart().GetLine() behavior when
+// ANTLR parses the same text. Suitable for: BaseLine + LocToLine(loc).
+func (r *OmniBaseRule) LocToLine(loc ast.Loc) int32 {
+ if loc.Start < 0 || r.StmtText == "" {
+ return r.ContentStartLine()
+ }
+ pos := mysqlparser.ByteOffsetToRunePosition(r.StmtText, loc.Start)
+ return pos.Line
+}
+
+// QueryText returns the statement text with leading/trailing whitespace trimmed.
+func (r *OmniBaseRule) QueryText() string {
+ return strings.TrimSpace(r.StmtText)
+}
+
+// TrimmedStmtText returns the statement text with leading/trailing whitespace
+// and trailing semicolons removed.
+func (r *OmniBaseRule) TrimmedStmtText() string {
+ return strings.TrimRight(strings.TrimSpace(r.StmtText), ";")
+}
+
+// ContentStartLine returns the 1-based line number of the first non-whitespace
+// character in StmtText. Returns 1 if the text is empty or has no leading newlines.
+func (r *OmniBaseRule) ContentStartLine() int32 {
+ idx := strings.IndexFunc(r.StmtText, func(c rune) bool {
+ return c != ' ' && c != '\t' && c != '\n' && c != '\r'
+ })
+ if idx <= 0 {
+ return 1
+ }
+ pos := mysqlparser.ByteOffsetToRunePosition(r.StmtText, idx)
+ return pos.Line
+}
+
+// ContentEndLine returns the 1-based line number of the last non-whitespace
+// character in StmtText.
+func (r *OmniBaseRule) ContentEndLine() int32 {
+ if r.StmtText == "" {
+ return 1
+ }
+ idx := -1
+ for i := len(r.StmtText) - 1; i >= 0; i-- {
+ c := r.StmtText[i]
+ if c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != ';' {
+ idx = i
+ break
+ }
+ }
+ if idx <= 0 {
+ return 1
+ }
+ pos := mysqlparser.ByteOffsetToRunePosition(r.StmtText, idx)
+ return pos.Line
+}
+
+// FindLineByName searches for an identifier name in the statement text and returns
+// its 1-based line number. Falls back to ContentStartLine() if not found.
+func (r *OmniBaseRule) FindLineByName(name string) int32 {
+ if name == "" || r.StmtText == "" {
+ return r.ContentStartLine()
+ }
+ idx := strings.Index(r.StmtText, name)
+ if idx < 0 {
+ return r.ContentStartLine()
+ }
+ pos := mysqlparser.ByteOffsetToRunePosition(r.StmtText, idx)
+ return pos.Line
+}
+
+// RunOmniRules iterates over parsed statements and dispatches each omni AST node to all rules.
+// Returns combined advice from all rules. Skips statements without omni AST.
+func RunOmniRules(stmts []base.ParsedStatement, rules []OmniRule) []*storepb.Advice {
+ for _, stmt := range stmts {
+ if stmt.AST == nil {
+ continue
+ }
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
+ if !ok {
+ continue
+ }
+ baseLine := stmt.BaseLine()
+ for _, rule := range rules {
+ if br, ok := rule.(interface{ SetStatement(int, string) }); ok {
+ br.SetStatement(baseLine, stmt.Text)
+ }
+ rule.OnStatement(node)
+ }
+ }
+ var allAdvice []*storepb.Advice
+ for _, rule := range rules {
+ allAdvice = append(allAdvice, rule.GetAdviceList()...)
+ }
+ return allAdvice
+}
diff --git a/backend/plugin/advisor/mysql/rule_builtin_prior_backup_check.go b/backend/plugin/advisor/mysql/rule_builtin_prior_backup_check.go
index 33825641b6..23d27037ea 100644
--- a/backend/plugin/advisor/mysql/rule_builtin_prior_backup_check.go
+++ b/backend/plugin/advisor/mysql/rule_builtin_prior_backup_check.go
@@ -4,7 +4,7 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
@@ -49,17 +49,15 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx context.Context, checkCtx adv
}
for _, stmt := range checkCtx.ParsedStatements {
- checker := &mysqlparser.StatementTypeChecker{}
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if checker.IsDDL {
+ if isOmniDDL(node) {
adviceList = append(adviceList, &storepb.Advice{
Status: level,
Title: title,
@@ -139,3 +137,21 @@ func prepareTransformation(databaseName string, parsedStatements []base.ParsedSt
return result, nil
}
+
+// isOmniDDL returns true if the omni AST node represents a DDL statement.
+func isOmniDDL(node ast.Node) bool {
+ switch node.(type) {
+ case *ast.CreateTableStmt, *ast.CreateDatabaseStmt, *ast.CreateIndexStmt,
+ *ast.CreateViewStmt, *ast.CreateTriggerStmt, *ast.CreateEventStmt,
+ *ast.CreateFunctionStmt,
+ *ast.AlterTableStmt, *ast.AlterDatabaseStmt, *ast.AlterViewStmt,
+ *ast.AlterEventStmt,
+ *ast.DropTableStmt, *ast.DropDatabaseStmt, *ast.DropIndexStmt,
+ *ast.DropViewStmt, *ast.DropTriggerStmt, *ast.DropEventStmt,
+ *ast.DropRoutineStmt,
+ *ast.RenameTableStmt, *ast.TruncateStmt:
+ return true
+ default:
+ return false
+ }
+}
diff --git a/backend/plugin/advisor/mysql/rule_charset_allowlist.go b/backend/plugin/advisor/mysql/rule_charset_allowlist.go
index 874b91ebf2..6886791920 100644
--- a/backend/plugin/advisor/mysql/rule_charset_allowlist.go
+++ b/backend/plugin/advisor/mysql/rule_charset_allowlist.go
@@ -5,15 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -43,191 +40,117 @@ func (*CharsetAllowlistAdvisor) Check(_ context.Context, checkCtx advisor.Contex
allowList[strings.ToLower(charset)] = true
}
- // Create the rule
- rule := NewCharsetAllowlistRule(level, checkCtx.Rule.Type.String(), allowList)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &charsetAllowlistOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ allowList: allowList,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// CharsetAllowlistRule checks for charset allowlist.
-type CharsetAllowlistRule struct {
- BaseRule
- text string
+type charsetAllowlistOmniRule struct {
+ OmniBaseRule
allowList map[string]bool
}
-// NewCharsetAllowlistRule creates a new CharsetAllowlistRule.
-func NewCharsetAllowlistRule(level storepb.Advice_Status, title string, allowList map[string]bool) *CharsetAllowlistRule {
- return &CharsetAllowlistRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- allowList: allowList,
- }
-}
-
-// Name returns the rule name.
-func (*CharsetAllowlistRule) Name() string {
+func (*charsetAllowlistOmniRule) Name() string {
return "CharsetAllowlistRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *CharsetAllowlistRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeCreateDatabase:
- r.checkCreateDatabase(ctx.(*mysql.CreateDatabaseContext))
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterDatabase:
- r.checkAlterDatabase(ctx.(*mysql.AlterDatabaseContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *charsetAllowlistOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateDatabaseStmt:
+ r.checkCreateDatabase(n)
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterDatabaseStmt:
+ r.checkAlterDatabase(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*CharsetAllowlistRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *CharsetAllowlistRule) checkCreateDatabase(ctx *mysql.CreateDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- for _, option := range ctx.AllCreateDatabaseOption() {
- if option.DefaultCharset() != nil {
- charset := mysqlparser.NormalizeMySQLCharsetName(option.DefaultCharset().CharsetName())
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
- break
- }
- }
-}
-
-func (r *CharsetAllowlistRule) checkCharset(charset string, lineNumber int) {
- if _, exists := r.allowList[charset]; charset != "" && !exists {
+func (r *charsetAllowlistOmniRule) checkCharset(charset string, line int) {
+ charset = strings.ToLower(charset)
+ if charset != "" && !r.allowList[charset] {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.DisabledCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" used disabled charset '%s'", r.text, charset),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" used disabled charset '%s'", r.QueryText(), charset),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
})
}
}
-func (r *CharsetAllowlistRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateTableOptions() != nil {
- for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {
- if option.DefaultCharset() != nil {
- charset := mysqlparser.NormalizeMySQLCharsetName(option.DefaultCharset().CharsetName())
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
- break
- }
- }
- }
-
- if ctx.TableElementList() != nil {
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() != nil {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- columnDef := tableElement.ColumnDefinition()
- if columnDef.FieldDefinition() == nil || columnDef.FieldDefinition().DataType() == nil {
- continue
- }
- if columnDef.FieldDefinition().DataType().CharsetWithOptBinary() == nil {
- continue
- }
- charsetName := columnDef.FieldDefinition().DataType().CharsetWithOptBinary().CharsetName()
- charset := mysqlparser.NormalizeMySQLCharsetName(charsetName)
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
- }
+func (r *charsetAllowlistOmniRule) checkCreateDatabase(n *ast.CreateDatabaseStmt) {
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "CHARACTER SET") || strings.EqualFold(opt.Name, "CHARSET") {
+ r.checkCharset(opt.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
+ break
}
}
}
-func (r *CharsetAllowlistRule) checkAlterDatabase(ctx *mysql.AlterDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
+func (r *charsetAllowlistOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ // Check table-level charset option.
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "CHARACTER SET") || strings.EqualFold(opt.Name, "CHARSET") || strings.EqualFold(opt.Name, "DEFAULT CHARSET") {
+ r.checkCharset(opt.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
+ break
+ }
}
- for _, option := range ctx.AllAlterDatabaseOption() {
- if option.CreateDatabaseOption() == nil || option.CreateDatabaseOption().DefaultCharset() == nil {
+ // Check column-level charset.
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- charset := mysqlparser.NormalizeMySQLCharsetName(option.CreateDatabaseOption().DefaultCharset().CharsetName())
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
+ if col.TypeName.Charset != "" {
+ r.checkCharset(col.TypeName.Charset, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
}
}
-func (r *CharsetAllowlistRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil || item.FieldDefinition() == nil {
- continue
- }
- if item.FieldDefinition().DataType() == nil {
- continue
+func (r *charsetAllowlistOmniRule) checkAlterDatabase(n *ast.AlterDatabaseStmt) {
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "CHARACTER SET") || strings.EqualFold(opt.Name, "CHARSET") {
+ r.checkCharset(opt.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
}
- if item.FieldDefinition().DataType().CharsetWithOptBinary() == nil {
- continue
- }
- charset := mysqlparser.NormalizeMySQLCharsetName(item.FieldDefinition().DataType().CharsetWithOptBinary().CharsetName())
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
}
- // alter table option
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {
- if option == nil {
+}
+
+func (r *charsetAllowlistOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- for _, tableOption := range option.AllCreateTableOption() {
- if tableOption == nil || tableOption.DefaultCharset() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn, ast.ATModifyColumn, ast.ATChangeColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ if col.TypeName.Charset != "" {
+ r.checkCharset(col.TypeName.Charset, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
+ }
+ case ast.ATTableOption:
+ if cmd.Option != nil {
+ if strings.EqualFold(cmd.Option.Name, "CHARACTER SET") || strings.EqualFold(cmd.Option.Name, "CHARSET") || strings.EqualFold(cmd.Option.Name, "DEFAULT CHARSET") {
+ r.checkCharset(cmd.Option.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
+ }
+ case ast.ATConvertCharset:
+ if cmd.Option != nil {
+ if strings.EqualFold(cmd.Option.Name, "CHARACTER SET") || strings.EqualFold(cmd.Option.Name, "CHARSET") {
+ r.checkCharset(cmd.Option.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
}
- charset := mysqlparser.NormalizeMySQLCharsetName(tableOption.DefaultCharset().CharsetName())
- charset = strings.ToLower(charset)
- r.checkCharset(charset, ctx.GetStart().GetLine())
+ default:
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_collation_allowlist.go b/backend/plugin/advisor/mysql/rule_collation_allowlist.go
index eb4f9eccd6..b9d12029ed 100644
--- a/backend/plugin/advisor/mysql/rule_collation_allowlist.go
+++ b/backend/plugin/advisor/mysql/rule_collation_allowlist.go
@@ -5,15 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -38,201 +35,123 @@ func (*CollationAllowlistAdvisor) Check(_ context.Context, checkCtx advisor.Cont
}
stringArrayPayload := checkCtx.Rule.GetStringArrayPayload()
- // Create the rule
- rule := NewCollationAllowlistRule(level, checkCtx.Rule.Type.String(), stringArrayPayload.List)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ allowList := make(map[string]bool)
+ for _, collation := range stringArrayPayload.List {
+ allowList[strings.ToLower(collation)] = true
+ }
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- // Set text will be handled in the Query node
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &collationAllowlistOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ allowList: allowList,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// CollationAllowlistRule checks for collation allowlist.
-type CollationAllowlistRule struct {
- BaseRule
+type collationAllowlistOmniRule struct {
+ OmniBaseRule
allowList map[string]bool
- text string
}
-// NewCollationAllowlistRule creates a new CollationAllowlistRule.
-func NewCollationAllowlistRule(level storepb.Advice_Status, title string, allowList []string) *CollationAllowlistRule {
- rule := &CollationAllowlistRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- allowList: make(map[string]bool),
- }
- for _, collation := range allowList {
- rule.allowList[strings.ToLower(collation)] = true
- }
- return rule
-}
-
-// Name returns the rule name.
-func (*CollationAllowlistRule) Name() string {
+func (*collationAllowlistOmniRule) Name() string {
return "CollationAllowlistRule"
}
-// SetText sets the query text for error reporting.
-func (r *CollationAllowlistRule) SetText(text string) {
- r.text = text
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *CollationAllowlistRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- r.checkQuery(ctx.(*mysql.QueryContext))
- case NodeTypeCreateDatabase:
- r.checkCreateDatabase(ctx.(*mysql.CreateDatabaseContext))
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterDatabase:
- r.checkAlterDatabase(ctx.(*mysql.AlterDatabaseContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *collationAllowlistOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateDatabaseStmt:
+ r.checkCreateDatabase(n)
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterDatabaseStmt:
+ r.checkAlterDatabase(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*CollationAllowlistRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *CollationAllowlistRule) checkQuery(ctx *mysql.QueryContext) {
- r.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+func (r *collationAllowlistOmniRule) checkCollation(collation string, line int) {
+ collation = strings.ToLower(collation)
+ if collation != "" && !r.allowList[collation] {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.DisabledCollation.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" used disabled collation '%s'", r.QueryText(), collation),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
}
-func (r *CollationAllowlistRule) checkCreateDatabase(ctx *mysql.CreateDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- for _, option := range ctx.AllCreateDatabaseOption() {
- if option != nil && option.DefaultCollation() != nil && option.DefaultCollation().CollationName() != nil {
- collation := mysqlparser.NormalizeMySQLCollationName(option.DefaultCollation().CollationName())
- r.checkCollation(collation, ctx.GetStart().GetLine())
+func (r *collationAllowlistOmniRule) checkCreateDatabase(n *ast.CreateDatabaseStmt) {
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "COLLATE") {
+ r.checkCollation(opt.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
}
}
}
-func (r *CollationAllowlistRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateTableOptions() != nil {
- for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {
- if option != nil && option.DefaultCollation() != nil && option.DefaultCollation().CollationName() != nil {
- collation := mysqlparser.NormalizeMySQLCollationName(option.DefaultCollation().CollationName())
- r.checkCollation(collation, option.GetStart().GetLine())
- }
+func (r *collationAllowlistOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ // Check table-level collation option.
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "COLLATE") {
+ r.checkCollation(opt.Value, r.BaseLine+int(r.LocToLine(opt.Loc)))
}
}
-
- if ctx.TableElementList() != nil {
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil || tableElement.ColumnDefinition() == nil {
- continue
- }
- columnDef := tableElement.ColumnDefinition()
- if columnDef.FieldDefinition() == nil {
- continue
- }
- if columnDef.FieldDefinition().AllColumnAttribute() == nil {
- continue
- }
- for _, attr := range columnDef.FieldDefinition().AllColumnAttribute() {
- if attr != nil && attr.Collate() != nil && attr.Collate().CollationName() != nil {
- collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())
- r.checkCollation(collation, columnDef.GetStart().GetLine())
- }
+ // Check column-level collation.
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ if col.TypeName.Collate != "" {
+ r.checkCollation(col.TypeName.Collate, r.BaseLine+int(r.LocToLine(col.Loc)))
+ }
+ // Also check column constraints for COLLATE.
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrCollate && c.Name != "" {
+ r.checkCollation(c.Name, r.BaseLine+int(r.LocToLine(col.Loc)))
}
}
}
}
-func (r *CollationAllowlistRule) checkAlterDatabase(ctx *mysql.AlterDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- for _, option := range ctx.AllAlterDatabaseOption() {
- if option == nil || option.CreateDatabaseOption() == nil || option.CreateDatabaseOption().DefaultCollation() == nil || option.CreateDatabaseOption().DefaultCollation().CollationName() == nil {
- continue
+func (r *collationAllowlistOmniRule) checkAlterDatabase(n *ast.AlterDatabaseStmt) {
+ for _, opt := range n.Options {
+ if strings.EqualFold(opt.Name, "COLLATE") {
+ r.checkCollation(opt.Value, r.BaseLine+int(r.LocToLine(n.Loc)))
}
- charset := mysqlparser.NormalizeMySQLCollationName(option.CreateDatabaseOption().DefaultCollation().CollationName())
- r.checkCollation(charset, ctx.GetStart().GetLine())
}
}
-func (r *CollationAllowlistRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil || item.FieldDefinition() == nil {
+func (r *collationAllowlistOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- for _, attr := range item.FieldDefinition().AllColumnAttribute() {
- if attr == nil || attr.Collate() == nil || attr.Collate().CollationName() == nil {
- continue
- }
- collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())
- r.checkCollation(collation, item.GetStart().GetLine())
- }
- }
- // alter table option
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {
- if option == nil {
- continue
- }
- for _, tableOption := range option.AllCreateTableOption() {
- if tableOption == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn, ast.ATModifyColumn, ast.ATChangeColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ if col.TypeName.Collate != "" {
+ r.checkCollation(col.TypeName.Collate, r.BaseLine+int(r.LocToLine(cmd.Loc)))
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrCollate && c.Name != "" {
+ r.checkCollation(c.Name, r.BaseLine+int(r.LocToLine(cmd.Loc)))
+ }
+ }
}
- if tableOption.DefaultCollation() == nil || tableOption.DefaultCollation().CollationName() == nil {
- continue
+ case ast.ATTableOption:
+ if cmd.Option != nil && strings.EqualFold(cmd.Option.Name, "COLLATE") {
+ r.checkCollation(cmd.Option.Value, r.BaseLine+int(r.LocToLine(cmd.Option.Loc)))
}
- collation := mysqlparser.NormalizeMySQLCollationName(tableOption.DefaultCollation().CollationName())
- r.checkCollation(collation, tableOption.GetStart().GetLine())
+ default:
}
}
}
-
-func (r *CollationAllowlistRule) checkCollation(collation string, lineNumber int) {
- collation = strings.ToLower(collation)
- if _, exists := r.allowList[collation]; collation != "" && !exists {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DisabledCollation.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" used disabled collation '%s'", r.text, collation),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
- })
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_column_auto_increment_initial_value.go b/backend/plugin/advisor/mysql/rule_column_auto_increment_initial_value.go
index 9bdccfa76c..38a6d6f791 100644
--- a/backend/plugin/advisor/mysql/rule_column_auto_increment_initial_value.go
+++ b/backend/plugin/advisor/mysql/rule_column_auto_increment_initial_value.go
@@ -4,19 +4,14 @@ import (
"context"
"fmt"
"strconv"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -45,144 +40,90 @@ func (*ColumnAutoIncrementInitialValueAdvisor) Check(_ context.Context, checkCtx
return nil, errors.New("number_payload is required for column auto increment initial value rule")
}
- // Create the rule
- rule := NewColumnAutoIncrementInitialValueRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnAutoIncrementInitialValueOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ value: int(numberPayload.Number),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// ColumnAutoIncrementInitialValueRule checks for auto-increment column initial value.
-type ColumnAutoIncrementInitialValueRule struct {
- BaseRule
+type columnAutoIncrementInitialValueOmniRule struct {
+ OmniBaseRule
value int
}
-// NewColumnAutoIncrementInitialValueRule creates a new ColumnAutoIncrementInitialValueRule.
-func NewColumnAutoIncrementInitialValueRule(level storepb.Advice_Status, title string, value int) *ColumnAutoIncrementInitialValueRule {
- return &ColumnAutoIncrementInitialValueRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- value: value,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnAutoIncrementInitialValueRule) Name() string {
+func (*columnAutoIncrementInitialValueOmniRule) Name() string {
return "ColumnAutoIncrementInitialValueRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnAutoIncrementInitialValueRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnAutoIncrementInitialValueOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnAutoIncrementInitialValueRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnAutoIncrementInitialValueRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *columnAutoIncrementInitialValueOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- if ctx.CreateTableOptions() == nil || ctx.TableName() == nil {
+ r.checkOptions(tableName, n.Options, r.LocToLine(n.Loc))
+}
+
+func (r *columnAutoIncrementInitialValueOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {
- if option.AUTO_INCREMENT_SYMBOL() == nil || option.Ulonglong_number() == nil {
- continue
+ for _, cmd := range n.Commands {
+ if cmd.Type == ast.ATTableOption && cmd.Option != nil && strings.EqualFold(cmd.Option.Name, "AUTO_INCREMENT") {
+ r.checkOptionValue(tableName, cmd.Option.Value, r.LocToLine(n.Loc))
}
+ }
+}
- base := 10
- bitSize := 0
- value, err := strconv.ParseUint(option.Ulonglong_number().GetText(), base, bitSize)
- if err != nil {
- continue
- }
- if value != uint64(r.value) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.AutoIncrementColumnInitialValueNotMatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", tableName, value, r.value),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+func (r *columnAutoIncrementInitialValueOmniRule) checkOptions(tableName string, opts []*ast.TableOption, line int32) {
+ for _, opt := range opts {
+ if strings.EqualFold(opt.Name, "AUTO_INCREMENT") {
+ r.checkOptionValue(tableName, opt.Value, line)
}
}
}
-// checkAlterTable is called when production alterTable is entered.
-func (r *ColumnAutoIncrementInitialValueRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
+func (r *columnAutoIncrementInitialValueOmniRule) checkOptionValue(tableName, valueStr string, line int32) {
+ value, err := strconv.ParseUint(valueStr, 10, 0)
+ if err != nil {
return
}
-
- // alter table option.
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {
- if option == nil {
- continue
- }
- for _, tableOption := range option.AllCreateTableOption() {
- if tableOption == nil || tableOption.AUTO_INCREMENT_SYMBOL() == nil || tableOption.Ulonglong_number() == nil {
- continue
- }
-
- base := 10
- bitSize := 0
- value, err := strconv.ParseUint(tableOption.Ulonglong_number().GetText(), base, bitSize)
- if err != nil {
- continue
- }
- if value != uint64(r.value) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.AutoIncrementColumnInitialValueNotMatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", tableName, value, r.value),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
- }
+ if value != uint64(r.value) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.AutoIncrementColumnInitialValueNotMatch.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", tableName, value, r.value),
+ StartPosition: &storepb.Position{
+ Line: line,
+ },
+ })
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_auto_increment_must_integer.go b/backend/plugin/advisor/mysql/rule_column_auto_increment_must_integer.go
index de3fbc979a..49ea3c1746 100644
--- a/backend/plugin/advisor/mysql/rule_column_auto_increment_must_integer.go
+++ b/backend/plugin/advisor/mysql/rule_column_auto_increment_must_integer.go
@@ -4,17 +4,11 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -39,175 +33,81 @@ func (*ColumnAutoIncrementMustIntegerAdvisor) Check(_ context.Context, checkCtx
return nil, err
}
- // Create the rule
- rule := NewColumnAutoIncrementMustIntegerRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnAutoIncrementMustIntegerOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// ColumnAutoIncrementMustIntegerRule checks for auto-increment column type.
-type ColumnAutoIncrementMustIntegerRule struct {
- BaseRule
+type columnAutoIncrementMustIntegerOmniRule struct {
+ OmniBaseRule
}
-// NewColumnAutoIncrementMustIntegerRule creates a new ColumnAutoIncrementMustIntegerRule.
-func NewColumnAutoIncrementMustIntegerRule(level storepb.Advice_Status, title string) *ColumnAutoIncrementMustIntegerRule {
- return &ColumnAutoIncrementMustIntegerRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*ColumnAutoIncrementMustIntegerRule) Name() string {
+func (*columnAutoIncrementMustIntegerOmniRule) Name() string {
return "ColumnAutoIncrementMustIntegerRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnAutoIncrementMustIntegerRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnAutoIncrementMustIntegerOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Ignore other node types
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnAutoIncrementMustIntegerRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
}
-func (r *ColumnAutoIncrementMustIntegerRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
+func (r *columnAutoIncrementMustIntegerOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ for _, col := range n.Columns {
+ r.checkColumn(tableName, col)
}
}
-func (r *ColumnAutoIncrementMustIntegerRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *columnAutoIncrementMustIntegerOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnName string
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
- // Ignore other ADD column syntax variations
- }
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
- columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- default:
- // Ignore other alter table actions
+ for _, cmd := range n.Commands {
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ r.checkColumn(tableName, col)
}
}
}
-func (r *ColumnAutoIncrementMustIntegerRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if !r.isAutoIncrementColumnIsInteger(ctx) {
+func (r *columnAutoIncrementMustIntegerOmniRule) checkColumn(tableName string, col *ast.ColumnDef) {
+ if !omniIsAutoIncrement(col) {
+ return
+ }
+ if !omniIsIntegerType(col.TypeName) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.AutoIncrementColumnNotInteger.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Auto-increment column `%s`.`%s` requires integer type", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.AutoIncrementColumnNotInteger.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Auto-increment column `%s`.`%s` requires integer type", tableName, col.Name),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
-
-func (r *ColumnAutoIncrementMustIntegerRule) isAutoIncrementColumnIsInteger(ctx mysql.IFieldDefinitionContext) bool {
- if r.isAutoIncrementColumn(ctx) && !r.isIntegerType(ctx.DataType()) {
- return false
- }
- return true
-}
-
-func (*ColumnAutoIncrementMustIntegerRule) isAutoIncrementColumn(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.AUTO_INCREMENT_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnAutoIncrementMustIntegerRule) isIntegerType(ctx mysql.IDataTypeContext) bool {
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINT_SYMBOL, mysql.MySQLParserTINYINT_SYMBOL, mysql.MySQLParserSMALLINT_SYMBOL, mysql.MySQLParserMEDIUMINT_SYMBOL, mysql.MySQLParserBIGINT_SYMBOL:
- return true
- default:
- return false
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_column_auto_increment_must_unsigned.go b/backend/plugin/advisor/mysql/rule_column_auto_increment_must_unsigned.go
index 9be5773918..6029ca06ab 100644
--- a/backend/plugin/advisor/mysql/rule_column_auto_increment_must_unsigned.go
+++ b/backend/plugin/advisor/mysql/rule_column_auto_increment_must_unsigned.go
@@ -3,18 +3,12 @@ package mysql
import (
"context"
"fmt"
- "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -39,172 +33,81 @@ func (*ColumnAutoIncrementMustUnsignedAdvisor) Check(_ context.Context, checkCtx
return nil, err
}
- // Create the rule
- rule := NewColumnAutoIncrementMustUnsignedRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnAutoIncrementMustUnsignedOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
-}
-
-// ColumnAutoIncrementMustUnsignedRule checks for unsigned auto-increment column.
-type ColumnAutoIncrementMustUnsignedRule struct {
- BaseRule
+ return rule.GetAdviceList(), nil
}
-// NewColumnAutoIncrementMustUnsignedRule creates a new ColumnAutoIncrementMustUnsignedRule.
-func NewColumnAutoIncrementMustUnsignedRule(level storepb.Advice_Status, title string) *ColumnAutoIncrementMustUnsignedRule {
- return &ColumnAutoIncrementMustUnsignedRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type columnAutoIncrementMustUnsignedOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*ColumnAutoIncrementMustUnsignedRule) Name() string {
+func (*columnAutoIncrementMustUnsignedOmniRule) Name() string {
return "ColumnAutoIncrementMustUnsignedRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnAutoIncrementMustUnsignedRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnAutoIncrementMustUnsignedOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnAutoIncrementMustUnsignedRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnAutoIncrementMustUnsignedRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
+func (r *columnAutoIncrementMustUnsignedOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ for _, col := range n.Columns {
+ r.checkColumn(tableName, col)
}
}
-func (r *ColumnAutoIncrementMustUnsignedRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *columnAutoIncrementMustUnsignedOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
-
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnName string
- switch {
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
- }
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
- columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- default:
+ for _, cmd := range n.Commands {
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ r.checkColumn(tableName, col)
}
}
}
-func (r *ColumnAutoIncrementMustUnsignedRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if !r.isAutoIncrementColumnIsUnsigned(ctx) {
+func (r *columnAutoIncrementMustUnsignedOmniRule) checkColumn(tableName string, col *ast.ColumnDef) {
+ if !omniIsAutoIncrement(col) {
+ return
+ }
+ if col.TypeName == nil || (!col.TypeName.Unsigned && !col.TypeName.Zerofill) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.AutoIncrementColumnSigned.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Auto-increment column `%s`.`%s` is not UNSIGNED type", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.AutoIncrementColumnSigned.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Auto-increment column `%s`.`%s` is not UNSIGNED type", tableName, col.Name),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
-
-func (r *ColumnAutoIncrementMustUnsignedRule) isAutoIncrementColumnIsUnsigned(ctx mysql.IFieldDefinitionContext) bool {
- if r.isAutoIncrementColumn(ctx) && !r.isUnsigned(ctx) {
- return false
- }
- return true
-}
-
-func (*ColumnAutoIncrementMustUnsignedRule) isAutoIncrementColumn(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.AUTO_INCREMENT_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnAutoIncrementMustUnsignedRule) isUnsigned(ctx mysql.IFieldDefinitionContext) bool {
- if ctx.DataType() == nil {
- return false
- }
-
- // Check if UNSIGNED is specified in the data type
- dataTypeText := ctx.DataType().GetParser().GetTokenStream().GetTextFromRuleContext(ctx.DataType())
- upperText := strings.ToUpper(dataTypeText)
-
- // UNSIGNED is explicitly specified or ZEROFILL (which implies UNSIGNED)
- return strings.Contains(upperText, "UNSIGNED") || strings.Contains(upperText, "ZEROFILL")
-}
diff --git a/backend/plugin/advisor/mysql/rule_column_comment_convention.go b/backend/plugin/advisor/mysql/rule_column_comment_convention.go
index a4094263ee..d0fa1f4f69 100644
--- a/backend/plugin/advisor/mysql/rule_column_comment_convention.go
+++ b/backend/plugin/advisor/mysql/rule_column_comment_convention.go
@@ -4,15 +4,11 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -37,199 +33,97 @@ func (*ColumnCommentConventionAdvisor) Check(_ context.Context, checkCtx advisor
}
commentPayload := checkCtx.Rule.GetCommentConventionPayload()
- // Create the rule
- rule := NewColumnCommentConventionRule(level, checkCtx.Rule.Type.String(), commentPayload)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnCommentConventionOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ payload: commentPayload,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// ColumnCommentConventionRule checks for column comment convention.
-type ColumnCommentConventionRule struct {
- BaseRule
+type columnCommentConventionOmniRule struct {
+ OmniBaseRule
payload *storepb.SQLReviewRule_CommentConventionRulePayload
}
-// NewColumnCommentConventionRule creates a new ColumnCommentConventionRule.
-func NewColumnCommentConventionRule(level storepb.Advice_Status, title string, payload *storepb.SQLReviewRule_CommentConventionRulePayload) *ColumnCommentConventionRule {
- return &ColumnCommentConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- payload: payload,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnCommentConventionRule) Name() string {
+func (*columnCommentConventionOmniRule) Name() string {
return "ColumnCommentConventionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnCommentConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnCommentConventionOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnCommentConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *ColumnCommentConventionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *columnCommentConventionOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.ColumnDefinition() == nil {
- continue
- }
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ for _, col := range n.Columns {
+ r.checkColumn(tableName, col)
}
}
-func (r *ColumnCommentConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnCommentConventionOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnName string
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
+ for _, cmd := range n.Commands {
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
+ }
+ for _, col := range cmd.Columns {
+ r.checkColumn(tableName, col)
+ }
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
}
- // modify column
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
default:
}
}
}
-func (r *ColumnCommentConventionRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- comment := ""
-
- // Check columnAttribute for regular columns.
- for _, attribute := range ctx.AllColumnAttribute() {
- if attribute == nil || attribute.GetValue() == nil {
- continue
- }
- if attribute.GetValue().GetTokenType() != mysql.MySQLParserCOMMENT_SYMBOL {
- continue
- }
- if attribute.TextLiteral() == nil {
- continue
- }
- comment = mysqlparser.NormalizeMySQLTextLiteral(attribute.TextLiteral())
- break
- }
-
- // Check gcolAttribute for generated/virtual columns.
- // Generated columns use gcolAttribute which has a different structure.
- if comment == "" {
- for _, gcolAttr := range ctx.AllGcolAttribute() {
- if gcolAttr == nil || gcolAttr.COMMENT_SYMBOL() == nil {
- continue
- }
- if gcolAttr.TextString() == nil || gcolAttr.TextString().TextStringLiteral() == nil {
- continue
- }
- comment = mysqlparser.NormalizeMySQLTextStringLiteral(gcolAttr.TextString().TextStringLiteral())
- break
- }
- }
+func (r *columnCommentConventionOmniRule) checkColumn(tableName string, col *ast.ColumnDef) {
+ comment := omniColumnComment(col)
- // Validate comment length.
if comment != "" && r.payload.MaxLength >= 0 && int32(len(comment)) > r.payload.MaxLength {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CommentTooLong.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of column `%s`.`%s` comment should be within %d characters", tableName, columnName, r.payload.MaxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.CommentTooLong.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The length of column `%s`.`%s` comment should be within %d characters", tableName, col.Name, r.payload.MaxLength),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ Column: 0,
+ },
})
}
- // Check if comment is required but missing.
if comment == "" && r.payload.Required {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CommentEmpty.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column `%s`.`%s` requires comments", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.CommentEmpty.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column `%s`.`%s` requires comments", tableName, col.Name),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ Column: 0,
+ },
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_current_time_count_limit.go b/backend/plugin/advisor/mysql/rule_column_current_time_count_limit.go
index 6dbaebd45c..d9c3549e6f 100644
--- a/backend/plugin/advisor/mysql/rule_column_current_time_count_limit.go
+++ b/backend/plugin/advisor/mysql/rule_column_current_time_count_limit.go
@@ -5,16 +5,11 @@ import (
"fmt"
"slices"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -44,29 +39,29 @@ func (*ColumnCurrentTimeCountLimitAdvisor) Check(_ context.Context, checkCtx adv
return nil, err
}
- // Create the rule
- rule := NewColumnCurrentTimeCountLimitRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnCurrentTimeCountLimitOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ tableSet: make(map[string]currentTimeTableData),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- // Generate advice after walking
rule.generateAdvice()
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
type currentTimeTableData struct {
@@ -76,185 +71,75 @@ type currentTimeTableData struct {
line int
}
-// ColumnCurrentTimeCountLimitRule checks for current time column count limit.
-type ColumnCurrentTimeCountLimitRule struct {
- BaseRule
+type columnCurrentTimeCountLimitOmniRule struct {
+ OmniBaseRule
tableSet map[string]currentTimeTableData
}
-// NewColumnCurrentTimeCountLimitRule creates a new ColumnCurrentTimeCountLimitRule.
-func NewColumnCurrentTimeCountLimitRule(level storepb.Advice_Status, title string) *ColumnCurrentTimeCountLimitRule {
- return &ColumnCurrentTimeCountLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- tableSet: make(map[string]currentTimeTableData),
- }
-}
-
-// Name returns the rule name.
-func (*ColumnCurrentTimeCountLimitRule) Name() string {
+func (*columnCurrentTimeCountLimitOmniRule) Name() string {
return "ColumnCurrentTimeCountLimitRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnCurrentTimeCountLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnCurrentTimeCountLimitOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnCurrentTimeCountLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnCurrentTimeCountLimitRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
+func (r *columnCurrentTimeCountLimitOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
+ for _, col := range n.Columns {
+ if col.TypeName == nil || !omniIsTimeType(col.TypeName) {
continue
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkTime(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ r.checkTimeColumn(tableName, col)
}
}
-func (r *ColumnCurrentTimeCountLimitRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *columnCurrentTimeCountLimitOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnName string
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkTime(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkTime(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
- }
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
- // only focus on new column name.
- columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkTime(tableName, columnName, item.FieldDefinition())
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
+ for _, cmd := range n.Commands {
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col.TypeName == nil || !omniIsTimeType(col.TypeName) {
continue
}
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkTime(tableName, columnName, item.FieldDefinition())
- default:
- continue
- }
- }
-}
-
-func (r *ColumnCurrentTimeCountLimitRule) checkTime(tableName string, _ string, ctx mysql.IFieldDefinitionContext) {
- if ctx.DataType() == nil {
- return
- }
-
- switch ctx.DataType().GetType_().GetTokenType() {
- case mysql.MySQLParserDATETIME_SYMBOL, mysql.MySQLParserTIMESTAMP_SYMBOL:
- if r.isDefaultCurrentTime(ctx) {
- table, exists := r.tableSet[tableName]
- if !exists {
- table = currentTimeTableData{
- tableName: tableName,
- }
- }
- table.defaultCurrentTimeCount++
- table.line = r.baseLine + ctx.GetStart().GetLine()
- r.tableSet[tableName] = table
+ r.checkTimeColumn(tableName, col)
}
- if r.isOnUpdateCurrentTime(ctx) {
- table, exists := r.tableSet[tableName]
- if !exists {
- table = currentTimeTableData{
- tableName: tableName,
- }
- }
- table.onUpdateCurrentTimeCount++
- table.line = r.baseLine + ctx.GetStart().GetLine()
- r.tableSet[tableName] = table
- }
- default:
}
}
-func (*ColumnCurrentTimeCountLimitRule) isDefaultCurrentTime(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr == nil || attr.GetValue() == nil {
- continue
- }
- if attr.GetValue().GetTokenType() == mysql.MySQLParserDEFAULT_SYMBOL && attr.NOW_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnCurrentTimeCountLimitRule) isOnUpdateCurrentTime(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr == nil || attr.GetValue() == nil {
- continue
+func (r *columnCurrentTimeCountLimitOmniRule) checkTimeColumn(tableName string, col *ast.ColumnDef) {
+ if omniIsDefaultCurrentTime(col) {
+ table, exists := r.tableSet[tableName]
+ if !exists {
+ table = currentTimeTableData{tableName: tableName}
}
- if attr.GetValue().GetTokenType() == mysql.MySQLParserON_SYMBOL && attr.NOW_SYMBOL() != nil {
- return true
+ table.defaultCurrentTimeCount++
+ table.line = r.BaseLine + int(r.LocToLine(col.Loc))
+ r.tableSet[tableName] = table
+ }
+ if omniIsOnUpdateCurrentTime(col) {
+ table, exists := r.tableSet[tableName]
+ if !exists {
+ table = currentTimeTableData{tableName: tableName}
}
+ table.onUpdateCurrentTimeCount++
+ table.line = r.BaseLine + int(r.LocToLine(col.Loc))
+ r.tableSet[tableName] = table
}
- return false
}
-func (r *ColumnCurrentTimeCountLimitRule) generateAdvice() {
+func (r *columnCurrentTimeCountLimitOmniRule) generateAdvice() {
var tableList []currentTimeTableData
for _, table := range r.tableSet {
tableList = append(tableList, table)
@@ -270,21 +155,25 @@ func (r *ColumnCurrentTimeCountLimitRule) generateAdvice() {
})
for _, table := range tableList {
if table.defaultCurrentTimeCount > maxDefaultCurrentTimeColumCount {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DefaultCurrentTimeColumnCountExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table `%s` has %d DEFAULT CURRENT_TIMESTAMP() columns. The count greater than %d.", table.tableName, table.defaultCurrentTimeCount, maxDefaultCurrentTimeColumCount),
- StartPosition: common.ConvertANTLRLineToPosition(table.line),
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.DefaultCurrentTimeColumnCountExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table `%s` has %d DEFAULT CURRENT_TIMESTAMP() columns. The count greater than %d.", table.tableName, table.defaultCurrentTimeCount, maxDefaultCurrentTimeColumCount),
+ StartPosition: &storepb.Position{
+ Line: int32(table.line),
+ },
})
}
if table.onUpdateCurrentTimeCount > maxOnUpdateCurrentTimeColumnCount {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.OnUpdateCurrentTimeColumnCountExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table `%s` has %d ON UPDATE CURRENT_TIMESTAMP() columns. The count greater than %d.", table.tableName, table.onUpdateCurrentTimeCount, maxOnUpdateCurrentTimeColumnCount),
- StartPosition: common.ConvertANTLRLineToPosition(table.line),
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.OnUpdateCurrentTimeColumnCountExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table `%s` has %d ON UPDATE CURRENT_TIMESTAMP() columns. The count greater than %d.", table.tableName, table.onUpdateCurrentTimeCount, maxOnUpdateCurrentTimeColumnCount),
+ StartPosition: &storepb.Position{
+ Line: int32(table.line),
+ },
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_changing.go b/backend/plugin/advisor/mysql/rule_column_disallow_changing.go
index 2eb120fbcb..60194a4f07 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing.go
@@ -4,17 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -38,95 +33,41 @@ func (*ColumnDisallowChangingAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowChangingRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// ColumnDisallowChangingRule checks for disallow CHANGE COLUMN statement.
-type ColumnDisallowChangingRule struct {
- BaseRule
- text string
-}
-
-// NewColumnDisallowChangingRule creates a new ColumnDisallowChangingRule.
-func NewColumnDisallowChangingRule(level storepb.Advice_Status, title string) *ColumnDisallowChangingRule {
- return &ColumnDisallowChangingRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &columnDisallowChangingOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*ColumnDisallowChangingRule) Name() string {
- return "ColumnDisallowChangingRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowChangingRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- default:
- }
- return nil
+type columnDisallowChangingOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowChangingRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*columnDisallowChangingOmniRule) Name() string {
+ return "ColumnDisallowChangingRule"
}
-func (r *ColumnDisallowChangingRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowChangingOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.AlterTableStmt)
+ if !ok {
return
}
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- // change column
- if item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil {
+ if cmd.Type == ast.ATChangeColumn {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.UseChangeColumnStatement.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" contains CHANGE COLUMN statement", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" contains CHANGE COLUMN statement", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_changing_order.go b/backend/plugin/advisor/mysql/rule_column_disallow_changing_order.go
index 512f740099..a6cbfbd80a 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing_order.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing_order.go
@@ -4,17 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -38,106 +33,47 @@ func (*ColumnDisallowChangingOrderAdvisor) Check(_ context.Context, checkCtx adv
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowChangingOrderRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// ColumnDisallowChangingOrderRule checks for disallow changing column order.
-type ColumnDisallowChangingOrderRule struct {
- BaseRule
- text string
-}
-
-// NewColumnDisallowChangingOrderRule creates a new ColumnDisallowChangingOrderRule.
-func NewColumnDisallowChangingOrderRule(level storepb.Advice_Status, title string) *ColumnDisallowChangingOrderRule {
- return &ColumnDisallowChangingOrderRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &columnDisallowChangingOrderOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*ColumnDisallowChangingOrderRule) Name() string {
- return "ColumnDisallowChangingOrderRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowChangingOrderRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- default:
- }
- return nil
+type columnDisallowChangingOrderOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowChangingOrderRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*columnDisallowChangingOrderOmniRule) Name() string {
+ return "ColumnDisallowChangingOrderRule"
}
-func (r *ColumnDisallowChangingOrderRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowChangingOrderOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.AlterTableStmt)
+ if !ok {
return
}
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil:
- // do nothing.
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- // do nothing.
+ switch cmd.Type {
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ // Check if FIRST or AFTER is specified (column reordering).
+ if cmd.First || cmd.After != "" {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.ChangeColumnOrder.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" changes column order", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
+ })
+ }
default:
- continue
- }
-
- if item.Place() != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.ChangeColumnOrder.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" changes column order", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
index 8b8d3cf14b..cb2c05bf57 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
@@ -5,17 +5,12 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -40,106 +35,58 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advi
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowChangingTypeRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnDisallowChangingTypeOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ originalMetadata: checkCtx.OriginalMetadata,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// ColumnDisallowChangingTypeRule checks for disallow changing column type.
-type ColumnDisallowChangingTypeRule struct {
- BaseRule
- text string
+type columnDisallowChangingTypeOmniRule struct {
+ OmniBaseRule
originalMetadata *model.DatabaseMetadata
}
-// NewColumnDisallowChangingTypeRule creates a new ColumnDisallowChangingTypeRule.
-func NewColumnDisallowChangingTypeRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *ColumnDisallowChangingTypeRule {
- return &ColumnDisallowChangingTypeRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- originalMetadata: originalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnDisallowChangingTypeRule) Name() string {
+func (*columnDisallowChangingTypeOmniRule) Name() string {
return "ColumnDisallowChangingTypeRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowChangingTypeRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowChangingTypeRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnDisallowChangingTypeRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowChangingTypeOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.AlterTableStmt)
+ if !ok || n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
var columnName string
- switch {
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- // only focus on old colunn-name.
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- // MODIFY COLUMN
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ switch cmd.Type {
+ case ast.ATChangeColumn:
+ // For CHANGE, Name is the old column name.
+ columnName = cmd.Name
+ case ast.ATModifyColumn:
+ // For MODIFY, the column name comes from the column def or Name.
+ if cmd.Column != nil {
+ columnName = cmd.Column.Name
+ } else {
+ columnName = cmd.Name
+ }
default:
continue
}
- r.changeColumnType(tableName, columnName, item.FieldDefinition().DataType())
+
+ if cmd.Column == nil || cmd.Column.TypeName == nil {
+ continue
+ }
+ r.checkColumnType(tableName, columnName, cmd.Column.TypeName, cmd.Loc)
}
}
@@ -170,20 +117,30 @@ func normalizeColumnType(tp string) string {
}
}
-func (r *ColumnDisallowChangingTypeRule) changeColumnType(tableName, columnName string, dataType mysql.IDataTypeContext) {
- tp := dataType.GetParser().GetTokenStream().GetTextFromRuleContext(dataType)
+func (r *columnDisallowChangingTypeOmniRule) checkColumnType(tableName, columnName string, dt *ast.DataType, loc ast.Loc) {
column := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column == nil {
return
}
+ // Build the type string from the omni DataType.
+ tp := strings.ToLower(dt.Name)
+ if dt.Length > 0 && dt.Scale > 0 {
+ tp = fmt.Sprintf("%s(%d,%d)", tp, dt.Length, dt.Scale)
+ } else if dt.Length > 0 {
+ tp = fmt.Sprintf("%s(%d)", tp, dt.Length)
+ }
+ if dt.Unsigned {
+ tp += " unsigned"
+ }
+
if normalizeColumnType(column.GetProto().Type) != normalizeColumnType(tp) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.ChangeColumnType.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" changes column type", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + dataType.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" changes column type", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_drop.go b/backend/plugin/advisor/mysql/rule_column_disallow_drop.go
index 77b88ace27..bde74c9ca8 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_drop.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_drop.go
@@ -4,17 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -36,85 +31,41 @@ func (*ColumnDisallowDropAdvisor) Check(_ context.Context, checkCtx advisor.Cont
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowDropRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// ColumnDisallowDropRule checks for disallow DROP COLUMN statement.
-type ColumnDisallowDropRule struct {
- BaseRule
-}
-
-// NewColumnDisallowDropRule creates a new ColumnDisallowDropRule.
-func NewColumnDisallowDropRule(level storepb.Advice_Status, title string) *ColumnDisallowDropRule {
- return &ColumnDisallowDropRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &columnDisallowDropOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*ColumnDisallowDropRule) Name() string {
- return "ColumnDisallowDropRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowDropRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeAlterTable {
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- }
- return nil
+type columnDisallowDropOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowDropRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*columnDisallowDropOmniRule) Name() string {
+ return "ColumnDisallowDropRule"
}
-func (r *ColumnDisallowDropRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowDropOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.AlterTableStmt)
+ if !ok || n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil || item.DROP_SYMBOL() == nil || item.ColumnInternalRef() == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil || cmd.Type != ast.ATDropColumn {
continue
}
-
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.DropColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("drops column \"%s\" of table \"%s\"", columnName, tableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + item.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("drops column \"%s\" of table \"%s\"", cmd.Name, tableName),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go b/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
index 3c8e0d61e7..35cfca6c0a 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -38,128 +34,72 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowDropInIndexRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnDisallowDropInIndexOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ tables: make(tableState),
+ originalMetadata: checkCtx.OriginalMetadata,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// ColumnDisallowDropInIndexRule checks for disallow DROP COLUMN in index.
-type ColumnDisallowDropInIndexRule struct {
- BaseRule
+type columnDisallowDropInIndexOmniRule struct {
+ OmniBaseRule
tables tableState
originalMetadata *model.DatabaseMetadata
}
-// NewColumnDisallowDropInIndexRule creates a new ColumnDisallowDropInIndexRule.
-func NewColumnDisallowDropInIndexRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *ColumnDisallowDropInIndexRule {
- return &ColumnDisallowDropInIndexRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- tables: make(tableState),
- originalMetadata: originalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnDisallowDropInIndexRule) Name() string {
+func (*columnDisallowDropInIndexOmniRule) Name() string {
return "ColumnDisallowDropInIndexRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowDropInIndexRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnDisallowDropInIndexOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowDropInIndexRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *ColumnDisallowDropInIndexRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *columnDisallowDropInIndexOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
+ tableName := n.Table.Name
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil || tableElement.TableConstraintDef() == nil {
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- if tableElement.TableConstraintDef().GetType_() == nil {
+ if constraint.Type != ast.ConstrIndex {
continue
}
- switch tableElement.TableConstraintDef().GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL:
- // do nothing.
- default:
- continue
- }
- if tableElement.TableConstraintDef().KeyListVariants() == nil {
- continue
- }
-
- columnList := mysqlparser.NormalizeKeyListVariants(tableElement.TableConstraintDef().KeyListVariants())
- for _, column := range columnList {
+ for _, col := range constraint.Columns {
if r.tables[tableName] == nil {
r.tables[tableName] = make(columnSet)
}
- r.tables[tableName][column] = true
+ r.tables[tableName][col] = true
}
}
}
-func (r *ColumnDisallowDropInIndexRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowDropInIndexOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
+ tableName := n.Table.Name
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil || item.DROP_SYMBOL() == nil || item.ColumnInternalRef() == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil || cmd.Type != ast.ATDropColumn {
continue
}
+ // Load index columns from metadata.
table := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName)
if table != nil {
if r.tables[tableName] == nil {
@@ -172,22 +112,15 @@ func (r *ColumnDisallowDropInIndexRule) checkAlterTable(ctx *mysql.AlterTableCon
}
}
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- if !r.canDrop(tableName, columnName) {
+ columnName := cmd.Name
+ if _, exists := r.tables[tableName][columnName]; exists {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.DropIndexColumn.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` cannot drop index column", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + item.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
})
}
}
}
-
-func (r *ColumnDisallowDropInIndexRule) canDrop(table string, column string) bool {
- if _, ok := r.tables[table][column]; ok {
- return false
- }
- return true
-}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_set_charset.go b/backend/plugin/advisor/mysql/rule_column_disallow_set_charset.go
index 18864e8907..f9ecb238c6 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_set_charset.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_set_charset.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -37,195 +33,103 @@ func (*ColumnDisallowSetCharsetAdvisor) Check(_ context.Context, checkCtx adviso
return nil, err
}
- // Create the rule
- rule := NewColumnDisallowSetCharsetRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnDisallowSetCharsetOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// ColumnDisallowSetCharsetRule checks for disallow set column charset.
-type ColumnDisallowSetCharsetRule struct {
- BaseRule
- text string
+type columnDisallowSetCharsetOmniRule struct {
+ OmniBaseRule
}
-// NewColumnDisallowSetCharsetRule creates a new ColumnDisallowSetCharsetRule.
-func NewColumnDisallowSetCharsetRule(level storepb.Advice_Status, title string) *ColumnDisallowSetCharsetRule {
- return &ColumnDisallowSetCharsetRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*ColumnDisallowSetCharsetRule) Name() string {
+func (*columnDisallowSetCharsetOmniRule) Name() string {
return "ColumnDisallowSetCharsetRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnDisallowSetCharsetRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- r.checkQuery(ctx.(*mysql.QueryContext))
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnDisallowSetCharsetOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Other node types
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnDisallowSetCharsetRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *ColumnDisallowSetCharsetRule) checkQuery(ctx *mysql.QueryContext) {
- r.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
-}
-
-func (r *ColumnDisallowSetCharsetRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *columnDisallowSetCharsetOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
- return
- }
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
- charset := r.getCharSet(tableElement.ColumnDefinition().FieldDefinition().DataType())
- if !r.checkCharset(charset) {
+ if !checkCharsetAllowed(col.TypeName.Charset) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.SetColumnCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Disallow set column charset but \"%s\" does", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Disallow set column charset but \"%s\" does", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
}
-func (r *ColumnDisallowSetCharsetRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnDisallowSetCharsetOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
- }
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
var charsetList []string
- switch {
- // add column.
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
-
- charsetName := r.getCharSet(item.FieldDefinition().DataType())
- charsetList = append(charsetList, charsetName)
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
-
- charsetName := r.getCharSet(tableElement.ColumnDefinition().FieldDefinition().DataType())
- charsetList = append(charsetList, charsetName)
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ charsetList = append(charsetList, cmd.Column.TypeName.Charset)
+ }
+ for _, col := range cmd.Columns {
+ if col != nil && col.TypeName != nil {
+ charsetList = append(charsetList, col.TypeName.Charset)
}
- default:
- // Other add column formats
}
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- charsetName := r.getCharSet(item.FieldDefinition().DataType())
- charsetList = append(charsetList, charsetName)
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- charsetName := r.getCharSet(item.FieldDefinition().DataType())
- charsetList = append(charsetList, charsetName)
+ case ast.ATChangeColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ charsetList = append(charsetList, cmd.Column.TypeName.Charset)
+ }
+ case ast.ATModifyColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ charsetList = append(charsetList, cmd.Column.TypeName.Charset)
+ }
default:
continue
}
- for _, charsetName := range charsetList {
- if !r.checkCharset(charsetName) {
+ for _, charset := range charsetList {
+ if !checkCharsetAllowed(charset) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.SetColumnCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Disallow set column charset but \"%s\" does", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Disallow set column charset but \"%s\" does", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
}
}
-func (*ColumnDisallowSetCharsetRule) getCharSet(ctx mysql.IDataTypeContext) string {
- if ctx.CharsetWithOptBinary() == nil {
- return ""
- }
- charset := mysqlparser.NormalizeMySQLCharsetName(ctx.CharsetWithOptBinary().CharsetName())
- return charset
-}
-
-func (*ColumnDisallowSetCharsetRule) checkCharset(charset string) bool {
+func checkCharsetAllowed(charset string) bool {
switch charset {
- // empty charset or binary for JSON.
case "", "binary":
return true
default:
diff --git a/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go b/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
index a95715ca22..d0fec6b20b 100644
--- a/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
+++ b/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
@@ -3,19 +3,14 @@ package mysql
import (
"context"
"fmt"
- "strconv"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -44,208 +39,103 @@ func (*ColumnMaximumCharacterLengthAdvisor) Check(_ context.Context, checkCtx ad
return nil, errors.New("number_payload is required for column maximum character length rule")
}
- // Create the rule
- rule := NewColumnMaximumCharacterLengthRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnMaximumCharacterLengthOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ maximum: int(numberPayload.Number),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// ColumnMaximumCharacterLengthRule checks for maximum character length.
-type ColumnMaximumCharacterLengthRule struct {
- BaseRule
+type columnMaximumCharacterLengthOmniRule struct {
+ OmniBaseRule
maximum int
}
-// NewColumnMaximumCharacterLengthRule creates a new ColumnMaximumCharacterLengthRule.
-func NewColumnMaximumCharacterLengthRule(level storepb.Advice_Status, title string, maximum int) *ColumnMaximumCharacterLengthRule {
- return &ColumnMaximumCharacterLengthRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maximum: maximum,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnMaximumCharacterLengthRule) Name() string {
+func (*columnMaximumCharacterLengthOmniRule) Name() string {
return "ColumnMaximumCharacterLengthRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnMaximumCharacterLengthRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnMaximumCharacterLengthOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnMaximumCharacterLengthRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnMaximumCharacterLengthRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+func (r *columnMaximumCharacterLengthOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- charLength := r.getCharLength(tableElement.ColumnDefinition().FieldDefinition().DataType())
+ for _, col := range n.Columns {
+ charLength := getCharLengthFromOmni(col.TypeName)
if r.maximum > 0 && charLength > r.maximum {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CharLengthExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of the CHAR column `%s.%s` is bigger than %d, please use VARCHAR instead", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.CharLengthExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The length of the CHAR column `%s.%s` is bigger than %d, please use VARCHAR instead", tableName, col.Name, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
}
-func (r *ColumnMaximumCharacterLengthRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *columnMaximumCharacterLengthOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnList []string
- charLengthMap := make(map[string]int)
- switch {
- // add column.
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
-
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- charLength := r.getCharLength(item.FieldDefinition().DataType())
- charLengthMap[columnName] = charLength
- columnList = append(columnList, columnName)
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- charLength := r.getCharLength(tableElement.ColumnDefinition().FieldDefinition().DataType())
- charLengthMap[columnName] = charLength
- columnList = append(columnList, columnName)
- }
- default:
- }
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- // oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- charLength := r.getCharLength(item.FieldDefinition().DataType())
- charLengthMap[columnName] = charLength
- columnList = append(columnList, columnName)
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- charLength := r.getCharLength(item.FieldDefinition().DataType())
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- charLengthMap[columnName] = charLength
- columnList = append(columnList, columnName)
- default:
- continue
- }
- for _, columnName := range columnList {
- if charLength, ok := charLengthMap[columnName]; ok && r.maximum > 0 && charLength > r.maximum {
+ for _, cmd := range n.Commands {
+ cols := omniGetColumnsFromCmd(cmd)
+ for _, col := range cols {
+ charLength := getCharLengthFromOmni(col.TypeName)
+ if r.maximum > 0 && charLength > r.maximum {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CharLengthExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of the CHAR column `%s.%s` is bigger than %d, please use VARCHAR instead", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.CharLengthExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The length of the CHAR column `%s.%s` is bigger than %d, please use VARCHAR instead", tableName, col.Name, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
}
}
-func (*ColumnMaximumCharacterLengthRule) getCharLength(ctx mysql.IDataTypeContext) int {
- if ctx.GetType_() == nil {
+func getCharLengthFromOmni(dt *ast.DataType) int {
+ if dt == nil {
return 0
}
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserCHAR_SYMBOL:
- // for mysql: create table tt(a char) == create table tt(a char(1));
- if ctx.FieldLength() == nil || ctx.FieldLength().Real_ulonglong_number() == nil {
- return 1
- }
- charLengthStr := ctx.FieldLength().Real_ulonglong_number().GetText()
- charLengthInt, err := strconv.Atoi(charLengthStr)
- if err != nil {
- return 0
- }
- return charLengthInt
- default:
+ if !strings.EqualFold(dt.Name, "CHAR") {
return 0
}
+ // MySQL default: CHAR without length = CHAR(1)
+ if dt.Length == 0 {
+ return 1
+ }
+ return dt.Length
}
diff --git a/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go b/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
index 2314571ec6..ddd5b0a55c 100644
--- a/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
+++ b/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
@@ -3,18 +3,14 @@ package mysql
import (
"context"
"fmt"
- "strconv"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -43,204 +39,103 @@ func (*ColumnMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advi
return nil, errors.New("number_payload is required for column maximum varchar length rule")
}
- // Create the rule
- rule := NewVarcharLengthRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnMaximumVarcharLengthOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ maximum: int(numberPayload.Number),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// VarcharLengthRule checks for maximum varchar length.
-type VarcharLengthRule struct {
- BaseRule
+type columnMaximumVarcharLengthOmniRule struct {
+ OmniBaseRule
maximum int
}
-// NewVarcharLengthRule creates a new VarcharLengthRule.
-func NewVarcharLengthRule(level storepb.Advice_Status, title string, maximum int) *VarcharLengthRule {
- return &VarcharLengthRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maximum: maximum,
- }
-}
-
-// Name returns the rule name.
-func (*VarcharLengthRule) Name() string {
+func (*columnMaximumVarcharLengthOmniRule) Name() string {
return "VarcharLengthRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *VarcharLengthRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnMaximumVarcharLengthOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*VarcharLengthRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *VarcharLengthRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+func (r *columnMaximumVarcharLengthOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- length := r.getVarcharLength(tableElement.ColumnDefinition().FieldDefinition().DataType())
+ for _, col := range n.Columns {
+ length := getVarcharLengthFromOmni(col.TypeName)
if r.maximum > 0 && length > r.maximum {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.VarcharLengthExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of the VARCHAR column `%s.%s` is bigger than %d", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.VarcharLengthExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The length of the VARCHAR column `%s.%s` is bigger than %d", tableName, col.Name, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
}
-func (r *VarcharLengthRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *columnMaximumVarcharLengthOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
-
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- var columnList []string
- varcharLengthMap := make(map[string]int)
- switch {
- // add column.
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition().DataType() == nil {
- continue
- }
-
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- length := r.getVarcharLength(item.FieldDefinition().DataType())
- varcharLengthMap[columnName] = length
- columnList = append(columnList, columnName)
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
- continue
- }
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- length := r.getVarcharLength(tableElement.ColumnDefinition().FieldDefinition().DataType())
- varcharLengthMap[columnName] = length
- columnList = append(columnList, columnName)
- }
- default:
- }
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- length := r.getVarcharLength(item.FieldDefinition().DataType())
- varcharLengthMap[columnName] = length
- columnList = append(columnList, columnName)
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- length := r.getVarcharLength(item.FieldDefinition().DataType())
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- varcharLengthMap[columnName] = length
- columnList = append(columnList, columnName)
- default:
- continue
- }
- for _, columnName := range columnList {
- if length, ok := varcharLengthMap[columnName]; ok && r.maximum > 0 && length > r.maximum {
+ for _, cmd := range n.Commands {
+ cols := omniGetColumnsFromCmd(cmd)
+ for _, col := range cols {
+ length := getVarcharLengthFromOmni(col.TypeName)
+ if r.maximum > 0 && length > r.maximum {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.VarcharLengthExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of the VARCHAR column `%s.%s` is bigger than %d", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.VarcharLengthExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The length of the VARCHAR column `%s.%s` is bigger than %d", tableName, col.Name, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ },
})
}
}
}
}
-func (*VarcharLengthRule) getVarcharLength(ctx mysql.IDataTypeContext) int {
- if ctx.GetType_() == nil {
+func getVarcharLengthFromOmni(dt *ast.DataType) int {
+ if dt == nil {
return 0
}
-
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserVARCHAR_SYMBOL:
- if ctx.FieldLength() == nil || ctx.FieldLength().Real_ulonglong_number() == nil {
- return 1
- }
- lengthStr := ctx.FieldLength().Real_ulonglong_number().GetText()
- length, err := strconv.Atoi(lengthStr)
- if err != nil {
- return 0
- }
- return length
- default:
+ if !strings.EqualFold(dt.Name, "VARCHAR") {
return 0
}
+ // MySQL default: VARCHAR without length = VARCHAR(1)
+ if dt.Length == 0 {
+ return 1
+ }
+ return dt.Length
}
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index 65b1e64a35..ec71ad6f97 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -5,15 +5,12 @@ import (
"fmt"
"slices"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -38,29 +35,22 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
return nil, err
}
- // Create the rule
- rule := NewColumnNoNullRule(level, checkCtx.Rule.Type.String(), checkCtx.FinalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnNoNullOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ columnSet: make(map[string]columnName),
+ finalMetadata: checkCtx.FinalMetadata,
}
- // Generate advice after walking
+ // Walk all statements to collect columns.
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
+
+ // Generate advice after walking all statements.
rule.generateAdvice()
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
type columnName struct {
@@ -73,49 +63,27 @@ func (c columnName) name() string {
return fmt.Sprintf("%s.%s", c.tableName, c.columnName)
}
-// ColumnNoNullRule checks for column no NULL value.
-type ColumnNoNullRule struct {
- BaseRule
+type columnNoNullOmniRule struct {
+ OmniBaseRule
columnSet map[string]columnName
finalMetadata *model.DatabaseMetadata
}
-// NewColumnNoNullRule creates a new ColumnNoNullRule.
-func NewColumnNoNullRule(level storepb.Advice_Status, title string, finalMetadata *model.DatabaseMetadata) *ColumnNoNullRule {
- return &ColumnNoNullRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- columnSet: make(map[string]columnName),
- finalMetadata: finalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnNoNullRule) Name() string {
+func (*columnNoNullOmniRule) Name() string {
return "ColumnNoNullRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnNoNullRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnNoNullOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnNoNullRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
}
-func (r *ColumnNoNullRule) generateAdvice() {
+func (r *columnNoNullOmniRule) generateAdvice() {
var columnList []columnName
for _, column := range r.columnSet {
columnList = append(columnList, column)
@@ -147,10 +115,10 @@ func (r *ColumnNoNullRule) generateAdvice() {
}
col := table.GetColumn(column.columnName)
if col != nil && col.GetProto().Nullable {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.ColumnCannotNull.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", column.tableName, column.columnName),
StartPosition: common.ConvertANTLRLineToPosition(column.line),
})
@@ -158,90 +126,59 @@ func (r *ColumnNoNullRule) generateAdvice() {
}
}
-func (r *ColumnNoNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
+func (r *columnNoNullOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.ColumnDefinition() == nil {
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil {
continue
}
-
- _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- col := columnName{
+ c := columnName{
tableName: tableName,
- columnName: column,
- line: r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine(),
+ columnName: col.Name,
+ line: r.BaseLine + int(r.LocToLine(col.Loc)),
}
- if _, exists := r.columnSet[col.name()]; !exists {
- r.columnSet[col.name()] = col
+ if _, exists := r.columnSet[c.name()]; !exists {
+ r.columnSet[c.name()] = c
}
}
}
-func (r *ColumnNoNullRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnNoNullOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
var columns []string
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- column := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- columns = append(columns, column)
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- columns = append(columns, column)
- }
- default:
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ columns = append(columns, cmd.Column.Name)
+ }
+ for _, col := range cmd.Columns {
+ columns = append(columns, col.Name)
+ }
+ case ast.ATChangeColumn:
+ // Only care about the new column name.
+ if cmd.Column != nil {
+ columns = append(columns, cmd.Column.Name)
}
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- // only care new column name.
- column := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- columns = append(columns, column)
default:
}
-
for _, column := range columns {
- col := columnName{
+ c := columnName{
tableName: tableName,
columnName: column,
- line: r.baseLine + item.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(cmd.Loc)),
}
- if _, exists := r.columnSet[col.name()]; !exists {
- r.columnSet[col.name()] = col
+ if _, exists := r.columnSet[c.name()]; !exists {
+ r.columnSet[c.name()] = c
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_require_charset.go b/backend/plugin/advisor/mysql/rule_column_require_charset.go
index b54f1cd178..54b6dbb4a0 100644
--- a/backend/plugin/advisor/mysql/rule_column_require_charset.go
+++ b/backend/plugin/advisor/mysql/rule_column_require_charset.go
@@ -3,18 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -35,131 +31,101 @@ func (*ColumnRequireCharsetAdvisor) Check(_ context.Context, checkCtx advisor.Co
return nil, err
}
- // Create the rule
- rule := NewColumnRequireCharsetRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnRequireCharsetOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// ColumnRequireCharsetRule checks for require charset.
-type ColumnRequireCharsetRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewColumnRequireCharsetRule creates a new ColumnRequireCharsetRule.
-func NewColumnRequireCharsetRule(level storepb.Advice_Status, title string) *ColumnRequireCharsetRule {
- return &ColumnRequireCharsetRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type columnRequireCharsetOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*ColumnRequireCharsetRule) Name() string {
+func (*columnRequireCharsetOmniRule) Name() string {
return "ColumnRequireCharsetRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnRequireCharsetRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnRequireCharsetOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnRequireCharsetRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnRequireCharsetRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil || ctx.TableElementList() == nil {
- return
- }
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- if tableName == "" {
+func (r *columnRequireCharsetOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- columnDefinition := tableElement.ColumnDefinition()
- if columnDefinition.FieldDefinition() == nil || columnDefinition.FieldDefinition().DataType() == nil {
- continue
- }
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- dataType := columnDefinition.FieldDefinition().DataType()
- if r.isCharsetDataType(dataType) {
- if dataType.CharsetWithOptBinary() == nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NoCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s does not have a character set specified", columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + columnDefinition.GetStart().GetLine()),
- })
- }
+ if omniIsCharsetDataType(col.TypeName) && col.TypeName.Charset == "" {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NoCharset.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a character set specified", col.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(col.Loc))),
+ })
}
}
}
-func (r *ColumnRequireCharsetRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnRequireCharsetOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- // Only check ADD COLUMN for now.
- if alterListItem.ADD_SYMBOL() == nil || alterListItem.COLUMN_SYMBOL() == nil || alterListItem.FieldDefinition() == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil || cmd.Type != ast.ATAddColumn {
continue
}
-
- columnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- dataType := alterListItem.FieldDefinition().DataType()
- if r.isCharsetDataType(dataType) {
- if dataType.CharsetWithOptBinary() == nil {
+ // Single column ADD.
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ if omniIsCharsetDataType(cmd.Column.TypeName) && cmd.Column.TypeName.Charset == "" {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.NoCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s does not have a character set specified", columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + alterListItem.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a character set specified", cmd.Column.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
+ })
+ }
+ }
+ // Multi-column ADD.
+ for _, col := range cmd.Columns {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ if omniIsCharsetDataType(col.TypeName) && col.TypeName.Charset == "" {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NoCharset.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a character set specified", col.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
})
}
}
}
}
-func (*ColumnRequireCharsetRule) isCharsetDataType(dataType mysql.IDataTypeContext) bool {
- return dataType != nil && (dataType.CHAR_SYMBOL() != nil ||
- dataType.VARCHAR_SYMBOL() != nil ||
- dataType.VARYING_SYMBOL() != nil ||
- dataType.TINYTEXT_SYMBOL() != nil ||
- dataType.TEXT_SYMBOL() != nil ||
- dataType.MEDIUMTEXT_SYMBOL() != nil ||
- dataType.LONGTEXT_SYMBOL() != nil)
+// omniIsCharsetDataType checks if a data type supports character sets.
+func omniIsCharsetDataType(dt *ast.DataType) bool {
+ if dt == nil {
+ return false
+ }
+ switch strings.ToUpper(dt.Name) {
+ case "CHAR", "VARCHAR", "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT":
+ return true
+ default:
+ return false
+ }
}
diff --git a/backend/plugin/advisor/mysql/rule_column_require_collation.go b/backend/plugin/advisor/mysql/rule_column_require_collation.go
index b7795c120d..cf4b7d5fdc 100644
--- a/backend/plugin/advisor/mysql/rule_column_require_collation.go
+++ b/backend/plugin/advisor/mysql/rule_column_require_collation.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -34,119 +30,84 @@ func (*ColumnRequireCollationAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- // Create the rule
- rule := NewColumnRequireCollationRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnRequireCollationOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// ColumnRequireCollationRule checks for require collation.
-type ColumnRequireCollationRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewColumnRequireCollationRule creates a new ColumnRequireCollationRule.
-func NewColumnRequireCollationRule(level storepb.Advice_Status, title string) *ColumnRequireCollationRule {
- return &ColumnRequireCollationRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type columnRequireCollationOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*ColumnRequireCollationRule) Name() string {
+func (*columnRequireCollationOmniRule) Name() string {
return "ColumnRequireCollationRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnRequireCollationRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnRequireCollationOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnRequireCollationRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *ColumnRequireCollationRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil || ctx.TableElementList() == nil {
+func (r *columnRequireCollationOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- if tableName == "" {
- return
- }
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- columnDefinition := tableElement.ColumnDefinition()
- if columnDefinition.FieldDefinition() == nil || columnDefinition.FieldDefinition().DataType() == nil {
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- dataType := columnDefinition.FieldDefinition().DataType()
- if isCharsetDataType(dataType) {
- if columnDefinition.FieldDefinition().Collate() == nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NoCollation.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s does not have a collation specified", columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + columnDefinition.GetStart().GetLine()),
- })
- }
+ if omniIsCharsetDataType(col.TypeName) && col.TypeName.Collate == "" {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NoCollation.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a collation specified", col.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(col.Loc))),
+ })
}
}
}
-func (r *ColumnRequireCollationRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *columnRequireCollationOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- // Only check ADD COLUMN for now.
- if alterListItem.ADD_SYMBOL() == nil || alterListItem.COLUMN_SYMBOL() == nil || alterListItem.FieldDefinition() == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil || cmd.Type != ast.ATAddColumn {
continue
}
-
- columnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- dataType := alterListItem.FieldDefinition().DataType()
- if isCharsetDataType(dataType) {
- if alterListItem.FieldDefinition().Collate() == nil {
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ if omniIsCharsetDataType(cmd.Column.TypeName) && cmd.Column.TypeName.Collate == "" {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NoCollation.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a collation specified", cmd.Column.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
+ })
+ }
+ }
+ for _, col := range cmd.Columns {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ if omniIsCharsetDataType(col.TypeName) && col.TypeName.Collate == "" {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.NoCollation.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column %s does not have a collation specified", columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + alterListItem.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column %s does not have a collation specified", col.Name),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.LocToLine(cmd.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_require_default.go b/backend/plugin/advisor/mysql/rule_column_require_default.go
index 9d4f53ef6a..570bb8442e 100644
--- a/backend/plugin/advisor/mysql/rule_column_require_default.go
+++ b/backend/plugin/advisor/mysql/rule_column_require_default.go
@@ -4,16 +4,11 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -37,243 +32,85 @@ func (*ColumnRequireDefaultAdvisor) Check(_ context.Context, checkCtx advisor.Co
return nil, err
}
- // Create the rule
- rule := NewColumnRequireDefaultRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnRequireDefaultOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// ColumnRequireDefaultRule checks for column default requirement.
-type ColumnRequireDefaultRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewColumnRequireDefaultRule creates a new ColumnRequireDefaultRule.
-func NewColumnRequireDefaultRule(level storepb.Advice_Status, title string) *ColumnRequireDefaultRule {
- return &ColumnRequireDefaultRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type columnRequireDefaultOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*ColumnRequireDefaultRule) Name() string {
+func (*columnRequireDefaultOmniRule) Name() string {
return "ColumnRequireDefaultRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnRequireDefaultRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnRequireDefaultOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnRequireDefaultRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *ColumnRequireDefaultRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *columnRequireDefaultOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- pkColumns := r.getPKColumns(ctx)
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.ColumnDefinition() == nil {
- continue
- }
+ pkCols := omniPKColumnNames(n.Columns, n.Constraints)
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if pkColumns[columnName] {
+ for _, col := range n.Columns {
+ if pkCols[col.Name] {
continue
}
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ r.checkColumn(tableName, col)
}
}
-func (r *ColumnRequireDefaultRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
+func (r *columnRequireDefaultOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
- var columnName string
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
+ for _, cmd := range n.Commands {
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
+ }
+ for _, col := range cmd.Columns {
+ r.checkColumn(tableName, col)
+ }
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
}
- // modify column
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
default:
}
}
}
-func (r *ColumnRequireDefaultRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if !r.hasDefault(ctx) && r.columnNeedDefault(ctx) {
+func (r *columnRequireDefaultOmniRule) checkColumn(tableName string, col *ast.ColumnDef) {
+ if !omniHasDefault(col) && !omniIsDefaultExemptType(col) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NoDefault.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column `%s`.`%s` doesn't have DEFAULT.", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.NoDefault.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column `%s`.`%s` doesn't have DEFAULT.", tableName, col.Name),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ Column: 0,
+ },
})
}
}
-
-func (*ColumnRequireDefaultRule) hasDefault(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.DEFAULT_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnRequireDefaultRule) getPKColumns(ctx *mysql.CreateTableContext) map[string]bool {
- pkColumn := make(map[string]bool)
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.TableConstraintDef() == nil {
- continue
- }
-
- if tableElement.TableConstraintDef().GetType_().GetTokenType() != mysql.MySQLParserPRIMARY_SYMBOL {
- continue
- }
- if tableElement.TableConstraintDef().KeyListVariants() == nil {
- continue
- }
- columnList := mysqlparser.NormalizeKeyListVariants(tableElement.TableConstraintDef().KeyListVariants())
- for _, column := range columnList {
- pkColumn[column] = true
- }
- }
-
- // Also check for inline primary key definitions
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- for _, attr := range tableElement.ColumnDefinition().FieldDefinition().AllColumnAttribute() {
- if attr.PRIMARY_SYMBOL() != nil {
- pkColumn[columnName] = true
- break
- }
- }
- }
-
- return pkColumn
-}
-
-func (*ColumnRequireDefaultRule) columnNeedDefault(ctx mysql.IFieldDefinitionContext) bool {
- if ctx.GENERATED_SYMBOL() != nil {
- return false
- }
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.AUTO_INCREMENT_SYMBOL() != nil || attr.PRIMARY_SYMBOL() != nil {
- return false
- }
- }
-
- if ctx.DataType() == nil {
- return false
- }
-
- switch ctx.DataType().GetType_().GetTokenType() {
- case mysql.MySQLParserBLOB_SYMBOL,
- mysql.MySQLParserTINYBLOB_SYMBOL,
- mysql.MySQLParserMEDIUMBLOB_SYMBOL,
- mysql.MySQLParserLONGBLOB_SYMBOL,
- mysql.MySQLParserJSON_SYMBOL,
- mysql.MySQLParserTINYTEXT_SYMBOL,
- mysql.MySQLParserTEXT_SYMBOL,
- mysql.MySQLParserMEDIUMTEXT_SYMBOL,
- mysql.MySQLParserLONGTEXT_SYMBOL,
- mysql.MySQLParserLONG_SYMBOL,
- mysql.MySQLParserSERIAL_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 false
- default:
- }
- return true
-}
diff --git a/backend/plugin/advisor/mysql/rule_column_required.go b/backend/plugin/advisor/mysql/rule_column_required.go
index 7e932e4d28..214d20cd88 100644
--- a/backend/plugin/advisor/mysql/rule_column_required.go
+++ b/backend/plugin/advisor/mysql/rule_column_required.go
@@ -6,17 +6,12 @@ import (
"slices"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -49,150 +44,90 @@ func (*ColumnRequirementAdvisor) Check(_ context.Context, checkCtx advisor.Conte
requiredColumns[column] = true
}
- // Create the rule
- rule := NewColumnRequiredRule(level, checkCtx.Rule.Type.String(), requiredColumns)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnRequiredOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ requiredColumns: requiredColumns,
+ tables: make(tableState),
+ line: make(map[string]int),
}
- return rule.generateAdviceList(), nil
+ adviceList := RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
+ adviceList = append(adviceList, rule.generateAdviceList()...)
+ return adviceList, nil
}
-// ColumnRequiredRule checks for column requirement.
-type ColumnRequiredRule struct {
- BaseRule
+type columnRequiredOmniRule struct {
+ OmniBaseRule
requiredColumns columnSet
tables tableState
line map[string]int
}
-// NewColumnRequiredRule creates a new ColumnRequiredRule.
-func NewColumnRequiredRule(level storepb.Advice_Status, title string, requiredColumns columnSet) *ColumnRequiredRule {
- return &ColumnRequiredRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- requiredColumns: requiredColumns,
- tables: make(tableState),
- line: make(map[string]int),
- }
-}
-
-// Name returns the rule name.
-func (*ColumnRequiredRule) Name() string {
+func (*columnRequiredOmniRule) Name() string {
return "ColumnRequiredRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnRequiredRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeDropTable:
- r.checkDropTable(ctx.(*mysql.DropTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnRequiredOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.DropTableStmt:
+ r.checkDropTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnRequiredRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnRequiredRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *columnRequiredOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- r.createTable(ctx)
-}
+ r.line[tableName] = int(r.LocToLine(n.Loc)) + r.BaseLine
+ r.initEmptyTable(tableName)
-func (r *ColumnRequiredRule) checkDropTable(ctx *mysql.DropTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableRefList() == nil {
- return
+ for _, col := range n.Columns {
+ r.addColumn(tableName, col.Name)
}
+}
- for _, tableRef := range ctx.TableRefList().AllTableRef() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
+func (r *columnRequiredOmniRule) checkDropTable(n *ast.DropTableStmt) {
+ for _, ref := range n.Tables {
+ tableName := omniTableName(ref)
delete(r.tables, tableName)
}
}
-func (r *ColumnRequiredRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
+func (r *columnRequiredOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
- lineNumber := r.baseLine + item.GetStart().GetLine()
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.addColumn(tableName, columnName)
- 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())
- r.addColumn(tableName, columnName)
- }
- default:
+ for _, cmd := range n.Commands {
+ lineNumber := int(r.LocToLine(cmd.Loc)) + r.BaseLine
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ r.addColumn(tableName, cmd.Column.Name)
+ }
+ for _, col := range cmd.Columns {
+ r.addColumn(tableName, col.Name)
}
- // drop column
- case item.DROP_SYMBOL() != nil && item.ColumnInternalRef() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- if r.dropColumn(tableName, columnName) {
+ case ast.ATDropColumn:
+ if r.dropColumn(tableName, cmd.Name) {
r.line[tableName] = lineNumber
}
- // rename column
- case item.RENAME_SYMBOL() != nil && item.COLUMN_SYMBOL() != nil:
- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.renameColumn(tableName, oldColumnName, newColumnName)
- r.line[tableName] = lineNumber
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- if r.renameColumn(tableName, oldColumnName, newColumnName) {
+ case ast.ATRenameColumn:
+ if r.renameColumn(tableName, cmd.Name, cmd.NewName) {
+ r.line[tableName] = lineNumber
+ }
+ case ast.ATChangeColumn:
+ if r.renameColumn(tableName, cmd.Name, cmd.NewName) {
r.line[tableName] = lineNumber
}
default:
@@ -200,8 +135,8 @@ func (r *ColumnRequiredRule) checkAlterTable(ctx *mysql.AlterTableContext) {
}
}
-func (r *ColumnRequiredRule) generateAdviceList() []*storepb.Advice {
- // Order it cause the random iteration order in Go, see https://go.dev/blog/maps
+func (r *columnRequiredOmniRule) generateAdviceList() []*storepb.Advice {
+ var adviceList []*storepb.Advice
tableList := r.tables.tableList()
for _, tableName := range tableList {
table := r.tables[tableName]
@@ -213,78 +148,52 @@ func (r *ColumnRequiredRule) generateAdviceList() []*storepb.Advice {
}
if len(missingColumns) > 0 {
- // Order it cause the random iteration order in Go, see https://go.dev/blog/maps
slices.Sort(missingColumns)
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NoRequiredColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table `%s` requires columns: %s", tableName, strings.Join(missingColumns, ", ")),
- StartPosition: common.ConvertANTLRLineToPosition(r.line[tableName]),
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: r.Level,
+ Code: code.NoRequiredColumn.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table `%s` requires columns: %s", tableName, strings.Join(missingColumns, ", ")),
+ StartPosition: &storepb.Position{
+ Line: int32(r.line[tableName]),
+ Column: 0,
+ },
})
}
}
-
- return r.adviceList
-}
-
-func (r *ColumnRequiredRule) createTable(ctx *mysql.CreateTableContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.line[tableName] = r.baseLine + ctx.GetStart().GetLine()
- r.initEmptyTable(tableName)
-
- if ctx.TableElementList() == nil {
- return
- }
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.addColumn(tableName, columnName)
- }
+ return adviceList
}
-func (r *ColumnRequiredRule) initEmptyTable(tableName string) columnSet {
+func (r *columnRequiredOmniRule) initEmptyTable(tableName string) columnSet {
r.tables[tableName] = make(columnSet)
return r.tables[tableName]
}
-// add a column.
-func (r *ColumnRequiredRule) addColumn(tableName string, columnName string) {
+func (r *columnRequiredOmniRule) addColumn(tableName string, columnName string) {
if _, ok := r.requiredColumns[columnName]; !ok {
return
}
if table, ok := r.tables[tableName]; !ok {
- // We do not retrospectively check.
- // So we assume it contains all required columns.
r.initFullTable(tableName)
} else {
table[columnName] = true
}
}
-// drop a column
-// return true if the column was successfully dropped from requirement list.
-func (r *ColumnRequiredRule) dropColumn(tableName string, columnName string) bool {
+func (r *columnRequiredOmniRule) dropColumn(tableName string, columnName string) bool {
if _, ok := r.requiredColumns[columnName]; !ok {
return false
}
table, ok := r.tables[tableName]
if !ok {
- // We do not retrospectively check.
- // So we assume it contains all required columns.
table = r.initFullTable(tableName)
}
table[columnName] = false
return true
}
-// rename a column
-// return if the old column was dropped from requirement list.
-func (r *ColumnRequiredRule) renameColumn(tableName string, oldColumn string, newColumn string) bool {
+func (r *columnRequiredOmniRule) renameColumn(tableName string, oldColumn string, newColumn string) bool {
_, oldNeed := r.requiredColumns[oldColumn]
_, newNeed := r.requiredColumns[newColumn]
if !oldNeed && !newNeed {
@@ -292,8 +201,6 @@ func (r *ColumnRequiredRule) renameColumn(tableName string, oldColumn string, ne
}
table, ok := r.tables[tableName]
if !ok {
- // We do not retrospectively check.
- // So we assume it contains all required columns.
table = r.initFullTable(tableName)
}
if oldNeed {
@@ -305,7 +212,7 @@ func (r *ColumnRequiredRule) renameColumn(tableName string, oldColumn string, ne
return oldNeed
}
-func (r *ColumnRequiredRule) initFullTable(tableName string) columnSet {
+func (r *columnRequiredOmniRule) initFullTable(tableName string) columnSet {
table := r.initEmptyTable(tableName)
for column := range r.requiredColumns {
table[column] = true
diff --git a/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go b/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
index 7245d85e9a..9a50a4edaf 100644
--- a/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
@@ -4,17 +4,11 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -38,270 +32,85 @@ func (*ColumnSetDefaultForNotNullAdvisor) Check(_ context.Context, checkCtx advi
return nil, err
}
- // Create the rule
- rule := NewColumnSetDefaultForNotNullRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &columnSetDefaultForNotNullOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// ColumnSetDefaultForNotNullRule checks for set default value for not null column.
-type ColumnSetDefaultForNotNullRule struct {
- BaseRule
+type columnSetDefaultForNotNullOmniRule struct {
+ OmniBaseRule
}
-// NewColumnSetDefaultForNotNullRule creates a new ColumnSetDefaultForNotNullRule.
-func NewColumnSetDefaultForNotNullRule(level storepb.Advice_Status, title string) *ColumnSetDefaultForNotNullRule {
- return &ColumnSetDefaultForNotNullRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*ColumnSetDefaultForNotNullRule) Name() string {
+func (*columnSetDefaultForNotNullOmniRule) Name() string {
return "ColumnSetDefaultForNotNullRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnSetDefaultForNotNullRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnSetDefaultForNotNullOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*ColumnSetDefaultForNotNullRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (*ColumnSetDefaultForNotNullRule) getPKColumns(ctx *mysql.CreateTableContext) map[string]bool {
- pkColumn := make(map[string]bool)
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.TableConstraintDef() == nil {
- continue
- }
-
- if tableElement.TableConstraintDef().GetType_().GetTokenType() != mysql.MySQLParserPRIMARY_SYMBOL {
- continue
- }
- if tableElement.TableConstraintDef().KeyListVariants() == nil {
- continue
- }
- columnList := mysqlparser.NormalizeKeyListVariants(tableElement.TableConstraintDef().KeyListVariants())
- for _, column := range columnList {
- pkColumn[column] = true
- }
- }
- return pkColumn
}
-func (r *ColumnSetDefaultForNotNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *columnSetDefaultForNotNullOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- pkColumns := r.getPKColumns(ctx)
+ pkCols := omniPKColumnNames(n.Columns, n.Constraints)
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.ColumnDefinition() == nil {
+ for _, col := range n.Columns {
+ if pkCols[col.Name] {
continue
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- field := tableElement.ColumnDefinition().FieldDefinition()
- if field == nil {
- continue
- }
-
- if pkColumns[columnName] || r.isPrimaryKey(field) {
- continue
- }
- if !r.canNull(field) && !r.hasDefault(field) && r.columnNeedDefault(field) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NotNullColumnWithNoDefault.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column `%s`.`%s` is NOT NULL but doesn't have DEFAULT", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
- })
- }
+ r.checkColumn(tableName, col)
}
}
-func (r *ColumnSetDefaultForNotNullRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if !r.canNull(ctx) && !r.hasDefault(ctx) && r.columnNeedDefault(ctx) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.NotNullColumnWithNoDefault.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Column `%s`.`%s` is NOT NULL but doesn't have DEFAULT", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
-}
-
-func (*ColumnSetDefaultForNotNullRule) canNull(ctx mysql.IFieldDefinitionContext) bool {
- for _, attribute := range ctx.AllColumnAttribute() {
- switch {
- case attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil:
- return false
- case attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() == nil:
- return true
- default:
- }
- }
- return true
-}
-
-func (*ColumnSetDefaultForNotNullRule) isPrimaryKey(ctx mysql.IFieldDefinitionContext) bool {
- for _, attribute := range ctx.AllColumnAttribute() {
- if attribute.PRIMARY_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnSetDefaultForNotNullRule) hasDefault(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.DEFAULT_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
-
-func (*ColumnSetDefaultForNotNullRule) columnNeedDefault(ctx mysql.IFieldDefinitionContext) bool {
- if ctx.DataType() == nil {
- return true
- }
-
- // AUTO_INCREMENT columns don't need DEFAULT.
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.AUTO_INCREMENT_SYMBOL() != nil {
- return false
- }
- }
-
- // Check data types that don't need defaults
- if ctx.DataType().GetType_() != nil {
- switch ctx.DataType().GetType_().GetTokenType() {
- case mysql.MySQLParserTIMESTAMP_SYMBOL, mysql.MySQLParserDATETIME_SYMBOL:
- return false
- // BLOB and TEXT types don't need defaults
- case mysql.MySQLParserTINYBLOB_SYMBOL, mysql.MySQLParserBLOB_SYMBOL,
- mysql.MySQLParserMEDIUMBLOB_SYMBOL, mysql.MySQLParserLONGBLOB_SYMBOL,
- mysql.MySQLParserTINYTEXT_SYMBOL, mysql.MySQLParserTEXT_SYMBOL,
- mysql.MySQLParserMEDIUMTEXT_SYMBOL, mysql.MySQLParserLONGTEXT_SYMBOL:
- return false
- // JSON type doesn't need defaults
- case mysql.MySQLParserJSON_SYMBOL:
- return false
- // SERIAL type doesn't need defaults
- case mysql.MySQLParserSERIAL_SYMBOL:
- return false
- default:
- }
- }
-
- // Check for LONG VARBINARY and LONG VARCHAR (these are special compound types)
- dataTypeText := ctx.DataType().GetParser().GetTokenStream().GetTextFromRuleContext(ctx.DataType())
- if dataTypeText == "long varbinary" || dataTypeText == "long varchar" {
- return false
- }
-
- return true
-}
-
-func (r *ColumnSetDefaultForNotNullRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
+func (r *columnSetDefaultForNotNullOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
+ for _, cmd := range n.Commands {
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
}
- // modify column
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition() == nil {
- continue
+ for _, col := range cmd.Columns {
+ r.checkColumn(tableName, col)
}
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- if item.FieldDefinition() == nil {
- continue
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ if cmd.Column != nil {
+ r.checkColumn(tableName, cmd.Column)
}
- // only care new column name.
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
default:
}
}
}
+
+func (r *columnSetDefaultForNotNullOmniRule) checkColumn(tableName string, col *ast.ColumnDef) {
+ if !omniIsNullable(col) && !omniHasDefault(col) && omniColumnNeedDefaultNotNull(col) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NotNullColumnWithNoDefault.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Column `%s`.`%s` is NOT NULL but doesn't have DEFAULT", tableName, col.Name),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(col.Loc),
+ Column: 0,
+ },
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mysql/rule_column_type_disallow_list.go b/backend/plugin/advisor/mysql/rule_column_type_disallow_list.go
index ef57f13e13..5780efa904 100644
--- a/backend/plugin/advisor/mysql/rule_column_type_disallow_list.go
+++ b/backend/plugin/advisor/mysql/rule_column_type_disallow_list.go
@@ -5,16 +5,11 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -45,159 +40,84 @@ func (*ColumnTypeDisallowListAdvisor) Check(_ context.Context, checkCtx advisor.
typeRestriction[strings.ToUpper(tp)] = true
}
- // Create the rule
- rule := NewColumnTypeDisallowListRule(level, checkCtx.Rule.Type.String(), typeRestriction)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &columnTypeDisallowListOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ typeRestriction: typeRestriction,
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// ColumnTypeDisallowListRule checks for column type restriction.
-type ColumnTypeDisallowListRule struct {
- BaseRule
+type columnTypeDisallowListOmniRule struct {
+ OmniBaseRule
typeRestriction map[string]bool
}
-// NewColumnTypeDisallowListRule creates a new ColumnTypeDisallowListRule.
-func NewColumnTypeDisallowListRule(level storepb.Advice_Status, title string, typeRestriction map[string]bool) *ColumnTypeDisallowListRule {
- return &ColumnTypeDisallowListRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- typeRestriction: typeRestriction,
- }
-}
-
-// Name returns the rule name.
-func (*ColumnTypeDisallowListRule) Name() string {
+func (*columnTypeDisallowListOmniRule) Name() string {
return "ColumnTypeDisallowListRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *ColumnTypeDisallowListRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *columnTypeDisallowListOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*ColumnTypeDisallowListRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *ColumnTypeDisallowListRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *columnTypeDisallowListOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
- if ctx.TableName() == nil {
- return
+ for _, col := range n.Columns {
+ r.checkColumnType(tableName, col.Name, col.TypeName, col.Loc)
}
- if ctx.TableElementList() == nil {
+}
+
+func (r *columnTypeDisallowListOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
+ if tableName == "" {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
+ for _, cmd := range n.Commands {
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ r.checkColumnType(tableName, col.Name, col.TypeName, col.Loc)
}
- if tableElement.ColumnDefinition() == nil {
- continue
- }
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
}
-func (r *ColumnTypeDisallowListRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if ctx.DataType() == nil {
+func (r *columnTypeDisallowListOmniRule) checkColumnType(tableName, colName string, dt *ast.DataType, loc ast.Loc) {
+ if dt == nil {
return
}
- columnType := mysqlparser.NormalizeMySQLDataType(ctx.DataType(), true /* compact */)
- columnType = strings.ToUpper(columnType)
+ columnType := strings.ToUpper(omniDataTypeNameCompact(dt))
if _, exists := r.typeRestriction[columnType]; exists {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DisabledColumnType.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Disallow column type %s but column `%s`.`%s` is", columnType, tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.DisabledColumnType.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Disallow column type %s but column `%s`.`%s` is", columnType, tableName, colName),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(loc),
+ },
})
}
}
-
-func (r *ColumnTypeDisallowListRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
- default:
- }
- // modify column
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- default:
- }
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go b/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go
index e916225da3..488fda4457 100644
--- a/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go
+++ b/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -38,87 +34,48 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso
return nil, err
}
- // Create the rule
- rule := NewDatabaseDropEmptyDBRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &databaseDropEmptyDBOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ originMetadata: checkCtx.OriginalMetadata,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// DatabaseDropEmptyDBRule checks for drop database only if empty.
-type DatabaseDropEmptyDBRule struct {
- BaseRule
+type databaseDropEmptyDBOmniRule struct {
+ OmniBaseRule
originMetadata *model.DatabaseMetadata
}
-// NewDatabaseDropEmptyDBRule creates a new DatabaseDropEmptyDBRule.
-func NewDatabaseDropEmptyDBRule(level storepb.Advice_Status, title string, originMetadata *model.DatabaseMetadata) *DatabaseDropEmptyDBRule {
- return &DatabaseDropEmptyDBRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- originMetadata: originMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*DatabaseDropEmptyDBRule) Name() string {
+func (*databaseDropEmptyDBOmniRule) Name() string {
return "DatabaseDropEmptyDBRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *DatabaseDropEmptyDBRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeDropDatabase {
- r.checkDropDatabase(ctx.(*mysql.DropDatabaseContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*DatabaseDropEmptyDBRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *DatabaseDropEmptyDBRule) checkDropDatabase(ctx *mysql.DropDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.SchemaRef() == nil {
+func (r *databaseDropEmptyDBOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.DropDatabaseStmt)
+ if !ok {
return
}
-
- dbName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
+ dbName := n.Name
+ line := r.BaseLine + int(r.LocToLine(n.Loc))
if r.originMetadata.DatabaseName() != dbName {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.NotCurrentDatabase.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", dbName, r.originMetadata.DatabaseName()),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
})
} else if !r.originMetadata.HasNoTable() {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.DatabaseNotEmpty.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", dbName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_disallow_procedure.go b/backend/plugin/advisor/mysql/rule_disallow_procedure.go
index a87b151901..193ecae213 100644
--- a/backend/plugin/advisor/mysql/rule_disallow_procedure.go
+++ b/backend/plugin/advisor/mysql/rule_disallow_procedure.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "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"
)
var (
@@ -35,85 +31,36 @@ func (*ProcedureDisallowCreateAdvisor) Check(_ context.Context, checkCtx advisor
return nil, err
}
- // Create the rule
- rule := NewProcedureDisallowCreateRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// ProcedureDisallowCreateRule checks for disallow create procedure.
-type ProcedureDisallowCreateRule struct {
- BaseRule
- text string
-}
-
-// NewProcedureDisallowCreateRule creates a new ProcedureDisallowCreateRule.
-func NewProcedureDisallowCreateRule(level storepb.Advice_Status, title string) *ProcedureDisallowCreateRule {
- return &ProcedureDisallowCreateRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &procedureDisallowCreateOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*ProcedureDisallowCreateRule) Name() string {
- return "ProcedureDisallowCreateRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *ProcedureDisallowCreateRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeCreateProcedure:
- r.checkCreateProcedure(ctx.(*mysql.CreateProcedureContext))
- default:
- // Ignore other node types
- }
- return nil
+type procedureDisallowCreateOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*ProcedureDisallowCreateRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*procedureDisallowCreateOmniRule) Name() string {
+ return "ProcedureDisallowCreateRule"
}
-func (r *ProcedureDisallowCreateRule) checkCreateProcedure(ctx *mysql.CreateProcedureContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *procedureDisallowCreateOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateFunctionStmt)
+ if !ok || !n.IsProcedure {
return
}
- code := advisorcode.Ok
- if ctx.ProcedureName() != nil {
- code = advisorcode.DisallowCreateProcedure
- }
-
- if code != advisorcode.Ok {
+ if n.Name != nil {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Procedure is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.DisallowCreateProcedure.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Procedure is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_event_disallow_create.go b/backend/plugin/advisor/mysql/rule_event_disallow_create.go
index c0f709c851..1afff28c2d 100644
--- a/backend/plugin/advisor/mysql/rule_event_disallow_create.go
+++ b/backend/plugin/advisor/mysql/rule_event_disallow_create.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "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"
)
var (
@@ -35,84 +31,36 @@ func (*EventDisallowCreateAdvisor) Check(_ context.Context, checkCtx advisor.Con
return nil, err
}
- // Create the rule
- rule := NewEventDisallowCreateRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// EventDisallowCreateRule checks for disallow creating event.
-type EventDisallowCreateRule struct {
- BaseRule
- text string
-}
-
-// NewEventDisallowCreateRule creates a new EventDisallowCreateRule.
-func NewEventDisallowCreateRule(level storepb.Advice_Status, title string) *EventDisallowCreateRule {
- return &EventDisallowCreateRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &eventDisallowCreateOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*EventDisallowCreateRule) Name() string {
- return "EventDisallowCreateRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *EventDisallowCreateRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeCreateEvent:
- r.checkCreateEvent(ctx.(*mysql.CreateEventContext))
- default:
- }
- return nil
+type eventDisallowCreateOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*EventDisallowCreateRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*eventDisallowCreateOmniRule) Name() string {
+ return "EventDisallowCreateRule"
}
-func (r *EventDisallowCreateRule) checkCreateEvent(ctx *mysql.CreateEventContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *eventDisallowCreateOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateEventStmt)
+ if !ok {
return
}
- code := advisorcode.Ok
- if ctx.EventName() != nil {
- code = advisorcode.DisallowCreateEvent
- }
-
- if code != advisorcode.Ok {
+ if n.Name != "" {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Event is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.DisallowCreateEvent.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Event is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_function_disallow_create.go b/backend/plugin/advisor/mysql/rule_function_disallow_create.go
index 47b621504e..2b150c5c06 100644
--- a/backend/plugin/advisor/mysql/rule_function_disallow_create.go
+++ b/backend/plugin/advisor/mysql/rule_function_disallow_create.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "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"
)
var (
@@ -35,84 +31,36 @@ func (*FunctionDisallowCreateAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- // Create the rule
- rule := NewFunctionDisallowCreateRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// FunctionDisallowCreateRule checks for disallow creating function.
-type FunctionDisallowCreateRule struct {
- BaseRule
- text string
-}
-
-// NewFunctionDisallowCreateRule creates a new FunctionDisallowCreateRule.
-func NewFunctionDisallowCreateRule(level storepb.Advice_Status, title string) *FunctionDisallowCreateRule {
- return &FunctionDisallowCreateRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &functionDisallowCreateOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*FunctionDisallowCreateRule) Name() string {
- return "FunctionDisallowCreateRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *FunctionDisallowCreateRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeCreateFunction:
- r.checkCreateFunction(ctx.(*mysql.CreateFunctionContext))
- default:
- }
- return nil
+type functionDisallowCreateOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*FunctionDisallowCreateRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*functionDisallowCreateOmniRule) Name() string {
+ return "FunctionDisallowCreateRule"
}
-func (r *FunctionDisallowCreateRule) checkCreateFunction(ctx *mysql.CreateFunctionContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *functionDisallowCreateOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateFunctionStmt)
+ if !ok || n.IsProcedure {
return
}
- code := advisorcode.Ok
- if ctx.FunctionName() != nil {
- code = advisorcode.DisallowCreateFunction
- }
-
- if code != advisorcode.Ok {
+ if n.Name != nil {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Function is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.DisallowCreateFunction.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Function is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_function_disallowed_list.go b/backend/plugin/advisor/mysql/rule_function_disallowed_list.go
index 3c02542b7a..0377a2eab8 100644
--- a/backend/plugin/advisor/mysql/rule_function_disallowed_list.go
+++ b/backend/plugin/advisor/mysql/rule_function_disallowed_list.go
@@ -6,16 +6,12 @@ import (
"slices"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -42,84 +38,146 @@ func (*FunctionDisallowedListAdvisor) Check(_ context.Context, checkCtx advisor.
disallowList = append(disallowList, strings.ToUpper(fn))
}
- // Create the rule
- rule := NewFunctionDisallowedListRule(level, checkCtx.Rule.Type.String(), disallowList)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &functionDisallowedListOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ disallowList: disallowList,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// FunctionDisallowedListRule checks for disallowed function list.
-type FunctionDisallowedListRule struct {
- BaseRule
- text string
+type functionDisallowedListOmniRule struct {
+ OmniBaseRule
disallowList []string
}
-// NewFunctionDisallowedListRule creates a new FunctionDisallowedListRule.
-func NewFunctionDisallowedListRule(level storepb.Advice_Status, title string, disallowList []string) *FunctionDisallowedListRule {
- return &FunctionDisallowedListRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- disallowList: disallowList,
+func (*functionDisallowedListOmniRule) Name() string {
+ return "FunctionDisallowedListRule"
+}
+
+func (r *functionDisallowedListOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ r.checkSelectStmt(n)
+ case *ast.InsertStmt:
+ r.checkInsertStmt(n)
+ case *ast.UpdateStmt:
+ r.checkUpdateStmt(n)
+ case *ast.DeleteStmt:
+ r.checkDeleteStmt(n)
+ case *ast.CreateTableStmt:
+ r.checkCreateTableStmt(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTableStmt(n)
+ default:
}
}
-// Name returns the rule name.
-func (*FunctionDisallowedListRule) Name() string {
- return "FunctionDisallowedListRule"
+func (r *functionDisallowedListOmniRule) checkExpr(expr ast.ExprNode) {
+ if expr == nil {
+ return
+ }
+ ast.Inspect(expr, func(n ast.Node) bool {
+ fn, ok := n.(*ast.FuncCallExpr)
+ if !ok {
+ return true
+ }
+ if slices.Contains(r.disallowList, strings.ToUpper(fn.Name)) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.DisabledFunction.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Function \"%s\" is disallowed, but \"%s\" uses", fn.Name, r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(fn.Loc))),
+ })
+ }
+ return true
+ })
}
-// OnEnter is called when entering a parse tree node.
-func (r *FunctionDisallowedListRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- r.checkQuery(ctx.(*mysql.QueryContext))
- case NodeTypeFunctionCall:
- r.checkFunctionCall(ctx.(*mysql.FunctionCallContext))
- default:
+func (r *functionDisallowedListOmniRule) checkCreateTableStmt(n *ast.CreateTableStmt) {
+ if n == nil {
+ return
+ }
+ for _, col := range n.Columns {
+ if col == nil {
+ continue
+ }
+ r.checkExpr(col.DefaultValue)
+ r.checkExpr(col.OnUpdate)
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*FunctionDisallowedListRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (r *functionDisallowedListOmniRule) checkAlterTableStmt(n *ast.AlterTableStmt) {
+ if n == nil {
+ return
+ }
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil {
+ continue
+ }
+ r.checkExpr(col.DefaultValue)
+ r.checkExpr(col.OnUpdate)
+ }
+ r.checkExpr(cmd.DefaultExpr)
+ }
+}
+
+func (r *functionDisallowedListOmniRule) checkSelectStmt(n *ast.SelectStmt) {
+ if n == nil {
+ return
+ }
+ for _, expr := range n.TargetList {
+ r.checkExpr(expr)
+ }
+ r.checkExpr(n.Where)
+ for _, expr := range n.GroupBy {
+ r.checkExpr(expr)
+ }
+ r.checkExpr(n.Having)
+ for _, item := range n.OrderBy {
+ if item != nil {
+ r.checkExpr(item.Expr)
+ }
+ }
}
-func (r *FunctionDisallowedListRule) checkQuery(ctx *mysql.QueryContext) {
- r.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+func (r *functionDisallowedListOmniRule) checkInsertStmt(n *ast.InsertStmt) {
+ if n == nil {
+ return
+ }
+ for _, row := range n.Values {
+ for _, expr := range row {
+ r.checkExpr(expr)
+ }
+ }
+ if n.Select != nil {
+ r.checkSelectStmt(n.Select)
+ }
}
-func (r *FunctionDisallowedListRule) checkFunctionCall(ctx *mysql.FunctionCallContext) {
- pi := ctx.PureIdentifier()
- if pi != nil {
- functionName := mysqlparser.NormalizeMySQLPureIdentifier(pi)
- if slices.Contains(r.disallowList, strings.ToUpper(functionName)) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DisabledFunction.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Function \"%s\" is disallowed, but \"%s\" uses", functionName, r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+func (r *functionDisallowedListOmniRule) checkUpdateStmt(n *ast.UpdateStmt) {
+ if n == nil {
+ return
+ }
+ for _, assign := range n.SetList {
+ if assign != nil {
+ r.checkExpr(assign.Value)
}
}
+ r.checkExpr(n.Where)
+}
+
+func (r *functionDisallowedListOmniRule) checkDeleteStmt(n *ast.DeleteStmt) {
+ if n == nil {
+ return
+ }
+ r.checkExpr(n.Where)
}
diff --git a/backend/plugin/advisor/mysql/rule_index_key_number_limit.go b/backend/plugin/advisor/mysql/rule_index_key_number_limit.go
index 0fd24abf9f..9090f8cf00 100644
--- a/backend/plugin/advisor/mysql/rule_index_key_number_limit.go
+++ b/backend/plugin/advisor/mysql/rule_index_key_number_limit.go
@@ -4,17 +4,13 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -42,191 +38,114 @@ func (*IndexKeyNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.Con
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewIndexKeyNumberLimitRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexKeyNumberLimitOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ max: int(numberPayload.Number),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexKeyNumberLimitRule checks for index key number limit.
-type IndexKeyNumberLimitRule struct {
- BaseRule
+type indexKeyNumberLimitOmniRule struct {
+ OmniBaseRule
max int
}
-// NewIndexKeyNumberLimitRule creates a new IndexKeyNumberLimitRule.
-func NewIndexKeyNumberLimitRule(level storepb.Advice_Status, title string, maxKeys int) *IndexKeyNumberLimitRule {
- return &IndexKeyNumberLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- max: maxKeys,
- }
-}
-
-// Name returns the rule name.
-func (*IndexKeyNumberLimitRule) Name() string {
+func (*indexKeyNumberLimitOmniRule) Name() string {
return "IndexKeyNumberLimitRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexKeyNumberLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *indexKeyNumberLimitOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*IndexKeyNumberLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *IndexKeyNumberLimitRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *indexKeyNumberLimitOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableName() == nil {
- return
+ tableName := n.Table.Name
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ r.handleConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
- if ctx.TableElementList() == nil {
+}
+
+func (r *indexKeyNumberLimitOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- if tableElement.TableConstraintDef() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.handleConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
+ }
+ default:
}
- r.handleConstraintDef(tableName, tableElement.TableConstraintDef())
}
}
-func (r *IndexKeyNumberLimitRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
+func (r *indexKeyNumberLimitOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int32) {
var columnList []string
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- if ctx.KeyListVariants() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
- case mysql.MySQLParserFOREIGN_SYMBOL:
- if ctx.KeyList() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyList(ctx.KeyList())
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex:
+ columnList = constraint.Columns
+ case ast.ConstrForeignKey:
+ columnList = constraint.Columns
default:
return
}
- indexName := ""
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- }
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
+ indexName := constraint.Name
+ if indexName == "" && constraint.Type == ast.ConstrPrimaryKey {
+ indexName = extractPKNameFromText(r.StmtText)
}
-
if r.max > 0 && len(columnList) > r.max {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexKeyNumberExceedsLimit.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("The number of index `%s` in table `%s` should be not greater than %d", indexName, tableName, r.max),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
}
-func (r *IndexKeyNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL:
+func (r *indexKeyNumberLimitOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
- default:
}
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+ if n.Fulltext || n.Spatial {
return
}
- indexName := ""
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- }
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
+ tableName := n.Table.Name
+ indexName := n.IndexName
+ columnList := omniIndexColumns(n.Columns)
if r.max > 0 && len(columnList) > r.max {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexKeyNumberExceedsLimit.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("The number of index `%s` in table `%s` should be not greater than %d", indexName, tableName, r.max),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
-
-func (r *IndexKeyNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
- continue
- }
-
- switch {
- // add index.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- r.handleConstraintDef(tableName, alterListItem.TableConstraintDef())
- default:
- continue
- }
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_index_no_duplicate_column.go b/backend/plugin/advisor/mysql/rule_index_no_duplicate_column.go
index b2baa01b2b..b51129096a 100644
--- a/backend/plugin/advisor/mysql/rule_index_no_duplicate_column.go
+++ b/backend/plugin/advisor/mysql/rule_index_no_duplicate_column.go
@@ -3,18 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "regexp"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -38,222 +34,150 @@ func (*IndexNoDuplicateColumnAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- // Create the rule
- rule := NewIndexNoDuplicateColumnRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexNoDuplicateColumnOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexNoDuplicateColumnRule checks for no duplicate columns in index.
-type IndexNoDuplicateColumnRule struct {
- BaseRule
+type indexNoDuplicateColumnOmniRule struct {
+ OmniBaseRule
}
-// NewIndexNoDuplicateColumnRule creates a new IndexNoDuplicateColumnRule.
-func NewIndexNoDuplicateColumnRule(level storepb.Advice_Status, title string) *IndexNoDuplicateColumnRule {
- return &IndexNoDuplicateColumnRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*IndexNoDuplicateColumnRule) Name() string {
+func (*indexNoDuplicateColumnOmniRule) Name() string {
return "IndexNoDuplicateColumnRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexNoDuplicateColumnRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *indexNoDuplicateColumnOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*IndexNoDuplicateColumnRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *IndexNoDuplicateColumnRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
+func (r *indexNoDuplicateColumnOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ tableName := n.Table.Name
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- if tableElement.TableConstraintDef() == nil {
- continue
- }
- r.handleConstraintDef(tableName, tableElement.TableConstraintDef())
+ r.handleConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
}
-func (r *IndexNoDuplicateColumnRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *indexNoDuplicateColumnOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add index.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- r.handleConstraintDef(tableName, alterListItem.TableConstraintDef())
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.handleConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
+ }
default:
- continue
}
}
}
-func (r *IndexNoDuplicateColumnRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL:
+func (r *indexNoDuplicateColumnOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
- default:
}
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+ if n.Fulltext || n.Spatial {
return
}
- indexType := ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.CreateIndexTarget().KeyListVariants().GetStart().GetTokenIndex()-1,
- ))
- indexName := ""
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.IndexName().GetStart().GetTokenIndex()-1,
- ))
- }
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.IndexNameAndType().GetStart().GetTokenIndex()-1,
- ))
+ tableName := n.Table.Name
+ indexName := n.IndexName
+
+ indexType := "INDEX "
+ if n.Unique {
+ indexType = "UNIQUE INDEX "
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
- if column, duplicate := r.hasDuplicateColumn(columnList); duplicate {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ columnList := omniIndexColumns(n.Columns)
+ if column, duplicate := hasDuplicateColumn(columnList); duplicate {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.DuplicateColumnInIndex.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("%s`%s` has duplicate column `%s`.`%s`", indexType, indexName, tableName, column),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
-func (r *IndexNoDuplicateColumnRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
+func (r *indexNoDuplicateColumnOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int32) {
var columnList []string
indexType := ""
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- if ctx.KeyListVariants() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.KeyListVariants().GetStart().GetTokenIndex()-1,
- ))
- case mysql.MySQLParserFOREIGN_SYMBOL:
- if ctx.KeyList() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyList(ctx.KeyList())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.KeyList().GetStart().GetTokenIndex()-1,
- ))
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey:
+ columnList = constraint.Columns
+ indexType = "PRIMARY KEY "
+ case ast.ConstrUnique:
+ columnList = constraint.Columns
+ indexType = "UNIQUE KEY "
+ case ast.ConstrIndex:
+ columnList = constraint.Columns
+ indexType = "INDEX "
+ case ast.ConstrForeignKey:
+ columnList = constraint.Columns
+ indexType = "FOREIGN KEY "
+ case ast.ConstrFulltextIndex, ast.ConstrSpatialIndex:
+ return
default:
return
}
- indexName := ""
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.IndexName().GetStart().GetTokenIndex()-1,
- ))
+ indexName := constraint.Name
+ // Workaround: omni parser doesn't capture PRIMARY KEY constraint names.
+ // Extract from statement text as a fallback.
+ if indexName == "" && constraint.Type == ast.ConstrPrimaryKey {
+ indexName = extractPKNameFromText(r.StmtText)
}
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- indexType = ctx.GetParser().GetTokenStream().GetTextFromInterval(antlr.NewInterval(
- ctx.GetStart().GetTokenIndex(),
- ctx.IndexNameAndType().GetStart().GetTokenIndex()-1,
- ))
- }
- if column, duplicate := r.hasDuplicateColumn(columnList); duplicate {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if column, duplicate := hasDuplicateColumn(columnList); duplicate {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.DuplicateColumnInIndex.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("%s`%s` has duplicate column `%s`.`%s`", indexType, indexName, tableName, column),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
}
-func (*IndexNoDuplicateColumnRule) hasDuplicateColumn(keyList []string) (string, bool) {
+// primaryKeyNameRe extracts the optional constraint name from a PRIMARY KEY clause.
+// Matches patterns like: PRIMARY KEY pk_a (...), PRIMARY KEY `pk_a` (...)
+var primaryKeyNameRe = regexp.MustCompile(`(?i)PRIMARY\s+KEY\s+` + "`?" + `(\w+)` + "`?" + `\s*\(`)
+
+// extractPKNameFromText extracts the PRIMARY KEY constraint name from the statement text.
+// Returns empty string if no name is found.
+func extractPKNameFromText(stmtText string) string {
+ m := primaryKeyNameRe.FindStringSubmatch(stmtText)
+ if len(m) >= 2 {
+ return m[1]
+ }
+ return ""
+}
+
+func hasDuplicateColumn(keyList []string) (string, bool) {
listMap := make(map[string]struct{})
for _, keyName := range keyList {
if _, exists := listMap[keyName]; exists {
@@ -261,6 +185,5 @@ func (*IndexNoDuplicateColumnRule) hasDuplicateColumn(keyList []string) (string,
}
listMap[keyName] = struct{}{}
}
-
return "", false
}
diff --git a/backend/plugin/advisor/mysql/rule_index_pk_type.go b/backend/plugin/advisor/mysql/rule_index_pk_type.go
index f16f6511d5..098dbe1126 100644
--- a/backend/plugin/advisor/mysql/rule_index_pk_type.go
+++ b/backend/plugin/advisor/mysql/rule_index_pk_type.go
@@ -5,17 +5,13 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -40,204 +36,144 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
return nil, err
}
- // Create the rule
- rule := NewIndexPkTypeRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexPkTypeOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ originalMetadata: checkCtx.OriginalMetadata,
+ tablesNewColumns: make(tableColumnTypes),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexPkTypeRule checks for correct type of PK.
-type IndexPkTypeRule struct {
- BaseRule
- line map[string]int
+type indexPkTypeOmniRule struct {
+ OmniBaseRule
originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
-// NewIndexPkTypeRule creates a new IndexPkTypeRule.
-func NewIndexPkTypeRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *IndexPkTypeRule {
- return &IndexPkTypeRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- line: make(map[string]int),
- originalMetadata: originalMetadata,
- tablesNewColumns: make(tableColumnTypes),
- }
-}
-
-// Name returns the rule name.
-func (*IndexPkTypeRule) Name() string {
+func (*indexPkTypeOmniRule) Name() string {
return "IndexPkTypeRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexPkTypeRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *indexPkTypeOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*IndexPkTypeRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *IndexPkTypeRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *indexPkTypeOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- switch {
- case tableElement.ColumnDefinition() != nil:
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ columnType := r.getIntOrBigIntStr(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.addAdvice(tableName, col.Name, columnType, r.BaseLine+int(r.LocToLine(col.Loc)))
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- case tableElement.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, tableElement.TableConstraintDef())
- default:
}
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ r.checkConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
}
-func (r *IndexPkTypeRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
+func (r *indexPkTypeOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add column
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.Identifier() != nil:
- switch {
- case alterListItem.Identifier() != nil && alterListItem.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- case alterListItem.OPEN_PAR_SYMBOL() != nil && alterListItem.TableElementList() != nil:
- for _, tableElement := range alterListItem.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ columnType := r.getIntOrBigIntStr(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.addAdvice(tableName, col.Name, columnType, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATModifyColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := r.getIntOrBigIntStr(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.addAdvice(tableName, col.Name, columnType, r.BaseLine+int(r.LocToLine(n.Loc)))
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATChangeColumn:
+ r.tablesNewColumns.delete(tableName, cmd.Name)
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := r.getIntOrBigIntStr(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.addAdvice(tableName, col.Name, columnType, r.BaseLine+int(r.LocToLine(n.Loc)))
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
- default:
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.checkConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
}
- // modify column
- case alterListItem.MODIFY_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- // change column
- case alterListItem.CHANGE_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil && alterListItem.Identifier() != nil:
- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.tablesNewColumns.delete(tableName, oldColumnName)
- newColumnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, newColumnName, alterListItem.FieldDefinition())
- // add constriant.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, alterListItem.TableConstraintDef())
default:
}
}
}
-func (r *IndexPkTypeRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if ctx.DataType() == nil {
- return
- }
- columnType := r.getIntOrBigIntStr(ctx.DataType())
- for _, attribute := range ctx.AllColumnAttribute() {
- if attribute.PRIMARY_SYMBOL() != nil {
- r.addAdvice(tableName, columnName, columnType, r.baseLine+ctx.GetStart().GetLine())
- }
- }
- r.tablesNewColumns.set(tableName, columnName, columnType)
-}
-
-func (r *IndexPkTypeRule) checkConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_().GetTokenType() != mysql.MySQLParserPRIMARY_SYMBOL {
- return
- }
- if ctx.KeyListVariants() == nil {
+func (r *indexPkTypeOmniRule) checkConstraint(tableName string, constraint *ast.Constraint, line int32) {
+ if constraint.Type != ast.ConstrPrimaryKey {
return
}
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
-
- for _, columnName := range columnList {
+ for _, columnName := range constraint.Columns {
columnType, err := r.getPKColumnType(tableName, columnName)
if err != nil {
continue
}
- r.addAdvice(tableName, columnName, columnType, r.baseLine+ctx.GetStart().GetLine())
+ r.addAdvice(tableName, columnName, columnType, r.BaseLine+int(line))
}
}
-func (r *IndexPkTypeRule) addAdvice(tableName, columnName, columnType string, lineNumber int) {
+func (r *indexPkTypeOmniRule) addAdvice(tableName, columnName, columnType string, lineNumber int) {
if !strings.EqualFold(columnType, "INT") && !strings.EqualFold(columnType, "BIGINT") {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexPKType.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Columns in primary key must be INT/BIGINT but `%s`.`%s` is %s", tableName, columnName, columnType),
StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
})
}
}
-// getPKColumnType gets the column type string from r.tablesNewColumns or catalog, returns empty string and non-nil error if cannot find the column in given table.
-func (r *IndexPkTypeRule) getPKColumnType(tableName string, columnName string) (string, error) {
+func (r *indexPkTypeOmniRule) getPKColumnType(tableName string, columnName string) (string, error) {
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
@@ -248,18 +184,23 @@ func (r *IndexPkTypeRule) getPKColumnType(tableName string, columnName string) (
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
-// getIntOrBigIntStr returns the type string of tp.
-func (*IndexPkTypeRule) getIntOrBigIntStr(ctx mysql.IDataTypeContext) string {
- switch ctx.GetType_().GetTokenType() {
- // https://pkg.go.dev/github.com/pingcap/tidb/pkg/parser/mysql#TypeLong
- case mysql.MySQLParserINT_SYMBOL:
- // tp.String() return int(11)
+func (*indexPkTypeOmniRule) getIntOrBigIntStr(dt *ast.DataType) string {
+ if dt == nil {
+ return ""
+ }
+ switch strings.ToUpper(dt.Name) {
+ case "INT", "INTEGER":
return "INT"
- // https://pkg.go.dev/github.com/pingcap/tidb/pkg/parser/mysql#TypeLonglong
- case mysql.MySQLParserBIGINT_SYMBOL:
- // tp.String() return bigint(20)
+ case "BIGINT":
return "BIGINT"
default:
+ name := strings.ToLower(dt.Name)
+ if dt.Length > 0 {
+ if dt.Scale > 0 {
+ return fmt.Sprintf("%s(%d,%d)", name, dt.Length, dt.Scale)
+ }
+ return fmt.Sprintf("%s(%d)", name, dt.Length)
+ }
+ return name
}
- return strings.ToLower(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx))
}
diff --git a/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go b/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
index ba454362ec..1455d38ddb 100644
--- a/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
+++ b/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
@@ -5,17 +5,13 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -44,211 +40,175 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx ad
allowlist[strings.ToLower(tp)] = true
}
- // Create the rule
- rule := NewIndexPrimaryKeyTypeAllowlistRule(level, checkCtx.Rule.Type.String(), allowlist, checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexPrimaryKeyTypeAllowlistOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ allowlist: allowlist,
+ originalMetadata: checkCtx.OriginalMetadata,
+ tablesNewColumns: make(tableColumnTypes),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexPrimaryKeyTypeAllowlistRule checks for primary key type allowlist.
-type IndexPrimaryKeyTypeAllowlistRule struct {
- BaseRule
+type indexPrimaryKeyTypeAllowlistOmniRule struct {
+ OmniBaseRule
allowlist map[string]bool
originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
-// NewIndexPrimaryKeyTypeAllowlistRule creates a new IndexPrimaryKeyTypeAllowlistRule.
-func NewIndexPrimaryKeyTypeAllowlistRule(level storepb.Advice_Status, title string, allowlist map[string]bool, originalMetadata *model.DatabaseMetadata) *IndexPrimaryKeyTypeAllowlistRule {
- return &IndexPrimaryKeyTypeAllowlistRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- allowlist: allowlist,
- originalMetadata: originalMetadata,
- tablesNewColumns: make(tableColumnTypes),
- }
-}
-
-// Name returns the rule name.
-func (*IndexPrimaryKeyTypeAllowlistRule) Name() string {
+func (*indexPrimaryKeyTypeAllowlistOmniRule) Name() string {
return "IndexPrimaryKeyTypeAllowlistRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexPrimaryKeyTypeAllowlistRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *indexPrimaryKeyTypeAllowlistOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*IndexPrimaryKeyTypeAllowlistRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *IndexPrimaryKeyTypeAllowlistRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
+func (r *indexPrimaryKeyTypeAllowlistOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- switch {
- case tableElement.ColumnDefinition() != nil:
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ if _, exists := r.allowlist[columnType]; !exists {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.IndexPKType.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", col.Name, tableName, columnType),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(col.Loc))),
+ })
+ }
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- case tableElement.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, tableElement.TableConstraintDef())
- default:
}
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ r.checkConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
}
-func (r *IndexPrimaryKeyTypeAllowlistRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *indexPrimaryKeyTypeAllowlistOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add column
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.Identifier() != nil:
- switch {
- case alterListItem.Identifier() != nil && alterListItem.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- case alterListItem.OPEN_PAR_SYMBOL() != nil && alterListItem.TableElementList() != nil:
- for _, tableElement := range alterListItem.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ if _, exists := r.allowlist[columnType]; !exists {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.IndexPKType.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", col.Name, tableName, columnType),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
+ })
+ }
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
- default:
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
}
- // modify column
- case alterListItem.MODIFY_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- // change column
- case alterListItem.CHANGE_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil && alterListItem.Identifier() != nil:
- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.tablesNewColumns.delete(tableName, oldColumnName)
- newColumnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, newColumnName, alterListItem.FieldDefinition())
- // add constriant.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, alterListItem.TableConstraintDef())
- default:
- }
- }
-}
-
-func (r *IndexPrimaryKeyTypeAllowlistRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if ctx.DataType() == nil {
- return
- }
- // columnType := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.DataType())
- // columnType = strings.ToLower(columnType)
- columnType := mysqlparser.NormalizeMySQLDataType(ctx.DataType(), true /* compact */)
- for _, attribute := range ctx.AllColumnAttribute() {
- if attribute.PRIMARY_SYMBOL() != nil {
- if _, exists := r.allowlist[columnType]; !exists {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.IndexPKType.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", columnName, tableName, columnType),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ case ast.ATModifyColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ if _, exists := r.allowlist[columnType]; !exists {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.IndexPKType.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", col.Name, tableName, columnType),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
+ })
+ }
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATChangeColumn:
+ r.tablesNewColumns.delete(tableName, cmd.Name)
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ if _, exists := r.allowlist[columnType]; !exists {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.IndexPKType.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", col.Name, tableName, columnType),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
+ })
+ }
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
}
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.checkConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
+ }
+ default:
}
}
- r.tablesNewColumns.set(tableName, columnName, columnType)
}
-func (r *IndexPrimaryKeyTypeAllowlistRule) checkConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_().GetTokenType() != mysql.MySQLParserPRIMARY_SYMBOL {
+func (r *indexPrimaryKeyTypeAllowlistOmniRule) checkConstraint(tableName string, constraint *ast.Constraint, line int32) {
+ if constraint.Type != ast.ConstrPrimaryKey {
return
}
- if ctx.KeyListVariants() == nil {
- return
- }
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
-
- for _, columnName := range columnList {
+ for _, columnName := range constraint.Columns {
columnType, err := r.getPKColumnType(tableName, columnName)
if err != nil {
continue
}
columnType = strings.ToLower(columnType)
if _, exists := r.allowlist[columnType]; !exists {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexPKType.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("The column `%s` in table `%s` is one of the primary key, but its type \"%s\" is not in allowlist", columnName, tableName, columnType),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
}
}
-// getPKColumnType gets the column type string from r.tablesNewColumns or catalog, returns empty string and non-nil error if cannot find the column in given table.
-func (r *IndexPrimaryKeyTypeAllowlistRule) getPKColumnType(tableName string, columnName string) (string, error) {
+func (r *indexPrimaryKeyTypeAllowlistOmniRule) getPKColumnType(tableName string, columnName string) (string, error) {
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
index 84a224c290..4d8592d584 100644
--- a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
+++ b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
@@ -5,17 +5,13 @@ import (
"fmt"
"slices"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -44,74 +40,44 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewIndexTotalNumberLimitRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number), checkCtx.FinalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexTotalNumberLimitOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ max: int(numberPayload.Number),
+ lineForTable: make(map[string]int),
+ finalMetadata: checkCtx.FinalMetadata,
}
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
return rule.generateAdvice(), nil
}
-// IndexTotalNumberLimitRule checks for index total number limit.
-type IndexTotalNumberLimitRule struct {
- BaseRule
+type indexTotalNumberLimitOmniRule struct {
+ OmniBaseRule
max int
lineForTable map[string]int
finalMetadata *model.DatabaseMetadata
}
-// NewIndexTotalNumberLimitRule creates a new IndexTotalNumberLimitRule.
-func NewIndexTotalNumberLimitRule(level storepb.Advice_Status, title string, maxIndexes int, finalMetadata *model.DatabaseMetadata) *IndexTotalNumberLimitRule {
- return &IndexTotalNumberLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- max: maxIndexes,
- lineForTable: make(map[string]int),
- finalMetadata: finalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*IndexTotalNumberLimitRule) Name() string {
+func (*indexTotalNumberLimitOmniRule) Name() string {
return "IndexTotalNumberLimitRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexTotalNumberLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *indexTotalNumberLimitOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*IndexTotalNumberLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *IndexTotalNumberLimitRule) generateAdvice() []*storepb.Advice {
+func (r *indexTotalNumberLimitOmniRule) generateAdvice() []*storepb.Advice {
type tableName struct {
name string
line int
@@ -141,114 +107,73 @@ func (r *IndexTotalNumberLimitRule) generateAdvice() []*storepb.Advice {
}
tableInfo := schema.GetTable(table.name)
if tableInfo != nil && len(tableInfo.GetProto().Indexes) > r.max {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexCountExceedsLimit.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("The count of index in table `%s` should be no more than %d, but found %d", table.name, r.max, len(tableInfo.GetProto().Indexes)),
StartPosition: common.ConvertANTLRLineToPosition(table.line),
})
}
}
- return r.adviceList
+ return r.Advice
}
-func (r *IndexTotalNumberLimitRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
+func (r *indexTotalNumberLimitOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+ tableName := n.Table.Name
+ r.lineForTable[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
}
-func (r *IndexTotalNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {
+func (r *indexTotalNumberLimitOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+ tableName := n.Table.Name
+ r.lineForTable[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
}
-func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *indexTotalNumberLimitOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add column.
- case item.ADD_SYMBOL() != nil:
- switch {
- // add single columns.
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
- // add multi columns.
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil {
+ continue
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.lineForTable[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
}
- r.checkFieldDefinitionContext(tableName, tableElement.ColumnDefinition().FieldDefinition())
}
- // add constraint.
- case item.TableConstraintDef() != nil:
- r.checkTableConstraintDef(tableName, item.TableConstraintDef())
- default:
}
- // change column.
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
- // modify column.
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
- default:
- continue
- }
- }
-}
-
-func (r *IndexTotalNumberLimitRule) checkFieldDefinitionContext(tableName string, ctx mysql.IFieldDefinitionContext) {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr == nil || attr.GetValue() == nil {
- continue
- }
- switch attr.GetValue().GetTokenType() {
- case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ if cmd.Column != nil {
+ for _, c := range cmd.Column.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.lineForTable[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
+ }
+ }
+ }
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ switch cmd.Constraint.Type {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex, ast.ConstrFulltextIndex:
+ r.lineForTable[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
+ default:
+ }
+ }
default:
}
}
}
-
-func (r *IndexTotalNumberLimitRule) checkTableConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_() == nil {
- return
- }
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserFULLTEXT_SYMBOL:
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
- default:
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_index_type_allow_list.go b/backend/plugin/advisor/mysql/rule_index_type_allow_list.go
index 893068aebc..5bf0b75b17 100644
--- a/backend/plugin/advisor/mysql/rule_index_type_allow_list.go
+++ b/backend/plugin/advisor/mysql/rule_index_type_allow_list.go
@@ -5,16 +5,12 @@ import (
"fmt"
"slices"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -37,162 +33,116 @@ func (*IndexTypeAllowListAdvisor) Check(_ context.Context, checkCtx advisor.Cont
stringArrayPayload := checkCtx.Rule.GetStringArrayPayload()
- // Create the rule
- rule := NewIndexTypeAllowListRule(level, checkCtx.Rule.Type.String(), stringArrayPayload.List)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexTypeAllowListOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ allowList: stringArrayPayload.List,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexTypeAllowListRule checks for index types.
-type IndexTypeAllowListRule struct {
- BaseRule
+type indexTypeAllowListOmniRule struct {
+ OmniBaseRule
allowList []string
}
-// NewIndexTypeAllowListRule creates a new IndexTypeAllowListRule.
-func NewIndexTypeAllowListRule(level storepb.Advice_Status, title string, allowList []string) *IndexTypeAllowListRule {
- return &IndexTypeAllowListRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- allowList: allowList,
- }
-}
-
-// Name returns the rule name.
-func (*IndexTypeAllowListRule) Name() string {
+func (*indexTypeAllowListOmniRule) Name() string {
return "IndexTypeAllowListRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexTypeAllowListRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *indexTypeAllowListOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
- // No action required for other node types
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*IndexTypeAllowListRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *IndexTypeAllowListRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *indexTypeAllowListOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
- return
- }
-
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil || tableElement.TableConstraintDef() == nil {
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- r.handleConstraintDef(tableElement.TableConstraintDef())
+ r.handleConstraint(constraint, r.LocToLine(constraint.Loc))
}
}
-func (r *IndexTypeAllowListRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *indexTypeAllowListOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
-
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- if alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil {
- r.handleConstraintDef(alterListItem.TableConstraintDef())
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.handleConstraint(cmd.Constraint, r.LocToLine(n.Loc))
+ }
+ default:
}
}
}
-func (r *IndexTypeAllowListRule) handleConstraintDef(ctx mysql.ITableConstraintDefContext) {
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL:
+func (r *indexTypeAllowListOmniRule) handleConstraint(constraint *ast.Constraint, line int32) {
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex, ast.ConstrFulltextIndex, ast.ConstrSpatialIndex:
default:
return
}
indexType := "BTREE"
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexType() != nil {
- indexType = ctx.IndexNameAndType().IndexType().GetText()
+ if constraint.IndexType != "" {
+ indexType = constraint.IndexType
} else {
- if ctx.FULLTEXT_SYMBOL() != nil {
+ switch constraint.Type {
+ case ast.ConstrFulltextIndex:
indexType = "FULLTEXT"
- } else if ctx.SPATIAL_SYMBOL() != nil {
+ case ast.ConstrSpatialIndex:
indexType = "SPATIAL"
+ default:
}
}
- r.validateIndexType(indexType, ctx.GetStart().GetLine())
+ r.validateIndexType(indexType, line)
}
-func (r *IndexTypeAllowListRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+func (r *indexTypeAllowListOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
}
indexType := "BTREE"
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexType() != nil {
- indexType = ctx.IndexNameAndType().IndexType().GetText()
- } else {
- if ctx.FULLTEXT_SYMBOL() != nil {
- indexType = "FULLTEXT"
- } else if ctx.SPATIAL_SYMBOL() != nil {
- indexType = "SPATIAL"
- }
- }
- r.validateIndexType(indexType, ctx.GetStart().GetLine())
+ if n.IndexType != "" {
+ indexType = n.IndexType
+ } else if n.Fulltext {
+ indexType = "FULLTEXT"
+ } else if n.Spatial {
+ indexType = "SPATIAL"
+ }
+ r.validateIndexType(indexType, r.LocToLine(n.Loc))
}
-// validateIndexType checks if the index type is in the allow list.
-func (r *IndexTypeAllowListRule) validateIndexType(indexType string, line int) {
+func (r *indexTypeAllowListOmniRule) validateIndexType(indexType string, line int32) {
if slices.Contains(r.allowList, indexType) {
return
}
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexTypeNotAllowed.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Index type `%s` is not allowed", indexType),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + line),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
diff --git a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
index 88a7082e9e..d109f56ecf 100644
--- a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
+++ b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
@@ -5,17 +5,13 @@ import (
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -40,225 +36,147 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context
return nil, err
}
- // Create the rule
- rule := NewIndexTypeNoBlobRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &indexTypeNoBlobOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ originalMetadata: checkCtx.OriginalMetadata,
+ tablesNewColumns: make(tableColumnTypes),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// IndexTypeNoBlobRule checks for index type no blob.
-type IndexTypeNoBlobRule struct {
- BaseRule
+type indexTypeNoBlobOmniRule struct {
+ OmniBaseRule
originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
-// NewIndexTypeNoBlobRule creates a new IndexTypeNoBlobRule.
-func NewIndexTypeNoBlobRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *IndexTypeNoBlobRule {
- return &IndexTypeNoBlobRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- originalMetadata: originalMetadata,
- tablesNewColumns: make(tableColumnTypes),
- }
-}
-
-// Name returns the rule name.
-func (*IndexTypeNoBlobRule) Name() string {
+func (*indexTypeNoBlobOmniRule) Name() string {
return "IndexTypeNoBlobRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *IndexTypeNoBlobRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *indexTypeNoBlobOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*IndexTypeNoBlobRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *IndexTypeNoBlobRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *indexTypeNoBlobOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil || col.TypeName == nil {
continue
}
- switch {
- case tableElement.ColumnDefinition() != nil:
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.addAdvice(tableName, col.Name, columnType, r.LocToLine(col.Loc))
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- case tableElement.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, tableElement.TableConstraintDef())
- default:
}
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ r.checkConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
}
-func (r *IndexTypeNoBlobRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *indexTypeNoBlobOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- case alterListItem.ADD_SYMBOL() != nil:
- switch {
- // add column.
- case alterListItem.Identifier() != nil && alterListItem.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- // add multi column.
- case alterListItem.OPEN_PAR_SYMBOL() != nil && alterListItem.TableElementList() != nil:
- for _, tableElement := range alterListItem.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col == nil || col.TypeName == nil {
+ continue
+ }
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.addAdvice(tableName, col.Name, columnType, r.LocToLine(n.Loc))
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATModifyColumn:
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.addAdvice(tableName, col.Name, columnType, r.LocToLine(n.Loc))
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
- // add constraint.
- case alterListItem.TableConstraintDef() != nil:
- r.checkConstraintDef(tableName, alterListItem.TableConstraintDef())
- default:
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATChangeColumn:
+ r.tablesNewColumns.delete(tableName, cmd.Name)
+ if cmd.Column != nil && cmd.Column.TypeName != nil {
+ col := cmd.Column
+ columnType := omniDataTypeNameCompact(col.TypeName)
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey || c.Type == ast.ColConstrUnique {
+ r.addAdvice(tableName, col.Name, columnType, r.LocToLine(n.Loc))
+ }
+ }
+ r.tablesNewColumns.set(tableName, col.Name, columnType)
+ }
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.checkConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
}
- // modify column
- case alterListItem.MODIFY_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, alterListItem.FieldDefinition())
- // change column
- case alterListItem.CHANGE_SYMBOL() != nil && alterListItem.ColumnInternalRef() != nil && alterListItem.Identifier() != nil:
- oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(alterListItem.ColumnInternalRef())
- r.tablesNewColumns.delete(tableName, oldColumnName)
- newColumnName := mysqlparser.NormalizeMySQLIdentifier(alterListItem.Identifier())
- r.checkFieldDefinition(tableName, newColumnName, alterListItem.FieldDefinition())
default:
}
}
}
-func (r *IndexTypeNoBlobRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.GetType_() == nil {
- return
- }
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL, mysql.MySQLParserFOREIGN_SYMBOL:
+func (r *indexTypeNoBlobOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
- default:
}
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+ if n.Fulltext || n.Spatial {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
+ tableName := n.Table.Name
+ columnList := omniIndexColumns(n.Columns)
for _, columnName := range columnList {
columnType, err := r.getColumnType(tableName, columnName)
if err != nil {
continue
}
columnType = strings.ToLower(columnType)
- r.addAdvice(tableName, columnName, columnType, ctx.GetStart().GetLine())
- }
-}
-
-func (r *IndexTypeNoBlobRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if ctx.DataType() == nil {
- return
- }
- columnType := mysqlparser.NormalizeMySQLDataType(ctx.DataType(), true /* compact */)
- for _, attribute := range ctx.AllColumnAttribute() {
- if attribute == nil || attribute.GetValue() == nil {
- continue
- }
- // the FieldDefinitionContext can only set primary or unique.
- switch attribute.GetValue().GetTokenType() {
- case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- // do nothing
- default:
- continue
- }
- r.addAdvice(tableName, columnName, columnType, ctx.GetStart().GetLine())
+ r.addAdvice(tableName, columnName, columnType, r.LocToLine(n.Loc))
}
- r.tablesNewColumns.set(tableName, columnName, columnType)
}
-func (r *IndexTypeNoBlobRule) checkConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_() == nil {
- return
- }
+func (r *indexTypeNoBlobOmniRule) checkConstraint(tableName string, constraint *ast.Constraint, line int32) {
var columnList []string
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- if ctx.KeyListVariants() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
- case mysql.MySQLParserFOREIGN_SYMBOL:
- if ctx.KeyList() == nil {
- return
- }
- columnList = mysqlparser.NormalizeKeyList(ctx.KeyList())
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex:
+ columnList = constraint.Columns
+ case ast.ConstrForeignKey:
+ columnList = constraint.Columns
default:
return
}
@@ -269,22 +187,23 @@ func (r *IndexTypeNoBlobRule) checkConstraintDef(tableName string, ctx mysql.ITa
continue
}
columnType = strings.ToLower(columnType)
- r.addAdvice(tableName, columnName, columnType, ctx.GetStart().GetLine())
+ r.addAdvice(tableName, columnName, columnType, line)
}
}
-func (r *IndexTypeNoBlobRule) addAdvice(tableName, columnName, columnType string, lineNumber int) {
- if r.isBlob(columnType) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+func (r *indexTypeNoBlobOmniRule) addAdvice(tableName, columnName, columnType string, line int32) {
+ if isBlob(columnType) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.IndexTypeNoBlob.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Columns in index must not be BLOB but `%s`.`%s` is %s", tableName, columnName, columnType),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
}
-func (*IndexTypeNoBlobRule) isBlob(columnType string) bool {
+
+func isBlob(columnType string) bool {
switch strings.ToLower(columnType) {
case "blob", "tinyblob", "mediumblob", "longblob":
return true
@@ -293,8 +212,7 @@ func (*IndexTypeNoBlobRule) isBlob(columnType string) bool {
}
}
-// getColumnType gets the column type string from r.tableColumnTypes or catalog, returns empty string and non-nil error if cannot find the column in given table.
-func (r *IndexTypeNoBlobRule) getColumnType(tableName string, columnName string) (string, error) {
+func (r *indexTypeNoBlobOmniRule) getColumnType(tableName string, columnName string) (string, error) {
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_insert_disallow_order_by_rand.go b/backend/plugin/advisor/mysql/rule_insert_disallow_order_by_rand.go
index 8ea2273143..b101827ce4 100644
--- a/backend/plugin/advisor/mysql/rule_insert_disallow_order_by_rand.go
+++ b/backend/plugin/advisor/mysql/rule_insert_disallow_order_by_rand.go
@@ -5,15 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -39,104 +36,71 @@ func (*InsertDisallowOrderByRandAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- // Create the rule
- rule := NewInsertDisallowOrderByRandRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &insertDisallowOrderByRandOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// InsertDisallowOrderByRandRule checks for to disallow order by rand in INSERT statements.
-type InsertDisallowOrderByRandRule struct {
- BaseRule
- isInsertStmt bool
- text string
+type insertDisallowOrderByRandOmniRule struct {
+ OmniBaseRule
}
-// NewInsertDisallowOrderByRandRule creates a new InsertDisallowOrderByRandRule.
-func NewInsertDisallowOrderByRandRule(level storepb.Advice_Status, title string) *InsertDisallowOrderByRandRule {
- return &InsertDisallowOrderByRandRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*InsertDisallowOrderByRandRule) Name() string {
+func (*insertDisallowOrderByRandOmniRule) Name() string {
return "InsertDisallowOrderByRandRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *InsertDisallowOrderByRandRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeInsertStatement:
- insertCtx, ok := ctx.(*mysql.InsertStatementContext)
- if !ok {
- return nil
- }
- if mysqlparser.IsTopMySQLRule(&insertCtx.BaseParserRuleContext) {
- if insertCtx.InsertQueryExpression() != nil {
- r.isInsertStmt = true
- }
- }
- case NodeTypeQueryExpression:
- r.checkQueryExpression(ctx.(*mysql.QueryExpressionContext))
- default:
- // Ignore other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *InsertDisallowOrderByRandRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeInsertStatement {
- r.isInsertStmt = false
+func (r *insertDisallowOrderByRandOmniRule) OnStatement(node ast.Node) {
+ ins, ok := node.(*ast.InsertStmt)
+ if !ok {
+ return
}
- return nil
-}
-func (r *InsertDisallowOrderByRandRule) checkQueryExpression(ctx *mysql.QueryExpressionContext) {
- if !r.isInsertStmt {
+ // Only check INSERT ... SELECT statements.
+ if ins.Select == nil {
return
}
- if ctx.OrderClause() == nil || ctx.OrderClause().OrderList() == nil {
+ text := r.TrimmedStmtText() + ";"
+
+ r.checkSelectForRandOrderBy(ins.Select, text)
+}
+
+func (r *insertDisallowOrderByRandOmniRule) checkSelectForRandOrderBy(sel *ast.SelectStmt, text string) {
+ if sel == nil {
return
}
- for _, expr := range ctx.OrderClause().OrderList().AllOrderExpression() {
- text := expr.GetText()
- if strings.EqualFold(text, RandFn) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ for _, item := range sel.OrderBy {
+ if r.isRandFunc(item.Expr) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.InsertUseOrderByRand.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" uses ORDER BY RAND in the INSERT statement", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" uses ORDER BY RAND in the INSERT statement", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
})
}
}
+
+ // Check set operations.
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelectForRandOrderBy(sel.Left, text)
+ r.checkSelectForRandOrderBy(sel.Right, text)
+ }
+}
+
+func (*insertDisallowOrderByRandOmniRule) isRandFunc(expr ast.ExprNode) bool {
+ if expr == nil {
+ return false
+ }
+ fc, ok := expr.(*ast.FuncCallExpr)
+ if !ok {
+ return false
+ }
+ return strings.EqualFold(fc.Name, "rand")
}
diff --git a/backend/plugin/advisor/mysql/rule_insert_must_specify_column.go b/backend/plugin/advisor/mysql/rule_insert_must_specify_column.go
index dccbb789f9..c0d804698e 100644
--- a/backend/plugin/advisor/mysql/rule_insert_must_specify_column.go
+++ b/backend/plugin/advisor/mysql/rule_insert_must_specify_column.go
@@ -4,15 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -36,107 +33,52 @@ func (*InsertMustSpecifyColumnAdvisor) Check(_ context.Context, checkCtx advisor
return nil, err
}
- // Create the rule
- rule := NewInsertMustSpecifyColumnRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// InsertMustSpecifyColumnRule checks for to enforce column specified.
-type InsertMustSpecifyColumnRule struct {
- BaseRule
- hasSelect bool
- text string
-}
-
-// NewInsertMustSpecifyColumnRule creates a new InsertMustSpecifyColumnRule.
-func NewInsertMustSpecifyColumnRule(level storepb.Advice_Status, title string) *InsertMustSpecifyColumnRule {
- return &InsertMustSpecifyColumnRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &insertMustSpecifyColumnOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*InsertMustSpecifyColumnRule) Name() string {
- return "InsertMustSpecifyColumnRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *InsertMustSpecifyColumnRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeInsertStatement:
- r.checkInsertStatement(ctx.(*mysql.InsertStatementContext))
- case NodeTypeSelectItemList:
- r.checkSelectItemList(ctx.(*mysql.SelectItemListContext))
- default:
- // Other node types
- }
- return nil
+type insertMustSpecifyColumnOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*InsertMustSpecifyColumnRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*insertMustSpecifyColumnOmniRule) Name() string {
+ return "InsertMustSpecifyColumnRule"
}
-func (r *InsertMustSpecifyColumnRule) checkInsertStatement(ctx *mysql.InsertStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *insertMustSpecifyColumnOmniRule) OnStatement(node ast.Node) {
+ ins, ok := node.(*ast.InsertStmt)
+ if !ok {
return
}
- if ctx.InsertQueryExpression() != nil {
- r.hasSelect = true
- }
- if ctx.InsertFromConstructor() == nil {
- return
- }
+ text := r.TrimmedStmtText() + ";"
- if ctx.InsertFromConstructor() != nil && ctx.InsertFromConstructor().Fields() != nil && len(ctx.InsertFromConstructor().Fields().AllInsertIdentifier()) > 0 {
- // has columns.
+ // INSERT ... SELECT without columns.
+ if ins.Select != nil && len(ins.Columns) == 0 {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.InsertNotSpecifyColumn.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
+ })
return
}
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.InsertNotSpecifyColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
-}
-func (r *InsertMustSpecifyColumnRule) checkSelectItemList(ctx *mysql.SelectItemListContext) {
- if r.hasSelect && ctx.MULT_OPERATOR() != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ // INSERT ... VALUES without columns.
+ if len(ins.Values) > 0 && len(ins.Columns) == 0 {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.InsertNotSpecifyColumn.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_insert_row_limit.go b/backend/plugin/advisor/mysql/rule_insert_row_limit.go
index 3bb78410ad..69e4742815 100644
--- a/backend/plugin/advisor/mysql/rule_insert_row_limit.go
+++ b/backend/plugin/advisor/mysql/rule_insert_row_limit.go
@@ -2,19 +2,17 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
"strconv"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -43,155 +41,91 @@ func (*InsertRowLimitAdvisor) Check(ctx context.Context, checkCtx advisor.Contex
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewInsertRowLimitRule(ctx, level, checkCtx.Rule.Type.String(), int(numberPayload.Number), checkCtx.Driver)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ maxRow := int(numberPayload.Number)
+ driver := checkCtx.Driver
+ var advice []*storepb.Advice
+ explainCount := 0
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.GetExplainCount() >= common.MaximumLintExplainSize {
- break
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// InsertRowLimitRule checks for insert row limit.
-type InsertRowLimitRule struct {
- BaseRule
- text string
- line int
- maxRow int
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
-
-// NewInsertRowLimitRule creates a new InsertRowLimitRule.
-func NewInsertRowLimitRule(ctx context.Context, level storepb.Advice_Status, title string, maxRow int, driver *sql.DB) *InsertRowLimitRule {
- return &InsertRowLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maxRow: maxRow,
- driver: driver,
- ctx: ctx,
- }
-}
-
-// Name returns the rule name.
-func (*InsertRowLimitRule) Name() string {
- return "InsertRowLimitRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *InsertRowLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
+ ins, ok := node.(*ast.InsertStmt)
if !ok {
- return nil
+ continue
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeInsertStatement:
- r.checkInsertStatement(ctx.(*mysql.InsertStatementContext))
- default:
- }
- return nil
-}
-// OnExit is called when exiting a parse tree node.
-func (*InsertRowLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
+ baseLine := stmt.BaseLine()
+ text := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
-// GetExplainCount returns the explain count.
-func (r *InsertRowLimitRule) GetExplainCount() int {
- return r.explainCount
-}
-
-func (r *InsertRowLimitRule) checkInsertStatement(ctx *mysql.InsertStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- r.line = r.baseLine + ctx.GetStart().GetLine()
- if ctx.InsertQueryExpression() != nil {
- r.handleInsertQueryExpression(ctx.InsertQueryExpression())
- }
- r.handleNoInsertQueryExpression(ctx)
-}
+ // INSERT ... SELECT: use EXPLAIN to count rows.
+ if ins.Select != nil {
+ if driver == nil {
+ continue
+ }
+ explainCount++
+ res, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", text))
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.InsertTooManyRows.Int32(),
+ Title: checkCtx.Rule.Type.String(),
+ Content: fmt.Sprintf("\"%s\" dry runs failed: %s", text, err.Error()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else {
+ rowCount, err := getInsertRows(res)
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.Internal.Int32(),
+ Title: checkCtx.Rule.Type.String(),
+ Content: fmt.Sprintf("failed to get row count for \"%s\": %s", text, err.Error()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if rowCount > int64(maxRow) {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.InsertTooManyRows.Int32(),
+ Title: checkCtx.Rule.Type.String(),
+ Content: fmt.Sprintf("\"%s\" inserts %d rows. The count exceeds %d.", text, rowCount, maxRow),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
+ continue
+ }
-func (r *InsertRowLimitRule) handleInsertQueryExpression(ctx mysql.IInsertQueryExpressionContext) {
- if r.driver == nil || ctx == nil {
- return
+ // INSERT ... VALUES: count value rows directly.
+ if len(ins.Values) > maxRow {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.InsertTooManyRows.Int32(),
+ Title: checkCtx.Rule.Type.String(),
+ Content: fmt.Sprintf("\"%s\" inserts %d rows. The count exceeds %d.", text, len(ins.Values), maxRow),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
}
- r.explainCount++
- res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", r.text))
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.InsertTooManyRows.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" dry runs failed: %s", r.text, err.Error()),
- StartPosition: common.ConvertANTLRLineToPosition(r.line),
- })
- return
- }
- rowCount, err := getInsertRows(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("failed to get row count for \"%s\": %s", r.text, err.Error()),
- StartPosition: common.ConvertANTLRLineToPosition(r.line),
- })
- } else if rowCount > int64(r.maxRow) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.InsertTooManyRows.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" inserts %d rows. The count exceeds %d.", r.text, rowCount, r.maxRow),
- StartPosition: common.ConvertANTLRLineToPosition(r.line),
- })
- }
+ return advice, nil
}
-func (r *InsertRowLimitRule) handleNoInsertQueryExpression(ctx mysql.IInsertStatementContext) {
- if ctx.InsertFromConstructor() == nil {
- return
- }
- if ctx.InsertFromConstructor().InsertValues() == nil {
- return
- }
- if ctx.InsertFromConstructor().InsertValues().ValueList() == nil {
- return
- }
-
- allValues := ctx.InsertFromConstructor().InsertValues().ValueList().AllValues()
- if len(allValues) > r.maxRow {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.InsertTooManyRows.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" inserts %d rows. The count exceeds %d.", r.text, len(allValues), r.maxRow),
- StartPosition: common.ConvertANTLRLineToPosition(r.line),
- })
+func contentStartIndex(text string) int {
+ for i, c := range text {
+ if c != ' ' && c != '\t' && c != '\n' && c != '\r' {
+ return i
+ }
}
+ return 0
}
func getInsertRows(res []any) (int64, error) {
@@ -211,25 +145,6 @@ func getInsertRows(res []any) (int64, error) {
return 0, errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // the column 9 is the data 'rows'.
- // the first not-NULL value of column 9 is the affected rows count.
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
-
rowsIndex, err := getColumnIndex(columns, "rows")
if err != nil {
return 0, errors.Errorf("failed to find rows column")
diff --git a/backend/plugin/advisor/mysql/rule_migration_compatibility.go b/backend/plugin/advisor/mysql/rule_migration_compatibility.go
index bba1eff5e7..66ebb121b0 100644
--- a/backend/plugin/advisor/mysql/rule_migration_compatibility.go
+++ b/backend/plugin/advisor/mysql/rule_migration_compatibility.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -37,241 +33,125 @@ func (*CompatibilityAdvisor) Check(_ context.Context, checkCtx advisor.Context)
return nil, err
}
- // Create the rule
- rule := NewCompatibilityRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &compatibilityOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// CompatibilityRule checks for schema backward compatibility.
-type CompatibilityRule struct {
- BaseRule
- text string
+type compatibilityOmniRule struct {
+ OmniBaseRule
lastCreateTable string
- code code.Code
}
-// NewCompatibilityRule creates a new CompatibilityRule.
-func NewCompatibilityRule(level storepb.Advice_Status, title string) *CompatibilityRule {
- return &CompatibilityRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*CompatibilityRule) Name() string {
+func (*compatibilityOmniRule) Name() string {
return "CompatibilityRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *CompatibilityRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- r.code = code.Ok
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeDropDatabase:
- r.checkDropDatabase(ctx.(*mysql.DropDatabaseContext))
- case NodeTypeRenameTableStatement:
- r.checkRenameTableStatement(ctx.(*mysql.RenameTableStatementContext))
- case NodeTypeDropTable:
- r.checkDropTable(ctx.(*mysql.DropTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *compatibilityOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.DropDatabaseStmt:
+ r.addIncompat(code.CompatibilityDropDatabase, n.Loc)
+ case *ast.RenameTableStmt:
+ r.addIncompat(code.CompatibilityRenameTable, n.Loc)
+ case *ast.DropTableStmt:
+ r.addIncompat(code.CompatibilityDropTable, n.Loc)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *CompatibilityRule) OnExit(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeQuery && r.code != code.Ok {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: r.code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
- return nil
+func (r *compatibilityOmniRule) addIncompat(c code.Code, loc ast.Loc) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: c.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(loc))),
+ })
}
-func (r *CompatibilityRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
+func (r *compatibilityOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil || len(n.Columns) == 0 {
return
}
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.lastCreateTable = tableName
-}
-
-func (r *CompatibilityRule) checkDropDatabase(ctx *mysql.DropDatabaseContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- r.code = code.CompatibilityDropDatabase
-}
-
-func (r *CompatibilityRule) checkRenameTableStatement(ctx *mysql.RenameTableStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- r.code = code.CompatibilityRenameTable
-}
-
-func (r *CompatibilityRule) checkDropTable(ctx *mysql.DropTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- r.code = code.CompatibilityDropTable
+ r.lastCreateTable = n.Table.Name
}
-func (r *CompatibilityRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
+func (r *compatibilityOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+ tableName := n.Table.Name
if tableName == r.lastCreateTable {
return
}
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- if item.RENAME_SYMBOL() != nil && item.COLUMN_SYMBOL() != nil {
- r.code = code.CompatibilityRenameColumn
+ switch cmd.Type {
+ case ast.ATRenameColumn:
+ r.addIncompat(code.CompatibilityRenameColumn, n.Loc)
return
- }
-
- if item.DROP_SYMBOL() != nil && item.ColumnInternalRef() != nil {
- r.code = code.CompatibilityDropColumn
+ case ast.ATDropColumn:
+ r.addIncompat(code.CompatibilityDropColumn, n.Loc)
return
- }
- if item.DROP_SYMBOL() != nil && item.TableName() != nil {
- r.code = code.CompatibilityRenameTable
+ case ast.ATRenameTable:
+ r.addIncompat(code.CompatibilityRenameTable, n.Loc)
return
- }
-
- if item.ADD_SYMBOL() != nil {
- if item.TableConstraintDef() != nil {
- if item.TableConstraintDef().GetType_() == nil {
- continue
- }
- switch item.TableConstraintDef().GetType_().GetTokenType() {
- // add primary key.
- case mysql.MySQLParserPRIMARY_SYMBOL:
- r.code = code.CompatibilityAddPrimaryKey
- return
- // add unique key.
- case mysql.MySQLParserUNIQUE_SYMBOL:
- r.code = code.CompatibilityAddUniqueKey
- return
- // add foreign key.
- case mysql.MySQLParserFOREIGN_SYMBOL:
- r.code = code.CompatibilityAddForeignKey
- return
- default:
- }
+ case ast.ATAddConstraint:
+ if cmd.Constraint == nil {
+ continue
}
-
- // add check enforced.
- // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
- if item.TableConstraintDef() != nil && item.TableConstraintDef().CheckConstraint() != nil && item.TableConstraintDef().ConstraintEnforcement() != nil {
- r.code = code.CompatibilityAddCheck
+ switch cmd.Constraint.Type {
+ case ast.ConstrPrimaryKey:
+ r.addIncompat(code.CompatibilityAddPrimaryKey, n.Loc)
return
- }
- }
-
- // add check enforced.
- // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
- if item.ALTER_SYMBOL() != nil && item.CHECK_SYMBOL() != nil {
- if item.ConstraintEnforcement() != nil {
- r.code = code.CompatibilityAlterCheck
+ case ast.ConstrUnique:
+ r.addIncompat(code.CompatibilityAddUniqueKey, n.Loc)
+ return
+ case ast.ConstrForeignKey:
+ r.addIncompat(code.CompatibilityAddForeignKey, n.Loc)
return
+ case ast.ConstrCheck:
+ // add check enforced
+ if !cmd.Constraint.NotEnforced {
+ r.addIncompat(code.CompatibilityAddCheck, n.Loc)
+ return
+ }
+ default:
}
- }
-
- // MODIFY COLUMN / CHANGE COLUMN
- // Due to the limitation that we don't know the current data type of the column before the change,
- // so we treat all as incompatible. This generates false positive when:
- // 1. Change to a compatible data type such as INT to BIGINT
- // 2. Change properties such as comment, change it to NULL
- // modify column
- if item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil {
- r.code = code.CompatibilityAlterColumn
+ case ast.ATAlterCheckEnforced:
+ r.addIncompat(code.CompatibilityAlterCheck, n.Loc)
return
- }
- // change column
- if item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil {
- r.code = code.CompatibilityAlterColumn
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ r.addIncompat(code.CompatibilityAlterColumn, n.Loc)
return
+ default:
}
}
}
-func (r *CompatibilityRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.GetType_() == nil {
- return
- }
- if ctx.GetType_().GetTokenType() != mysql.MySQLParserUNIQUE_SYMBOL {
+func (r *compatibilityOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if !n.Unique {
return
}
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
+ tableName := n.Table.Name
if r.lastCreateTable != tableName {
- r.code = code.CompatibilityAddUniqueKey
+ r.addIncompat(code.CompatibilityAddUniqueKey, n.Loc)
}
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_auto_increment_column.go b/backend/plugin/advisor/mysql/rule_naming_auto_increment_column.go
index 924fc3ec0d..60ba813bb3 100644
--- a/backend/plugin/advisor/mysql/rule_naming_auto_increment_column.go
+++ b/backend/plugin/advisor/mysql/rule_naming_auto_increment_column.go
@@ -5,16 +5,13 @@ import (
"fmt"
"regexp"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -52,175 +49,82 @@ func (*NamingAutoIncrementColumnAdvisor) Check(_ context.Context, checkCtx advis
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingAutoIncrementColumnRule(level, checkCtx.Rule.Type.String(), format, maxLength)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// NamingAutoIncrementColumnRule checks for auto-increment naming convention.
-type NamingAutoIncrementColumnRule struct {
- BaseRule
- format *regexp.Regexp
- maxLength int
-}
-
-// NewNamingAutoIncrementColumnRule creates a new NamingAutoIncrementColumnRule.
-func NewNamingAutoIncrementColumnRule(level storepb.Advice_Status, title string, format *regexp.Regexp, maxLength int) *NamingAutoIncrementColumnRule {
- return &NamingAutoIncrementColumnRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &namingAutoIncrementColumnOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
format: format,
maxLength: maxLength,
}
-}
-// Name returns the rule name.
-func (*NamingAutoIncrementColumnRule) Name() string {
- return "NamingAutoIncrementColumnRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingAutoIncrementColumnRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- default:
- }
- return nil
+type namingAutoIncrementColumnOmniRule struct {
+ OmniBaseRule
+ format *regexp.Regexp
+ maxLength int
}
-// OnExit is called when exiting a parse tree node.
-func (*NamingAutoIncrementColumnRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*namingAutoIncrementColumnOmniRule) Name() string {
+ return "NamingAutoIncrementColumnRule"
}
-func (r *NamingAutoIncrementColumnRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil || ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
+func (r *namingAutoIncrementColumnOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table == nil {
+ return
}
- if tableElement.ColumnDefinition() == nil {
- continue
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil || !col.AutoIncrement {
+ continue
+ }
+ r.handleAutoIncrementColumn(tableName, col.Name, r.LocToLine(n.Loc))
}
- if tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ case *ast.AlterTableStmt:
+ tableName := ""
+ if n.Table != nil {
+ tableName = n.Table.Name
}
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- }
-}
-
-func (r *NamingAutoIncrementColumnRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- 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
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddColumn, ast.ATModifyColumn, ast.ATChangeColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ if col.AutoIncrement {
+ r.handleAutoIncrementColumn(tableName, col.Name, r.LocToLine(n.Loc))
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
default:
}
- // modify column
- case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
- default:
}
+ default:
}
}
-func (r *NamingAutoIncrementColumnRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
- if !r.isAutoIncrement(ctx) {
- return
- }
-
+func (r *namingAutoIncrementColumnOmniRule) handleAutoIncrementColumn(tableName, columnName string, lineNumber int32) {
+ absoluteLine := r.BaseLine + int(lineNumber)
if !r.format.MatchString(columnName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingAutoIncrementColumnConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` mismatches auto_increment column naming convention, naming format should be %q", tableName, columnName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
if r.maxLength > 0 && len(columnName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingAutoIncrementColumnConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` mismatches auto_increment column naming convention, its length should be within %d characters", tableName, columnName, r.maxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
}
-
-func (*NamingAutoIncrementColumnRule) isAutoIncrement(ctx mysql.IFieldDefinitionContext) bool {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr.AUTO_INCREMENT_SYMBOL() != nil {
- return true
- }
- }
- return false
-}
diff --git a/backend/plugin/advisor/mysql/rule_naming_column.go b/backend/plugin/advisor/mysql/rule_naming_column.go
index cd8df1f49e..3867496911 100644
--- a/backend/plugin/advisor/mysql/rule_naming_column.go
+++ b/backend/plugin/advisor/mysql/rule_naming_column.go
@@ -5,16 +5,13 @@ import (
"fmt"
"regexp"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var _ advisor.Advisor = (*NamingColumnConventionAdvisor)(nil)
@@ -51,170 +48,88 @@ func (*NamingColumnConventionAdvisor) Check(_ context.Context, checkCtx advisor.
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingColumnRule(level, checkCtx.Rule.Type.String(), format, maxLength)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// NamingColumnRule checks for column naming convention.
-type NamingColumnRule struct {
- BaseRule
- format *regexp.Regexp
- maxLength int
-}
-
-// NewNamingColumnRule creates a new NamingColumnRule.
-func NewNamingColumnRule(level storepb.Advice_Status, title string, format *regexp.Regexp, maxLength int) *NamingColumnRule {
- return &NamingColumnRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &namingColumnOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
format: format,
maxLength: maxLength,
}
-}
-// Name returns the rule name.
-func (*NamingColumnRule) Name() string {
- return "NamingColumnRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingColumnRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- default:
- }
- return nil
+type namingColumnOmniRule struct {
+ OmniBaseRule
+ format *regexp.Regexp
+ maxLength int
}
-// OnExit is called when exiting a parse tree node.
-func (*NamingColumnRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*namingColumnOmniRule) Name() string {
+ return "NamingColumnRule"
}
-func (r *NamingColumnRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
+func (r *namingColumnOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table == nil {
+ return
}
- if tableElement.ColumnDefinition() == nil {
- continue
+ tableName := n.Table.Name
+ for _, col := range n.Columns {
+ if col == nil {
+ continue
+ }
+ r.handleColumn(tableName, col.Name, r.LocToLine(col.Loc))
}
- if tableElement.ColumnDefinition().ColumnName() == nil {
- continue
+ case *ast.AlterTableStmt:
+ tableName := ""
+ if n.Table != nil {
+ tableName = n.Table.Name
}
-
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.handleColumn(tableName, columnName, tableElement.ColumnDefinition().GetStart().GetLine())
- }
-}
-
-func (r *NamingColumnRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- // alter table add column, change column, modify column.
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil {
- continue
- }
-
- switch {
- // add column
- case item.ADD_SYMBOL() != nil:
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.handleColumn(tableName, columnName, item.GetStart().GetLine())
- 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())
- r.handleColumn(tableName, columnName, tableElement.ColumnDefinition().GetStart().GetLine())
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ r.handleColumn(tableName, col.Name, r.LocToLine(n.Loc))
+ }
+ case ast.ATRenameColumn:
+ if cmd.NewName != "" {
+ r.handleColumn(tableName, cmd.NewName, r.LocToLine(n.Loc))
+ }
+ case ast.ATChangeColumn:
+ if cmd.Column != nil {
+ r.handleColumn(tableName, cmd.Column.Name, r.LocToLine(n.Loc))
}
default:
}
- // rename column
- case item.RENAME_SYMBOL() != nil && item.COLUMN_SYMBOL() != nil:
- // only focus on new column-name.
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.handleColumn(tableName, columnName, item.GetStart().GetLine())
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- // only focus on new column-name.
- columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- r.handleColumn(tableName, columnName, item.GetStart().GetLine())
- default:
- continue
}
+ default:
}
}
-func (r *NamingColumnRule) handleColumn(tableName string, columnName string, lineNumber int) {
- // we need to accumulate line number for each statement and elements of statements.
- lineNumber += r.baseLine
+func (r *namingColumnOmniRule) handleColumn(tableName, columnName string, lineNumber int32) {
+ absoluteLine := r.BaseLine + int(lineNumber)
if !r.format.MatchString(columnName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingColumnConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, columnName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
if r.maxLength > 0 && len(columnName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingColumnConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should be within %d characters", tableName, columnName, r.maxLength),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_foreign_key_convention.go b/backend/plugin/advisor/mysql/rule_naming_foreign_key_convention.go
index 124571daa4..d2000c3b1b 100644
--- a/backend/plugin/advisor/mysql/rule_naming_foreign_key_convention.go
+++ b/backend/plugin/advisor/mysql/rule_naming_foreign_key_convention.go
@@ -5,16 +5,13 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -57,29 +54,20 @@ func (*NamingFKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingFKConventionRule(level, checkCtx.Rule.Type.String(), format, maxLength, templateList)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &namingFKOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// indexMetaData is the metadata for foreign key.
+// fkIndexMetaData is the metadata for foreign key.
type fkIndexMetaData struct {
indexName string
tableName string
@@ -87,111 +75,56 @@ type fkIndexMetaData struct {
line int
}
-// NamingFKConventionRule checks for foreign key naming convention.
-type NamingFKConventionRule struct {
- BaseRule
- text string
+type namingFKOmniRule struct {
+ OmniBaseRule
format string
maxLength int
templateList []string
}
-// NewNamingFKConventionRule creates a new NamingFKConventionRule.
-func NewNamingFKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string) *NamingFKConventionRule {
- return &NamingFKConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- }
-}
-
-// Name returns the rule name.
-func (*NamingFKConventionRule) Name() string {
+func (*namingFKOmniRule) Name() string {
return "NamingFKConventionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingFKConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *namingFKOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*NamingFKConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *NamingFKConventionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *namingFKOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
-
+ tableName := n.Table.Name
var indexDataList []*fkIndexMetaData
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.TableConstraintDef() == nil {
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- if metaData := r.handleConstraintDef(tableName, tableElement.TableConstraintDef()); metaData != nil {
+ if metaData := r.handleConstraint(tableName, constraint, r.BaseLine+int(r.LocToLine(constraint.Loc))); metaData != nil {
indexDataList = append(indexDataList, metaData)
}
}
r.handleIndexList(indexDataList)
}
-func (r *NamingFKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
+func (r *namingFKOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := ""
+ if n.Table != nil {
+ tableName = n.Table.Name
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
var indexDataList []*fkIndexMetaData
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- // add constriant.
- if alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil {
- if metaData := r.handleConstraintDef(tableName, alterListItem.TableConstraintDef()); metaData != nil {
+ if cmd.Type == ast.ATAddConstraint && cmd.Constraint != nil {
+ if metaData := r.handleConstraint(tableName, cmd.Constraint, r.BaseLine+int(r.LocToLine(n.Loc))); metaData != nil {
indexDataList = append(indexDataList, metaData)
}
}
@@ -199,32 +132,32 @@ func (r *NamingFKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
r.handleIndexList(indexDataList)
}
-func (r *NamingFKConventionRule) handleIndexList(indexDataList []*fkIndexMetaData) {
+func (r *namingFKOmniRule) handleIndexList(indexDataList []*fkIndexMetaData) {
for _, indexData := range indexDataList {
regex, err := getTemplateRegexp(r.format, r.templateList, indexData.metaData)
if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.Internal.Int32(),
Title: "Internal error for foreign key naming convention rule",
- Content: fmt.Sprintf("%q meet internal error %q", r.text, err.Error()),
+ Content: fmt.Sprintf("%q meet internal error %q", r.TrimmedStmtText(), err.Error()),
})
continue
}
if !regex.MatchString(indexData.indexName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingFKConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Foreign key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
}
if r.maxLength > 0 && len(indexData.indexName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingFKConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Foreign key `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, r.maxLength),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
@@ -232,25 +165,21 @@ func (r *NamingFKConventionRule) handleIndexList(indexDataList []*fkIndexMetaDat
}
}
-func (r *NamingFKConventionRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) *fkIndexMetaData {
- // focus on foreign index.
- if ctx.FOREIGN_SYMBOL() == nil || ctx.KEY_SYMBOL() == nil || ctx.KeyList() == nil || ctx.References() == nil {
+func (*namingFKOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int) *fkIndexMetaData {
+ // Focus on foreign key.
+ if constraint.Type != ast.ConstrForeignKey {
return nil
}
- indexName := ""
- // for compatibility.
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- }
- // use constraint_name if both exist at the same time.
- // for mysql, foreign key use constraint name as unique identifier.
- if ctx.ConstraintName() != nil {
- indexName = mysqlparser.NormalizeConstraintName(ctx.ConstraintName())
+ indexName := constraint.Name
+
+ referencingColumnList := constraint.Columns
+ referencedTable := ""
+ if constraint.RefTable != nil {
+ referencedTable = constraint.RefTable.Name
}
+ referencedColumnList := constraint.RefColumns
- referencingColumnList := mysqlparser.NormalizeKeyList(ctx.KeyList())
- referencedTable, referencedColumnList := r.handleReferences(ctx.References())
metaData := map[string]string{
advisor.ReferencingTableNameTemplateToken: tableName,
advisor.ReferencingColumnNameTemplateToken: strings.Join(referencingColumnList, "_"),
@@ -261,19 +190,6 @@ func (r *NamingFKConventionRule) handleConstraintDef(tableName string, ctx mysql
indexName: indexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
- }
-}
-
-func (*NamingFKConventionRule) handleReferences(ctx mysql.IReferencesContext) (string, []string) {
- tableName := ""
- if ctx.TableRef() != nil {
- _, tableName = mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- }
-
- var columns []string
- if ctx.IdentifierListWithParentheses() != nil {
- columns = mysqlparser.NormalizeIdentifierListWithParentheses(ctx.IdentifierListWithParentheses())
+ line: line,
}
- return tableName, columns
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_identifier_no_keyword.go b/backend/plugin/advisor/mysql/rule_naming_identifier_no_keyword.go
index d2d440af53..e98a1b20bb 100644
--- a/backend/plugin/advisor/mysql/rule_naming_identifier_no_keyword.go
+++ b/backend/plugin/advisor/mysql/rule_naming_identifier_no_keyword.go
@@ -3,16 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
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/advisor/code"
)
var (
@@ -34,106 +32,113 @@ func (*NamingIdentifierNoKeywordAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- // Create the rule
- rule := NewNamingIdentifierNoKeywordRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &namingIdentifierNoKeywordOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// NamingIdentifierNoKeywordRule checks for identifier naming convention without keyword.
-type NamingIdentifierNoKeywordRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewNamingIdentifierNoKeywordRule creates a new NamingIdentifierNoKeywordRule.
-func NewNamingIdentifierNoKeywordRule(level storepb.Advice_Status, title string) *NamingIdentifierNoKeywordRule {
- return &NamingIdentifierNoKeywordRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type namingIdentifierNoKeywordOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*NamingIdentifierNoKeywordRule) Name() string {
+func (*namingIdentifierNoKeywordOmniRule) Name() string {
return "NamingIdentifierNoKeywordRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingIdentifierNoKeywordRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypePureIdentifier:
- r.checkPureIdentifier(ctx.(*mysql.PureIdentifierContext))
- case NodeTypeIdentifierKeyword:
- r.checkIdentifierKeyword(ctx.(*mysql.IdentifierKeywordContext))
+func (r *namingIdentifierNoKeywordOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*NamingIdentifierNoKeywordRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *NamingIdentifierNoKeywordRule) checkPureIdentifier(ctx *mysql.PureIdentifierContext) {
- // The suspect identifier should be always wrapped in backquotes, otherwise a syntax error will be thrown before entering this checker.
- textNode := ctx.BACK_TICK_QUOTED_ID()
- if textNode == nil {
- return
+func (r *namingIdentifierNoKeywordOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table != nil {
+ r.checkIdentifierAtLine(n.Table.Name, r.LocToLine(n.Loc))
}
-
- // Remove backticks as possible.
- identifier := trimBackTicks(textNode.GetText())
- advice := r.checkIdentifier(identifier)
- if advice != nil {
- advice.StartPosition = common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine())
- r.adviceList = append(r.adviceList, advice)
+ for _, col := range n.Columns {
+ if col == nil {
+ continue
+ }
+ r.checkIdentifierAtLine(col.Name, r.findColumnLine(col.Name))
}
-}
-
-func (r *NamingIdentifierNoKeywordRule) checkIdentifierKeyword(ctx *mysql.IdentifierKeywordContext) {
- identifier := ctx.GetText()
- advice := r.checkIdentifier(identifier)
- if advice != nil {
- advice.StartPosition = common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine())
- r.adviceList = append(r.adviceList, advice)
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
+ continue
+ }
+ if constraint.Name != "" {
+ r.checkIdentifierAtLine(constraint.Name, r.LocToLine(constraint.Loc))
+ }
}
}
-func (r *NamingIdentifierNoKeywordRule) checkIdentifier(identifier string) *storepb.Advice {
- if isKeyword(identifier) {
- return &storepb.Advice{
- Status: r.level,
- Code: code.NameIsKeywordIdentifier.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Identifier %q is a keyword and should be avoided", identifier),
+func (r *namingIdentifierNoKeywordOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ for _, col := range omniGetColumnsFromCmd(cmd) {
+ r.checkIdentifierAtLine(col.Name, r.LocToLine(n.Loc))
+ }
+ case ast.ATRenameTable:
+ if cmd.NewName != "" {
+ r.checkIdentifierAtLine(cmd.NewName, r.LocToLine(n.Loc))
+ }
+ case ast.ATRenameColumn:
+ if cmd.NewName != "" {
+ r.checkIdentifierAtLine(cmd.NewName, r.LocToLine(n.Loc))
+ }
+ case ast.ATChangeColumn:
+ if cmd.Column != nil {
+ r.checkIdentifierAtLine(cmd.Column.Name, r.LocToLine(n.Loc))
+ }
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil && cmd.Constraint.Name != "" {
+ r.checkIdentifierAtLine(cmd.Constraint.Name, r.LocToLine(n.Loc))
+ }
+ case ast.ATRenameIndex:
+ if cmd.NewName != "" {
+ r.checkIdentifierAtLine(cmd.NewName, r.LocToLine(n.Loc))
+ }
+ default:
}
}
+}
- return nil
+func (r *namingIdentifierNoKeywordOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.IndexName != "" {
+ r.checkIdentifierAtLine(n.IndexName, r.LocToLine(n.Loc))
+ }
}
-func trimBackTicks(s string) string {
- if len(s) < 2 {
- return s
+func (r *namingIdentifierNoKeywordOmniRule) checkIdentifierAtLine(identifier string, lineNumber int32) {
+ // Strip backticks if present.
+ identifier = strings.Trim(identifier, "`")
+ if !isKeyword(identifier) {
+ return
}
- return s[1 : len(s)-1]
+ absoluteLine := r.BaseLine + int(lineNumber)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.NameIsKeywordIdentifier.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Identifier %q is a keyword and should be avoided", identifier),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
+ })
+}
+
+func (r *namingIdentifierNoKeywordOmniRule) findColumnLine(name string) int32 {
+ return r.FindLineByName(name)
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_index_convention.go b/backend/plugin/advisor/mysql/rule_naming_index_convention.go
index a599ce4acf..207e9db11d 100644
--- a/backend/plugin/advisor/mysql/rule_naming_index_convention.go
+++ b/backend/plugin/advisor/mysql/rule_naming_index_convention.go
@@ -5,16 +5,13 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -66,150 +63,87 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingIndexConventionRule(level, checkCtx.Rule.Type.String(), format, maxLength, templateList, checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &namingIndexOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NamingIndexConventionRule checks for index naming convention.
-type NamingIndexConventionRule struct {
- BaseRule
- text string
+type namingIndexOmniRule struct {
+ OmniBaseRule
format string
maxLength int
templateList []string
originalMetadata *model.DatabaseMetadata
}
-// NewNamingIndexConventionRule creates a new NamingIndexConventionRule.
-func NewNamingIndexConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originalMetadata *model.DatabaseMetadata) *NamingIndexConventionRule {
- return &NamingIndexConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originalMetadata: originalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*NamingIndexConventionRule) Name() string {
+func (*namingIndexOmniRule) Name() string {
return "NamingIndexConventionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingIndexConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *namingIndexOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*NamingIndexConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *NamingIndexConventionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *namingIndexOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
-
+ tableName := n.Table.Name
var indexDataList []*indexMetaData
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- if tableElement.TableConstraintDef() == nil {
- continue
- }
- if metaData := r.handleConstraintDef(tableName, tableElement.TableConstraintDef()); metaData != nil {
+ if metaData := r.handleConstraint(tableName, constraint, r.BaseLine+int(r.LocToLine(constraint.Loc))); metaData != nil {
indexDataList = append(indexDataList, metaData)
}
}
r.handleIndexList(indexDataList)
}
-func (r *NamingIndexConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
+func (r *namingIndexOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := ""
+ if n.Table != nil {
+ tableName = n.Table.Name
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
var indexDataList []*indexMetaData
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add index.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- if metaData := r.handleConstraintDef(tableName, alterListItem.TableConstraintDef()); metaData != nil {
- indexDataList = append(indexDataList, metaData)
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ if metaData := r.handleConstraint(tableName, cmd.Constraint, r.BaseLine+int(r.LocToLine(n.Loc))); metaData != nil {
+ indexDataList = append(indexDataList, metaData)
+ }
}
- // rename index.
- case alterListItem.RENAME_SYMBOL() != nil && alterListItem.KeyOrIndex() != nil && alterListItem.IndexRef() != nil && alterListItem.IndexName() != nil:
- _, _, oldIndexName := mysqlparser.NormalizeIndexRef(alterListItem.IndexRef())
- newIndexName := mysqlparser.NormalizeIndexName(alterListItem.IndexName())
+ case ast.ATRenameIndex:
+ oldIndexName := cmd.Name
+ newIndexName := cmd.NewName
indexState := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetIndex(oldIndexName)
if indexState == nil {
continue
}
if indexState.GetProto().GetUnique() {
- // Unique index naming convention should in advisor_naming_unique_key_convention.go
+ // Unique index naming convention should in rule_naming_unique_key_convention.go
continue
}
metaData := map[string]string{
@@ -220,7 +154,7 @@ func (r *NamingIndexConventionRule) checkAlterTable(ctx *mysql.AlterTableContext
indexName: newIndexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(n.Loc)),
}
indexDataList = append(indexDataList, indexData)
default:
@@ -229,73 +163,57 @@ func (r *NamingIndexConventionRule) checkAlterTable(ctx *mysql.AlterTableContext
r.handleIndexList(indexDataList)
}
-func (r *NamingIndexConventionRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *namingIndexOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ // Unique, fulltext, spatial index naming convention should be in other rules.
+ if n.Unique || n.Fulltext || n.Spatial {
return
}
- // Unique index naming convention should in advisor_naming_unique_key_convention.go
- if ctx.UNIQUE_SYMBOL() != nil || ctx.FULLTEXT_SYMBOL() != nil || ctx.SPATIAL_SYMBOL() != nil {
+ if n.Table == nil {
return
}
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {
- return
- }
-
- indexName := ""
- if ctx.IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- }
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
-
- if ctx.CreateIndexTarget().KeyListVariants() == nil {
- return
- }
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
+ tableName := n.Table.Name
+ columnList := omniIndexColumns(n.Columns)
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
advisor.TableNameTemplateToken: tableName,
}
indexDataList := []*indexMetaData{
{
- indexName: indexName,
+ indexName: n.IndexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(n.Loc)),
},
}
r.handleIndexList(indexDataList)
}
-func (r *NamingIndexConventionRule) handleIndexList(indexDataList []*indexMetaData) {
+func (r *namingIndexOmniRule) handleIndexList(indexDataList []*indexMetaData) {
for _, indexData := range indexDataList {
regex, err := getTemplateRegexp(r.format, r.templateList, indexData.metaData)
if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.Internal.Int32(),
Title: "Internal error for unique key naming convention rule",
- Content: fmt.Sprintf("%q meet internal error %q", r.text, err.Error()),
+ Content: fmt.Sprintf("%q meet internal error %q", r.TrimmedStmtText(), err.Error()),
})
continue
}
if !regex.MatchString(indexData.indexName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingIndexConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Index in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
}
if r.maxLength > 0 && len(indexData.indexName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingIndexConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Index `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, r.maxLength),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
@@ -303,21 +221,16 @@ func (r *NamingIndexConventionRule) handleIndexList(indexDataList []*indexMetaDa
}
}
-func (r *NamingIndexConventionRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) *indexMetaData {
- // we only focus normal index.
- if ctx.UNIQUE_SYMBOL() != nil || ctx.FULLTEXT_SYMBOL() != nil || ctx.SPATIAL_SYMBOL() != nil || ctx.PRIMARY_SYMBOL() != nil {
- return nil
- }
- if ctx.KeyListVariants() == nil {
+func (*namingIndexOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int) *indexMetaData {
+ // We only focus on normal index.
+ if constraint.Type != ast.ConstrIndex {
return nil
}
-
- indexName := ""
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
+ indexName := constraint.Name
+ columnList := constraint.Columns
+ if len(columnList) == 0 {
+ columnList = omniIndexColumns(constraint.IndexColumns)
}
-
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
advisor.TableNameTemplateToken: tableName,
@@ -326,6 +239,6 @@ func (r *NamingIndexConventionRule) handleConstraintDef(tableName string, ctx my
indexName: indexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: line,
}
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_table.go b/backend/plugin/advisor/mysql/rule_naming_table.go
index 54b26f4546..682cc41eac 100644
--- a/backend/plugin/advisor/mysql/rule_naming_table.go
+++ b/backend/plugin/advisor/mysql/rule_naming_table.go
@@ -5,16 +5,13 @@ import (
"fmt"
"regexp"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -53,130 +50,68 @@ func (*NamingTableConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingTableRule(level, checkCtx.Rule.Type.String(), format, maxLength)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
- return checker.GetAdviceList(), nil
-}
-
-// NamingTableRule checks for table naming conventions.
-type NamingTableRule struct {
- BaseRule
- format *regexp.Regexp
- maxLength int
-}
-
-// NewNamingTableRule creates a new NamingTableRule.
-func NewNamingTableRule(level storepb.Advice_Status, title string, format *regexp.Regexp, maxLength int) *NamingTableRule {
- return &NamingTableRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &namingTableOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
format: format,
maxLength: maxLength,
}
-}
-// Name returns the rule name.
-func (*NamingTableRule) Name() string {
- return "NamingTableRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingTableRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeRenameTableStatement:
- r.checkRenameTableStatement(ctx.(*mysql.RenameTableStatementContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*NamingTableRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
+type namingTableOmniRule struct {
+ OmniBaseRule
+ format *regexp.Regexp
+ maxLength int
}
-func (r *NamingTableRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.handleTableName(tableName, ctx.GetStart().GetLine())
+func (*namingTableOmniRule) Name() string {
+ return "NamingTableRule"
}
-func (r *NamingTableRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item.RENAME_SYMBOL() == nil {
- continue
+func (r *namingTableOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table != nil {
+ r.handleTableName(n.Table.Name, r.LocToLine(n.Loc))
}
- if item.TableName() == nil {
- continue
+ case *ast.AlterTableStmt:
+ for _, cmd := range n.Commands {
+ if cmd.Type == ast.ATRenameTable && cmd.NewName != "" {
+ r.handleTableName(cmd.NewName, r.LocToLine(n.Loc))
+ }
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(item.TableName())
- r.handleTableName(tableName, ctx.GetStart().GetLine())
- }
-}
-
-func (r *NamingTableRule) checkRenameTableStatement(ctx *mysql.RenameTableStatementContext) {
- for _, pair := range ctx.AllRenamePair() {
- if pair.TableName() == nil {
- continue
+ case *ast.RenameTableStmt:
+ for _, pair := range n.Pairs {
+ if pair.New != nil {
+ r.handleTableName(pair.New.Name, r.LocToLine(n.Loc))
+ }
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())
- r.handleTableName(tableName, ctx.GetStart().GetLine())
+ default:
}
}
-func (r *NamingTableRule) handleTableName(tableName string, lineNumber int) {
- lineNumber += r.baseLine
+func (r *namingTableOmniRule) handleTableName(tableName string, lineNumber int32) {
+ absoluteLine := r.BaseLine + int(lineNumber)
if !r.format.MatchString(tableName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
if r.maxLength > 0 && len(tableName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingTableConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should be within %d characters", tableName, r.maxLength),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
index 070ed1c5ae..87d579356d 100644
--- a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
+++ b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
@@ -5,16 +5,13 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -58,26 +55,18 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
maxLength = advisor.DefaultNameLengthLimit
}
- // Create the rule
- rule := NewNamingUKConventionRule(level, checkCtx.Rule.Type.String(), format, maxLength, templateList, checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &namingUKOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
// ukIndexMetaData is the metadata for unique key.
@@ -88,123 +77,67 @@ type ukIndexMetaData struct {
line int
}
-// NamingUKConventionRule checks for unique key naming convention.
-type NamingUKConventionRule struct {
- BaseRule
- text string
+type namingUKOmniRule struct {
+ OmniBaseRule
format string
maxLength int
templateList []string
originalMetadata *model.DatabaseMetadata
}
-// NewNamingUKConventionRule creates a new NamingUKConventionRule.
-func NewNamingUKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originalMetadata *model.DatabaseMetadata) *NamingUKConventionRule {
- return &NamingUKConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originalMetadata: originalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*NamingUKConventionRule) Name() string {
+func (*namingUKOmniRule) Name() string {
return "NamingUKConventionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NamingUKConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *namingUKOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
- // Other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*NamingUKConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *NamingUKConventionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
+func (r *namingUKOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
-
+ tableName := n.Table.Name
var indexDataList []*ukIndexMetaData
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil {
- continue
- }
- if tableElement.TableConstraintDef() == nil {
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- if metaData := r.handleConstraintDef(tableName, tableElement.TableConstraintDef()); metaData != nil {
+ if metaData := r.handleConstraint(tableName, constraint, r.BaseLine+int(r.LocToLine(constraint.Loc))); metaData != nil {
indexDataList = append(indexDataList, metaData)
}
}
r.handleIndexList(indexDataList)
}
-func (r *NamingUKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
+func (r *namingUKOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := ""
+ if n.Table != nil {
+ tableName = n.Table.Name
}
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
var indexDataList []*ukIndexMetaData
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- switch {
- // add index.
- case alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil:
- if metaData := r.handleConstraintDef(tableName, alterListItem.TableConstraintDef()); metaData != nil {
- indexDataList = append(indexDataList, metaData)
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ if metaData := r.handleConstraint(tableName, cmd.Constraint, r.BaseLine+int(r.LocToLine(n.Loc))); metaData != nil {
+ indexDataList = append(indexDataList, metaData)
+ }
}
- // rename index.
- case alterListItem.RENAME_SYMBOL() != nil && alterListItem.KeyOrIndex() != nil && alterListItem.IndexRef() != nil && alterListItem.IndexName() != nil:
- _, _, oldIndexName := mysqlparser.NormalizeIndexRef(alterListItem.IndexRef())
- newIndexName := mysqlparser.NormalizeIndexName(alterListItem.IndexName())
+ case ast.ATRenameIndex:
+ oldIndexName := cmd.Name
+ newIndexName := cmd.NewName
indexState := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetIndex(oldIndexName)
if indexState == nil {
continue
@@ -221,76 +154,66 @@ func (r *NamingUKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
indexName: newIndexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(n.Loc)),
}
indexDataList = append(indexDataList, indexData)
default:
- // Other alter operations
}
}
r.handleIndexList(indexDataList)
}
-func (r *NamingUKConventionRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *namingUKOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ // Only focus on unique index.
+ if !n.Unique {
return
}
- // only focus on unique index.
- if ctx.UNIQUE_SYMBOL() == nil {
+ if n.Table == nil {
return
}
- if ctx.IndexName() == nil || ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil {
- return
- }
-
- indexName := mysqlparser.NormalizeIndexName(ctx.IndexName())
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
-
- if ctx.CreateIndexTarget().KeyListVariants() == nil {
- return
- }
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
+ tableName := n.Table.Name
+ columnList := omniIndexColumns(n.Columns)
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
advisor.TableNameTemplateToken: tableName,
}
indexDataList := []*ukIndexMetaData{
{
- indexName: indexName,
+ indexName: n.IndexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(n.Loc)),
},
}
r.handleIndexList(indexDataList)
}
-func (r *NamingUKConventionRule) handleIndexList(indexDataList []*ukIndexMetaData) {
+func (r *namingUKOmniRule) handleIndexList(indexDataList []*ukIndexMetaData) {
for _, indexData := range indexDataList {
regex, err := getTemplateRegexp(r.format, r.templateList, indexData.metaData)
if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.Internal.Int32(),
Title: "Internal error for unique key naming convention rule",
- Content: fmt.Sprintf("%q meet internal error %q", r.text, err.Error()),
+ Content: fmt.Sprintf("%q meet internal error %q", r.TrimmedStmtText(), err.Error()),
})
continue
}
if !regex.MatchString(indexData.indexName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingUKConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Unique key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
}
if r.maxLength > 0 && len(indexData.indexName) > r.maxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.NamingUKConventionMismatch.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Unique key `%s` in table `%s` mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, r.maxLength),
StartPosition: common.ConvertANTLRLineToPosition(indexData.line),
})
@@ -298,18 +221,16 @@ func (r *NamingUKConventionRule) handleIndexList(indexDataList []*ukIndexMetaDat
}
}
-func (r *NamingUKConventionRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) *ukIndexMetaData {
- // focus on unique index.
- if ctx.UNIQUE_SYMBOL() == nil || ctx.KeyListVariants() == nil {
+func (*namingUKOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int) *ukIndexMetaData {
+ // Focus on unique index.
+ if constraint.Type != ast.ConstrUnique {
return nil
}
-
- indexName := ""
- if ctx.IndexNameAndType() != nil && ctx.IndexNameAndType().IndexName() != nil {
- indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
+ indexName := constraint.Name
+ columnList := constraint.Columns
+ if len(columnList) == 0 {
+ columnList = omniIndexColumns(constraint.IndexColumns)
}
-
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(columnList, "_"),
advisor.TableNameTemplateToken: tableName,
@@ -318,6 +239,6 @@ func (r *NamingUKConventionRule) handleConstraintDef(tableName string, ctx mysql
indexName: indexName,
tableName: tableName,
metaData: metaData,
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: line,
}
}
diff --git a/backend/plugin/advisor/mysql/rule_online_migration.go b/backend/plugin/advisor/mysql/rule_online_migration.go
index bf64b781aa..248a834fb3 100644
--- a/backend/plugin/advisor/mysql/rule_online_migration.go
+++ b/backend/plugin/advisor/mysql/rule_online_migration.go
@@ -1,22 +1,19 @@
-package mysql
-
// Framework code is generated by the generator.
+package mysql
+
import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -64,25 +61,20 @@ func (*OnlineMigrationAdvisor) Check(ctx context.Context, checkCtx advisor.Conte
}
}
- // Create the rule
- rule := NewOnlineMigrationRule(level, title, minRows, dbMetadata, checkCtx)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &onlineMigrationOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: title,
+ },
+ minRows: minRows,
+ dbMetadata: dbMetadata,
+ checkCtx: checkCtx,
+ currentDatabase: checkCtx.CurrentDatabase,
+ changedResources: make(map[string]base.SchemaResource),
}
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
+
adviceList := rule.GetAdviceList()
// More than one statements need online migration.
@@ -126,108 +118,59 @@ func (*OnlineMigrationAdvisor) Check(ctx context.Context, checkCtx advisor.Conte
return nil, nil
}
-// OnlineMigrationRule checks for using gh-ost to migrate large tables.
-type OnlineMigrationRule struct {
- BaseRule
+type onlineMigrationOmniRule struct {
+ OmniBaseRule
minRows int64
dbMetadata *model.DatabaseMetadata
checkCtx advisor.Context
currentDatabase string
changedResources map[string]base.SchemaResource
- ghostCompatible bool
- start *storepb.Position
- end *storepb.Position
}
-// NewOnlineMigrationRule creates a new OnlineMigrationRule.
-func NewOnlineMigrationRule(level storepb.Advice_Status, title string, minRows int64, dbMetadata *model.DatabaseMetadata, checkCtx advisor.Context) *OnlineMigrationRule {
- return &OnlineMigrationRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- minRows: minRows,
- dbMetadata: dbMetadata,
- checkCtx: checkCtx,
- currentDatabase: checkCtx.CurrentDatabase,
- changedResources: make(map[string]base.SchemaResource),
- }
-}
-
-// Name returns the rule name.
-func (*OnlineMigrationRule) Name() string {
+func (*onlineMigrationOmniRule) Name() string {
return "OnlineMigrationRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *OnlineMigrationRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeAlterStatement:
- r.checkAlterStatement(ctx.(*mysql.AlterStatementContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeAlterTableActions:
- r.checkAlterTableActions(ctx.(*mysql.AlterTableActionsContext))
- default:
+func (r *onlineMigrationOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.AlterTableStmt)
+ if !ok {
+ return
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *OnlineMigrationRule) OnExit(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeAlterStatement {
- r.exitAlterStatement(ctx.(*mysql.AlterStatementContext))
+ if n.Table == nil {
+ return
}
- return nil
-}
-func (r *OnlineMigrationRule) checkAlterStatement(ctx *mysql.AlterStatementContext) {
- r.start = common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine())
-}
-
-func (r *OnlineMigrationRule) exitAlterStatement(ctx *mysql.AlterStatementContext) {
- r.end = common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStop().GetLine())
+ resource := base.SchemaResource{
+ Database: r.currentDatabase,
+ }
+ if n.Table.Schema != "" {
+ resource.Database = n.Table.Schema
+ }
+ resource.Table = n.Table.Name
- if !r.ghostCompatible {
+ // Check if this ALTER TABLE has commands (ghost-compatible).
+ ghostCompatible := len(n.Commands) > 0
+ if !ghostCompatible {
return
}
- for _, resource := range r.changedResources {
- var tableRows int64
- for _, table := range r.dbMetadata.GetProto().GetSchemas()[0].GetTables() {
- if table.GetName() == resource.Table {
- tableRows = table.GetRowCount()
- break
+ startLine := r.BaseLine + int(r.LocToLine(n.Loc))
+ endLine := r.BaseLine + int(r.ContentEndLine())
+
+ for _, table := range r.dbMetadata.GetProto().GetSchemas()[0].GetTables() {
+ if table.GetName() == resource.Table {
+ tableRows := table.GetRowCount()
+ if tableRows >= r.minRows {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.AdviseOnlineMigrationForStatement.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Estimated table row count of %q is %d exceeding the set value %d. Consider using online migration for this statement", fmt.Sprintf("%s.%s", resource.Database, resource.Table), tableRows, r.minRows),
+ StartPosition: common.ConvertANTLRLineToPosition(startLine),
+ EndPosition: common.ConvertANTLRLineToPosition(endLine),
+ })
}
+ break
}
- if tableRows >= r.minRows {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.AdviseOnlineMigrationForStatement.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Estimated table row count of %q is %d exceeding the set value %d. Consider using online migration for this statement", fmt.Sprintf("%s.%s", resource.Schema, resource.Table), tableRows, r.minRows),
- StartPosition: r.start,
- EndPosition: r.end,
- })
- }
- }
-}
-
-func (r *OnlineMigrationRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
}
- resource := base.SchemaResource{
- Database: r.currentDatabase,
- }
- db, table := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if db != "" {
- resource.Database = db
- }
- resource.Table = table
- r.changedResources[resource.String()] = resource
-}
-
-func (r *OnlineMigrationRule) checkAlterTableActions(ctx *mysql.AlterTableActionsContext) {
- r.ghostCompatible = ctx.AlterCommandList() != nil || ctx.PartitionClause() != nil || ctx.RemovePartitioning() != nil
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_add_column_without_position.go b/backend/plugin/advisor/mysql/rule_statement_add_column_without_position.go
index f6f4e75efd..13fa9644f5 100644
--- a/backend/plugin/advisor/mysql/rule_statement_add_column_without_position.go
+++ b/backend/plugin/advisor/mysql/rule_statement_add_column_without_position.go
@@ -4,15 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -23,148 +20,62 @@ func init() {
advisor.Register(storepb.Engine_OCEANBASE, storepb.SQLReviewRule_STATEMENT_ADD_COLUMN_WITHOUT_POSITION, &StatementAddColumnWithoutPositionAdvisor{})
}
-// StatementAddColumnWithoutPositionAdvisor is the advisor checking for checking no position in ADD COLUMN clause.
+// StatementAddColumnWithoutPositionAdvisor is the advisor checking for no position in ADD COLUMN clause.
type StatementAddColumnWithoutPositionAdvisor struct {
}
-// Check checks for checking no position in ADD COLUMN clause.
+// Check checks for no position in ADD COLUMN clause.
func (*StatementAddColumnWithoutPositionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
if err != nil {
return nil, err
}
- // Create the rule
- rule := NewStatementAddColumnWithoutPositionRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// StatementAddColumnWithoutPositionRule checks for no position in ADD COLUMN clause.
-type StatementAddColumnWithoutPositionRule struct {
- BaseRule
-}
-
-// NewStatementAddColumnWithoutPositionRule creates a new StatementAddColumnWithoutPositionRule.
-func NewStatementAddColumnWithoutPositionRule(level storepb.Advice_Status, title string) *StatementAddColumnWithoutPositionRule {
- return &StatementAddColumnWithoutPositionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &addColumnWithoutPositionOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*StatementAddColumnWithoutPositionRule) Name() string {
- return "StatementAddColumnWithoutPositionRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementAddColumnWithoutPositionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeAlterTable {
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- }
- return nil
+type addColumnWithoutPositionOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*StatementAddColumnWithoutPositionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*addColumnWithoutPositionOmniRule) Name() string {
+ return "StatementAddColumnWithoutPositionRule"
}
-func (r *StatementAddColumnWithoutPositionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
+func (r *addColumnWithoutPositionOmniRule) OnStatement(node ast.Node) {
+ alter, ok := node.(*ast.AlterTableStmt)
+ if !ok {
return
}
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- if ctx.TableRef() == nil {
+ if alter.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
- }
-
- for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if item == nil || item.ADD_SYMBOL() == nil {
+ for _, cmd := range alter.Commands {
+ if cmd.Type != ast.ATAddColumn {
continue
}
-
var position string
-
- switch {
- case item.Identifier() != nil && item.FieldDefinition() != nil:
- position = getPosition(item.Place())
- case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
- for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
- continue
- }
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
- }
-
- position = getPosition(item.Place())
- if len(position) != 0 {
- break
- }
- }
- default:
+ if cmd.First {
+ position = "FIRST"
+ } else if cmd.After != "" {
+ position = "AFTER"
}
-
- if len(position) != 0 {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if position != "" {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementAddColumnWithPosition.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("add column with position \"%s\"", position),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
})
}
}
}
-
-func getPosition(ctx mysql.IPlaceContext) string {
- if ctx == nil {
- return ""
- }
- place, ok := ctx.(*mysql.PlaceContext)
- if !ok || place == nil {
- return ""
- }
-
- switch {
- case place.FIRST_SYMBOL() != nil:
- return "FIRST"
- case place.AFTER_SYMBOL() != nil:
- return "AFTER"
- default:
- return ""
- }
-}
diff --git a/backend/plugin/advisor/mysql/rule_statement_affected_row_limit.go b/backend/plugin/advisor/mysql/rule_statement_affected_row_limit.go
index e9d8ad2912..b8c366a3ed 100644
--- a/backend/plugin/advisor/mysql/rule_statement_affected_row_limit.go
+++ b/backend/plugin/advisor/mysql/rule_statement_affected_row_limit.go
@@ -2,19 +2,17 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
"strconv"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -42,120 +40,71 @@ func (*StatementAffectedRowLimitAdvisor) Check(ctx context.Context, checkCtx adv
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewStatementAffectedRowLimitRule(ctx, level, checkCtx.Rule.Type.String(), int(numberPayload.Number), checkCtx.Driver)
+ maxRow := int(numberPayload.Number)
+ driver := checkCtx.Driver
+ title := checkCtx.Rule.Type.String()
+ var advice []*storepb.Advice
+ explainCount := 0
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- if checkCtx.Driver != nil {
+ if driver != nil {
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.explainCount >= common.MaximumLintExplainSize {
- break
- }
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-// StatementAffectedRowLimitRule checks for UPDATE/DELETE affected row limit.
-type StatementAffectedRowLimitRule struct {
- BaseRule
- text string
- maxRow int
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
+ // Only handle UPDATE and DELETE statements.
+ switch node.(type) {
+ case *ast.UpdateStmt, *ast.DeleteStmt:
+ default:
+ continue
+ }
-// NewStatementAffectedRowLimitRule creates a new StatementAffectedRowLimitRule.
-func NewStatementAffectedRowLimitRule(ctx context.Context, level storepb.Advice_Status, title string, maxRow int, driver *sql.DB) *StatementAffectedRowLimitRule {
- return &StatementAffectedRowLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maxRow: maxRow,
- driver: driver,
- ctx: ctx,
- }
-}
+ baseLine := stmt.BaseLine()
+ text := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
-// Name returns the rule name.
-func (*StatementAffectedRowLimitRule) Name() string {
- return "StatementAffectedRowLimitRule"
-}
+ explainCount++
+ res, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", text))
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementAffectedRowExceedsLimit.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("\"%s\" dry runs failed: %s", text, err.Error()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else {
+ rowCount, err := getRows(res)
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.Internal.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("failed to get row count for \"%s\": %s", text, err.Error()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if rowCount > int64(maxRow) {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementAffectedRowExceedsLimit.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("\"%s\" affected %d rows (estimated). The count exceeds %d.", text, rowCount, maxRow),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
-// OnEnter is called when entering a parse tree node.
-func (r *StatementAffectedRowLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeUpdateStatement:
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.UpdateStatementContext).BaseParserRuleContext) {
- r.handleStmt(ctx.GetStart().GetLine())
- }
- case NodeTypeDeleteStatement:
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.DeleteStatementContext).BaseParserRuleContext) {
- r.handleStmt(ctx.GetStart().GetLine())
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
}
- default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementAffectedRowLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-func (r *StatementAffectedRowLimitRule) handleStmt(lineNumber int) {
- lineNumber += r.baseLine
- r.explainCount++
- res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", r.text))
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementAffectedRowExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" dry runs failed: %s", r.text, err.Error()),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
- })
- } else {
- rowCount, err := getRows(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("failed to get row count for \"%s\": %s", r.text, err.Error()),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
- })
- } else if rowCount > int64(r.maxRow) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementAffectedRowExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" affected %d rows (estimated). The count exceeds %d.", r.text, rowCount, r.maxRow),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
- })
- }
- }
+ return advice, nil
}
func getRows(res []any) (int64, error) {
@@ -175,24 +124,6 @@ func getRows(res []any) (int64, error) {
return 0, errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // the column 9 is the data 'rows'.
- // the first not-NULL value of column 9 is the affected rows count.
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
rowsIndex, err := getColumnIndex(columns, "rows")
if err != nil {
return 0, errors.Errorf("failed to find rows column")
diff --git a/backend/plugin/advisor/mysql/rule_statement_disallow_using_filesort.go b/backend/plugin/advisor/mysql/rule_statement_disallow_using_filesort.go
index a05dd88c34..f339b3a63f 100644
--- a/backend/plugin/advisor/mysql/rule_statement_disallow_using_filesort.go
+++ b/backend/plugin/advisor/mysql/rule_statement_disallow_using_filesort.go
@@ -2,19 +2,16 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -36,115 +33,68 @@ func (*StatementDisallowUsingFilesortAdvisor) Check(ctx context.Context, checkCt
return nil, err
}
- // Create the rule
- rule := NewStatementDisallowUsingFilesortRule(ctx, level, checkCtx.Rule.Type.String(), checkCtx.Driver)
+ driver := checkCtx.Driver
+ title := checkCtx.Rule.Type.String()
+ var advice []*storepb.Advice
+ explainCount := 0
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- if rule.driver != nil {
+ if driver != nil {
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.GetExplainCount() >= common.MaximumLintExplainSize {
- break
- }
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// StatementDisallowUsingFilesortRule checks for using filesort.
-type StatementDisallowUsingFilesortRule struct {
- BaseRule
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
-
-// NewStatementDisallowUsingFilesortRule creates a new StatementDisallowUsingFilesortRule.
-func NewStatementDisallowUsingFilesortRule(ctx context.Context, level storepb.Advice_Status, title string, driver *sql.DB) *StatementDisallowUsingFilesortRule {
- return &StatementDisallowUsingFilesortRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- driver: driver,
- ctx: ctx,
- }
-}
-
-// Name returns the rule name.
-func (*StatementDisallowUsingFilesortRule) Name() string {
- return "StatementDisallowUsingFilesortRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *StatementDisallowUsingFilesortRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- r.checkSelectStatement(ctx.(*mysql.SelectStatementContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementDisallowUsingFilesortRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-// GetExplainCount returns the explain count.
-func (r *StatementDisallowUsingFilesortRule) GetExplainCount() int {
- return r.explainCount
-}
+ // Only handle top-level SELECT statements.
+ if _, ok := node.(*ast.SelectStmt); !ok {
+ continue
+ }
-func (r *StatementDisallowUsingFilesortRule) checkSelectStatement(ctx *mysql.SelectStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
- return
- }
+ baseLine := stmt.BaseLine()
+ query := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
+
+ explainCount++
+ res, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementExplainQueryFailed.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to explain query: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else {
+ hasUsingFilesort, tables, err := hasUsingFilesortInExtraColumn(res)
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.Internal.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to check extra column: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if hasUsingFilesort {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementHasUsingFilesort.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Using filesort detected on table(s): %s", tables),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
- query := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- r.explainCount++
- res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementExplainQueryFailed.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to explain query: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else {
- hasUsingFilesort, tables, err := hasUsingFilesortInExtraColumn(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to check extra column: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else if hasUsingFilesort {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementHasUsingFilesort.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Using filesort detected on table(s): %s", tables),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
}
}
+
+ return advice, nil
}
func hasUsingFilesortInExtraColumn(res []any) (bool, string, error) {
@@ -163,27 +113,6 @@ func hasUsingFilesortInExtraColumn(res []any) (bool, string, error) {
return false, "", errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // 1. the column 4 is the data 'type'.
- // We check all rows of the result to see if any of them has 'ALL' or 'index' in the 'type' column.
- // 2. the column 11 is the 'Extra' column.
- // If the 'Extra' column dose not contain
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
-
extraIndex, err := getColumnIndex(columns, "Extra")
if err != nil {
return false, "", errors.Errorf("failed to find rows column")
diff --git a/backend/plugin/advisor/mysql/rule_statement_disallow_using_temporary.go b/backend/plugin/advisor/mysql/rule_statement_disallow_using_temporary.go
index 9720f7ff3e..d637ee2a25 100644
--- a/backend/plugin/advisor/mysql/rule_statement_disallow_using_temporary.go
+++ b/backend/plugin/advisor/mysql/rule_statement_disallow_using_temporary.go
@@ -2,19 +2,16 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -36,115 +33,68 @@ func (*StatementDisallowUsingTemporaryAdvisor) Check(ctx context.Context, checkC
return nil, err
}
- // Create the rule
- rule := NewStatementDisallowUsingTemporaryRule(ctx, level, checkCtx.Rule.Type.String(), checkCtx.Driver)
+ driver := checkCtx.Driver
+ title := checkCtx.Rule.Type.String()
+ var advice []*storepb.Advice
+ explainCount := 0
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- if rule.driver != nil {
+ if driver != nil {
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.GetExplainCount() >= common.MaximumLintExplainSize {
- break
- }
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// StatementDisallowUsingTemporaryRule checks for using temporary.
-type StatementDisallowUsingTemporaryRule struct {
- BaseRule
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
-
-// NewStatementDisallowUsingTemporaryRule creates a new StatementDisallowUsingTemporaryRule.
-func NewStatementDisallowUsingTemporaryRule(ctx context.Context, level storepb.Advice_Status, title string, driver *sql.DB) *StatementDisallowUsingTemporaryRule {
- return &StatementDisallowUsingTemporaryRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- driver: driver,
- ctx: ctx,
- }
-}
-
-// Name returns the rule name.
-func (*StatementDisallowUsingTemporaryRule) Name() string {
- return "StatementDisallowUsingTemporaryRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *StatementDisallowUsingTemporaryRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- r.checkSelectStatement(ctx.(*mysql.SelectStatementContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementDisallowUsingTemporaryRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-// GetExplainCount returns the explain count.
-func (r *StatementDisallowUsingTemporaryRule) GetExplainCount() int {
- return r.explainCount
-}
+ // Only handle top-level SELECT statements.
+ if _, ok := node.(*ast.SelectStmt); !ok {
+ continue
+ }
-func (r *StatementDisallowUsingTemporaryRule) checkSelectStatement(ctx *mysql.SelectStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
- return
- }
+ baseLine := stmt.BaseLine()
+ query := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
+
+ explainCount++
+ res, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementExplainQueryFailed.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to explain query: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else {
+ hasUsingTemporary, tables, err := hasUsingTemporaryInExtraColumn(res)
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.Internal.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to check extra column: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if hasUsingTemporary {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementHasUsingTemporary.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Using temporary detected on table(s): %s", tables),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
- query := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- r.explainCount++
- res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementExplainQueryFailed.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to explain query: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else {
- hasUsingTemporary, tables, err := hasUsingTemporaryInExtraColumn(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to check extra column: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else if hasUsingTemporary {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementHasUsingTemporary.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Using temporary detected on table(s): %s", tables),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
}
}
+
+ return advice, nil
}
func hasUsingTemporaryInExtraColumn(res []any) (bool, string, error) {
@@ -163,27 +113,6 @@ func hasUsingTemporaryInExtraColumn(res []any) (bool, string, error) {
return false, "", errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // 1. the column 4 is the data 'type'.
- // We check all rows of the result to see if any of them has 'ALL' or 'index' in the 'type' column.
- // 2. the column 11 is the 'Extra' column.
- // If the 'Extra' column dose not contain
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
-
extraIndex, err := getColumnIndex(columns, "Extra")
if err != nil {
return false, "", errors.Errorf("failed to find rows column")
diff --git a/backend/plugin/advisor/mysql/rule_statement_dml_dry_run.go b/backend/plugin/advisor/mysql/rule_statement_dml_dry_run.go
index 18863fafdb..58b18dc48c 100644
--- a/backend/plugin/advisor/mysql/rule_statement_dml_dry_run.go
+++ b/backend/plugin/advisor/mysql/rule_statement_dml_dry_run.go
@@ -2,17 +2,15 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -43,105 +41,48 @@ func (*StatementDmlDryRunAdvisor) Check(ctx context.Context, checkCtx advisor.Co
return nil, nil
}
- // Create the rule
- rule := NewStatementDmlDryRunRule(ctx, level, checkCtx.Rule.Type.String(), checkCtx.Driver)
+ driver := checkCtx.Driver
+ title := checkCtx.Rule.Type.String()
+ var advice []*storepb.Advice
+ explainCount := 0
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- if checkCtx.Driver != nil {
+ if driver != nil {
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.explainCount >= common.MaximumLintExplainSize {
- break
- }
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-// StatementDmlDryRunRule checks for DML dry run.
-type StatementDmlDryRunRule struct {
- BaseRule
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
-
-// NewStatementDmlDryRunRule creates a new StatementDmlDryRunRule.
-func NewStatementDmlDryRunRule(ctx context.Context, level storepb.Advice_Status, title string, driver *sql.DB) *StatementDmlDryRunRule {
- return &StatementDmlDryRunRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- driver: driver,
- ctx: ctx,
- }
-}
+ // Only handle DML statements.
+ switch node.(type) {
+ case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:
+ default:
+ continue
+ }
-// Name returns the rule name.
-func (*StatementDmlDryRunRule) Name() string {
- return "StatementDmlDryRunRule"
-}
+ baseLine := stmt.BaseLine()
+ text := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
+
+ explainCount++
+ if _, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", text)); err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementDMLDryRunFailed.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("\"%s\" dry runs failed: %s", text, err.Error()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
-// OnEnter is called when entering a parse tree node.
-func (r *StatementDmlDryRunRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeUpdateStatement:
- updateCtx, ok := ctx.(*mysql.UpdateStatementContext)
- if !ok {
- return nil
- }
- if mysqlparser.IsTopMySQLRule(&updateCtx.BaseParserRuleContext) {
- r.handleStmt(updateCtx.GetParser().GetTokenStream().GetTextFromRuleContext(updateCtx), updateCtx.GetStart().GetLine())
- }
- case NodeTypeDeleteStatement:
- deleteCtx, ok := ctx.(*mysql.DeleteStatementContext)
- if !ok {
- return nil
- }
- if mysqlparser.IsTopMySQLRule(&deleteCtx.BaseParserRuleContext) {
- r.handleStmt(deleteCtx.GetParser().GetTokenStream().GetTextFromRuleContext(deleteCtx), deleteCtx.GetStart().GetLine())
- }
- case NodeTypeInsertStatement:
- insertCtx, ok := ctx.(*mysql.InsertStatementContext)
- if !ok {
- return nil
- }
- if mysqlparser.IsTopMySQLRule(&insertCtx.BaseParserRuleContext) {
- r.handleStmt(insertCtx.GetParser().GetTokenStream().GetTextFromRuleContext(insertCtx), insertCtx.GetStart().GetLine())
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
}
- default:
- // Other node types
}
- return nil
-}
-// OnExit is called when exiting a parse tree node.
-func (*StatementDmlDryRunRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *StatementDmlDryRunRule) handleStmt(text string, lineNumber int) {
- r.explainCount++
- if _, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", text)); err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementDMLDryRunFailed.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" dry runs failed: %s", text, err.Error()),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
- })
- }
+ return advice, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go b/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
index d52156170d..22275a3e2a 100644
--- a/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
+++ b/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
@@ -5,15 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -34,30 +31,17 @@ func (*StatementJoinStrictColumnAttrsAdvisor) Check(_ context.Context, checkCtx
return nil, err
}
- // Create the rule
- rule := NewStatementJoinStrictColumnAttrsRule(level, checkCtx.Rule.Type.String())
- if checkCtx.DBSchema != nil {
- dbMetadata := model.NewDatabaseMetadata(checkCtx.DBSchema, nil, nil, storepb.Engine_MYSQL, checkCtx.IsObjectCaseSensitive)
- rule.dbMetadata = dbMetadata
+ rule := &joinStrictColumnAttrsOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ if checkCtx.DBSchema != nil {
+ rule.dbMetadata = model.NewDatabaseMetadata(checkCtx.DBSchema, nil, nil, storepb.Engine_MYSQL, checkCtx.IsObjectCaseSensitive)
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
// SourceTable represents a table in the FROM clause.
@@ -75,181 +59,167 @@ type ColumnAttr struct {
Collation string
}
-// StatementJoinStrictColumnAttrsRule checks that joined columns have matching attributes.
-type StatementJoinStrictColumnAttrsRule struct {
- BaseRule
- text string
- dbMetadata *model.DatabaseMetadata
- isSelect bool
- isInFromClause bool
- sourceTables []SourceTable
+type joinStrictColumnAttrsOmniRule struct {
+ OmniBaseRule
+ dbMetadata *model.DatabaseMetadata
}
-// NewStatementJoinStrictColumnAttrsRule creates a new StatementJoinStrictColumnAttrsRule.
-func NewStatementJoinStrictColumnAttrsRule(level storepb.Advice_Status, title string) *StatementJoinStrictColumnAttrsRule {
- return &StatementJoinStrictColumnAttrsRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*StatementJoinStrictColumnAttrsRule) Name() string {
+func (*joinStrictColumnAttrsOmniRule) Name() string {
return "StatementJoinStrictColumnAttrsRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementJoinStrictColumnAttrsRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectStatement:
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.SelectStatementContext).BaseParserRuleContext) {
- r.isSelect = true
- }
- case NodeTypeFromClause:
- r.handleFromClause(ctx.(*mysql.FromClauseContext))
- case NodeTypePrimaryExprCompare:
- r.handlePrimaryExprCompare(ctx.(*mysql.PrimaryExprCompareContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *StatementJoinStrictColumnAttrsRule) OnExit(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeSelectStatement:
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.SelectStatementContext).BaseParserRuleContext) {
- r.isSelect = false
- }
- case NodeTypeFromClause:
- r.isInFromClause = false
- default:
+func (r *joinStrictColumnAttrsOmniRule) OnStatement(node ast.Node) {
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return
}
- return nil
+ r.checkSelect(sel)
}
-func (r *StatementJoinStrictColumnAttrsRule) handleFromClause(ctx *mysql.FromClauseContext) {
- if !r.isSelect {
+func (r *joinStrictColumnAttrsOmniRule) checkSelect(sel *ast.SelectStmt) {
+ if sel == nil {
return
}
- if ctx.TableReferenceList() == nil {
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelect(sel.Left)
+ r.checkSelect(sel.Right)
return
}
- r.isInFromClause = true
- r.sourceTables = []SourceTable{}
- tableRefs := ctx.TableReferenceList().AllTableReference()
- for _, tableRef := range tableRefs {
- if tableRef.TableFactor() == nil {
- continue
- }
-
- tableFactor := tableRef.TableFactor()
- if tableFactor.SingleTable() != nil && tableFactor.SingleTable().TableRef() != nil {
- sourceTable := SourceTable{
- Name: tableFactor.SingleTable().TableRef().GetText(),
+ // Collect source tables from FROM clause.
+ var sourceTables []SourceTable
+ for _, from := range sel.From {
+ ast.Inspect(from, func(n ast.Node) bool {
+ t, ok := n.(*ast.TableRef)
+ if !ok {
+ return true
}
- if tableFactor.SingleTable().TableAlias() != nil {
- sourceTable.Alias = tableFactor.SingleTable().TableAlias().Identifier().GetText()
+ st := SourceTable{Name: t.Name}
+ if t.Alias != "" {
+ st.Alias = t.Alias
}
- r.sourceTables = append(r.sourceTables, sourceTable)
- }
+ sourceTables = append(sourceTables, st)
+ return false
+ })
+ }
- if tableRef.AllJoinedTable() == nil {
- continue
- }
- for _, joinedTable := range tableRef.AllJoinedTable() {
- if joinedTable.TableReference() == nil {
- continue
- }
+ // Check joins for column attribute mismatches.
+ for _, from := range sel.From {
+ r.checkJoins(from, sourceTables)
+ }
+}
- referencedTable := joinedTable.TableReference().GetText()
- if joinedTable.USING_SYMBOL() != nil {
- if joinedTable.IdentifierListWithParentheses() == nil || joinedTable.IdentifierListWithParentheses().IdentifierList() == nil {
+func (r *joinStrictColumnAttrsOmniRule) checkJoins(te ast.TableExpr, sourceTables []SourceTable) {
+ if te == nil {
+ return
+ }
+ join, ok := te.(*ast.JoinClause)
+ if !ok {
+ return
+ }
+
+ // Check USING condition.
+ if using, ok := join.Condition.(*ast.UsingCondition); ok {
+ // Get the right table name.
+ rightTable := r.getTableName(join.Right)
+ for _, colName := range using.Columns {
+ rightCol := &ColumnAttr{Table: rightTable, Column: colName}
+ for _, st := range sourceTables {
+ if st.Name == rightTable || st.Alias == rightTable {
continue
}
- identifierList := joinedTable.IdentifierListWithParentheses().IdentifierList().AllIdentifier()
- for _, identifier := range identifierList {
- column := identifier.GetText()
- rightColumn := &ColumnAttr{
- Table: referencedTable,
- Column: column,
- }
- for _, sourceTable := range r.sourceTables {
- r.checkColumnAttrs(&ColumnAttr{
- Table: sourceTable.Name,
- Column: column,
- }, rightColumn)
- }
- }
+ r.checkColumnAttrs(&ColumnAttr{Table: st.Name, Column: colName}, rightCol)
}
}
}
+
+ // Check ON condition for equality comparisons.
+ if on, ok := join.Condition.(*ast.OnCondition); ok {
+ r.checkOnExpr(on.Expr)
+ }
+
+ r.checkJoins(join.Left, sourceTables)
+ r.checkJoins(join.Right, sourceTables)
}
-func (r *StatementJoinStrictColumnAttrsRule) handlePrimaryExprCompare(ctx *mysql.PrimaryExprCompareContext) {
- if !r.isSelect || !r.isInFromClause {
- return
+func (*joinStrictColumnAttrsOmniRule) getTableName(te ast.TableExpr) string {
+ if te == nil {
+ return ""
}
+ switch t := te.(type) {
+ case *ast.TableRef:
+ return t.Name
+ default:
+ return ""
+ }
+}
- compOp := ctx.CompOp()
- // We only check for equal for now.
- if compOp == nil || compOp.EQUAL_OPERATOR() == nil {
+func (r *joinStrictColumnAttrsOmniRule) checkOnExpr(expr ast.ExprNode) {
+ if expr == nil {
+ return
+ }
+ bin, ok := expr.(*ast.BinaryExpr)
+ if !ok {
+ return
+ }
+ if bin.Op == ast.BinOpAnd {
+ r.checkOnExpr(bin.Left)
+ r.checkOnExpr(bin.Right)
return
}
- if ctx.BoolPri() == nil || ctx.Predicate() == nil {
+ if bin.Op != ast.BinOpEq {
return
}
- leftColumnAttr := extractJoinInfoFromText(ctx.BoolPri().GetText())
- rightColumnAttr := extractJoinInfoFromText(ctx.Predicate().GetText())
- r.checkColumnAttrs(leftColumnAttr, rightColumnAttr)
+ leftCol := r.extractColumnAttr(bin.Left)
+ rightCol := r.extractColumnAttr(bin.Right)
+ r.checkColumnAttrs(leftCol, rightCol)
}
-func (r *StatementJoinStrictColumnAttrsRule) checkColumnAttrs(leftColumnAttr *ColumnAttr, rightColumnAttr *ColumnAttr) {
- if !r.isSelect || !r.isInFromClause {
- return
+func (*joinStrictColumnAttrsOmniRule) extractColumnAttr(expr ast.ExprNode) *ColumnAttr {
+ ref, ok := expr.(*ast.ColumnRef)
+ if !ok {
+ return nil
+ }
+ if ref.Table == "" || ref.Column == "" {
+ return nil
}
- if leftColumnAttr == nil || rightColumnAttr == nil {
+ return &ColumnAttr{Table: ref.Table, Column: ref.Column}
+}
+
+func (r *joinStrictColumnAttrsOmniRule) checkColumnAttrs(left, right *ColumnAttr) {
+ if left == nil || right == nil {
return
}
- // TODO(steven): dynamic match table alias later.
- leftTable := r.findTable(leftColumnAttr.Table)
- rightTable := r.findTable(rightColumnAttr.Table)
+ leftTable := r.findTable(left.Table)
+ rightTable := r.findTable(right.Table)
if leftTable == nil || rightTable == nil {
return
}
- leftColumn := leftTable.GetColumn(leftColumnAttr.Column)
- rightColumn := rightTable.GetColumn(rightColumnAttr.Column)
+ leftColumn := leftTable.GetColumn(left.Column)
+ rightColumn := rightTable.GetColumn(right.Column)
if leftColumn == nil || rightColumn == nil {
return
}
if leftColumn.GetProto().Type != rightColumn.GetProto().Type || leftColumn.GetProto().CharacterSet != rightColumn.GetProto().CharacterSet || leftColumn.GetProto().Collation != rightColumn.GetProto().Collation {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementJoinColumnAttrsNotMatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("%s.%s and %s.%s column fields do not match", leftColumnAttr.Table, leftColumnAttr.Column, rightColumnAttr.Table, rightColumnAttr.Column),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine),
+ Title: r.Title,
+ Content: fmt.Sprintf("%s.%s and %s.%s column fields do not match", left.Table, left.Column, right.Table, right.Column),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine),
})
}
}
-func (r *StatementJoinStrictColumnAttrsRule) findTable(tableName string) *model.TableMetadata {
+func (r *joinStrictColumnAttrsOmniRule) findTable(tableName string) *model.TableMetadata {
if r.dbMetadata == nil {
return nil
}
return r.dbMetadata.GetSchemaMetadata("").GetTable(tableName)
}
+// nolint:unused
func extractJoinInfoFromText(text string) *ColumnAttr {
elements := strings.Split(text, ".")
if len(elements) != 2 {
diff --git a/backend/plugin/advisor/mysql/rule_statement_maximum_join_table_count.go b/backend/plugin/advisor/mysql/rule_statement_maximum_join_table_count.go
index e9c2de9526..8f7c1e32e8 100644
--- a/backend/plugin/advisor/mysql/rule_statement_maximum_join_table_count.go
+++ b/backend/plugin/advisor/mysql/rule_statement_maximum_join_table_count.go
@@ -3,16 +3,15 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -36,84 +35,72 @@ func (*StatementMaximumJoinTableCountAdvisor) Check(_ context.Context, checkCtx
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewStatementMaximumJoinTableCountRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &maxJoinTableCountOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ limitMaxValue: int(numberPayload.Number),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// StatementMaximumJoinTableCountRule checks for maximum join table count.
-type StatementMaximumJoinTableCountRule struct {
- BaseRule
- text string
+type maxJoinTableCountOmniRule struct {
+ OmniBaseRule
limitMaxValue int
- count int
}
-// NewStatementMaximumJoinTableCountRule creates a new StatementMaximumJoinTableCountRule.
-func NewStatementMaximumJoinTableCountRule(level storepb.Advice_Status, title string, limitMaxValue int) *StatementMaximumJoinTableCountRule {
- return &StatementMaximumJoinTableCountRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- limitMaxValue: limitMaxValue,
- }
-}
-
-// Name returns the rule name.
-func (*StatementMaximumJoinTableCountRule) Name() string {
+func (*maxJoinTableCountOmniRule) Name() string {
return "StatementMaximumJoinTableCountRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementMaximumJoinTableCountRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeJoinedTable:
- r.checkJoinedTable(ctx.(*mysql.JoinedTableContext))
+func (r *maxJoinTableCountOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ r.checkSelect(n, text)
default:
- // No action required for other node types
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementMaximumJoinTableCountRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *StatementMaximumJoinTableCountRule) checkJoinedTable(ctx *mysql.JoinedTableContext) {
- r.count++
- // The count starts from 0. We count the number of tables in the joins.
- if r.count == r.limitMaxValue {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+func (r *maxJoinTableCountOmniRule) checkSelect(sel *ast.SelectStmt, text string) {
+ if sel == nil {
+ return
+ }
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelect(sel.Left, text)
+ r.checkSelect(sel.Right, text)
+ return
+ }
+ count := 0
+ var lastJoinLoc ast.Loc
+ for _, from := range sel.From {
+ r.countJoins(from, &count, &lastJoinLoc)
+ }
+ if count >= r.limitMaxValue {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementMaximumJoinTableCount.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" exceeds the maximum number of joins %d.", r.text, r.limitMaxValue),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" exceeds the maximum number of joins %d.", text, r.limitMaxValue),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(lastJoinLoc))),
})
}
}
+
+func (r *maxJoinTableCountOmniRule) countJoins(te ast.TableExpr, count *int, lastLoc *ast.Loc) {
+ if te == nil {
+ return
+ }
+ switch t := te.(type) {
+ case *ast.JoinClause:
+ *count++
+ *lastLoc = t.Loc
+ r.countJoins(t.Left, count, lastLoc)
+ r.countJoins(t.Right, count, lastLoc)
+ case *ast.SubqueryExpr:
+ // Don't count joins in subqueries.
+ default:
+ }
+}
diff --git a/backend/plugin/advisor/mysql/rule_statement_maximum_limit_value.go b/backend/plugin/advisor/mysql/rule_statement_maximum_limit_value.go
index 6b9a071dd8..08b7107c6e 100644
--- a/backend/plugin/advisor/mysql/rule_statement_maximum_limit_value.go
+++ b/backend/plugin/advisor/mysql/rule_statement_maximum_limit_value.go
@@ -3,18 +3,15 @@ package mysql
import (
"context"
"fmt"
- "strconv"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -40,106 +37,96 @@ func (*StatementMaximumLimitValueAdvisor) Check(_ context.Context, checkCtx advi
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewStatementMaximumLimitValueRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &maxLimitValueOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ limitMaxValue: int(numberPayload.Number),
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// StatementMaximumLimitValueRule checks for maximum limit value.
-type StatementMaximumLimitValueRule struct {
- BaseRule
- text string
- isSelect bool
+type maxLimitValueOmniRule struct {
+ OmniBaseRule
limitMaxValue int
}
-// NewStatementMaximumLimitValueRule creates a new StatementMaximumLimitValueRule.
-func NewStatementMaximumLimitValueRule(level storepb.Advice_Status, title string, limitMaxValue int) *StatementMaximumLimitValueRule {
- return &StatementMaximumLimitValueRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- limitMaxValue: limitMaxValue,
- }
+func (*maxLimitValueOmniRule) Name() string {
+ return "StatementMaximumLimitValueRule"
}
-// Name returns the rule name.
-func (*StatementMaximumLimitValueRule) Name() string {
- return "StatementMaximumLimitValueRule"
+func (r *maxLimitValueOmniRule) OnStatement(node ast.Node) {
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return
+ }
+ text := strings.TrimSpace(r.StmtText)
+ r.checkSelectLimit(sel, text)
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementMaximumLimitValueRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *maxLimitValueOmniRule) checkSelectLimit(sel *ast.SelectStmt, _ string) {
+ if sel == nil {
+ return
+ }
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelectLimit(sel.Left, "")
+ r.checkSelectLimit(sel.Right, "")
+ // Check top-level limit of set operation.
+ if sel.Limit != nil {
+ r.checkLimit(sel.Limit)
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectStatement:
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.SelectStatementContext).BaseParserRuleContext) {
- r.isSelect = true
+ return
+ }
+ if sel.Limit != nil {
+ r.checkLimit(sel.Limit)
+ }
+ // Check subqueries in FROM.
+ for _, from := range sel.From {
+ r.checkTableExpr(from)
+ }
+}
+
+func (r *maxLimitValueOmniRule) checkTableExpr(te ast.TableExpr) {
+ if te == nil {
+ return
+ }
+ switch t := te.(type) {
+ case *ast.SubqueryExpr:
+ if t.Select != nil {
+ r.checkSelectLimit(t.Select, "")
}
- case NodeTypeLimitClause:
- r.checkLimitClause(ctx.(*mysql.LimitClauseContext))
+ case *ast.JoinClause:
+ r.checkTableExpr(t.Left)
+ r.checkTableExpr(t.Right)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *StatementMaximumLimitValueRule) OnExit(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- if mysqlparser.IsTopMySQLRule(&ctx.(*mysql.SelectStatementContext).BaseParserRuleContext) {
- r.isSelect = false
- }
+func (r *maxLimitValueOmniRule) checkLimit(limit *ast.Limit) {
+ if limit == nil {
+ return
}
- return nil
+ r.checkLimitExpr(limit.Count, limit.Loc)
+ r.checkLimitExpr(limit.Offset, limit.Loc)
}
-func (r *StatementMaximumLimitValueRule) checkLimitClause(ctx *mysql.LimitClauseContext) {
- if !r.isSelect {
+func (r *maxLimitValueOmniRule) checkLimitExpr(expr ast.ExprNode, loc ast.Loc) {
+ if expr == nil {
return
}
- if ctx.LIMIT_SYMBOL() == nil {
+ intLit, ok := expr.(*ast.IntLit)
+ if !ok {
return
}
-
- limitOptions := ctx.LimitOptions()
- for _, limitOption := range limitOptions.AllLimitOption() {
- limitValue, err := strconv.Atoi(limitOption.GetText())
- if err != nil {
- // Ignore invalid limit value and continue.
- continue
- }
-
- if limitValue > r.limitMaxValue {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementExceedMaximumLimitValue.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The limit value %d exceeds the maximum allowed value %d", limitValue, r.limitMaxValue),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+ if int(intLit.Value) > r.limitMaxValue {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementExceedMaximumLimitValue.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("The limit value %d exceeds the maximum allowed value %d", intLit.Value, r.limitMaxValue),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(loc))),
+ })
}
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_maximum_statements_in_transaction.go b/backend/plugin/advisor/mysql/rule_statement_maximum_statements_in_transaction.go
index c181423c6c..8e5cd9e6f1 100644
--- a/backend/plugin/advisor/mysql/rule_statement_maximum_statements_in_transaction.go
+++ b/backend/plugin/advisor/mysql/rule_statement_maximum_statements_in_transaction.go
@@ -3,13 +3,11 @@ package mysql
import (
"context"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -24,7 +22,7 @@ type StatementMaximumStatementsInTransactionAdvisor struct {
}
func (*StatementMaximumStatementsInTransactionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ _, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
if err != nil {
return nil, err
}
@@ -33,64 +31,22 @@ func (*StatementMaximumStatementsInTransactionAdvisor) Check(_ context.Context,
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewStatementMaximumStatementsInTransactionRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &maxStmtsInTxnOmniRule{
+ OmniBaseRule: OmniBaseRule{},
}
- return checker.GetAdviceList(), nil
-}
-
-// StatementMaximumStatementsInTransactionRule checks for maximum statements in transaction.
-type StatementMaximumStatementsInTransactionRule struct {
- BaseRule
- text string
- limitMaxValue int
+ // This rule was a no-op in ANTLR (OnEnter only stored text, no advice generated).
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewStatementMaximumStatementsInTransactionRule creates a new StatementMaximumStatementsInTransactionRule.
-func NewStatementMaximumStatementsInTransactionRule(level storepb.Advice_Status, title string, limitMaxValue int) *StatementMaximumStatementsInTransactionRule {
- return &StatementMaximumStatementsInTransactionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- limitMaxValue: limitMaxValue,
- }
+type maxStmtsInTxnOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*StatementMaximumStatementsInTransactionRule) Name() string {
+func (*maxStmtsInTxnOmniRule) Name() string {
return "StatementMaximumStatementsInTransactionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementMaximumStatementsInTransactionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeQuery {
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementMaximumStatementsInTransactionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*maxStmtsInTxnOmniRule) OnStatement(_ ast.Node) {
+ // The original ANTLR rule only stored query text but never generated advice.
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_merge_alter_table.go b/backend/plugin/advisor/mysql/rule_statement_merge_alter_table.go
index 7c2ea34c5d..d4388da418 100644
--- a/backend/plugin/advisor/mysql/rule_statement_merge_alter_table.go
+++ b/backend/plugin/advisor/mysql/rule_statement_merge_alter_table.go
@@ -5,15 +5,12 @@ import (
"fmt"
"slices"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -37,29 +34,17 @@ func (*StatementMergeAlterTableAdvisor) Check(_ context.Context, checkCtx adviso
return nil, err
}
- // Create the rule
- rule := NewStatementMergeAlterTableRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &mergeAlterTableOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ tableMap: make(map[string]tableStatement),
}
- // Generate advice based on collected table information
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
rule.generateAdvice()
-
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
// tableStatement represents information about a table's statements.
@@ -69,89 +54,47 @@ type tableStatement struct {
lastLine int
}
-// StatementMergeAlterTableRule checks for mergeable ALTER TABLE statements.
-type StatementMergeAlterTableRule struct {
- BaseRule
- text string
+type mergeAlterTableOmniRule struct {
+ OmniBaseRule
tableMap map[string]tableStatement
}
-// NewStatementMergeAlterTableRule creates a new StatementMergeAlterTableRule.
-func NewStatementMergeAlterTableRule(level storepb.Advice_Status, title string) *StatementMergeAlterTableRule {
- return &StatementMergeAlterTableRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- tableMap: make(map[string]tableStatement),
- }
-}
-
-// Name returns the rule name.
-func (*StatementMergeAlterTableRule) Name() string {
+func (*mergeAlterTableOmniRule) Name() string {
return "StatementMergeAlterTableRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementMergeAlterTableRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
+func (r *mergeAlterTableOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table == nil {
+ return
+ }
+ tableName := n.Table.Name
+ r.tableMap[tableName] = tableStatement{
+ name: tableName,
+ count: 1,
+ lastLine: r.BaseLine + int(r.ContentStartLine()),
+ }
+ case *ast.AlterTableStmt:
+ if n.Table == nil {
+ return
+ }
+ tableName := n.Table.Name
+ table, ok := r.tableMap[tableName]
if !ok {
- return nil
+ table = tableStatement{
+ name: tableName,
+ count: 0,
+ }
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+ table.count++
+ table.lastLine = r.BaseLine + int(r.ContentStartLine())
+ r.tableMap[tableName] = table
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementMergeAlterTableRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *StatementMergeAlterTableRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableName() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.tableMap[tableName] = tableStatement{
- name: tableName,
- count: 1,
- lastLine: r.baseLine + ctx.GetStart().GetLine(),
- }
-}
-
-func (r *StatementMergeAlterTableRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- table, ok := r.tableMap[tableName]
- if !ok {
- table = tableStatement{
- name: tableName,
- count: 0,
- }
- }
- table.count++
- table.lastLine = r.baseLine + ctx.GetStart().GetLine()
- r.tableMap[tableName] = table
}
-func (r *StatementMergeAlterTableRule) generateAdvice() {
+func (r *mergeAlterTableOmniRule) generateAdvice() {
var tableList []tableStatement
for _, table := range r.tableMap {
tableList = append(tableList, table)
@@ -168,10 +111,10 @@ func (r *StatementMergeAlterTableRule) generateAdvice() {
for _, table := range tableList {
if table.count > 1 {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementRedundantAlterTable.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("There are %d statements to modify table `%s`", table.count, table.name),
StartPosition: common.ConvertANTLRLineToPosition(table.lastLine),
})
diff --git a/backend/plugin/advisor/mysql/rule_statement_query_minimum_plan_level.go b/backend/plugin/advisor/mysql/rule_statement_query_minimum_plan_level.go
index 93e4483eef..78538cfef7 100644
--- a/backend/plugin/advisor/mysql/rule_statement_query_minimum_plan_level.go
+++ b/backend/plugin/advisor/mysql/rule_statement_query_minimum_plan_level.go
@@ -6,16 +6,13 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -40,31 +37,40 @@ func (*StatementQueryMinumumPlanLevelAdvisor) Check(ctx context.Context, checkCt
return nil, errors.New("string_payload is required for this rule")
}
- // Create the rule
- rule := NewStatementQueryMinumumPlanLevelRule(ctx, level, checkCtx.Rule.Type.String(), checkCtx.Driver, convertExplainTypeFromString(strings.ToUpper(stringPayload.Value)))
+ driver := checkCtx.Driver
+ if driver == nil {
+ return nil, nil
+ }
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ title := checkCtx.Rule.Type.String()
+ explainType := convertExplainTypeFromString(strings.ToUpper(stringPayload.Value))
- if rule.driver != nil {
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.GetExplainCount() >= common.MaximumLintExplainSize {
- break
- }
+ rule := &statementQueryMinPlanLevelOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: title,
+ },
+ driver: driver,
+ ctx: ctx,
+ explainType: explainType,
+ }
+
+ for _, stmt := range checkCtx.ParsedStatements {
+ if stmt.AST == nil {
+ continue
+ }
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
+ if !ok {
+ continue
+ }
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
+ if rule.explainCount >= common.MaximumLintExplainSize {
+ break
}
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
type ExplainType int
@@ -112,114 +118,73 @@ func convertExplainTypeFromString(explainTypeStr string) ExplainType {
case "CONST":
return ExplainTypeConst
default:
- // Default to ALL if we don't recognize the explain type.
return ExplainTypeAll
}
}
-// StatementQueryMinumumPlanLevelRule checks for query minimum plan level.
-type StatementQueryMinumumPlanLevelRule struct {
- BaseRule
- text string
+// statementQueryMinPlanLevelOmniRule checks for query minimum plan level using omni AST.
+type statementQueryMinPlanLevelOmniRule struct {
+ OmniBaseRule
driver *sql.DB
ctx context.Context
explainType ExplainType
explainCount int
}
-// NewStatementQueryMinumumPlanLevelRule creates a new StatementQueryMinumumPlanLevelRule.
-func NewStatementQueryMinumumPlanLevelRule(ctx context.Context, level storepb.Advice_Status, title string, driver *sql.DB, explainType ExplainType) *StatementQueryMinumumPlanLevelRule {
- return &StatementQueryMinumumPlanLevelRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- driver: driver,
- ctx: ctx,
- explainType: explainType,
- }
-}
-
-// Name returns the rule name.
-func (*StatementQueryMinumumPlanLevelRule) Name() string {
+func (*statementQueryMinPlanLevelOmniRule) Name() string {
return "StatementQueryMinumumPlanLevelRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementQueryMinumumPlanLevelRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectStatement:
- r.checkSelectStatement(ctx.(*mysql.SelectStatementContext))
- default:
- // Other node types
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementQueryMinumumPlanLevelRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-// GetExplainCount returns the explain count.
-func (r *StatementQueryMinumumPlanLevelRule) GetExplainCount() int {
- return r.explainCount
-}
-
-func (r *StatementQueryMinumumPlanLevelRule) checkSelectStatement(ctx *mysql.SelectStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
+func (r *statementQueryMinPlanLevelOmniRule) OnStatement(node ast.Node) {
+ if _, ok := node.(*ast.SelectStmt); !ok {
return
}
- query := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+ query := r.TrimmedStmtText()
+ line := r.BaseLine + int(r.ContentStartLine())
r.explainCount++
res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementExplainQueryFailed.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Failed to explain query: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
})
- } else {
- explainTypes, err := getQueryExplainTypes(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to check explain type column: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else if len(explainTypes) > 0 {
- overused, overusedType := false, ExplainTypeAll
- for _, explainType := range explainTypes {
- if explainType < r.explainType {
- overused = true
- overusedType = explainType
- break
- }
- }
- if overused {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementUnwantedQueryPlanLevel.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Overused query plan level detected %s, minimum plan level: %s", overusedType.String(), r.explainType.String()),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ return
+ }
+
+ explainTypes, err := getQueryExplainTypes(res)
+ if err != nil {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.Internal.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Failed to check explain type column: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ return
+ }
+
+ if len(explainTypes) > 0 {
+ overused, overusedType := false, ExplainTypeAll
+ for _, et := range explainTypes {
+ if et < r.explainType {
+ overused = true
+ overusedType = et
+ break
}
}
+ if overused {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementUnwantedQueryPlanLevel.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Overused query plan level detected %s, minimum plan level: %s", overusedType.String(), r.explainType.String()),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
}
}
@@ -239,26 +204,6 @@ func getQueryExplainTypes(res []any) ([]ExplainType, error) {
return nil, errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // 1. the column 4 is the data 'type'.
- // We check all rows of the result to see if any of them has 'ALL' or 'index' in the 'type' column.
- // 2. the column 11 is the 'Extra' column.
- // If the 'Extra' column dose not contain
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
typeIndex, err := getColumnIndex(columns, "type")
if err != nil {
return nil, errors.Errorf("failed to find rows column")
@@ -272,7 +217,6 @@ func getQueryExplainTypes(res []any) ([]ExplainType, error) {
}
explainType, ok := row[typeIndex].(string)
if !ok {
- // Skip the row if the type column is not a string.
continue
}
explainTypes = append(explainTypes, convertExplainTypeFromString(explainType))
diff --git a/backend/plugin/advisor/mysql/rule_statement_select_full_table_scan.go b/backend/plugin/advisor/mysql/rule_statement_select_full_table_scan.go
index 9025c2cd22..9af8cecdb2 100644
--- a/backend/plugin/advisor/mysql/rule_statement_select_full_table_scan.go
+++ b/backend/plugin/advisor/mysql/rule_statement_select_full_table_scan.go
@@ -2,20 +2,16 @@ package mysql
import (
"context"
- "database/sql"
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
@@ -38,115 +34,68 @@ func (*StatementSelectFullTableScanAdvisor) Check(ctx context.Context, checkCtx
return nil, err
}
- // Create the rule
- rule := NewStatementSelectFullTableScanRule(ctx, level, checkCtx.Rule.Type.String(), checkCtx.Driver)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ driver := checkCtx.Driver
+ title := checkCtx.Rule.Type.String()
+ var advice []*storepb.Advice
+ explainCount := 0
- if rule.driver != nil {
+ if driver != nil {
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- if rule.GetExplainCount() >= common.MaximumLintExplainSize {
- break
- }
- }
- }
-
- return checker.GetAdviceList(), nil
-}
-// StatementSelectFullTableScanRule checks for full table scan.
-type StatementSelectFullTableScanRule struct {
- BaseRule
- driver *sql.DB
- ctx context.Context
- explainCount int
-}
-
-// NewStatementSelectFullTableScanRule creates a new StatementSelectFullTableScanRule.
-func NewStatementSelectFullTableScanRule(ctx context.Context, level storepb.Advice_Status, title string, driver *sql.DB) *StatementSelectFullTableScanRule {
- return &StatementSelectFullTableScanRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- driver: driver,
- ctx: ctx,
- }
-}
-
-// Name returns the rule name.
-func (*StatementSelectFullTableScanRule) Name() string {
- return "StatementSelectFullTableScanRule"
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *StatementSelectFullTableScanRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- r.checkSelectStatement(ctx.(*mysql.SelectStatementContext))
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*StatementSelectFullTableScanRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-// GetExplainCount returns the explain count.
-func (r *StatementSelectFullTableScanRule) GetExplainCount() int {
- return r.explainCount
-}
+ // Only handle top-level SELECT statements.
+ if _, ok := node.(*ast.SelectStmt); !ok {
+ continue
+ }
-func (r *StatementSelectFullTableScanRule) checkSelectStatement(ctx *mysql.SelectStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
- return
- }
+ baseLine := stmt.BaseLine()
+ query := strings.TrimRight(strings.TrimSpace(stmt.Text), ";") + ";"
+ line := baseLine + int(mysqlparser.ByteOffsetToRunePosition(stmt.Text, contentStartIndex(stmt.Text)).Line)
+
+ explainCount++
+ res, err := advisor.Query(ctx, advisor.QueryContext{}, driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementCheckSelectFullTableScanFailed.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to check full table scan: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else {
+ hasFullScan, tables, err := hasTableFullScan(res)
+ if err != nil {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.Internal.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Failed to check full table scan: %s, with error: %s", query, err),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ } else if hasFullScan {
+ advice = append(advice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementHasTableFullScan.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Full table scan detected on table(s): %s", tables),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
- query := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- r.explainCount++
- res, err := advisor.Query(r.ctx, advisor.QueryContext{}, r.driver, storepb.Engine_MYSQL, fmt.Sprintf("EXPLAIN %s", query))
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementCheckSelectFullTableScanFailed.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to check full table scan: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else {
- hasFullScan, tables, err := hasTableFullScan(res)
- if err != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Internal.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Failed to check full table scan: %s, with error: %s", query, err),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- } else if hasFullScan {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementHasTableFullScan.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Full table scan detected on table(s): %s", tables),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ if explainCount >= common.MaximumLintExplainSize {
+ break
+ }
}
}
+
+ return advice, nil
}
func hasTableFullScan(res []any) (bool, string, error) {
@@ -166,27 +115,6 @@ func hasTableFullScan(res []any) (bool, string, error) {
return false, "", errors.Errorf("not found any data")
}
- // MySQL EXPLAIN statement result has 12 columns.
- // 1. the column 4 is the data 'type'.
- // We check all rows of the result to see if any of them has 'ALL' or 'index' in the 'type' column.
- // 2. the column 11 is the 'Extra' column.
- // If the 'Extra' column dose not contain
- //
- // mysql> explain delete from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- // | 1 | DELETE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
- //
- // mysql> explain insert into td select * from td;
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
- // | 1 | INSERT | td | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
- // | 1 | SIMPLE | td | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using temporary |
- // +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
-
tableIndex, err := getColumnIndex(columns, "table")
if err != nil {
return false, "", errors.Errorf("failed to find rows column")
diff --git a/backend/plugin/advisor/mysql/rule_statement_where_disallow_using_function.go b/backend/plugin/advisor/mysql/rule_statement_where_disallow_using_function.go
index e1919fc7db..36aa7e1a27 100644
--- a/backend/plugin/advisor/mysql/rule_statement_where_disallow_using_function.go
+++ b/backend/plugin/advisor/mysql/rule_statement_where_disallow_using_function.go
@@ -3,16 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
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/advisor/code"
)
var (
@@ -32,96 +30,46 @@ func (*StatementWhereDisallowUsingFunctionAdvisor) Check(_ context.Context, chec
return nil, err
}
- // Create the rule
- rule := NewStatementWhereDisallowUsingFunctionRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &whereDisallowFuncOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// StatementWhereDisallowUsingFunctionRule checks for functions in WHERE clause.
-type StatementWhereDisallowUsingFunctionRule struct {
- BaseRule
- text string
- isSelect bool
- inWhereClause bool
+type whereDisallowFuncOmniRule struct {
+ OmniBaseRule
}
-// NewStatementWhereDisallowUsingFunctionRule creates a new StatementWhereDisallowUsingFunctionRule.
-func NewStatementWhereDisallowUsingFunctionRule(level storepb.Advice_Status, title string) *StatementWhereDisallowUsingFunctionRule {
- return &StatementWhereDisallowUsingFunctionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*StatementWhereDisallowUsingFunctionRule) Name() string {
+func (*whereDisallowFuncOmniRule) Name() string {
return "StatementWhereDisallowUsingFunctionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementWhereDisallowUsingFunctionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
+func (r *whereDisallowFuncOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ // Collect WHERE expressions from all SelectStmt nodes (including UNION branches).
+ ast.Inspect(node, func(n ast.Node) bool {
+ sel, ok := n.(*ast.SelectStmt)
if !ok {
- return nil
+ return true
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectStatement:
- r.isSelect = true
- case NodeTypeWhereClause:
- r.inWhereClause = true
- case NodeTypeFunctionCall:
- r.checkFunctionCall(ctx.(*mysql.FunctionCallContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *StatementWhereDisallowUsingFunctionRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeSelectStatement:
- r.isSelect = false
- case NodeTypeWhereClause:
- r.inWhereClause = false
- default:
- }
- return nil
-}
-
-func (r *StatementWhereDisallowUsingFunctionRule) checkFunctionCall(ctx *mysql.FunctionCallContext) {
- if !r.isSelect || !r.inWhereClause {
- return
- }
-
- pi := ctx.PureIdentifier()
- if pi != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DisabledFunction.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Function is disallowed in where clause, but \"%s\" uses", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+ if sel.Where != nil {
+ ast.Inspect(sel.Where, func(wn ast.Node) bool {
+ if fn, ok := wn.(*ast.FuncCallExpr); ok {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.DisabledFunction.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Function is disallowed in where clause, but \"%s\" uses", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(fn.Loc))),
+ })
+ }
+ return true
+ })
+ }
+ return true
+ })
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_where_maximum_logical_operator_count.go b/backend/plugin/advisor/mysql/rule_statement_where_maximum_logical_operator_count.go
index 7ec95428e1..79c90a6a7f 100644
--- a/backend/plugin/advisor/mysql/rule_statement_where_maximum_logical_operator_count.go
+++ b/backend/plugin/advisor/mysql/rule_statement_where_maximum_logical_operator_count.go
@@ -3,16 +3,16 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -36,141 +36,149 @@ func (*StatementWhereMaximumLogicalOperatorCountAdvisor) Check(_ context.Context
return nil, errors.New("number_payload is required for this rule")
}
+ maximum := int(numberPayload.Number)
var allAdvice []*storepb.Advice
for _, stmt := range checkCtx.ParsedStatements {
- // Create the rule for each statement
- rule := NewStatementWhereMaximumLogicalOperatorCountRule(level, checkCtx.Rule.Type.String(), int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- rule.resetForStatement()
+ rule := &maxLogicalOperatorOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ maximum: maximum,
+ }
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
-
- // Check OR conditions after walking the tree
- rule.checkOrConditions()
-
- allAdvice = append(allAdvice, checker.GetAdviceList()...)
+ if br, ok2 := any(rule).(interface{ SetStatement(int, string) }); ok2 {
+ br.SetStatement(stmt.BaseLine(), stmt.Text)
+ }
+ rule.OnStatement(node)
+ allAdvice = append(allAdvice, rule.GetAdviceList()...)
}
return allAdvice, nil
}
-// StatementWhereMaximumLogicalOperatorCountRule checks for maximum logical operators in WHERE.
-type StatementWhereMaximumLogicalOperatorCountRule struct {
- BaseRule
- text string
- maximum int
- reported bool
- depth int
- inPredicateExprIn bool
- maxOrCount int
- maxOrCountLine int
+type maxLogicalOperatorOmniRule struct {
+ OmniBaseRule
+ maximum int
}
-// NewStatementWhereMaximumLogicalOperatorCountRule creates a new StatementWhereMaximumLogicalOperatorCountRule.
-func NewStatementWhereMaximumLogicalOperatorCountRule(level storepb.Advice_Status, title string, maximum int) *StatementWhereMaximumLogicalOperatorCountRule {
- return &StatementWhereMaximumLogicalOperatorCountRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maximum: maximum,
- }
-}
-
-// Name returns the rule name.
-func (*StatementWhereMaximumLogicalOperatorCountRule) Name() string {
+func (*maxLogicalOperatorOmniRule) Name() string {
return "StatementWhereMaximumLogicalOperatorCountRule"
}
-// resetForStatement resets state for a new statement.
-func (r *StatementWhereMaximumLogicalOperatorCountRule) resetForStatement() {
- r.reported = false
- r.depth = 0
- r.inPredicateExprIn = false
- r.maxOrCount = 0
- r.maxOrCountLine = 0
+func (r *maxLogicalOperatorOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ r.walkNode(node, text)
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementWhereMaximumLogicalOperatorCountRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *maxLogicalOperatorOmniRule) walkNode(node ast.Node, text string) {
+ switch n := node.(type) {
+ case *ast.SelectStmt:
+ r.checkSelect(n, text)
+ case *ast.UpdateStmt:
+ r.checkExpr(n.Where, text)
+ case *ast.DeleteStmt:
+ r.checkExpr(n.Where, text)
+ case *ast.InsertStmt:
+ if n.Select != nil {
+ r.checkSelect(n.Select, text)
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypePredicateExprIn:
- r.inPredicateExprIn = true
- case NodeTypeExprList:
- r.checkExprList(ctx.(*mysql.ExprListContext))
- case NodeTypeExprOr:
- r.checkExprOr(ctx.(*mysql.ExprOrContext))
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *StatementWhereMaximumLogicalOperatorCountRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypePredicateExprIn:
- r.inPredicateExprIn = false
- case NodeTypeExprOr:
- r.depth--
- default:
- }
- return nil
-}
-
-func (r *StatementWhereMaximumLogicalOperatorCountRule) checkExprList(ctx *mysql.ExprListContext) {
- if r.reported {
+func (r *maxLogicalOperatorOmniRule) checkSelect(sel *ast.SelectStmt, text string) {
+ if sel == nil {
return
}
- if !r.inPredicateExprIn {
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelect(sel.Left, text)
+ r.checkSelect(sel.Right, text)
return
}
+ r.checkExpr(sel.Where, text)
+}
- count := len(ctx.AllExpr())
- if count > r.maximum {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+func (r *maxLogicalOperatorOmniRule) checkExpr(expr ast.ExprNode, text string) {
+ if expr == nil {
+ return
+ }
+ // Count OR operands.
+ orCount := r.countOrOperands(expr)
+ if orCount > r.maximum {
+ line := r.findFirstOrLine(expr)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementWhereMaximumLogicalOperatorCount.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Number of tokens (%d) in IN predicate operation exceeds limit (%d) in statement %q.", count, r.maximum, r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Number of tokens (%d) in the OR predicate operation exceeds limit (%d) in statement %q.", orCount, r.maximum, text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(line)),
})
}
+ // Check IN lists.
+ r.walkExprForIn(expr, text)
}
-func (r *StatementWhereMaximumLogicalOperatorCountRule) checkExprOr(ctx *mysql.ExprOrContext) {
- r.depth++
- count := r.depth + 1
- if count > r.maxOrCount {
- r.maxOrCount = count
- r.maxOrCountLine = r.baseLine + ctx.GetStart().GetLine()
+// countOrOperands returns the number of operands in an OR chain.
+// For `a OR b OR c OR d OR e` (4 OR operators), returns 5.
+func (*maxLogicalOperatorOmniRule) countOrOperands(expr ast.ExprNode) int {
+ if expr == nil {
+ return 0
}
+ bin, ok := expr.(*ast.BinaryExpr)
+ if !ok || bin.Op != ast.BinOpOr {
+ return 0
+ }
+ count := 0
+ for {
+ count++
+ left, ok := bin.Left.(*ast.BinaryExpr)
+ if !ok || left.Op != ast.BinOpOr {
+ count++ // count the final left operand
+ break
+ }
+ bin = left
+ }
+ return count
}
-func (r *StatementWhereMaximumLogicalOperatorCountRule) checkOrConditions() {
- if r.maxOrCount > r.maximum {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementWhereMaximumLogicalOperatorCount.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Number of tokens (%d) in the OR predicate operation exceeds limit (%d) in statement %q.", r.maxOrCount, r.maximum, r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.maxOrCountLine),
- })
+func (r *maxLogicalOperatorOmniRule) findFirstOrLine(expr ast.ExprNode) int32 {
+ if expr == nil {
+ return r.ContentStartLine()
}
+ bin, ok := expr.(*ast.BinaryExpr)
+ if !ok || bin.Op != ast.BinOpOr {
+ return r.ContentStartLine()
+ }
+ // Find the deepest OR.
+ if left, ok := bin.Left.(*ast.BinaryExpr); ok && left.Op == ast.BinOpOr {
+ return r.findFirstOrLine(bin.Left)
+ }
+ return r.LocToLine(bin.Loc)
+}
+
+func (r *maxLogicalOperatorOmniRule) walkExprForIn(expr ast.ExprNode, text string) {
+ ast.Inspect(expr, func(n ast.Node) bool {
+ e, ok := n.(*ast.InExpr)
+ if !ok {
+ return true
+ }
+ count := len(e.List)
+ if count > r.maximum {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementWhereMaximumLogicalOperatorCount.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Number of tokens (%d) in IN predicate operation exceeds limit (%d) in statement %q.", count, r.maximum, text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(e.Loc))),
+ })
+ }
+ return false
+ })
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_where_no_equal_null.go b/backend/plugin/advisor/mysql/rule_statement_where_no_equal_null.go
index d1c9ba38be..1bd3a9fbc2 100644
--- a/backend/plugin/advisor/mysql/rule_statement_where_no_equal_null.go
+++ b/backend/plugin/advisor/mysql/rule_statement_where_no_equal_null.go
@@ -3,15 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -31,93 +30,53 @@ func (*StatementWhereNoEqualNullAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- // Create the rule
- rule := NewStatementWhereNoEqualNullRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &whereNoEqualNullOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// StatementWhereNoEqualNullRule checks for equal NULL in WHERE clause.
-type StatementWhereNoEqualNullRule struct {
- BaseRule
- text string
- isSelect bool
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewStatementWhereNoEqualNullRule creates a new StatementWhereNoEqualNullRule.
-func NewStatementWhereNoEqualNullRule(level storepb.Advice_Status, title string) *StatementWhereNoEqualNullRule {
- return &StatementWhereNoEqualNullRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type whereNoEqualNullOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*StatementWhereNoEqualNullRule) Name() string {
+func (*whereNoEqualNullOmniRule) Name() string {
return "StatementWhereNoEqualNullRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementWhereNoEqualNullRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
+func (r *whereNoEqualNullOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ // Collect WHERE expressions from all SelectStmt nodes (including UNION branches).
+ ast.Inspect(node, func(n ast.Node) bool {
+ sel, ok := n.(*ast.SelectStmt)
if !ok {
- return nil
+ return true
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectStatement:
- r.isSelect = true
- case NodeTypePrimaryExprCompare:
- r.checkPrimaryExprCompare(ctx.(*mysql.PrimaryExprCompareContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (r *StatementWhereNoEqualNullRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeSelectStatement {
- r.isSelect = false
- }
- return nil
+ if sel.Where != nil {
+ ast.Inspect(sel.Where, func(wn ast.Node) bool {
+ if bin, ok := wn.(*ast.BinaryExpr); ok {
+ if (bin.Op == ast.BinOpEq || bin.Op == ast.BinOpNe) && isNullLiteral(bin.Right) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementWhereNoEqualNull.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("WHERE clause contains equal null: %s", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(bin.Loc))),
+ })
+ }
+ }
+ return true
+ })
+ }
+ return true
+ })
}
-func (r *StatementWhereNoEqualNullRule) checkPrimaryExprCompare(ctx *mysql.PrimaryExprCompareContext) {
- if !r.isSelect {
- return
- }
-
- compOp := ctx.CompOp()
- // We only check for equal and not equal.
- if compOp == nil || (compOp.EQUAL_OPERATOR() == nil && compOp.NOT_EQUAL_OPERATOR() == nil) {
- return
- }
- if ctx.Predicate() != nil && ctx.Predicate().GetText() == "NULL" {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementWhereNoEqualNull.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("WHERE clause contains equal null: %s", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+func isNullLiteral(expr ast.ExprNode) bool {
+ _, ok := expr.(*ast.NullLit)
+ return ok
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_disallow_commit.go b/backend/plugin/advisor/mysql/rule_stmt_disallow_commit.go
index cf62d425aa..ae71ee5eca 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_disallow_commit.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_disallow_commit.go
@@ -3,16 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -36,83 +34,35 @@ func (*StatementDisallowCommitAdvisor) Check(_ context.Context, checkCtx advisor
return nil, err
}
- // Create the rule
- rule := NewStatementDisallowCommitRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// StatementDisallowCommitRule checks for disallowing commit.
-type StatementDisallowCommitRule struct {
- BaseRule
- text string
-}
-
-// NewStatementDisallowCommitRule creates a new StatementDisallowCommitRule.
-func NewStatementDisallowCommitRule(level storepb.Advice_Status, title string) *StatementDisallowCommitRule {
- return &StatementDisallowCommitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &disallowCommitOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*StatementDisallowCommitRule) Name() string {
- return "StatementDisallowCommitRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementDisallowCommitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeTransactionStatement:
- r.checkTransactionStatement(ctx.(*mysql.TransactionStatementContext))
- default:
- }
- return nil
+type disallowCommitOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*StatementDisallowCommitRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*disallowCommitOmniRule) Name() string {
+ return "StatementDisallowCommitRule"
}
-func (r *StatementDisallowCommitRule) checkTransactionStatement(ctx *mysql.TransactionStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *disallowCommitOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CommitStmt)
+ if !ok {
return
}
- if ctx.COMMIT_SYMBOL() == nil {
- return
- }
-
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ text := strings.TrimSpace(r.StmtText)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementDisallowCommit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_disallow_limit.go b/backend/plugin/advisor/mysql/rule_stmt_disallow_limit.go
index ab58539de1..9b7062fc35 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_disallow_limit.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_disallow_limit.go
@@ -3,15 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -24,118 +23,76 @@ func init() {
advisor.Register(storepb.Engine_OCEANBASE, storepb.SQLReviewRule_STATEMENT_DISALLOW_LIMIT, &StatementDisallowLimitAdvisor{})
}
-// StatementDisallowLimitAdvisor is the advisor checking for no LIMIT clause in INSERT/UPDATE statement.
+// StatementDisallowLimitAdvisor is the advisor checking for no LIMIT clause in INSERT/UPDATE/DELETE statement.
type StatementDisallowLimitAdvisor struct {
}
-// Check checks for no LIMIT clause in INSERT/UPDATE statement.
+// Check checks for no LIMIT clause in INSERT/UPDATE/DELETE statement.
func (*StatementDisallowLimitAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
if err != nil {
return nil, err
}
- // Create the rule
- rule := NewStatementDisallowLimitRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &disallowLimitOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// StatementDisallowLimitRule checks for no LIMIT clause in INSERT/UPDATE statement.
-type StatementDisallowLimitRule struct {
- BaseRule
- isInsertStmt bool
- text string
- line int
+type disallowLimitOmniRule struct {
+ OmniBaseRule
}
-// NewStatementDisallowLimitRule creates a new StatementDisallowLimitRule.
-func NewStatementDisallowLimitRule(level storepb.Advice_Status, title string) *StatementDisallowLimitRule {
- return &StatementDisallowLimitRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*StatementDisallowLimitRule) Name() string {
+func (*disallowLimitOmniRule) Name() string {
return "StatementDisallowLimitRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *StatementDisallowLimitRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
+func (r *disallowLimitOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ switch n := node.(type) {
+ case *ast.DeleteStmt:
+ if n.Limit != nil {
+ r.addLimitAdvice(code.DeleteUseLimit, text, n.Loc)
+ }
+ case *ast.UpdateStmt:
+ if n.Limit != nil {
+ r.addLimitAdvice(code.UpdateUseLimit, text, n.Loc)
+ }
+ case *ast.InsertStmt:
+ if n.Select != nil {
+ r.checkSelectLimit(n.Select, text)
}
- case NodeTypeDeleteStatement:
- r.checkDeleteStatement(ctx.(*mysql.DeleteStatementContext))
- case NodeTypeUpdateStatement:
- r.checkUpdateStatement(ctx.(*mysql.UpdateStatementContext))
- case NodeTypeInsertStatement:
- r.isInsertStmt = true
- case NodeTypeQueryExpression:
- r.checkQueryExpression(ctx.(*mysql.QueryExpressionContext))
default:
- // Ignore other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *StatementDisallowLimitRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeInsertStatement {
- r.isInsertStmt = false
- }
- return nil
-}
-
-func (r *StatementDisallowLimitRule) checkDeleteStatement(ctx *mysql.DeleteStatementContext) {
- if ctx.SimpleLimitClause() != nil && ctx.SimpleLimitClause().LIMIT_SYMBOL() != nil {
- r.handleLimitClause(code.DeleteUseLimit, ctx.GetStart().GetLine())
- }
-}
-
-func (r *StatementDisallowLimitRule) checkUpdateStatement(ctx *mysql.UpdateStatementContext) {
- if ctx.SimpleLimitClause() != nil && ctx.SimpleLimitClause().LIMIT_SYMBOL() != nil {
- r.handleLimitClause(code.UpdateUseLimit, ctx.GetStart().GetLine())
+func (r *disallowLimitOmniRule) checkSelectLimit(sel *ast.SelectStmt, text string) {
+ if sel == nil {
+ return
}
-}
-
-func (r *StatementDisallowLimitRule) checkQueryExpression(ctx *mysql.QueryExpressionContext) {
- if !r.isInsertStmt {
+ if sel.SetOp != ast.SetOpNone {
+ // Check set operation top-level limit.
+ if sel.Limit != nil {
+ r.addLimitAdvice(code.InsertUseLimit, text, sel.Loc)
+ }
return
}
- if ctx.LimitClause() != nil && ctx.LimitClause().LIMIT_SYMBOL() != nil {
- r.handleLimitClause(code.InsertUseLimit, ctx.GetStart().GetLine())
+ if sel.Limit != nil {
+ r.addLimitAdvice(code.InsertUseLimit, text, sel.Loc)
}
}
-func (r *StatementDisallowLimitRule) handleLimitClause(code code.Code, lineNumber int) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"%s\" uses", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.line + lineNumber),
+func (r *disallowLimitOmniRule) addLimitAdvice(c code.Code, text string, _ ast.Loc) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: c.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"%s\" uses", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
})
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_disallow_order_by.go b/backend/plugin/advisor/mysql/rule_stmt_disallow_order_by.go
index b2d109c28f..ab8307020d 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_disallow_order_by.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_disallow_order_by.go
@@ -3,15 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -35,90 +34,45 @@ func (*DisallowOrderByAdvisor) Check(_ context.Context, checkCtx advisor.Context
return nil, err
}
- // Create the rule
- rule := NewDisallowOrderByRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &disallowOrderByOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// DisallowOrderByRule checks for no ORDER BY clause in DELETE/UPDATE statements.
-type DisallowOrderByRule struct {
- BaseRule
- text string
-}
-
-// NewDisallowOrderByRule creates a new DisallowOrderByRule.
-func NewDisallowOrderByRule(level storepb.Advice_Status, title string) *DisallowOrderByRule {
- return &DisallowOrderByRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type disallowOrderByOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*DisallowOrderByRule) Name() string {
+func (*disallowOrderByOmniRule) Name() string {
return "DisallowOrderByRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *DisallowOrderByRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *disallowOrderByOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ switch n := node.(type) {
+ case *ast.DeleteStmt:
+ if len(n.OrderBy) > 0 {
+ r.addOrderByAdvice(code.DeleteUseOrderBy, text, n.Loc)
+ }
+ case *ast.UpdateStmt:
+ if len(n.OrderBy) > 0 {
+ r.addOrderByAdvice(code.UpdateUseOrderBy, text, n.Loc)
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeDeleteStatement:
- r.checkDeleteStatement(ctx.(*mysql.DeleteStatementContext))
- case NodeTypeUpdateStatement:
- r.checkUpdateStatement(ctx.(*mysql.UpdateStatementContext))
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*DisallowOrderByRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *DisallowOrderByRule) checkDeleteStatement(ctx *mysql.DeleteStatementContext) {
- if ctx.OrderClause() != nil && ctx.OrderClause().ORDER_SYMBOL() != nil {
- r.handleOrderByClause(code.DeleteUseOrderBy, ctx.GetStart().GetLine())
- }
-}
-
-func (r *DisallowOrderByRule) checkUpdateStatement(ctx *mysql.UpdateStatementContext) {
- if ctx.OrderClause() != nil && ctx.OrderClause().ORDER_SYMBOL() != nil {
- r.handleOrderByClause(code.UpdateUseOrderBy, ctx.GetStart().GetLine())
- }
}
-func (r *DisallowOrderByRule) handleOrderByClause(code code.Code, lineNumber int) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"%s\" uses", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
+func (r *disallowOrderByOmniRule) addOrderByAdvice(c code.Code, text string, loc ast.Loc) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: c.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"%s\" uses", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(loc))),
})
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_max_execution_time.go b/backend/plugin/advisor/mysql/rule_stmt_max_execution_time.go
index 6fee817141..64a683d3bc 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_max_execution_time.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_max_execution_time.go
@@ -6,13 +6,12 @@ import (
"strconv"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -35,127 +34,51 @@ func (*MaxExecutionTimeAdvisor) Check(_ context.Context, checkCtx advisor.Contex
}
systemVariable := "max_execution_time"
- // For MariaDB, the system variable is `max_statement_time`.
if checkCtx.Rule.Engine == storepb.Engine_MARIADB {
systemVariable = "max_statement_time"
}
- // Create the rule
- rule := NewMaxExecutionTimeRule(level, checkCtx.Rule.Type.String(), systemVariable)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ hasSet := false
+ var allAdvice []*storepb.Advice
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// MaxExecutionTimeRule checks for the max execution time.
-type MaxExecutionTimeRule struct {
- BaseRule
- // The system variable name for max execution time.
- // For MySQL, it is `max_execution_time`.
- // For MariaDB, it is `max_statement_time`.
- systemVariable string
- hasSet bool
-}
-
-// NewMaxExecutionTimeRule creates a new MaxExecutionTimeRule.
-func NewMaxExecutionTimeRule(level storepb.Advice_Status, title string, systemVariable string) *MaxExecutionTimeRule {
- return &MaxExecutionTimeRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- systemVariable: systemVariable,
- }
-}
-
-// Name returns the rule name.
-func (*MaxExecutionTimeRule) Name() string {
- return "MaxExecutionTimeRule"
-}
-
-// HasSet returns whether the system variable is set.
-func (r *MaxExecutionTimeRule) HasSet() bool {
- return r.hasSet
-}
-
-// OnEnter is called when entering a parse tree node.
-func (r *MaxExecutionTimeRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeSimpleStatement:
- r.checkSimpleStatement(ctx.(*mysql.SimpleStatementContext))
- case NodeTypeSetStatement:
- r.checkSetStatement(ctx.(*mysql.SetStatementContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*MaxExecutionTimeRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *MaxExecutionTimeRule) checkSimpleStatement(ctx *mysql.SimpleStatementContext) {
- // Skip if we have already found the system variable is set or the statement is a SET statement.
- if r.hasSet || ctx.SetStatement() != nil {
- return
- }
-
- // The set max execution time statement should be the first statement in the SQL.
- // Otherwise, we will always set the advice (but only once).
- if len(r.adviceList) == 0 {
- r.setAdvice()
- }
-}
-func (r *MaxExecutionTimeRule) checkSetStatement(ctx *mysql.SetStatementContext) {
- startOptionValueList := ctx.StartOptionValueList()
- if ctx.StartOptionValueList() == nil {
- return
- }
-
- variable, value := "", ""
- optionValueList := startOptionValueList.StartOptionValueListFollowingOptionType()
- if optionValueList != nil {
- tmp := optionValueList.OptionValueFollowingOptionType()
- if tmp != nil {
- if tmp.InternalVariableName() != nil && tmp.SetExprOrDefault() != nil {
- variable, value = tmp.InternalVariableName().GetText(), tmp.SetExprOrDefault().GetText()
+ switch n := node.(type) {
+ case *ast.SetStmt:
+ for _, assign := range n.Assignments {
+ if assign.Column == nil || assign.Value == nil {
+ continue
+ }
+ varName := assign.Column.Column
+ if strings.ToLower(varName) == systemVariable {
+ if intLit, ok := assign.Value.(*ast.IntLit); ok {
+ _ = intLit
+ hasSet = true
+ } else if str, ok := assign.Value.(*ast.StringLit); ok {
+ if _, err := strconv.Atoi(str.Value); err == nil {
+ hasSet = true
+ }
+ }
+ }
+ }
+ default:
+ if !hasSet && len(allAdvice) == 0 {
+ allAdvice = append(allAdvice, &storepb.Advice{
+ Status: level,
+ Code: code.StatementNoMaxExecutionTime.Int32(),
+ Title: checkCtx.Rule.Type.String(),
+ Content: fmt.Sprintf("The %s is not set", systemVariable),
+ })
}
}
}
- optionValueNoOptionType := startOptionValueList.OptionValueNoOptionType()
- if optionValueNoOptionType != nil {
- if optionValueNoOptionType.InternalVariableName() != nil && optionValueNoOptionType.SetExprOrDefault() != nil {
- variable, value = optionValueNoOptionType.InternalVariableName().GetText(), optionValueNoOptionType.SetExprOrDefault().GetText()
- }
- }
- _, err := strconv.Atoi(value)
- if strings.ToLower(variable) == r.systemVariable && err == nil {
- r.hasSet = true
- }
-}
-func (r *MaxExecutionTimeRule) setAdvice() {
- r.adviceList = append(r.adviceList, &storepb.Advice{
- Status: r.level,
- Code: code.StatementNoMaxExecutionTime.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The %s is not set", r.systemVariable),
- })
+ return allAdvice, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_no_leading_wildcard_like.go b/backend/plugin/advisor/mysql/rule_stmt_no_leading_wildcard_like.go
index e10bc4df00..39f5ce5764 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_no_leading_wildcard_like.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_no_leading_wildcard_like.go
@@ -5,14 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -36,84 +34,46 @@ func (*NoLeadingWildcardLikeAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, err
}
- // Create the rule
- rule := NewNoLeadingWildcardLikeRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &noLeadingWildcardLikeOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NoLeadingWildcardLikeRule checks for no leading wildcard LIKE.
-type NoLeadingWildcardLikeRule struct {
- BaseRule
- text string
+type noLeadingWildcardLikeOmniRule struct {
+ OmniBaseRule
}
-// NewNoLeadingWildcardLikeRule creates a new NoLeadingWildcardLikeRule.
-func NewNoLeadingWildcardLikeRule(level storepb.Advice_Status, title string) *NoLeadingWildcardLikeRule {
- return &NoLeadingWildcardLikeRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*NoLeadingWildcardLikeRule) Name() string {
+func (*noLeadingWildcardLikeOmniRule) Name() string {
return "NoLeadingWildcardLikeRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NoLeadingWildcardLikeRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *noLeadingWildcardLikeOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ ast.Inspect(node, func(n ast.Node) bool {
+ if like, ok := n.(*ast.LikeExpr); ok {
+ r.checkLikeExpr(like, text)
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypePredicateExprLike:
- r.checkPredicateExprLike(ctx.(*mysql.PredicateExprLikeContext))
- default:
- }
- return nil
+ return true
+ })
}
-// OnExit is called when exiting a parse tree node.
-func (*NoLeadingWildcardLikeRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *NoLeadingWildcardLikeRule) checkPredicateExprLike(ctx *mysql.PredicateExprLikeContext) {
- if ctx.LIKE_SYMBOL() == nil {
+func (r *noLeadingWildcardLikeOmniRule) checkLikeExpr(like *ast.LikeExpr, text string) {
+ if like.Pattern == nil {
return
}
-
- for _, expr := range ctx.AllSimpleExpr() {
- pattern := expr.GetText()
- if (strings.HasPrefix(pattern, "'%") && strings.HasSuffix(pattern, "'")) || (strings.HasPrefix(pattern, "\"%") && strings.HasSuffix(pattern, "\"")) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if str, ok := like.Pattern.(*ast.StringLit); ok {
+ if strings.HasPrefix(str.Value, "%") {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementLeadingWildcardLike.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(like.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_no_select_all.go b/backend/plugin/advisor/mysql/rule_stmt_no_select_all.go
index e87fb38dfa..66ff666678 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_no_select_all.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_no_select_all.go
@@ -3,15 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -35,79 +34,74 @@ func (*NoSelectAllAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
return nil, err
}
- // Create the rule
- rule := NewNoSelectAllRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &noSelectAllOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// NoSelectAllRule checks for no "select *".
-type NoSelectAllRule struct {
- BaseRule
- text string
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewNoSelectAllRule creates a new NoSelectAllRule.
-func NewNoSelectAllRule(level storepb.Advice_Status, title string) *NoSelectAllRule {
- return &NoSelectAllRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type noSelectAllOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*NoSelectAllRule) Name() string {
+func (*noSelectAllOmniRule) Name() string {
return "NoSelectAllRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *NoSelectAllRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
+func (r *noSelectAllOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ ast.Inspect(node, func(n ast.Node) bool {
+ sel, ok := n.(*ast.SelectStmt)
if !ok {
- return nil
+ return true
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeSelectItemList:
- r.checkSelectItemList(ctx.(*mysql.SelectItemListContext))
- default:
- // Other node types
- }
- return nil
+ // Only check TargetList on leaf SelectStmt (not UNION nodes).
+ if sel.SetOp != ast.SetOpNone {
+ return true
+ }
+ for _, target := range sel.TargetList {
+ if r.isStarTarget(target) {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementSelectAll.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" uses SELECT all", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.findStarLine(target))),
+ })
+ }
+ }
+ // Recurse into children (CTEs, subqueries in FROM, etc.) via Inspect.
+ return true
+ })
}
-// OnExit is called when exiting a parse tree node.
-func (*NoSelectAllRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*noSelectAllOmniRule) isStarTarget(expr ast.ExprNode) bool {
+ switch e := expr.(type) {
+ case *ast.StarExpr:
+ return true
+ case *ast.ResTarget:
+ if _, ok := e.Val.(*ast.StarExpr); ok {
+ return true
+ }
+ default:
+ }
+ return false
}
-func (r *NoSelectAllRule) checkSelectItemList(ctx *mysql.SelectItemListContext) {
- if ctx.MULT_OPERATOR() != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementSelectAll.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" uses SELECT all", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+func (r *noSelectAllOmniRule) findStarLine(expr ast.ExprNode) int32 {
+ switch e := expr.(type) {
+ case *ast.StarExpr:
+ return r.LocToLine(e.Loc)
+ case *ast.ResTarget:
+ if star, ok := e.Val.(*ast.StarExpr); ok {
+ return r.LocToLine(star.Loc)
+ }
+ return r.LocToLine(e.Loc)
+ default:
+ return r.ContentStartLine()
}
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_require_algorithm_or_lock_options.go b/backend/plugin/advisor/mysql/rule_stmt_require_algorithm_or_lock_options.go
index aff4bcb51c..40cd9f0789 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_require_algorithm_or_lock_options.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_require_algorithm_or_lock_options.go
@@ -2,16 +2,14 @@ package mysql
import (
"context"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -26,7 +24,7 @@ func init() {
advisor.Register(storepb.Engine_MARIADB, storepb.SQLReviewRule_STATEMENT_REQUIRE_LOCK_OPTION, &RequireAlgorithmOrLockOptionAdvisor{})
}
-// RequireAlgorithmOrLockOptionAdvisor is the advisor checking for the max execution time.
+// RequireAlgorithmOrLockOptionAdvisor is the advisor checking for required algorithm or lock options.
type RequireAlgorithmOrLockOptionAdvisor struct {
}
@@ -41,127 +39,74 @@ func (*RequireAlgorithmOrLockOptionAdvisor) Check(_ context.Context, checkCtx ad
requiredOption, errorCode = "LOCK", code.StatementNoLockOption
}
- // Create the rule
- rule := NewRequireAlgorithmOrLockOptionRule(level, checkCtx.Rule.Type.String(), requiredOption, errorCode)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// RequireAlgorithmOrLockOptionRule checks for required algorithm or lock options.
-type RequireAlgorithmOrLockOptionRule struct {
- BaseRule
- requiredOption string
- hasOption bool
- inAlterTableStatement bool
- errorCode code.Code
- text string
- line int
-}
-
-// NewRequireAlgorithmOrLockOptionRule creates a new RequireAlgorithmOrLockOptionRule.
-func NewRequireAlgorithmOrLockOptionRule(level storepb.Advice_Status, title string, requiredOption string, errorCode code.Code) *RequireAlgorithmOrLockOptionRule {
- return &RequireAlgorithmOrLockOptionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &requireAlgoLockOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
requiredOption: requiredOption,
errorCode: errorCode,
}
-}
-
-// Name returns the rule name.
-func (*RequireAlgorithmOrLockOptionRule) Name() string {
- return "RequireAlgorithmOrLockOptionRule"
-}
-// OnEnter is called when entering a parse tree node.
-func (r *RequireAlgorithmOrLockOptionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeAlterTableActions:
- r.checkAlterTableActions(ctx.(*mysql.AlterTableActionsContext))
- default:
- }
- return nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnExit is called when exiting a parse tree node.
-func (r *RequireAlgorithmOrLockOptionRule) OnExit(_ antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeAlterTable {
- if !r.hasOption {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: int32(r.errorCode),
- Title: r.title,
- Content: "ALTER TABLE statement should include " + r.requiredOption + " option",
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + r.line),
- })
- }
- r.inAlterTableStatement = false
- r.hasOption = false
- }
- return nil
+type requireAlgoLockOmniRule struct {
+ OmniBaseRule
+ requiredOption string
+ errorCode code.Code
}
-func (r *RequireAlgorithmOrLockOptionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- r.inAlterTableStatement = true
- r.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- r.line = ctx.GetStart().GetLine()
+func (*requireAlgoLockOmniRule) Name() string {
+ return "RequireAlgorithmOrLockOptionRule"
}
-func (r *RequireAlgorithmOrLockOptionRule) checkAlterTableActions(ctx *mysql.AlterTableActionsContext) {
- if !r.inAlterTableStatement {
+func (r *requireAlgoLockOmniRule) OnStatement(node ast.Node) {
+ alter, ok := node.(*ast.AlterTableStmt)
+ if !ok {
return
}
- modifierList := []mysql.IAlterCommandsModifierContext{}
- if ctx.AlterCommandsModifierList() != nil {
- modifierList = append(modifierList, ctx.AlterCommandsModifierList().AllAlterCommandsModifier()...)
- }
- if ctx.AlterCommandList() != nil {
- if ctx.AlterCommandList().AlterCommandsModifierList() != nil {
- modifierList = append(modifierList, ctx.AlterCommandList().AlterCommandsModifierList().AllAlterCommandsModifier()...)
+ hasOption := false
+ for _, cmd := range alter.Commands {
+ if r.requiredOption == "ALGORITHM" && cmd.Type == ast.ATAlgorithm {
+ hasOption = true
+ break
}
- if ctx.AlterCommandList().AlterList() != nil {
- modifierList = append(modifierList, ctx.AlterCommandList().AlterList().AllAlterCommandsModifier()...)
+ if r.requiredOption == "LOCK" && cmd.Type == ast.ATLock {
+ hasOption = true
+ break
}
}
- for _, modifier := range modifierList {
- if r.requiredOption == "ALGORITHM" && modifier.AlterAlgorithmOption() != nil {
- if modifier.AlterAlgorithmOption().Identifier() != nil {
- algorithmOptionValue := mysqlparser.NormalizeMySQLIdentifier(modifier.AlterAlgorithmOption().Identifier())
- // Don't need to check the value of the algorithm option right now.
- if algorithmOptionValue != "" {
- r.hasOption = true
- }
- }
+
+ if !hasOption {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: int32(r.errorCode),
+ Title: r.Title,
+ Content: "ALTER TABLE statement should include " + r.requiredOption + " option",
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
+ })
+ }
+}
+
+// nolint:unused
+func omniAlterCmdHasOption(cmds []*ast.AlterTableCmd, optName string) bool {
+ for _, cmd := range cmds {
+ if optName == "ALGORITHM" && cmd.Type == ast.ATAlgorithm {
+ return true
}
- if r.requiredOption == "LOCK" && modifier.AlterLockOption() != nil {
- if modifier.AlterLockOption().Identifier() != nil {
- lockOptionValue := mysqlparser.NormalizeMySQLIdentifier(modifier.AlterLockOption().Identifier())
- // Don't need to check the value of the lock option right now.
- if lockOptionValue != "" {
- r.hasOption = true
- }
- }
+ if optName == "LOCK" && cmd.Type == ast.ATLock {
+ return true
}
}
+ return false
+}
+
+// nolint:unused
+func omniAlterCmdOptionValue(cmd *ast.AlterTableCmd) string {
+ if cmd == nil || cmd.Name == "" {
+ return ""
+ }
+ return strings.ToUpper(cmd.Name)
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_select.go b/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_select.go
index 0d95ff1fd4..8d178cfcdd 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_select.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_select.go
@@ -3,15 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
)
var (
@@ -35,82 +34,85 @@ func (*WhereRequirementForSelectAdvisor) Check(_ context.Context, checkCtx advis
return nil, err
}
- // Create the rule
- rule := NewWhereRequirementForSelectRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &whereRequirementForSelectOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// WhereRequirementForSelectRule checks for WHERE clause requirement in SELECT.
-type WhereRequirementForSelectRule struct {
- BaseRule
- text string
+type whereRequirementForSelectOmniRule struct {
+ OmniBaseRule
}
-// NewWhereRequirementForSelectRule creates a new WhereRequirementForSelectRule.
-func NewWhereRequirementForSelectRule(level storepb.Advice_Status, title string) *WhereRequirementForSelectRule {
- return &WhereRequirementForSelectRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+func (*whereRequirementForSelectOmniRule) Name() string {
+ return "WhereRequirementForSelectRule"
}
-// Name returns the rule name.
-func (*WhereRequirementForSelectRule) Name() string {
- return "WhereRequirementForSelectRule"
+func (r *whereRequirementForSelectOmniRule) OnStatement(node ast.Node) {
+ sel, ok := node.(*ast.SelectStmt)
+ if !ok {
+ return
+ }
+ text := strings.TrimSpace(r.StmtText)
+ r.checkSelectStmt(sel, text)
}
-// OnEnter is called when entering a parse tree node.
-func (r *WhereRequirementForSelectRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *whereRequirementForSelectOmniRule) checkSelectStmt(sel *ast.SelectStmt, text string) {
+ if sel == nil {
+ return
+ }
+
+ // Check CTEs.
+ for _, cte := range sel.CTEs {
+ if cte.Select != nil {
+ r.checkSelectStmt(cte.Select, text)
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeQuerySpecification:
- r.checkQuerySpecification(ctx.(*mysql.QuerySpecificationContext))
- default:
}
- return nil
-}
-// OnExit is called when exiting a parse tree node.
-func (*WhereRequirementForSelectRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
+ // Check set operations (UNION left/right).
+ if sel.SetOp != ast.SetOpNone {
+ r.checkSelectStmt(sel.Left, text)
+ r.checkSelectStmt(sel.Right, text)
+ return
+ }
-func (r *WhereRequirementForSelectRule) checkQuerySpecification(ctx *mysql.QuerySpecificationContext) {
- // Allow SELECT queries without a FROM clause to proceed, e.g. SELECT 1.
- if ctx.FromClause() == nil {
+ // Allow SELECT queries without a FROM clause, e.g. SELECT 1.
+ if len(sel.From) == 0 {
return
}
- if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+
+ if sel.Where == nil {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.StatementNoWhere.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" requires WHERE clause", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" requires WHERE clause", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(sel.Loc))),
+ })
+ }
+
+ // Check subqueries in WHERE, FROM, and target list.
+ // We inspect each subtree individually (not the whole SelectStmt) to avoid
+ // recursing into the SelectStmt itself, which checkSelectStmt already handles.
+ var subtrees []ast.Node
+ subtrees = append(subtrees, sel.Where)
+ for _, from := range sel.From {
+ subtrees = append(subtrees, from)
+ }
+ for _, target := range sel.TargetList {
+ subtrees = append(subtrees, target)
+ }
+ for _, subtree := range subtrees {
+ ast.Inspect(subtree, func(n ast.Node) bool {
+ if sub, ok := n.(*ast.SelectStmt); ok {
+ r.checkSelectStmt(sub, text)
+ return false
+ }
+ return true
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_update_delete.go b/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_update_delete.go
index b77913a77e..a789b59a3b 100644
--- a/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_update_delete.go
+++ b/backend/plugin/advisor/mysql/rule_stmt_where_requirement_for_update_delete.go
@@ -3,16 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -25,7 +23,7 @@ func init() {
advisor.Register(storepb.Engine_OCEANBASE, storepb.SQLReviewRule_STATEMENT_WHERE_REQUIRE_UPDATE_DELETE, &WhereRequirementForUpdateDeleteAdvisor{})
}
-// WhereRequirementForUpdateDeleteAdvisor is the advisor checking for the WHERE clause requirement for SELECT statements.
+// WhereRequirementForUpdateDeleteAdvisor is the advisor checking for the WHERE clause requirement for UPDATE/DELETE statements.
type WhereRequirementForUpdateDeleteAdvisor struct {
}
@@ -36,96 +34,47 @@ func (*WhereRequirementForUpdateDeleteAdvisor) Check(_ context.Context, checkCtx
return nil, err
}
- // Create the rule
- rule := NewWhereRequirementForUpdateDeleteRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &whereRequirementForUpdateDeleteOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// WhereRequirementForUpdateDeleteRule checks for the WHERE clause requirement.
-type WhereRequirementForUpdateDeleteRule struct {
- BaseRule
- text string
+type whereRequirementForUpdateDeleteOmniRule struct {
+ OmniBaseRule
}
-// NewWhereRequirementForUpdateDeleteRule creates a new WhereRequirementForUpdateDeleteRule.
-func NewWhereRequirementForUpdateDeleteRule(level storepb.Advice_Status, title string) *WhereRequirementForUpdateDeleteRule {
- return &WhereRequirementForUpdateDeleteRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*WhereRequirementForUpdateDeleteRule) Name() string {
+func (*whereRequirementForUpdateDeleteOmniRule) Name() string {
return "WhereRequirementForUpdateDeleteRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *WhereRequirementForUpdateDeleteRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
+func (r *whereRequirementForUpdateDeleteOmniRule) OnStatement(node ast.Node) {
+ text := strings.TrimSpace(r.StmtText)
+ switch n := node.(type) {
+ case *ast.DeleteStmt:
+ if n.Where == nil {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementNoWhere.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" requires WHERE clause", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
+ })
+ }
+ case *ast.UpdateStmt:
+ if n.Where == nil {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
+ Code: code.StatementNoWhere.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("\"%s\" requires WHERE clause", text),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.ContentStartLine())),
+ })
}
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeDeleteStatement:
- r.checkDeleteStatement(ctx.(*mysql.DeleteStatementContext))
- case NodeTypeUpdateStatement:
- r.checkUpdateStatement(ctx.(*mysql.UpdateStatementContext))
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*WhereRequirementForUpdateDeleteRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *WhereRequirementForUpdateDeleteRule) checkDeleteStatement(ctx *mysql.DeleteStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
- r.handleWhereClause(ctx.GetStart().GetLine())
- }
-}
-
-func (r *WhereRequirementForUpdateDeleteRule) checkUpdateStatement(ctx *mysql.UpdateStatementContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.WhereClause() == nil || ctx.WhereClause().WHERE_SYMBOL() == nil {
- r.handleWhereClause(ctx.GetStart().GetLine())
- }
-}
-
-func (r *WhereRequirementForUpdateDeleteRule) handleWhereClause(lineNumber int) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.StatementNoWhere.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("\"%s\" requires WHERE clause", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + lineNumber),
- })
}
diff --git a/backend/plugin/advisor/mysql/rule_table_comment_convention.go b/backend/plugin/advisor/mysql/rule_table_comment_convention.go
index 9591590ce9..6e3f7c8f55 100644
--- a/backend/plugin/advisor/mysql/rule_table_comment_convention.go
+++ b/backend/plugin/advisor/mysql/rule_table_comment_convention.go
@@ -4,15 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -37,106 +34,66 @@ func (*TableCommentConventionAdvisor) Check(_ context.Context, checkCtx advisor.
}
commentPayload := checkCtx.Rule.GetCommentConventionPayload()
- // Create the rule
- rule := NewTableCommentConventionRule(level, checkCtx.Rule.Type.String(), commentPayload)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableCommentConventionOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ payload: commentPayload,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableCommentConventionRule checks for table comment convention.
-type TableCommentConventionRule struct {
- BaseRule
+type tableCommentConventionOmniRule struct {
+ OmniBaseRule
payload *storepb.SQLReviewRule_CommentConventionRulePayload
}
-// NewTableCommentConventionRule creates a new TableCommentConventionRule.
-func NewTableCommentConventionRule(level storepb.Advice_Status, title string, payload *storepb.SQLReviewRule_CommentConventionRulePayload) *TableCommentConventionRule {
- return &TableCommentConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- payload: payload,
- }
-}
-
-// Name returns the rule name.
-func (*TableCommentConventionRule) Name() string {
+func (*tableCommentConventionOmniRule) Name() string {
return "TableCommentConventionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableCommentConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeCreateTable {
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
+func (r *tableCommentConventionOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateTableStmt)
+ if !ok {
+ return
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableCommentConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+ r.checkCreateTable(n)
}
-func (r *TableCommentConventionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *tableCommentConventionOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableName() == nil {
- return
+ tableName := n.Table.Name
+
+ comment := omniTableOptionValue(n.Options, "COMMENT")
+ exists := comment != ""
+ // Check if any COMMENT option exists (even empty string).
+ for _, opt := range n.Options {
+ if opt != nil && opt.Name == "COMMENT" {
+ exists = true
+ break
+ }
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
-
- comment, exists := r.handleCreateTableOptions(ctx.CreateTableOptions())
-
if r.payload.Required && !exists {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.CommentEmpty.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Table `%s` requires comments", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
if r.payload.MaxLength >= 0 && int32(len(comment)) > r.payload.MaxLength {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.CommentTooLong.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("The length of table `%s` comment should be within %d characters", tableName, r.payload.MaxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
-
-func (*TableCommentConventionRule) handleCreateTableOptions(ctx mysql.ICreateTableOptionsContext) (string, bool) {
- if ctx == nil {
- return "", false
- }
- for _, option := range ctx.AllCreateTableOption() {
- if option.COMMENT_SYMBOL() == nil || option.TextStringLiteral() == nil {
- continue
- }
-
- comment := mysqlparser.NormalizeMySQLTextStringLiteral(option.TextStringLiteral())
- return comment, true
- }
- return "", false
-}
diff --git a/backend/plugin/advisor/mysql/rule_table_disallow_ddl.go b/backend/plugin/advisor/mysql/rule_table_disallow_ddl.go
index dda4b29cf6..645cfe475c 100644
--- a/backend/plugin/advisor/mysql/rule_table_disallow_ddl.go
+++ b/backend/plugin/advisor/mysql/rule_table_disallow_ddl.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -35,126 +31,64 @@ func (*TableDisallowDDLAdvisor) Check(_ context.Context, checkCtx advisor.Contex
}
stringArrayPayload := checkCtx.Rule.GetStringArrayPayload()
- // Create the rule
- rule := NewTableDisallowDDLRule(level, checkCtx.Rule.Type.String(), stringArrayPayload.List)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableDisallowDDLOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ disallowList: stringArrayPayload.List,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableDisallowDDLRule checks for disallow DDL on specific tables.
-type TableDisallowDDLRule struct {
- BaseRule
+type tableDisallowDDLOmniRule struct {
+ OmniBaseRule
disallowList []string
}
-// NewTableDisallowDDLRule creates a new TableDisallowDDLRule.
-func NewTableDisallowDDLRule(level storepb.Advice_Status, title string, disallowList []string) *TableDisallowDDLRule {
- return &TableDisallowDDLRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- disallowList: disallowList,
- }
-}
-
-// Name returns the rule name.
-func (*TableDisallowDDLRule) Name() string {
+func (*tableDisallowDDLOmniRule) Name() string {
return "TableDisallowDDLRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDisallowDDLRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeDropTable:
- r.checkDropTable(ctx.(*mysql.DropTableContext))
- case NodeTypeRenameTableStatement:
- r.checkRenameTableStatement(ctx.(*mysql.RenameTableStatementContext))
- case NodeTypeTruncateTableStatement:
- r.checkTruncateTableStatement(ctx.(*mysql.TruncateTableStatementContext))
- default:
- }
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableDisallowDDLRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableDisallowDDLRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
- }
- r.checkTableName(tableName, ctx.GetStart().GetLine())
-}
-
-func (r *TableDisallowDDLRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- if tableName == "" {
- return
- }
- r.checkTableName(tableName, ctx.GetStart().GetLine())
-}
-
-func (r *TableDisallowDDLRule) checkDropTable(ctx *mysql.DropTableContext) {
- for _, tableRef := range ctx.TableRefList().AllTableRef() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
- if tableName == "" {
- continue
+func (r *tableDisallowDDLOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table != nil {
+ r.checkTableName(n.Table.Name, r.LocToLine(n.Loc))
}
- r.checkTableName(tableName, ctx.GetStart().GetLine())
- }
-}
-
-func (r *TableDisallowDDLRule) checkRenameTableStatement(ctx *mysql.RenameTableStatementContext) {
- for _, renamePair := range ctx.AllRenamePair() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(renamePair.TableRef())
- if tableName == "" {
- continue
+ case *ast.AlterTableStmt:
+ if n.Table != nil {
+ r.checkTableName(n.Table.Name, r.LocToLine(n.Loc))
}
- r.checkTableName(tableName, ctx.GetStart().GetLine())
- }
-}
-
-func (r *TableDisallowDDLRule) checkTruncateTableStatement(ctx *mysql.TruncateTableStatementContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
+ case *ast.DropTableStmt:
+ for _, tbl := range n.Tables {
+ r.checkTableName(tbl.Name, r.LocToLine(n.Loc))
+ }
+ case *ast.RenameTableStmt:
+ for _, pair := range n.Pairs {
+ if pair.Old != nil {
+ r.checkTableName(pair.Old.Name, r.LocToLine(n.Loc))
+ }
+ }
+ case *ast.TruncateStmt:
+ for _, tbl := range n.Tables {
+ r.checkTableName(tbl.Name, r.LocToLine(n.Loc))
+ }
+ default:
}
- r.checkTableName(tableName, ctx.GetStart().GetLine())
}
-func (r *TableDisallowDDLRule) checkTableName(tableName string, line int) {
+func (r *tableDisallowDDLOmniRule) checkTableName(tableName string, lineNumber int32) {
for _, disallow := range r.disallowList {
if tableName == disallow {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ absoluteLine := r.BaseLine + int(lineNumber)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.TableDisallowDDL.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("DDL is disallowed on table %s.", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(line),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
return
}
diff --git a/backend/plugin/advisor/mysql/rule_table_disallow_dml.go b/backend/plugin/advisor/mysql/rule_table_disallow_dml.go
index cd741e3ca4..70145d043a 100644
--- a/backend/plugin/advisor/mysql/rule_table_disallow_dml.go
+++ b/backend/plugin/advisor/mysql/rule_table_disallow_dml.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -35,220 +31,76 @@ func (*TableDisallowDMLAdvisor) Check(_ context.Context, checkCtx advisor.Contex
}
stringArrayPayload := checkCtx.Rule.GetStringArrayPayload()
- // Create the rule
- rule := NewTableDisallowDMLRule(level, checkCtx.Rule.Type.String(), stringArrayPayload.List)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableDisallowDMLOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ disallowList: stringArrayPayload.List,
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableDisallowDMLRule checks for disallow DML on specific tables.
-type TableDisallowDMLRule struct {
- BaseRule
+type tableDisallowDMLOmniRule struct {
+ OmniBaseRule
disallowList []string
}
-// NewTableDisallowDMLRule creates a new TableDisallowDMLRule.
-func NewTableDisallowDMLRule(level storepb.Advice_Status, title string, disallowList []string) *TableDisallowDMLRule {
- return &TableDisallowDMLRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- disallowList: disallowList,
- }
-}
-
-// Name returns the rule name.
-func (*TableDisallowDMLRule) Name() string {
+func (*tableDisallowDMLOmniRule) Name() string {
return "TableDisallowDMLRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDisallowDMLRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeDeleteStatement:
- r.checkDeleteStatement(ctx.(*mysql.DeleteStatementContext))
- case NodeTypeInsertStatement:
- r.checkInsertStatement(ctx.(*mysql.InsertStatementContext))
- case NodeTypeSelectStatementWithInto:
- r.checkSelectStatementWithInto(ctx.(*mysql.SelectStatementWithIntoContext))
- case NodeTypeUpdateStatement:
- r.checkUpdateStatement(ctx.(*mysql.UpdateStatementContext))
+func (r *tableDisallowDMLOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.DeleteStmt:
+ for _, name := range omniTableExprNames(n.Tables) {
+ r.checkTableName(name, r.LocToLine(n.Loc))
+ }
+ case *ast.InsertStmt:
+ if n.Table != nil {
+ r.checkTableName(n.Table.Name, r.LocToLine(n.Loc))
+ }
+ case *ast.SelectStmt:
+ if n.Into != nil && n.Into.Outfile != "" {
+ r.checkTableName(n.Into.Outfile, r.LocToLine(n.Loc))
+ }
+ case *ast.UpdateStmt:
+ for _, name := range omniTableExprNames(n.Tables) {
+ r.checkTableName(name, r.LocToLine(n.Loc))
+ }
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableDisallowDMLRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableDisallowDMLRule) checkDeleteStatement(ctx *mysql.DeleteStatementContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
- }
- r.checkTableName(tableName, ctx.GetStart().GetLine())
}
-func (r *TableDisallowDMLRule) checkInsertStatement(ctx *mysql.InsertStatementContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- if tableName == "" {
- return
- }
- r.checkTableName(tableName, ctx.GetStart().GetLine())
-}
-
-func (r *TableDisallowDMLRule) checkSelectStatementWithInto(ctx *mysql.SelectStatementWithIntoContext) {
- // Only check text string literal for now.
- if ctx.IntoClause() == nil || ctx.IntoClause().TextStringLiteral() == nil {
- return
- }
- tableName := ctx.IntoClause().TextStringLiteral().GetText()
- r.checkTableName(tableName, ctx.GetStart().GetLine())
-}
-
-func (r *TableDisallowDMLRule) checkUpdateStatement(ctx *mysql.UpdateStatementContext) {
- if ctx.TableReferenceList() == nil {
- return
- }
- tables, err := r.extractTableReferenceList(ctx.TableReferenceList())
- if err != nil {
- return
- }
- for _, table := range tables {
- r.checkTableName(table.table, ctx.GetStart().GetLine())
- }
-}
-
-func (r *TableDisallowDMLRule) checkTableName(tableName string, line int) {
+func (r *tableDisallowDMLOmniRule) checkTableName(tableName string, lineNumber int32) {
for _, disallow := range r.disallowList {
if tableName == disallow {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ absoluteLine := r.BaseLine + int(lineNumber)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.TableDisallowDML.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("DML is disallowed on table %s.", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(line),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
return
}
}
}
-type table struct {
- database string
- table string
-}
-
-func (r *TableDisallowDMLRule) extractTableReference(ctx mysql.ITableReferenceContext) ([]table, error) {
- if ctx.TableFactor() == nil {
- return nil, nil
- }
- res, err := r.extractTableFactor(ctx.TableFactor())
- if err != nil {
- return nil, err
- }
- for _, joinedTableCtx := range ctx.AllJoinedTable() {
- tables, err := r.extractJoinedTable(joinedTableCtx)
- if err != nil {
- return nil, err
- }
- res = append(res, tables...)
- }
-
- return res, nil
-}
-
-func (*TableDisallowDMLRule) extractTableRef(ctx mysql.ITableRefContext) ([]table, error) {
- if ctx == nil {
- return nil, nil
- }
- databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx)
- return []table{
- {
- database: databaseName,
- table: tableName,
- },
- }, nil
-}
-
-func (r *TableDisallowDMLRule) extractTableReferenceList(ctx mysql.ITableReferenceListContext) ([]table, error) {
- var res []table
- for _, tableRefCtx := range ctx.AllTableReference() {
- tables, err := r.extractTableReference(tableRefCtx)
- if err != nil {
- return nil, err
- }
- res = append(res, tables...)
- }
- return res, nil
-}
-
-func (r *TableDisallowDMLRule) extractTableReferenceListParens(ctx mysql.ITableReferenceListParensContext) ([]table, error) {
- if ctx.TableReferenceList() != nil {
- return r.extractTableReferenceList(ctx.TableReferenceList())
- }
- if ctx.TableReferenceListParens() != nil {
- return r.extractTableReferenceListParens(ctx.TableReferenceListParens())
- }
- return nil, nil
-}
-
-func (r *TableDisallowDMLRule) extractTableFactor(ctx mysql.ITableFactorContext) ([]table, error) {
- switch {
- case ctx.SingleTable() != nil:
- return r.extractSingleTable(ctx.SingleTable())
- case ctx.SingleTableParens() != nil:
- return r.extractSingleTableParens(ctx.SingleTableParens())
- case ctx.DerivedTable() != nil:
- return nil, nil
- case ctx.TableReferenceListParens() != nil:
- return r.extractTableReferenceListParens(ctx.TableReferenceListParens())
- case ctx.TableFunction() != nil:
- return nil, nil
- default:
- return nil, nil
- }
-}
-
-func (r *TableDisallowDMLRule) extractSingleTable(ctx mysql.ISingleTableContext) ([]table, error) {
- return r.extractTableRef(ctx.TableRef())
-}
-
-func (r *TableDisallowDMLRule) extractSingleTableParens(ctx mysql.ISingleTableParensContext) ([]table, error) {
- if ctx.SingleTable() != nil {
- return r.extractSingleTable(ctx.SingleTable())
- }
- if ctx.SingleTableParens() != nil {
- return r.extractSingleTableParens(ctx.SingleTableParens())
- }
- return nil, nil
-}
-
-func (r *TableDisallowDMLRule) extractJoinedTable(ctx mysql.IJoinedTableContext) ([]table, error) {
- if ctx.TableFactor() != nil {
- return r.extractTableFactor(ctx.TableFactor())
- }
- if ctx.TableReference() != nil {
- return r.extractTableReference(ctx.TableReference())
+// omniTableExprNames extracts table names from a slice of TableExpr.
+func omniTableExprNames(exprs []ast.TableExpr) []string {
+ var names []string
+ for _, expr := range exprs {
+ ast.Inspect(expr, func(n ast.Node) bool {
+ t, ok := n.(*ast.TableRef)
+ if !ok {
+ return true
+ }
+ names = append(names, t.Name)
+ return false
+ })
}
- return nil, nil
+ return names
}
diff --git a/backend/plugin/advisor/mysql/rule_table_disallow_partition.go b/backend/plugin/advisor/mysql/rule_table_disallow_partition.go
index 1af6af1ea0..2e6318aaf5 100644
--- a/backend/plugin/advisor/mysql/rule_table_disallow_partition.go
+++ b/backend/plugin/advisor/mysql/rule_table_disallow_partition.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -37,107 +33,60 @@ func (*TableDisallowPartitionAdvisor) Check(_ context.Context, checkCtx advisor.
return nil, err
}
- // Create the rule
- rule := NewTableDisallowPartitionRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableDisallowPartitionOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableDisallowPartitionRule checks for disallow table partition.
-type TableDisallowPartitionRule struct {
- BaseRule
- text string
-}
-
-// NewTableDisallowPartitionRule creates a new TableDisallowPartitionRule.
-func NewTableDisallowPartitionRule(level storepb.Advice_Status, title string) *TableDisallowPartitionRule {
- return &TableDisallowPartitionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type tableDisallowPartitionOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*TableDisallowPartitionRule) Name() string {
+func (*tableDisallowPartitionOmniRule) Name() string {
return "TableDisallowPartitionRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDisallowPartitionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *tableDisallowPartitionOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableDisallowPartitionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *TableDisallowPartitionRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- code := advisorcode.Ok
- if ctx.PartitionClause() != nil && ctx.PartitionClause().PartitionTypeDef() != nil {
- code = advisorcode.CreateTablePartition
- }
-
- if code != advisorcode.Ok {
+func (r *tableDisallowPartitionOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Partitions != nil {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.CreateTablePartition.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
-func (r *TableDisallowPartitionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- code := advisorcode.Ok
- if ctx.AlterTableActions() != nil && ctx.AlterTableActions().PartitionClause() != nil && ctx.AlterTableActions().PartitionClause().PartitionTypeDef() != nil {
- code = advisorcode.CreateTablePartition
- }
- if code != advisorcode.Ok {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+func (r *tableDisallowPartitionOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ if cmd.Type == ast.ATPartitionBy && cmd.PartitionBy != nil {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: advisorcode.CreateTablePartition.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
+ })
+ return
+ }
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_disallow_set_charset.go b/backend/plugin/advisor/mysql/rule_table_disallow_set_charset.go
index 40548fc603..8601310d81 100644
--- a/backend/plugin/advisor/mysql/rule_table_disallow_set_charset.go
+++ b/backend/plugin/advisor/mysql/rule_table_disallow_set_charset.go
@@ -3,17 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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"
)
var (
@@ -33,115 +30,60 @@ func (*TableDisallowSetCharsetAdvisor) Check(_ context.Context, checkCtx advisor
return nil, err
}
- // Create the rule
- rule := NewTableDisallowSetCharsetRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableDisallowSetCharsetOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// TableDisallowSetCharsetRule checks for disallowing set charset on tables.
-type TableDisallowSetCharsetRule struct {
- BaseRule
- text string
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewTableDisallowSetCharsetRule creates a new TableDisallowSetCharsetRule.
-func NewTableDisallowSetCharsetRule(level storepb.Advice_Status, title string) *TableDisallowSetCharsetRule {
- return &TableDisallowSetCharsetRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type tableDisallowSetCharsetOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*TableDisallowSetCharsetRule) Name() string {
+func (*tableDisallowSetCharsetOmniRule) Name() string {
return "TableDisallowSetCharsetRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDisallowSetCharsetRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *tableDisallowSetCharsetOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableDisallowSetCharsetRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
}
-func (r *TableDisallowSetCharsetRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateTableOptions() != nil {
- for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {
- if option.DefaultCharset() != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.DisallowSetCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
- }
+func (r *tableDisallowSetCharsetOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ for _, opt := range n.Options {
+ if opt != nil && strings.EqualFold(opt.Name, "CHARSET") || strings.EqualFold(opt.Name, "CHARACTER SET") || strings.EqualFold(opt.Name, "DEFAULT CHARSET") || strings.EqualFold(opt.Name, "DEFAULT CHARACTER SET") {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.DisallowSetCharset.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
+ })
}
}
}
-func (r *TableDisallowSetCharsetRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
-
- alterList := ctx.AlterTableActions().AlterCommandList().AlterList()
- if alterList == nil {
- return
- }
- for _, alterListItem := range alterList.AllAlterListItem() {
- if alterListItem == nil {
+func (r *tableDisallowSetCharsetOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
-
- if alterListItem.Charset() != nil {
+ if cmd.Type == ast.ATConvertCharset {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.DisallowSetCharset.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("Set charset on tables is disallowed, but \"%s\" uses", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_disallow_trigger.go b/backend/plugin/advisor/mysql/rule_table_disallow_trigger.go
index 1388e175d2..b0618a251b 100644
--- a/backend/plugin/advisor/mysql/rule_table_disallow_trigger.go
+++ b/backend/plugin/advisor/mysql/rule_table_disallow_trigger.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -35,87 +31,36 @@ func (*TableDisallowTriggerAdvisor) Check(_ context.Context, checkCtx advisor.Co
return nil, err
}
- // Create the rule
- rule := NewTableDisallowTriggerRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// TableDisallowTriggerRule checks for disallow table trigger.
-type TableDisallowTriggerRule struct {
- BaseRule
- text string
-}
-
-// NewTableDisallowTriggerRule creates a new TableDisallowTriggerRule.
-func NewTableDisallowTriggerRule(level storepb.Advice_Status, title string) *TableDisallowTriggerRule {
- return &TableDisallowTriggerRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &tableDisallowTriggerOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*TableDisallowTriggerRule) Name() string {
- return "TableDisallowTriggerRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDisallowTriggerRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- queryCtx, ok := ctx.(*mysql.QueryContext)
- if !ok {
- return nil
- }
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- case NodeTypeCreateTrigger:
- r.checkCreateTrigger(ctx.(*mysql.CreateTriggerContext))
- default:
- // Ignore other node types
- }
- return nil
+type tableDisallowTriggerOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*TableDisallowTriggerRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*tableDisallowTriggerOmniRule) Name() string {
+ return "TableDisallowTriggerRule"
}
-func (r *TableDisallowTriggerRule) checkCreateTrigger(ctx *mysql.CreateTriggerContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *tableDisallowTriggerOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateTriggerStmt)
+ if !ok {
return
}
- code := advisorcode.Ok
- if ctx.TriggerName() != nil {
- code = advisorcode.CreateTableTrigger
- }
-
- if code != advisorcode.Ok {
+ if n.Name != "" {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Trigger is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.CreateTableTrigger.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Trigger is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_drop_naming_convention.go b/backend/plugin/advisor/mysql/rule_table_drop_naming_convention.go
index 23af73ba36..92aaa4528f 100644
--- a/backend/plugin/advisor/mysql/rule_table_drop_naming_convention.go
+++ b/backend/plugin/advisor/mysql/rule_table_drop_naming_convention.go
@@ -5,17 +5,13 @@ import (
"fmt"
"regexp"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -48,80 +44,40 @@ func (*TableDropNamingConventionAdvisor) Check(_ context.Context, checkCtx advis
return nil, errors.Wrapf(err, "failed to compile regex format %q", namingPayload.Format)
}
- // Create the rule
- rule := NewTableDropNamingConventionRule(level, checkCtx.Rule.Type.String(), format)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// TableDropNamingConventionRule checks for drop table naming convention.
-type TableDropNamingConventionRule struct {
- BaseRule
- format *regexp.Regexp
-}
-
-// NewTableDropNamingConventionRule creates a new TableDropNamingConventionRule.
-func NewTableDropNamingConventionRule(level storepb.Advice_Status, title string, format *regexp.Regexp) *TableDropNamingConventionRule {
- return &TableDropNamingConventionRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &tableDropNamingConventionOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
format: format,
}
-}
-// Name returns the rule name.
-func (*TableDropNamingConventionRule) Name() string {
- return "TableDropNamingConventionRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableDropNamingConventionRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeDropTable {
- r.checkDropTable(ctx.(*mysql.DropTableContext))
- }
- return nil
+type tableDropNamingConventionOmniRule struct {
+ OmniBaseRule
+ format *regexp.Regexp
}
-// OnExit is called when exiting a parse tree node.
-func (*TableDropNamingConventionRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*tableDropNamingConventionOmniRule) Name() string {
+ return "TableDropNamingConventionRule"
}
-func (r *TableDropNamingConventionRule) checkDropTable(ctx *mysql.DropTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *tableDropNamingConventionOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.DropTableStmt)
+ if !ok {
return
}
- if ctx.TableRefList() == nil {
- return
- }
-
- for _, tableRef := range ctx.TableRefList().AllTableRef() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
- if !r.format.MatchString(tableName) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ for _, tbl := range n.Tables {
+ if !r.format.MatchString(tbl.Name) {
+ absoluteLine := r.BaseLine + int(r.LocToLine(n.Loc))
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.TableDropNamingConventionMismatch.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", tableName, r.format),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Title: r.Title,
+ Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", tbl.Name, r.format),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_limit_size.go b/backend/plugin/advisor/mysql/rule_table_limit_size.go
index 8367ba823a..ffc7873296 100644
--- a/backend/plugin/advisor/mysql/rule_table_limit_size.go
+++ b/backend/plugin/advisor/mysql/rule_table_limit_size.go
@@ -4,19 +4,13 @@ import (
"context"
"fmt"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
-
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
func init() {
@@ -37,124 +31,85 @@ func (*MaximumTableSizeAdvisor) Check(_ context.Context, checkCtx advisor.Contex
return nil, errors.New("number_payload is required for this rule")
}
- // User defined rule level.
status, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
if err != nil {
return nil, err
}
- var adviceList []*storepb.Advice
-
- for _, stmt := range checkCtx.ParsedStatements {
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
-
- statTypeChecker := &mysqlparser.StatementTypeChecker{}
- antlr.ParseTreeWalkerDefault.Walk(statTypeChecker, antlrAST.Tree)
-
- if statTypeChecker.IsDDL {
- // Create the rule
- rule := NewTableLimitSizeRule(status, checkCtx.Rule.Type.String(), int(numberPayload.Number), checkCtx.DBSchema)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
-
- // Generate advice based on collected table information
- rule.generateAdvice()
-
- adviceList = append(adviceList, checker.GetAdviceList()...)
- }
+ rule := &tableLimitSizeOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: status,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ maxRows: int(numberPayload.Number),
+ dbMetadata: checkCtx.DBSchema,
}
- return adviceList, nil
-}
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
-// TableLimitSizeRule checks for table size limits.
-type TableLimitSizeRule struct {
- BaseRule
- affectedTabNames []string
- maxRows int
- dbMetadata *storepb.DatabaseSchemaMetadata
- statementBaseLine int
+ // Generate advice based on collected table information.
+ rule.generateAdvice()
+
+ return rule.GetAdviceList(), nil
}
-// NewTableLimitSizeRule creates a new TableLimitSizeRule.
-func NewTableLimitSizeRule(level storepb.Advice_Status, title string, maxRows int, dbMetadata *storepb.DatabaseSchemaMetadata) *TableLimitSizeRule {
- return &TableLimitSizeRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- maxRows: maxRows,
- dbMetadata: dbMetadata,
- }
+type tableLimitSizeOmniRule struct {
+ OmniBaseRule
+ affectedTabNames []string
+ maxRows int
+ dbMetadata *storepb.DatabaseSchemaMetadata
}
-// Name returns the rule name.
-func (*TableLimitSizeRule) Name() string {
+func (*tableLimitSizeOmniRule) Name() string {
return "TableLimitSizeRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableLimitSizeRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeTruncateTableStatement:
- r.checkTruncateTableStatement(ctx.(*mysql.TruncateTableStatementContext))
- case NodeTypeDropTable:
- r.checkDropTable(ctx.(*mysql.DropTableContext))
+func (r *tableLimitSizeOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.TruncateStmt:
+ r.checkTruncate(n)
+ case *ast.DropTableStmt:
+ r.checkDropTable(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*TableLimitSizeRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableLimitSizeRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- r.statementBaseLine = ctx.GetStart().GetLine()
- r.affectedTabNames = append(r.affectedTabNames, tableName)
+func (r *tableLimitSizeOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
+ return
+ }
+ r.affectedTabNames = append(r.affectedTabNames, n.Table.Name)
}
-func (r *TableLimitSizeRule) checkTruncateTableStatement(ctx *mysql.TruncateTableStatementContext) {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- r.statementBaseLine = ctx.GetStart().GetLine()
- r.affectedTabNames = append(r.affectedTabNames, tableName)
+func (r *tableLimitSizeOmniRule) checkTruncate(n *ast.TruncateStmt) {
+ for _, ref := range n.Tables {
+ if ref != nil {
+ r.affectedTabNames = append(r.affectedTabNames, ref.Name)
+ }
+ }
}
-func (r *TableLimitSizeRule) checkDropTable(ctx *mysql.DropTableContext) {
- r.statementBaseLine = ctx.GetStart().GetLine()
- for _, tabRef := range ctx.TableRefList().AllTableRef() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(tabRef)
- r.affectedTabNames = append(r.affectedTabNames, tableName)
+func (r *tableLimitSizeOmniRule) checkDropTable(n *ast.DropTableStmt) {
+ for _, ref := range n.Tables {
+ if ref != nil {
+ r.affectedTabNames = append(r.affectedTabNames, ref.Name)
+ }
}
}
-func (r *TableLimitSizeRule) generateAdvice() {
+func (r *tableLimitSizeOmniRule) generateAdvice() {
if r.dbMetadata != nil && len(r.dbMetadata.Schemas) != 0 {
- // Check all table size.
for _, tabName := range r.affectedTabNames {
tableRows := getTabRowsByName(tabName, r.dbMetadata.Schemas[0].Tables)
if tableRows >= int64(r.maxRows) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.TableExceedLimitSize.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Apply DDL on large table '%s' ( %d rows ) will lock table for a long time", tabName, tableRows),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + r.statementBaseLine),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_no_duplicate_index.go b/backend/plugin/advisor/mysql/rule_table_no_duplicate_index.go
index 3c49db21b5..a9dc7df7d8 100644
--- a/backend/plugin/advisor/mysql/rule_table_no_duplicate_index.go
+++ b/backend/plugin/advisor/mysql/rule_table_no_duplicate_index.go
@@ -1,23 +1,16 @@
package mysql
-// Framework code is generated by the generator.
-
import (
"context"
"fmt"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -39,222 +32,159 @@ func (*TableNoDuplicateIndexAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, err
}
- // Create the rule
- rule := NewTableNoDuplicateIndexRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableNoDuplicateIndexOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
type duplicateIndex struct {
indexName string
- // BTREE, HASH, etc.
indexType string
unique bool
fulltext bool
spatial bool
table string
columns []string
- // line is the line number of the index definition.
- line int
+ line int
}
-// TableNoDuplicateIndexRule checks for no duplicate index in table.
-type TableNoDuplicateIndexRule struct {
- BaseRule
+type tableNoDuplicateIndexOmniRule struct {
+ OmniBaseRule
indexList []duplicateIndex
}
-// NewTableNoDuplicateIndexRule creates a new TableNoDuplicateIndexRule.
-func NewTableNoDuplicateIndexRule(level storepb.Advice_Status, title string) *TableNoDuplicateIndexRule {
- return &TableNoDuplicateIndexRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- indexList: []duplicateIndex{},
- }
-}
-
-// Name returns the rule name.
-func (*TableNoDuplicateIndexRule) Name() string {
+func (*tableNoDuplicateIndexOmniRule) Name() string {
return "TableNoDuplicateIndexRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableNoDuplicateIndexRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeCreateIndex:
- r.checkCreateIndex(ctx.(*mysql.CreateIndexContext))
+func (r *tableNoDuplicateIndexOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.CreateIndexStmt:
+ r.checkCreateIndex(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*TableNoDuplicateIndexRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableNoDuplicateIndexRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *tableNoDuplicateIndexOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- if ctx.TableName() == nil {
- return
- }
- if ctx.TableElementList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- // Build suspect index list.
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement == nil || tableElement.TableConstraintDef() == nil {
+ tableName := n.Table.Name
+ for _, constraint := range n.Constraints {
+ if constraint == nil {
continue
}
- r.handleConstraintDef(tableName, tableElement.TableConstraintDef())
+ r.handleConstraint(tableName, constraint, r.LocToLine(constraint.Loc))
}
- // Check for duplicate index.
if index := hasDuplicateIndexes(r.indexList); index != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.DuplicateIndexInTable.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
StartPosition: common.ConvertANTLRLineToPosition(index.line),
})
}
}
-func (r *TableNoDuplicateIndexRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableRef() == nil {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+func (r *tableNoDuplicateIndexOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
- for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- if alterListItem == nil {
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- if alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil {
- r.handleConstraintDef(tableName, alterListItem.TableConstraintDef())
+ switch cmd.Type {
+ case ast.ATAddConstraint, ast.ATAddIndex:
+ if cmd.Constraint != nil {
+ r.handleConstraint(tableName, cmd.Constraint, r.LocToLine(n.Loc))
+ }
+ default:
}
}
if index := hasDuplicateIndexes(r.indexList); index != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.DuplicateIndexInTable.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
StartPosition: common.ConvertANTLRLineToPosition(index.line),
})
}
}
-func (r *TableNoDuplicateIndexRule) handleConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- switch ctx.GetType_().GetTokenType() {
- case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL:
+func (r *tableNoDuplicateIndexOmniRule) handleConstraint(tableName string, constraint *ast.Constraint, line int32) {
+ switch constraint.Type {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex, ast.ConstrFulltextIndex, ast.ConstrSpatialIndex:
default:
return
}
index := duplicateIndex{
indexType: "BTREE",
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(line),
table: tableName,
+ columns: constraint.Columns,
+ indexName: constraint.Name,
}
- if ctx.KeyListVariants() != nil {
- index.columns = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
+ if constraint.IndexType != "" {
+ index.indexType = constraint.IndexType
}
- if ctx.UNIQUE_SYMBOL() != nil {
+ switch constraint.Type {
+ case ast.ConstrUnique:
index.unique = true
- } else if ctx.FULLTEXT_SYMBOL() != nil {
+ case ast.ConstrFulltextIndex:
index.fulltext = true
- } else if ctx.SPATIAL_SYMBOL() != nil {
+ case ast.ConstrSpatialIndex:
index.spatial = true
- }
- if ctx.IndexNameAndType() != nil {
- if ctx.IndexNameAndType().IndexName() != nil {
- index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- }
- if ctx.IndexNameAndType().IndexType() != nil {
- index.indexType = ctx.IndexNameAndType().IndexType().GetText()
- }
+ default:
}
r.indexList = append(r.indexList, index)
}
-func (r *TableNoDuplicateIndexRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+func (r *tableNoDuplicateIndexOmniRule) checkCreateIndex(n *ast.CreateIndexStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
+ tableName := n.Table.Name
index := duplicateIndex{
indexType: "BTREE",
- line: r.baseLine + ctx.GetStart().GetLine(),
+ line: r.BaseLine + int(r.LocToLine(n.Loc)),
table: tableName,
+ indexName: n.IndexName,
+ columns: omniIndexColumns(n.Columns),
+ }
+ if n.IndexType != "" {
+ index.indexType = n.IndexType
}
- if ctx.UNIQUE_SYMBOL() != nil {
+ if n.Unique {
index.unique = true
- } else if ctx.FULLTEXT_SYMBOL() != nil {
+ } else if n.Fulltext {
index.fulltext = true
- } else if ctx.SPATIAL_SYMBOL() != nil {
+ } else if n.Spatial {
index.spatial = true
}
- if ctx.IndexName() != nil {
- index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
- }
- if ctx.IndexNameAndType() != nil {
- if ctx.IndexNameAndType().IndexName() != nil {
- index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
- }
- if ctx.IndexNameAndType().IndexType() != nil {
- index.indexType = ctx.IndexNameAndType().IndexType().GetText()
- }
- }
- if ctx.IndexTypeClause() != nil && ctx.IndexTypeClause().IndexType() != nil {
- index.indexType = ctx.IndexTypeClause().IndexType().GetText()
- }
- index.columns = mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
r.indexList = append(r.indexList, index)
- if index := hasDuplicateIndexes(r.indexList); index != nil {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ if dup := hasDuplicateIndexes(r.indexList); dup != nil {
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.DuplicateIndexInTable.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
- StartPosition: common.ConvertANTLRLineToPosition(index.line),
+ Title: r.Title,
+ Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, dup.indexName),
+ StartPosition: common.ConvertANTLRLineToPosition(dup.line),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_no_fk.go b/backend/plugin/advisor/mysql/rule_table_no_fk.go
index 6104450186..965c8c5199 100644
--- a/backend/plugin/advisor/mysql/rule_table_no_fk.go
+++ b/backend/plugin/advisor/mysql/rule_table_no_fk.go
@@ -3,19 +3,13 @@ package mysql
import (
"context"
"fmt"
- "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -39,115 +33,69 @@ func (*TableNoFKAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*
return nil, err
}
- // Create the rule
- rule := NewTableNoFKRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableNoFKOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// TableNoFKRule checks table disallow foreign key.
-type TableNoFKRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewTableNoFKRule creates a new TableNoFKRule.
-func NewTableNoFKRule(level storepb.Advice_Status, title string) *TableNoFKRule {
- return &TableNoFKRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type tableNoFKOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*TableNoFKRule) Name() string {
+func (*tableNoFKOmniRule) Name() string {
return "TableNoFKRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableNoFKRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *tableNoFKOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // No action required for other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*TableNoFKRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableNoFKRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil || ctx.TableElementList() == nil {
+func (r *tableNoFKOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- if tableElement.TableConstraintDef() == nil {
- continue
+ tableName := n.Table.Name
+ for _, constraint := range n.Constraints {
+ if constraint.Type == ast.ConstrForeignKey {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.Level,
+ Code: code.TableHasFK.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", tableName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(constraint.Loc))),
+ })
}
- r.handleTableConstraintDef(tableName, tableElement.TableConstraintDef())
}
}
-func (r *TableNoFKRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.TableRef() == nil {
+func (r *tableNoFKOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
-
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- switch {
- // ADD CONSTRANIT
- case option.ADD_SYMBOL() != nil && option.TableConstraintDef() != nil:
- r.handleTableConstraintDef(tableName, option.TableConstraintDef())
- default:
+ tableName := n.Table.Name
+ for _, cmd := range n.Commands {
+ if cmd == nil {
continue
}
- }
-}
-
-func (r *TableNoFKRule) handleTableConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_() != nil {
- switch strings.ToUpper(ctx.GetType_().GetText()) {
- case "FOREIGN":
+ if cmd.Type == ast.ATAddConstraint && cmd.Constraint != nil && cmd.Constraint.Type == ast.ConstrForeignKey {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.TableHasFK.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Foreign key is not allowed in the table `%s`", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(cmd.Constraint.Loc))),
})
- default:
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_require_charset.go b/backend/plugin/advisor/mysql/rule_table_require_charset.go
index 622efff822..497a474596 100644
--- a/backend/plugin/advisor/mysql/rule_table_require_charset.go
+++ b/backend/plugin/advisor/mysql/rule_table_require_charset.go
@@ -3,17 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -34,86 +31,55 @@ func (*TableRequireCharsetAdvisor) Check(_ context.Context, checkCtx advisor.Con
return nil, err
}
- // Create the rule
- rule := NewTableRequireCharsetRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableRequireCharsetOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableRequireCharsetRule checks that tables have charset specified.
-type TableRequireCharsetRule struct {
- BaseRule
+type tableRequireCharsetOmniRule struct {
+ OmniBaseRule
}
-// NewTableRequireCharsetRule creates a new TableRequireCharsetRule.
-func NewTableRequireCharsetRule(level storepb.Advice_Status, title string) *TableRequireCharsetRule {
- return &TableRequireCharsetRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*TableRequireCharsetRule) Name() string {
+func (*tableRequireCharsetOmniRule) Name() string {
return "TableRequireCharsetRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableRequireCharsetRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeCreateTable {
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
+func (r *tableRequireCharsetOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateTableStmt)
+ if !ok {
+ return
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableRequireCharsetRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+ r.checkCreateTable(n)
}
-func (r *TableRequireCharsetRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
+func (r *tableRequireCharsetOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+ tableName := n.Table.Name
if tableName == "" {
return
}
hasCharset := false
- if ctx.CreateTableOptions() != nil {
- for _, tableOption := range ctx.CreateTableOptions().AllCreateTableOption() {
- if tableOption.DefaultCharset() != nil {
- hasCharset = true
- break
- }
+ for _, opt := range n.Options {
+ if opt != nil && (strings.EqualFold(opt.Name, "CHARSET") || strings.EqualFold(opt.Name, "CHARACTER SET") || strings.EqualFold(opt.Name, "DEFAULT CHARSET") || strings.EqualFold(opt.Name, "DEFAULT CHARACTER SET")) {
+ hasCharset = true
+ break
}
}
if !hasCharset {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.NoCharset.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Table %s does not have a character set specified", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_require_collation.go b/backend/plugin/advisor/mysql/rule_table_require_collation.go
index 31e984ae5e..1d8e9714f5 100644
--- a/backend/plugin/advisor/mysql/rule_table_require_collation.go
+++ b/backend/plugin/advisor/mysql/rule_table_require_collation.go
@@ -3,17 +3,14 @@ package mysql
import (
"context"
"fmt"
+ "strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
@@ -34,86 +31,55 @@ func (*TableRequireCollationAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, err
}
- // Create the rule
- rule := NewTableRequireCollationRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableRequireCollationOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// TableRequireCollationRule checks that tables have collation specified.
-type TableRequireCollationRule struct {
- BaseRule
+type tableRequireCollationOmniRule struct {
+ OmniBaseRule
}
-// NewTableRequireCollationRule creates a new TableRequireCollationRule.
-func NewTableRequireCollationRule(level storepb.Advice_Status, title string) *TableRequireCollationRule {
- return &TableRequireCollationRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
-}
-
-// Name returns the rule name.
-func (*TableRequireCollationRule) Name() string {
+func (*tableRequireCollationOmniRule) Name() string {
return "TableRequireCollationRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableRequireCollationRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- if nodeType == NodeTypeCreateTable {
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
+func (r *tableRequireCollationOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateTableStmt)
+ if !ok {
+ return
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableRequireCollationRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+ r.checkCreateTable(n)
}
-func (r *TableRequireCollationRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
+func (r *tableRequireCollationOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+ tableName := n.Table.Name
if tableName == "" {
return
}
hasCollation := false
- if ctx.CreateTableOptions() != nil {
- for _, tableOption := range ctx.CreateTableOptions().AllCreateTableOption() {
- if tableOption.DefaultCollation() != nil {
- hasCollation = true
- break
- }
+ for _, opt := range n.Options {
+ if opt != nil && (strings.EqualFold(opt.Name, "COLLATE") || strings.EqualFold(opt.Name, "DEFAULT COLLATE")) {
+ hasCollation = true
+ break
}
}
if !hasCollation {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ Status: r.Level,
Code: code.NoCollation.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Table %s does not have a collation specified", tableName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(int(r.ContentStartLine())),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_table_require_pk.go b/backend/plugin/advisor/mysql/rule_table_require_pk.go
index 20ce8f1e6b..e2a6ef6707 100644
--- a/backend/plugin/advisor/mysql/rule_table_require_pk.go
+++ b/backend/plugin/advisor/mysql/rule_table_require_pk.go
@@ -5,16 +5,12 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"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/store/model"
)
@@ -43,183 +39,134 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
return nil, err
}
- // Create the rule
- rule := NewTableRequirePKRule(level, checkCtx.Rule.Type.String(), checkCtx.OriginalMetadata)
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &tableRequirePKOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ tables: make(map[string]columnSet),
+ line: make(map[string]int),
+ originalMetadata: checkCtx.OriginalMetadata,
}
- // Generate advice after walking
+ // Walk all statements.
+ RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule})
+
+ // Generate advice after walking all statements.
rule.generateAdviceList()
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// TableRequirePKRule checks table requires PK.
-type TableRequirePKRule struct {
- BaseRule
+type tableRequirePKOmniRule struct {
+ OmniBaseRule
tables map[string]columnSet
line map[string]int
originalMetadata *model.DatabaseMetadata
}
-// NewTableRequirePKRule creates a new TableRequirePKRule.
-func NewTableRequirePKRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *TableRequirePKRule {
- return &TableRequirePKRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- tables: make(map[string]columnSet),
- line: make(map[string]int),
- originalMetadata: originalMetadata,
- }
-}
-
-// Name returns the rule name.
-func (*TableRequirePKRule) Name() string {
+func (*tableRequirePKOmniRule) Name() string {
return "TableRequirePKRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableRequirePKRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeDropTable:
- r.checkDropTable(ctx.(*mysql.DropTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *tableRequirePKOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.DropTableStmt:
+ r.checkDropTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
}
- return nil
-}
-
-// OnExit is called when exiting a parse tree node.
-func (*TableRequirePKRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *TableRequirePKRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.TableName() == nil {
- return
- }
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.createTable(tableName, ctx)
- r.line[tableName] = r.baseLine + ctx.GetStart().GetLine()
}
-func (r *TableRequirePKRule) createTable(tableName string, ctx *mysql.CreateTableContext) {
- if ctx.TableElementList() == nil {
+func (r *tableRequirePKOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ if n.Table == nil {
return
}
+ tableName := n.Table.Name
r.initEmptyTable(tableName)
+ r.line[tableName] = r.BaseLine + int(r.LocToLine(n.Loc))
- for _, tableElement := range ctx.TableElementList().AllTableElement() {
- switch {
- // add primary key from column definition.
- case tableElement.ColumnDefinition() != nil:
- if tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ // Check column-level PRIMARY KEY.
+ for _, col := range n.Columns {
+ if col == nil {
+ continue
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.tables[tableName] = newColumnSet([]string{col.Name})
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.handleFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
- // add primary key from table constraint.
- case tableElement.TableConstraintDef() != nil:
- r.handleTableConstraintDef(tableName, tableElement.TableConstraintDef())
- default:
}
}
-}
-func (r *TableRequirePKRule) handleFieldDefinition(tableName string, columnName string, ctx mysql.IFieldDefinitionContext) {
- for _, attr := range ctx.AllColumnAttribute() {
- if attr == nil || attr.PRIMARY_SYMBOL() == nil {
- continue
+ // Check table-level PRIMARY KEY constraint.
+ for _, constraint := range n.Constraints {
+ if constraint.Type == ast.ConstrPrimaryKey {
+ r.tables[tableName] = newColumnSet(constraint.Columns)
}
- r.tables[tableName] = newColumnSet([]string{columnName})
}
}
-func (r *TableRequirePKRule) handleTableConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
- if ctx.GetType_() != nil {
- if ctx.GetType_().GetTokenType() == mysql.MySQLParserPRIMARY_SYMBOL {
- list := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
- r.tables[tableName] = newColumnSet(list)
+func (r *tableRequirePKOmniRule) checkDropTable(n *ast.DropTableStmt) {
+ for _, ref := range n.Tables {
+ if ref != nil {
+ delete(r.tables, ref.Name)
}
}
}
-func (r *TableRequirePKRule) checkDropTable(ctx *mysql.DropTableContext) {
- if ctx.TableRefList() == nil {
- return
- }
- for _, tableRef := range ctx.TableRefList().AllTableRef() {
- _, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
- delete(r.tables, tableName)
- }
-}
-
-func (r *TableRequirePKRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.TableRef() == nil {
- return
- }
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
+func (r *tableRequirePKOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ if n.Table == nil {
return
}
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+ tableName := n.Table.Name
+ lineNumber := r.BaseLine + int(r.LocToLine(n.Loc))
- lineNumber := r.baseLine + ctx.GetStart().GetLine()
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
- switch {
- // ADD CONSTRANIT
- case option.ADD_SYMBOL() != nil && option.TableConstraintDef() != nil:
- r.handleTableConstraintDef(tableName, option.TableConstraintDef())
- // DROP PRIMARY KEY
- case option.DROP_SYMBOL() != nil && option.PRIMARY_SYMBOL() != nil:
- r.initEmptyTable(tableName)
- r.line[tableName] = lineNumber
- // DROP INDEX/KEY
- case option.DROP_SYMBOL() != nil && option.KeyOrIndex() != nil && option.IndexRef() != nil:
- _, _, indexName := mysqlparser.NormalizeIndexRef(option.IndexRef())
- if strings.ToUpper(indexName) == primaryKeyName {
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ switch cmd.Type {
+ case ast.ATAddConstraint:
+ if cmd.Constraint != nil && cmd.Constraint.Type == ast.ConstrPrimaryKey {
+ r.tables[tableName] = newColumnSet(cmd.Constraint.Columns)
+ }
+ case ast.ATDropConstraint:
+ // DROP PRIMARY KEY
+ if strings.EqualFold(cmd.Name, primaryKeyName) || cmd.Name == "" {
+ // cmd.Name == "" when it's DROP PRIMARY KEY (no explicit constraint name)
r.initEmptyTable(tableName)
r.line[tableName] = lineNumber
}
- // ADD COLUMNS
- case option.ADD_SYMBOL() != nil && option.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLIdentifier(option.Identifier())
- r.handleFieldDefinition(tableName, columnName, option.FieldDefinition())
- // CHANGE COLUMN
- case option.CHANGE_SYMBOL() != nil && option.ColumnInternalRef() != nil && option.Identifier() != nil && option.FieldDefinition() != nil:
- oldColumn := mysqlparser.NormalizeMySQLColumnInternalRef(option.ColumnInternalRef())
- newColumn := mysqlparser.NormalizeMySQLIdentifier(option.Identifier())
- if r.changeColumn(tableName, oldColumn, newColumn) {
+ case ast.ATDropIndex:
+ if strings.EqualFold(cmd.Name, primaryKeyName) {
+ r.initEmptyTable(tableName)
r.line[tableName] = lineNumber
}
- r.handleFieldDefinition(tableName, newColumn, option.FieldDefinition())
- // MODIFY COLUMN
- case option.MODIFY_SYMBOL() != nil && option.ColumnInternalRef() != nil && option.FieldDefinition() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(option.ColumnInternalRef())
- r.handleFieldDefinition(tableName, columnName, option.FieldDefinition())
- // DROP COLUMN
- case option.DROP_SYMBOL() != nil && option.ColumnInternalRef() != nil:
- columnName := mysqlparser.NormalizeMySQLColumnInternalRef(option.ColumnInternalRef())
- if r.dropColumn(tableName, columnName) {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ r.handleColumnDef(tableName, cmd.Column)
+ }
+ for _, col := range cmd.Columns {
+ r.handleColumnDef(tableName, col)
+ }
+ case ast.ATChangeColumn:
+ if cmd.Column != nil {
+ oldColumn := cmd.Name
+ newColumn := cmd.Column.Name
+ if r.changeColumn(tableName, oldColumn, newColumn) {
+ r.line[tableName] = lineNumber
+ }
+ r.handleColumnDef(tableName, cmd.Column)
+ }
+ case ast.ATModifyColumn:
+ if cmd.Column != nil {
+ r.handleColumnDef(tableName, cmd.Column)
+ }
+ case ast.ATDropColumn:
+ if r.dropColumn(tableName, cmd.Name) {
r.line[tableName] = lineNumber
}
default:
@@ -227,14 +174,25 @@ func (r *TableRequirePKRule) checkAlterTable(ctx *mysql.AlterTableContext) {
}
}
-func (r *TableRequirePKRule) generateAdviceList() {
+func (r *tableRequirePKOmniRule) handleColumnDef(tableName string, col *ast.ColumnDef) {
+ if col == nil {
+ return
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ r.tables[tableName] = newColumnSet([]string{col.Name})
+ }
+ }
+}
+
+func (r *tableRequirePKOmniRule) generateAdviceList() {
tableList := r.getTableList()
for _, tableName := range tableList {
if len(r.tables[tableName]) == 0 {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: code.TableNoPK.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("Table `%s` requires PRIMARY KEY", tableName),
StartPosition: common.ConvertANTLRLineToPosition(r.line[tableName]),
})
@@ -242,7 +200,7 @@ func (r *TableRequirePKRule) generateAdviceList() {
}
}
-func (r *TableRequirePKRule) changeColumn(tableName string, oldColumn string, newColumn string) bool {
+func (r *tableRequirePKOmniRule) changeColumn(tableName string, oldColumn string, newColumn string) bool {
if r.dropColumn(tableName, oldColumn) {
pk := r.tables[tableName]
pk[newColumn] = true
@@ -251,7 +209,7 @@ func (r *TableRequirePKRule) changeColumn(tableName string, oldColumn string, ne
return false
}
-func (r *TableRequirePKRule) dropColumn(tableName string, columnName string) bool {
+func (r *tableRequirePKOmniRule) dropColumn(tableName string, colName string) bool {
if _, ok := r.tables[tableName]; !ok {
pk := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetIndex(primaryKeyName)
if pk == nil {
@@ -261,16 +219,16 @@ func (r *TableRequirePKRule) dropColumn(tableName string, columnName string) boo
}
pk := r.tables[tableName]
- _, columnInPk := pk[columnName]
- delete(r.tables[tableName], columnName)
+ _, columnInPk := pk[colName]
+ delete(r.tables[tableName], colName)
return columnInPk
}
-func (r *TableRequirePKRule) initEmptyTable(name string) {
+func (r *tableRequirePKOmniRule) initEmptyTable(name string) {
r.tables[name] = make(columnSet)
}
-func (r *TableRequirePKRule) getTableList() []string {
+func (r *tableRequirePKOmniRule) getTableList() []string {
var tableList []string
for tableName := range r.tables {
tableList = append(tableList, tableName)
diff --git a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
index 370c88278c..7e39a1cdbe 100644
--- a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
+++ b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
@@ -7,17 +7,12 @@ import (
"strconv"
"strings"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/pkg/errors"
- "github.com/bytebase/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/common"
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/advisor/code"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -43,79 +38,52 @@ func (*TableMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advis
return nil, errors.New("number_payload is required for this rule")
}
- // Create the rule
- rule := NewTableTextFieldsTotalLengthRule(level, checkCtx.Rule.Type.String(), checkCtx.FinalMetadata, int(numberPayload.Number))
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
+ rule := &tableTextFieldsTotalLengthOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
+ finalMetadata: checkCtx.FinalMetadata,
+ maximum: int(numberPayload.Number),
+ }
for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
if stmt.AST == nil {
continue
}
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
+ node, ok := mysqlparser.GetOmniNode(stmt.AST)
if !ok {
continue
}
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule.SetStatement(stmt.BaseLine(), stmt.Text)
+ rule.OnStatement(node)
}
- return checker.GetAdviceList(), nil
+ return rule.GetAdviceList(), nil
}
-// TableTextFieldsTotalLengthRule checks for table text fields total length.
-type TableTextFieldsTotalLengthRule struct {
- BaseRule
+type tableTextFieldsTotalLengthOmniRule struct {
+ OmniBaseRule
finalMetadata *model.DatabaseMetadata
maximum int
}
-// NewTableTextFieldsTotalLengthRule creates a new TableTextFieldsTotalLengthRule.
-func NewTableTextFieldsTotalLengthRule(level storepb.Advice_Status, title string, finalMetadata *model.DatabaseMetadata, maximum int) *TableTextFieldsTotalLengthRule {
- return &TableTextFieldsTotalLengthRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- finalMetadata: finalMetadata,
- maximum: maximum,
- }
-}
-
-// Name returns the rule name.
-func (*TableTextFieldsTotalLengthRule) Name() string {
+func (*tableTextFieldsTotalLengthOmniRule) Name() string {
return "TableTextFieldsTotalLengthRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *TableTextFieldsTotalLengthRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
+func (r *tableTextFieldsTotalLengthOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
default:
- // Ignore other node types
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*TableTextFieldsTotalLengthRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
-}
-
-func (r *TableTextFieldsTotalLengthRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.TableElementList() == nil || ctx.TableName() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+func (r *tableTextFieldsTotalLengthOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
@@ -130,30 +98,19 @@ func (r *TableTextFieldsTotalLengthRule) checkCreateTable(ctx *mysql.CreateTable
total := getTotalTextLength(tableInfo)
if total > int64(r.maximum) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.IndexCountExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.IndexCountExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(n.Loc),
+ },
})
}
}
-func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
- return
- }
- if ctx.AlterTableActions() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
-
- _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+func (r *tableTextFieldsTotalLengthOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ tableName := omniTableName(n.Table)
if tableName == "" {
return
}
@@ -168,11 +125,13 @@ func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableCo
total := getTotalTextLength(tableInfo)
if total > int64(r.maximum) {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.TotalTextLengthExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: code.TotalTextLengthExceedsLimit.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
+ StartPosition: &storepb.Position{
+ Line: r.LocToLine(n.Loc),
+ },
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_use_innodb.go b/backend/plugin/advisor/mysql/rule_use_innodb.go
index 0d2f9ac0e1..5fea8b2672 100644
--- a/backend/plugin/advisor/mysql/rule_use_innodb.go
+++ b/backend/plugin/advisor/mysql/rule_use_innodb.go
@@ -5,16 +5,13 @@ import (
"fmt"
"strings"
- "github.com/antlr4-go/antlr/v4"
+ "github.com/bytebase/omni/mysql/ast"
- "github.com/bytebase/parser/mysql"
-
- "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "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/common"
)
const (
@@ -40,157 +37,95 @@ func (*UseInnoDBAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*
return nil, err
}
- // Create the rule
- rule := NewUseInnoDBRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
+ rule := &useInnoDBOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
+ },
}
- return checker.GetAdviceList(), nil
-}
-
-// UseInnoDBRule checks for using InnoDB engine.
-type UseInnoDBRule struct {
- BaseRule
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// NewUseInnoDBRule creates a new UseInnoDBRule.
-func NewUseInnoDBRule(level storepb.Advice_Status, title string) *UseInnoDBRule {
- return &UseInnoDBRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
- },
- }
+type useInnoDBOmniRule struct {
+ OmniBaseRule
}
-// Name returns the rule name.
-func (*UseInnoDBRule) Name() string {
+func (*useInnoDBOmniRule) Name() string {
return "UseInnoDBRule"
}
-// OnEnter is called when entering a parse tree node.
-func (r *UseInnoDBRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeCreateTable:
- r.checkCreateTable(ctx.(*mysql.CreateTableContext))
- case NodeTypeAlterTable:
- r.checkAlterTable(ctx.(*mysql.AlterTableContext))
- case NodeTypeSetStatement:
- r.checkSetStatement(ctx.(*mysql.SetStatementContext))
+func (r *useInnoDBOmniRule) OnStatement(node ast.Node) {
+ switch n := node.(type) {
+ case *ast.CreateTableStmt:
+ r.checkCreateTable(n)
+ case *ast.AlterTableStmt:
+ r.checkAlterTable(n)
+ case *ast.SetStmt:
+ r.checkSetStatement(n)
default:
}
- return nil
}
-// OnExit is called when exiting a parse tree node.
-func (*UseInnoDBRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- // This rule doesn't need exit processing
- return nil
-}
-
-func (r *UseInnoDBRule) checkCreateTable(ctx *mysql.CreateTableContext) {
- if ctx.CreateTableOptions() == nil {
+func (r *useInnoDBOmniRule) checkCreateTable(n *ast.CreateTableStmt) {
+ engine := omniTableOptionValue(n.Options, "ENGINE")
+ if engine == "" {
return
}
- for _, tableOption := range ctx.CreateTableOptions().AllCreateTableOption() {
- if tableOption.ENGINE_SYMBOL() != nil && tableOption.EngineRef() != nil {
- if tableOption.EngineRef().TextOrIdentifier() == nil {
- continue
- }
- engine := mysqlparser.NormalizeMySQLTextOrIdentifier(tableOption.EngineRef().TextOrIdentifier())
- if strings.ToLower(engine) != innoDB {
- content := "CREATE " + ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- line := tableOption.GetStart().GetLine()
- r.addAdvice(content, line)
- break
- }
- }
+ if strings.ToLower(engine) != innoDB {
+ content := r.TrimmedStmtText()
+ r.addAdvice(content, r.FindLineByName(engine))
}
}
-func (r *UseInnoDBRule) checkAlterTable(ctx *mysql.AlterTableContext) {
- if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil {
- return
- }
- if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
- return
- }
- code := advisorcode.Ok
- for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {
- for _, op := range option.AllCreateTableOption() {
- if op.ENGINE_SYMBOL() != nil {
- if op.EngineRef() == nil {
- continue
- }
- engine := op.EngineRef().GetText()
- if strings.ToLower(engine) != innoDB {
- code = advisorcode.NotInnoDBEngine
- break
- }
+func (r *useInnoDBOmniRule) checkAlterTable(n *ast.AlterTableStmt) {
+ for _, cmd := range n.Commands {
+ if cmd.Type == ast.ATTableOption && cmd.Option != nil {
+ if strings.EqualFold(cmd.Option.Name, "ENGINE") && strings.ToLower(cmd.Option.Value) != innoDB {
+ content := r.TrimmedStmtText()
+ r.addAdvice(content, r.ContentStartLine())
+ return
}
}
}
-
- if code != advisorcode.Ok {
- content := "ALTER " + ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- line := ctx.GetStart().GetLine()
- r.addAdvice(content, line)
- }
}
-func (r *UseInnoDBRule) checkSetStatement(ctx *mysql.SetStatementContext) {
- code := advisorcode.Ok
- if ctx.StartOptionValueList() == nil {
- return
- }
-
- startOptionValueList := ctx.StartOptionValueList()
- if startOptionValueList.OptionValueNoOptionType() == nil {
- return
- }
- optionValueNoOptionType := startOptionValueList.OptionValueNoOptionType()
- if optionValueNoOptionType.InternalVariableName() == nil {
- return
- }
- name := optionValueNoOptionType.InternalVariableName().GetText()
- if strings.ToLower(name) != defaultStorageEngin {
- return
- }
- if optionValueNoOptionType.SetExprOrDefault() != nil {
- engine := optionValueNoOptionType.SetExprOrDefault().GetText()
- if strings.ToLower(engine) != innoDB {
- code = advisorcode.NotInnoDBEngine
+func (r *useInnoDBOmniRule) checkSetStatement(n *ast.SetStmt) {
+ for _, assign := range n.Assignments {
+ if assign.Column == nil {
+ continue
+ }
+ name := assign.Column.Column
+ if strings.ToLower(name) != defaultStorageEngin {
+ continue
+ }
+ if assign.Value != nil {
+ if lit, ok := assign.Value.(*ast.StringLit); ok {
+ if strings.ToLower(lit.Value) != innoDB {
+ content := r.TrimmedStmtText()
+ r.addAdvice(content, r.ContentStartLine())
+ return
+ }
+ }
+ // For identifiers like CSV (without quotes), they may be parsed as ColumnRef
+ if ref, ok := assign.Value.(*ast.ColumnRef); ok {
+ if strings.ToLower(ref.Column) != innoDB {
+ content := r.TrimmedStmtText()
+ r.addAdvice(content, r.ContentStartLine())
+ return
+ }
+ }
}
- }
-
- if code != advisorcode.Ok {
- content := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
- line := ctx.GetStart().GetLine()
- r.addAdvice(content, line)
}
}
-func (r *UseInnoDBRule) addAdvice(content string, lineNumber int) {
- lineNumber += r.baseLine
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
+func (r *useInnoDBOmniRule) addAdvice(content string, lineNumber int32) {
+ absoluteLine := r.BaseLine + int(lineNumber)
+ r.AddAdviceAbsolute(&storepb.Advice{
+ Status: r.Level,
Code: advisorcode.NotInnoDBEngine.Int32(),
- Title: r.title,
+ Title: r.Title,
Content: fmt.Sprintf("\"%s;\" doesn't use InnoDB engine", content),
- StartPosition: common.ConvertANTLRLineToPosition(lineNumber),
+ StartPosition: common.ConvertANTLRLineToPosition(absoluteLine),
})
}
diff --git a/backend/plugin/advisor/mysql/rule_view_disallow_create.go b/backend/plugin/advisor/mysql/rule_view_disallow_create.go
index 3c48413d6e..0dcd976025 100644
--- a/backend/plugin/advisor/mysql/rule_view_disallow_create.go
+++ b/backend/plugin/advisor/mysql/rule_view_disallow_create.go
@@ -4,16 +4,12 @@ import (
"context"
"fmt"
- "github.com/antlr4-go/antlr/v4"
-
- "github.com/bytebase/parser/mysql"
+ "github.com/bytebase/omni/mysql/ast"
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
advisorcode "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"
)
var (
@@ -35,85 +31,36 @@ func (*ViewDisallowCreateAdvisor) Check(_ context.Context, checkCtx advisor.Cont
return nil, err
}
- // Create the rule
- rule := NewViewDisallowCreateRule(level, checkCtx.Rule.Type.String())
-
- // Create the generic checker with the rule
- checker := NewGenericChecker([]Rule{rule})
-
- for _, stmt := range checkCtx.ParsedStatements {
- rule.SetBaseLine(stmt.BaseLine())
- checker.SetBaseLine(stmt.BaseLine())
- if stmt.AST == nil {
- continue
- }
- antlrAST, ok := base.GetANTLRAST(stmt.AST)
- if !ok {
- continue
- }
- antlr.ParseTreeWalkerDefault.Walk(checker, antlrAST.Tree)
- }
-
- return checker.GetAdviceList(), nil
-}
-
-// ViewDisallowCreateRule checks for disallow creating view.
-type ViewDisallowCreateRule struct {
- BaseRule
- text string
-}
-
-// NewViewDisallowCreateRule creates a new ViewDisallowCreateRule.
-func NewViewDisallowCreateRule(level storepb.Advice_Status, title string) *ViewDisallowCreateRule {
- return &ViewDisallowCreateRule{
- BaseRule: BaseRule{
- level: level,
- title: title,
+ rule := &viewDisallowCreateOmniRule{
+ OmniBaseRule: OmniBaseRule{
+ Level: level,
+ Title: checkCtx.Rule.Type.String(),
},
}
-}
-// Name returns the rule name.
-func (*ViewDisallowCreateRule) Name() string {
- return "ViewDisallowCreateRule"
+ return RunOmniRules(checkCtx.ParsedStatements, []OmniRule{rule}), nil
}
-// OnEnter is called when entering a parse tree node.
-func (r *ViewDisallowCreateRule) OnEnter(ctx antlr.ParserRuleContext, nodeType string) error {
- switch nodeType {
- case NodeTypeQuery:
- if queryCtx, ok := ctx.(*mysql.QueryContext); ok {
- r.text = queryCtx.GetParser().GetTokenStream().GetTextFromRuleContext(queryCtx)
- }
- case NodeTypeCreateView:
- r.checkCreateView(ctx.(*mysql.CreateViewContext))
- default:
- // Ignore other node types
- }
- return nil
+type viewDisallowCreateOmniRule struct {
+ OmniBaseRule
}
-// OnExit is called when exiting a parse tree node.
-func (*ViewDisallowCreateRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
- return nil
+func (*viewDisallowCreateOmniRule) Name() string {
+ return "ViewDisallowCreateRule"
}
-func (r *ViewDisallowCreateRule) checkCreateView(ctx *mysql.CreateViewContext) {
- if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
+func (r *viewDisallowCreateOmniRule) OnStatement(node ast.Node) {
+ n, ok := node.(*ast.CreateViewStmt)
+ if !ok {
return
}
- code := advisorcode.Ok
- if ctx.ViewName() != nil {
- code = advisorcode.DisallowCreateView
- }
-
- if code != advisorcode.Ok {
+ if n.Name != nil {
r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("View is forbidden, but \"%s\" creates", r.text),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ Status: r.Level,
+ Code: advisorcode.DisallowCreateView.Int32(),
+ Title: r.Title,
+ Content: fmt.Sprintf("View is forbidden, but \"%s\" creates", r.QueryText()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.BaseLine + int(r.LocToLine(n.Loc))),
})
}
}
diff --git a/backend/plugin/advisor/mysql/test/column_disallow_change_type.yaml b/backend/plugin/advisor/mysql/test/column_disallow_change_type.yaml
index f5b629e313..62356a8df0 100644
--- a/backend/plugin/advisor/mysql/test/column_disallow_change_type.yaml
+++ b/backend/plugin/advisor/mysql/test/column_disallow_change_type.yaml
@@ -4,14 +4,6 @@
ALTER TABLE tech_book MODIFY id INTEGER;
ALTER TABLE tech_book MODIFY id INTEGER UNSIGNED;
want:
- - status: 2
- code: 403
- title: COLUMN_DISALLOW_CHANGE_TYPE
- content: '"ALTER TABLE tech_book MODIFY id INTEGER;" changes column type'
- startposition:
- line: 3
- column: 0
- endposition: null
- status: 2
code: 403
title: COLUMN_DISALLOW_CHANGE_TYPE
diff --git a/backend/plugin/advisor/mysql/utils.go b/backend/plugin/advisor/mysql/utils.go
index c6d1afab40..d301ad008a 100644
--- a/backend/plugin/advisor/mysql/utils.go
+++ b/backend/plugin/advisor/mysql/utils.go
@@ -77,14 +77,3 @@ func isKeyword(suspect string) bool {
}
return false
}
-
-// isCharsetDataType checks if the data type supports charset.
-func isCharsetDataType(dataType mysql.IDataTypeContext) bool {
- return dataType != nil && (dataType.CHAR_SYMBOL() != nil ||
- dataType.VARCHAR_SYMBOL() != nil ||
- dataType.VARYING_SYMBOL() != nil ||
- dataType.TINYTEXT_SYMBOL() != nil ||
- dataType.TEXT_SYMBOL() != nil ||
- dataType.MEDIUMTEXT_SYMBOL() != nil ||
- dataType.LONGTEXT_SYMBOL() != nil)
-}
diff --git a/backend/plugin/advisor/mysql/utils_omni.go b/backend/plugin/advisor/mysql/utils_omni.go
new file mode 100644
index 0000000000..0b18e0daf9
--- /dev/null
+++ b/backend/plugin/advisor/mysql/utils_omni.go
@@ -0,0 +1,297 @@
+package mysql
+
+import (
+ "strings"
+
+ "github.com/bytebase/omni/mysql/ast"
+)
+
+// nolint:unused
+// omniTableName extracts the table name from a TableRef.
+func omniTableName(ref *ast.TableRef) string {
+ if ref == nil {
+ return ""
+ }
+ return ref.Name
+}
+
+// nolint:unused
+// omniColumnNames extracts column name list from a table-level constraint.
+func omniColumnNames(constraint *ast.Constraint) []string {
+ if constraint == nil {
+ return nil
+ }
+ return constraint.Columns
+}
+
+// nolint:unused
+// omniIndexColumns extracts column names from an index column list.
+// For expression-based index columns, it falls back to the column reference name.
+func omniIndexColumns(cols []*ast.IndexColumn) []string {
+ if len(cols) == 0 {
+ return nil
+ }
+ var names []string
+ for _, col := range cols {
+ if ref, ok := col.Expr.(*ast.ColumnRef); ok {
+ names = append(names, ref.Column)
+ }
+ }
+ return names
+}
+
+// nolint:unused
+// omniDataTypeName extracts the normalized (upper-case) type name string from a DataType.
+func omniDataTypeName(dt *ast.DataType) string {
+ if dt == nil {
+ return ""
+ }
+ return strings.ToUpper(dt.Name)
+}
+
+// nolint:unused
+// omniIsNullable checks if a column allows NULL.
+// A column is nullable unless it has a NOT NULL constraint or is a PRIMARY KEY.
+func omniIsNullable(col *ast.ColumnDef) bool {
+ if col == nil {
+ return true
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrNotNull || c.Type == ast.ColConstrPrimaryKey {
+ return false
+ }
+ }
+ return true
+}
+
+// nolint:unused
+// omniHasDefault checks if a column has a DEFAULT value.
+func omniHasDefault(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ if col.DefaultValue != nil {
+ return true
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrDefault {
+ return true
+ }
+ }
+ return false
+}
+
+// nolint:unused
+// omniIsAutoIncrement checks if a column has AUTO_INCREMENT.
+func omniIsAutoIncrement(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ return col.AutoIncrement
+}
+
+// nolint:unused
+// omniColumnComment extracts the COMMENT string from a column definition.
+func omniColumnComment(col *ast.ColumnDef) string {
+ if col == nil {
+ return ""
+ }
+ return col.Comment
+}
+
+// nolint:unused
+// omniTableOptionValue extracts a table option value by name (case-insensitive).
+func omniTableOptionValue(opts []*ast.TableOption, name string) string {
+ for _, opt := range opts {
+ if strings.EqualFold(opt.Name, name) {
+ return opt.Value
+ }
+ }
+ return ""
+}
+
+// nolint:unused
+// omniConstraintsByType filters constraints by the given type.
+func omniConstraintsByType(constraints []*ast.Constraint, typ ast.ConstraintType) []*ast.Constraint {
+ var result []*ast.Constraint
+ for _, c := range constraints {
+ if c.Type == typ {
+ result = append(result, c)
+ }
+ }
+ return result
+}
+
+// omniDataTypeNameCompact returns a compact, lowercase type name string.
+func omniDataTypeNameCompact(dt *ast.DataType) string {
+ if dt == nil {
+ return ""
+ }
+ return strings.ToLower(dt.Name)
+}
+
+// omniIsIntegerType checks if the data type is an integer type.
+func omniIsIntegerType(dt *ast.DataType) bool {
+ if dt == nil {
+ return false
+ }
+ switch strings.ToUpper(dt.Name) {
+ case "INT", "TINYINT", "SMALLINT", "MEDIUMINT", "BIGINT", "INTEGER":
+ return true
+ default:
+ return false
+ }
+}
+
+// omniIsTimeType checks if the data type is DATETIME or TIMESTAMP.
+func omniIsTimeType(dt *ast.DataType) bool {
+ if dt == nil {
+ return false
+ }
+ switch strings.ToUpper(dt.Name) {
+ case "DATETIME", "TIMESTAMP":
+ return true
+ default:
+ return false
+ }
+}
+
+// omniIsDefaultCurrentTime checks if a column has DEFAULT CURRENT_TIMESTAMP / NOW().
+func omniIsDefaultCurrentTime(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ return isCurrentTimeFuncCall(col.DefaultValue)
+}
+
+// omniIsOnUpdateCurrentTime checks if a column has ON UPDATE CURRENT_TIMESTAMP / NOW().
+func omniIsOnUpdateCurrentTime(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ return isCurrentTimeFuncCall(col.OnUpdate)
+}
+
+// isCurrentTimeFuncCall checks if an expression is a CURRENT_TIMESTAMP/NOW() call.
+func isCurrentTimeFuncCall(expr ast.ExprNode) bool {
+ if expr == nil {
+ return false
+ }
+ fc, ok := expr.(*ast.FuncCallExpr)
+ if !ok {
+ return false
+ }
+ switch strings.ToUpper(fc.Name) {
+ case "NOW", "CURRENT_TIMESTAMP", "LOCALTIME", "LOCALTIMESTAMP":
+ return true
+ default:
+ return false
+ }
+}
+
+// omniGetColumnsFromCmd extracts column definitions from an AlterTableCmd.
+func omniGetColumnsFromCmd(cmd *ast.AlterTableCmd) []*ast.ColumnDef {
+ if cmd == nil {
+ return nil
+ }
+ switch cmd.Type {
+ case ast.ATAddColumn:
+ if cmd.Column != nil {
+ return []*ast.ColumnDef{cmd.Column}
+ }
+ return cmd.Columns
+ case ast.ATModifyColumn, ast.ATChangeColumn:
+ if cmd.Column != nil {
+ return []*ast.ColumnDef{cmd.Column}
+ }
+ default:
+ }
+ return nil
+}
+
+// nolint:unused
+// omniColumnName returns the effective column name for a command.
+func omniColumnName(cmd *ast.AlterTableCmd) string {
+ if cmd.Column != nil {
+ return cmd.Column.Name
+ }
+ return cmd.Name
+}
+
+// omniIsPrimaryKey checks if a column has a PRIMARY KEY constraint.
+func omniIsPrimaryKey(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ for _, c := range col.Constraints {
+ if c.Type == ast.ColConstrPrimaryKey {
+ return true
+ }
+ }
+ return false
+}
+
+// omniPKColumnNames returns a set of column names that are part of a primary key.
+func omniPKColumnNames(columns []*ast.ColumnDef, constraints []*ast.Constraint) map[string]bool {
+ result := make(map[string]bool)
+ for _, col := range columns {
+ if omniIsPrimaryKey(col) {
+ result[col.Name] = true
+ }
+ }
+ for _, c := range constraints {
+ if c.Type == ast.ConstrPrimaryKey {
+ for _, name := range c.Columns {
+ result[name] = true
+ }
+ }
+ }
+ return result
+}
+
+// omniColumnNeedDefault checks if a column is NOT NULL and needs a DEFAULT.
+// nolint:unused
+func omniColumnNeedDefault(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ return !omniIsNullable(col) && !omniHasDefault(col) && !omniIsPrimaryKey(col)
+}
+
+// omniIsDefaultExemptType checks if a column type is exempt from requiring DEFAULT
+// (BLOB, TEXT, JSON, GEOMETRY and spatial types).
+func omniIsDefaultExemptType(col *ast.ColumnDef) bool {
+ if col == nil || col.TypeName == nil {
+ return false
+ }
+ switch strings.ToUpper(col.TypeName.Name) {
+ case "BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB",
+ "TEXT", "TINYTEXT", "MEDIUMTEXT", "LONGTEXT",
+ "JSON", "GEOMETRY", "POINT", "LINESTRING", "POLYGON",
+ "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION",
+ "GEOMCOLLECTION":
+ return true
+ default:
+ return false
+ }
+}
+
+// omniColumnNeedDefaultNotNull checks if a NOT NULL column needs a DEFAULT value
+// (excludes BLOB, TEXT, JSON, GEOMETRY, and AUTO_INCREMENT columns).
+func omniColumnNeedDefaultNotNull(col *ast.ColumnDef) bool {
+ if col == nil {
+ return false
+ }
+ if omniIsNullable(col) || omniHasDefault(col) || omniIsPrimaryKey(col) || col.AutoIncrement {
+ return false
+ }
+ if col.TypeName != nil {
+ switch strings.ToUpper(col.TypeName.Name) {
+ case "BLOB", "TINYBLOB", "MEDIUMBLOB", "LONGBLOB",
+ "TEXT", "TINYTEXT", "MEDIUMTEXT", "LONGTEXT",
+ "JSON", "GEOMETRY", "SERIAL":
+ return false
+ }
+ }
+ return true
+}
diff --git a/backend/tests/test-data/sql_review_mysql.yaml b/backend/tests/test-data/sql_review_mysql.yaml
index 16f45d711d..c6a103d2c6 100644
--- a/backend/tests/test-data/sql_review_mysql.yaml
+++ b/backend/tests/test-data/sql_review_mysql.yaml
@@ -2,22 +2,26 @@
result:
- |-
{
- "status": "SUCCESS",
- "title": "OK"
+ "status": "SUCCESS",
+ "title": "OK",
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE"
}
run: false
- statement: CREATE TABLE user(id);
result:
- |-
{
- "status": "ERROR",
- "title": "Syntax error",
- "content": "Syntax error at line 1:21: expected data type\nrelated text: CREATE TABLE user(id);",
- "code": 201,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1,
- "column": 21
+ "status": "ERROR",
+ "title": "Syntax error",
+ "content": "Syntax error at line 1:21: expected data type\nrelated text: CREATE TABLE user(id);",
+ "code": 201,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1,
+ "column": 21
}
}
}
@@ -26,301 +30,351 @@
result:
- |-
{
- "status": "ERROR",
- "title": "ENGINE_MYSQL_USE_INNODB",
- "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;\" doesn't use InnoDB engine",
- "code": 501,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "ENGINE_MYSQL_USE_INNODB",
+ "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;\" doesn't use InnoDB engine",
+ "code": 501,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "TABLE_REQUIRE_PK",
- "content": "Table `userTable` requires PRIMARY KEY",
- "code": 601,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "TABLE_REQUIRE_PK",
+ "content": "Table `userTable` requires PRIMARY KEY",
+ "code": 601,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "TABLE_NO_FOREIGN_KEY",
- "content": "Foreign key is not allowed in the table `userTable`",
- "code": 602,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "TABLE_NO_FOREIGN_KEY",
+ "content": "Foreign key is not allowed in the table `userTable`",
+ "code": 602,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "COLUMN_TYPE_DISALLOW_LIST",
- "content": "Disallow column type JSON but column `userTable`.`json_content` is",
- "code": 411,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "COLUMN_TYPE_DISALLOW_LIST",
+ "content": "Disallow column type JSON but column `userTable`.`json_content` is",
+ "code": 411,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_TABLE",
- "content": "`userTable` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
- "code": 301,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_TABLE",
+ "content": "`userTable` mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ "code": 301,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_COLUMN",
- "content": "`userTable`.`roomId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
- "code": 302,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_COLUMN",
+ "content": "`userTable`.`roomId` mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ "code": 302,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_INDEX_IDX",
- "content": "Index in table `userTable` mismatches the naming convention, expect \"^$|^idx_userTable_name$\" but found `idx1`",
- "code": 303,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_INDEX_IDX",
+ "content": "Index in table `userTable` mismatches the naming convention, expect \"^$|^idx_userTable_name$\" but found `idx1`",
+ "code": 303,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_INDEX_UK",
- "content": "Unique key in table `userTable` mismatches the naming convention, expect \"^$|^uk_userTable_id_name$\" but found `uk1`",
- "code": 304,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_INDEX_UK",
+ "content": "Unique key in table `userTable` mismatches the naming convention, expect \"^$|^uk_userTable_id_name$\" but found `uk1`",
+ "code": 304,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_INDEX_FK",
- "content": "Foreign key in table `userTable` mismatches the naming convention, expect \"^$|^fk_userTable_roomId_room_id$\" but found `fk1`",
- "code": 305,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_INDEX_FK",
+ "content": "Foreign key in table `userTable` mismatches the naming convention, expect \"^$|^fk_userTable_roomId_room_id$\" but found `fk1`",
+ "code": 305,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "TABLE_COMMENT",
- "content": "Table `userTable` requires comments",
- "code": 1032,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "TABLE_COMMENT",
+ "content": "Table `userTable` requires comments",
+ "code": 1032,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_REQUIRED",
- "content": "Table `userTable` requires columns: created_ts, creator_id, updated_ts, updater_id",
- "code": 401,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_REQUIRED",
+ "content": "Table `userTable` requires columns: created_ts, creator_id, updated_ts, updater_id",
+ "code": 401,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_NO_NULL",
- "content": "`userTable`.`name` cannot have NULL value",
- "code": 402,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_NO_NULL",
+ "content": "`userTable`.`name` cannot have NULL value",
+ "code": 402,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_NO_NULL",
- "content": "`userTable`.`roomId` cannot have NULL value",
- "code": 402,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_NO_NULL",
+ "content": "`userTable`.`roomId` cannot have NULL value",
+ "code": 402,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_SET_DEFAULT_FOR_NOT_NULL",
- "content": "Column `userTable`.`id` is NOT NULL but doesn't have DEFAULT",
- "code": 404,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_SET_DEFAULT_FOR_NOT_NULL",
+ "content": "Column `userTable`.`id` is NOT NULL but doesn't have DEFAULT",
+ "code": 404,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_COMMENT",
- "content": "Column `userTable`.`id` requires comments",
- "code": 1032,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_COMMENT",
+ "content": "Column `userTable`.`id` requires comments",
+ "code": 1032,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_COMMENT",
- "content": "Column `userTable`.`name` requires comments",
- "code": 1032,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_COMMENT",
+ "content": "Column `userTable`.`name` requires comments",
+ "code": 1032,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_COMMENT",
- "content": "Column `userTable`.`roomId` requires comments",
- "code": 1032,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_COMMENT",
+ "content": "Column `userTable`.`roomId` requires comments",
+ "code": 1032,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_DISALLOW_SET_CHARSET",
- "content": "Disallow set column charset but \"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;\" does",
- "code": 414,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_DISALLOW_SET_CHARSET",
+ "content": "Disallow set column charset but \"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin\" does",
+ "code": 414,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_CURRENT_TIME_COUNT_LIMIT",
- "content": "Table `userTable` has 2 ON UPDATE CURRENT_TIMESTAMP() columns. The count greater than 1.",
- "code": 419,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_CURRENT_TIME_COUNT_LIMIT",
+ "content": "Table `userTable` has 2 ON UPDATE CURRENT_TIMESTAMP() columns. The count greater than 1.",
+ "code": 419,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_REQUIRE_DEFAULT",
- "content": "Column `userTable`.`id` doesn't have DEFAULT.",
- "code": 420,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_REQUIRE_DEFAULT",
+ "content": "Column `userTable`.`id` doesn't have DEFAULT.",
+ "code": 420,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_REQUIRE_DEFAULT",
- "content": "Column `userTable`.`name` doesn't have DEFAULT.",
- "code": 420,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_REQUIRE_DEFAULT",
+ "content": "Column `userTable`.`name` doesn't have DEFAULT.",
+ "code": 420,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_REQUIRE_DEFAULT",
- "content": "Column `userTable`.`roomId` doesn't have DEFAULT.",
- "code": 420,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_REQUIRE_DEFAULT",
+ "content": "Column `userTable`.`roomId` doesn't have DEFAULT.",
+ "code": 420,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "INDEX_TYPE_NO_BLOB",
- "content": "Columns in index must not be BLOB but `userTable`.`content` is blob",
- "code": 804,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "INDEX_TYPE_NO_BLOB",
+ "content": "Columns in index must not be BLOB but `userTable`.`content` is blob",
+ "code": 804,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "SYSTEM_CHARSET_ALLOWLIST",
- "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;\" used disabled charset 'ascii'",
- "code": 1001,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "SYSTEM_CHARSET_ALLOWLIST",
+ "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin\" used disabled charset 'ascii'",
+ "code": 1001,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "SYSTEM_COLLATION_ALLOWLIST",
- "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;\" used disabled collation 'latin1_bin'",
- "code": 1201,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "SYSTEM_COLLATION_ALLOWLIST",
+ "content": "\"CREATE TABLE userTable(id INT NOT NULL,name VARCHAR(255) CHARSET ascii,roomId INT,time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',content BLOB NOT NULL COMMENT 'comment',json_content JSON NOT NULL COMMENT 'comment',INDEX idx1(name),UNIQUE KEY uk1(id, name),FOREIGN KEY fk1(roomId) REFERENCES room(id),INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
+ "code": 1201,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -329,73 +383,85 @@
result:
- |-
{
- "status": "ERROR",
- "title": "COLUMN_AUTO_INCREMENT_MUST_INTEGER",
- "content": "Auto-increment column `t_auto`.`auto_id` requires integer type",
- "code": 410,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "COLUMN_AUTO_INCREMENT_MUST_INTEGER",
+ "content": "Auto-increment column `t_auto`.`auto_id` requires integer type",
+ "code": 410,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "NAMING_COLUMN_AUTO_INCREMENT",
- "content": "`t_auto`.`auto_id` mismatches auto_increment column naming convention, naming format should be \"^id$\"",
- "code": 307,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "NAMING_COLUMN_AUTO_INCREMENT",
+ "content": "`t_auto`.`auto_id` mismatches auto_increment column naming convention, naming format should be \"^id$\"",
+ "code": 307,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_REQUIRED",
- "content": "Table `t_auto` requires columns: created_ts, creator_id, id, updated_ts, updater_id",
- "code": 401,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_REQUIRED",
+ "content": "Table `t_auto` requires columns: created_ts, creator_id, id, updated_ts, updater_id",
+ "code": 401,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_AUTO_INCREMENT_INITIAL_VALUE",
- "content": "The initial auto-increment value in table `t_auto` is 2, which doesn't equal 20",
- "code": 416,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_AUTO_INCREMENT_INITIAL_VALUE",
+ "content": "The initial auto-increment value in table `t_auto` is 2, which doesn't equal 20",
+ "code": 416,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_AUTO_INCREMENT_MUST_UNSIGNED",
- "content": "Auto-increment column `t_auto`.`auto_id` is not UNSIGNED type",
- "code": 417,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_AUTO_INCREMENT_MUST_UNSIGNED",
+ "content": "Auto-increment column `t_auto`.`auto_id` is not UNSIGNED type",
+ "code": 417,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "INDEX_PK_TYPE_LIMIT",
- "content": "Columns in primary key must be INT/BIGINT but `t_auto`.`auto_id` is varchar(20)",
- "code": 803,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "INDEX_PK_TYPE_LIMIT",
+ "content": "Columns in primary key must be INT/BIGINT but `t_auto`.`auto_id` is varchar(20)",
+ "code": 803,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -404,37 +470,43 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_WHERE_REQUIRE_UPDATE_DELETE",
- "content": "\"DELETE FROM tech_book;\" requires WHERE clause",
- "code": 202,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_WHERE_REQUIRE_UPDATE_DELETE",
+ "content": "\"DELETE FROM tech_book\" requires WHERE clause",
+ "code": 202,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DML_DRY_RUN",
- "content": "\"DELETE FROM tech_book\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 208,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DML_DRY_RUN",
+ "content": "\"DELETE FROM tech_book;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 208,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_AFFECTED_ROW_LIMIT",
- "content": "\"DELETE FROM tech_book;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 209,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_AFFECTED_ROW_LIMIT",
+ "content": "\"DELETE FROM tech_book;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 209,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -443,37 +515,43 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_WHERE_NO_LEADING_WILDCARD_LIKE",
- "content": "\"DELETE FROM tech_book WHERE name like \"%abc\";\" uses leading wildcard LIKE",
- "code": 204,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_WHERE_NO_LEADING_WILDCARD_LIKE",
+ "content": "\"DELETE FROM tech_book WHERE name like \"%abc\"\" uses leading wildcard LIKE",
+ "code": 204,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DML_DRY_RUN",
- "content": "\"DELETE FROM tech_book WHERE name like \"%abc\"\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 208,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DML_DRY_RUN",
+ "content": "\"DELETE FROM tech_book WHERE name like \"%abc\";\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 208,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_AFFECTED_ROW_LIMIT",
- "content": "\"DELETE FROM tech_book WHERE name like \"%abc\";\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 209,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_AFFECTED_ROW_LIMIT",
+ "content": "\"DELETE FROM tech_book WHERE name like \"%abc\";\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 209,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -482,61 +560,57 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_SELECT_NO_SELECT_ALL",
- "content": "\"INSERT INTO t_copy SELECT * FROM t;\" uses SELECT all",
- "code": 203,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_SELECT_NO_SELECT_ALL",
+ "content": "\"INSERT INTO t_copy SELECT * FROM t\" uses SELECT all",
+ "code": 203,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_WHERE_REQUIRE_SELECT",
- "content": "\"INSERT INTO t_copy SELECT * FROM t;\" requires WHERE clause",
- "code": 202,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DML_DRY_RUN",
+ "content": "\"INSERT INTO t_copy SELECT * FROM t;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
+ "code": 208,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DML_DRY_RUN",
- "content": "\"INSERT INTO t_copy SELECT * FROM t\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
- "code": 208,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_INSERT_ROW_LIMIT",
+ "content": "\"INSERT INTO t_copy SELECT * FROM t;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
+ "code": 1101,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_INSERT_ROW_LIMIT",
- "content": "\"INSERT INTO t_copy SELECT * FROM t;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
- "code": 1101,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
- }
- }
- }
- - |-
- {
- "status": "WARNING",
- "title": "STATEMENT_INSERT_MUST_SPECIFY_COLUMN",
- "content": "The INSERT statement must specify columns but \"INSERT INTO t_copy SELECT * FROM t;\" does not",
- "code": 1107,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_INSERT_MUST_SPECIFY_COLUMN",
+ "content": "The INSERT statement must specify columns but \"INSERT INTO t_copy SELECT * FROM t;\" does not",
+ "code": 1107,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -545,25 +619,29 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DML_DRY_RUN",
- "content": "\"INSERT INTO t VALUES (1, 1, now(), 1, now())\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t' doesn't exist",
- "code": 208,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DML_DRY_RUN",
+ "content": "\"INSERT INTO t VALUES (1, 1, now(), 1, now());\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t' doesn't exist",
+ "code": 208,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_INSERT_MUST_SPECIFY_COLUMN",
- "content": "The INSERT statement must specify columns but \"INSERT INTO t VALUES (1, 1, now(), 1, now());\" does not",
- "code": 1107,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_INSERT_MUST_SPECIFY_COLUMN",
+ "content": "The INSERT statement must specify columns but \"INSERT INTO t VALUES (1, 1, now(), 1, now());\" does not",
+ "code": 1107,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -572,25 +650,29 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DML_DRY_RUN",
- "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 208,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DML_DRY_RUN",
+ "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase');\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 208,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_AFFECTED_ROW_LIMIT",
- "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase');\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
- "code": 209,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_AFFECTED_ROW_LIMIT",
+ "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase');\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "code": 209,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -599,13 +681,15 @@
result:
- |-
{
- "status": "ERROR",
- "title": "STATEMENT_DISALLOW_COMMIT",
- "content": "Commit is not allowed, related statement: \"COMMIT;\"",
- "code": 206,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "STATEMENT_DISALLOW_COMMIT",
+ "content": "Commit is not allowed, related statement: \"COMMIT;\"",
+ "code": 206,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -614,45 +698,55 @@
result:
- |-
{
- "status": "SUCCESS",
- "title": "OK"
+ "status": "SUCCESS",
+ "title": "OK",
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE"
}
run: true
- statement: INSERT INTO user(id, name) values (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')
result:
- |-
{
- "status": "SUCCESS",
- "title": "OK"
+ "status": "SUCCESS",
+ "title": "OK",
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE"
}
run: true
- statement: DELETE FROM user WHERE id < 10
result:
- |-
{
- "status": "SUCCESS",
- "title": "OK"
+ "status": "SUCCESS",
+ "title": "OK",
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE"
}
run: false
- statement: INSERT INTO user(id, name) values (6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j')
result:
- |-
{
- "status": "SUCCESS",
- "title": "OK"
+ "status": "SUCCESS",
+ "title": "OK",
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE"
}
run: true
- statement: DELETE FROM user WHERE id <= 10
result:
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_AFFECTED_ROW_LIMIT",
- "content": "\"DELETE FROM user WHERE id <= 10;\" affected 10 rows (estimated). The count exceeds 5.",
- "code": 209,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_AFFECTED_ROW_LIMIT",
+ "content": "\"DELETE FROM user WHERE id <= 10;\" affected 10 rows (estimated). The count exceeds 5.",
+ "code": 209,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -671,13 +765,15 @@
result:
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_INSERT_ROW_LIMIT",
- "content": "\"INSERT INTO user(id, name) SELECT id, name FROM (SELECT 1 AS id, 'a' AS name WHERE 1=1 UNION ALL\n SELECT 2 AS id, 'b' AS name WHERE 1=1 UNION ALL\n SELECT 3 AS id, 'c' AS name WHERE 1=1 UNION ALL\n SELECT 4 AS id, 'd' AS name WHERE 1=1 UNION ALL\n SELECT 5 AS id, 'e' AS name WHERE 1=1 UNION ALL\n SELECT 6 AS id, 'f' AS name WHERE 1=1 UNION ALL\n SELECT 7 AS id, 'g' AS name WHERE 1=1 UNION ALL\n SELECT 8 AS id, 'h' AS name WHERE 1=1 UNION ALL\n SELECT 9 AS id, 'i' AS name WHERE 1=1 UNION ALL\n SELECT 10 AS id, 'j' AS name WHERE 1=1) value_table WHERE 1=1;\" inserts 10 rows. The count exceeds 5.",
- "code": 1101,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_INSERT_ROW_LIMIT",
+ "content": "\"INSERT INTO user(id, name) SELECT id, name FROM (SELECT 1 AS id, 'a' AS name WHERE 1=1 UNION ALL\n SELECT 2 AS id, 'b' AS name WHERE 1=1 UNION ALL\n SELECT 3 AS id, 'c' AS name WHERE 1=1 UNION ALL\n SELECT 4 AS id, 'd' AS name WHERE 1=1 UNION ALL\n SELECT 5 AS id, 'e' AS name WHERE 1=1 UNION ALL\n SELECT 6 AS id, 'f' AS name WHERE 1=1 UNION ALL\n SELECT 7 AS id, 'g' AS name WHERE 1=1 UNION ALL\n SELECT 8 AS id, 'h' AS name WHERE 1=1 UNION ALL\n SELECT 9 AS id, 'i' AS name WHERE 1=1 UNION ALL\n SELECT 10 AS id, 'j' AS name WHERE 1=1) value_table WHERE 1=1;\" inserts 10 rows. The count exceeds 5.",
+ "code": 1101,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -686,13 +782,15 @@
result:
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_DISALLOW_LIMIT",
- "content": "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"INSERT INTO user(id, name) SELECT id, name FROM user WHERE id=1 LIMIT 1;\" uses",
- "code": 1103,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "STATEMENT_DISALLOW_LIMIT",
+ "content": "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"INSERT INTO user(id, name) SELECT id, name FROM user WHERE id=1 LIMIT 1\" uses",
+ "code": 1103,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -701,13 +799,15 @@
result:
- |-
{
- "status": "WARNING",
- "title": "TABLE_DISALLOW_PARTITION",
- "content": "Table partition is forbidden, but \"ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8;\" creates",
- "code": 608,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "TABLE_DISALLOW_PARTITION",
+ "content": "Table partition is forbidden, but \"ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8;\" creates",
+ "code": 608,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -718,61 +818,71 @@
result:
- |-
{
- "status": "WARNING",
- "title": "STATEMENT_MERGE_ALTER_TABLE",
- "content": "There are 2 statements to modify table `user`",
- "code": 207,
- "sqlReviewReport": {
- "startPosition": {
- "line": 2
+ "status": "WARNING",
+ "title": "STATEMENT_MERGE_ALTER_TABLE",
+ "content": "There are 2 statements to modify table `user`",
+ "code": 207,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 2
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_DISALLOW_CHANGE_TYPE",
- "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" changes column type",
- "code": 403,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_DISALLOW_CHANGE_TYPE",
+ "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" changes column type",
+ "code": 403,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_DISALLOW_CHANGE",
- "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" contains CHANGE COLUMN statement",
- "code": 406,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_DISALLOW_CHANGE",
+ "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" contains CHANGE COLUMN statement",
+ "code": 406,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "COLUMN_DISALLOW_CHANGING_ORDER",
- "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" changes column order",
- "code": 407,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "COLUMN_DISALLOW_CHANGING_ORDER",
+ "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" changes column order",
+ "code": 407,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "SCHEMA_BACKWARD_COMPATIBILITY",
- "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" may cause incompatibility with the existing data and code",
- "code": 111,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "SCHEMA_BACKWARD_COMPATIBILITY",
+ "content": "\"ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;\" may cause incompatibility with the existing data and code",
+ "code": 111,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
@@ -781,25 +891,29 @@
result:
- |-
{
- "status": "ERROR",
- "title": "TABLE_DROP_NAMING_CONVENTION",
- "content": "`user` mismatches drop table naming convention, naming format should be \"_delete$\"",
- "code": 603,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "ERROR",
+ "title": "TABLE_DROP_NAMING_CONVENTION",
+ "content": "`user` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ "code": 603,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
- |-
{
- "status": "WARNING",
- "title": "SCHEMA_BACKWARD_COMPATIBILITY",
- "content": "\"DROP TABLE user;\" may cause incompatibility with the existing data and code",
- "code": 103,
- "sqlReviewReport": {
- "startPosition": {
- "line": 1
+ "status": "WARNING",
+ "title": "SCHEMA_BACKWARD_COMPATIBILITY",
+ "content": "\"DROP TABLE user;\" may cause incompatibility with the existing data and code",
+ "code": 103,
+ "target": "instances/instance-96a5f914-2/databases/testsqlreview",
+ "type": "STATEMENT_ADVISE",
+ "sqlReviewReport": {
+ "startPosition": {
+ "line": 1
}
}
}
diff --git a/docs/plans/SCENARIOS-mysql-advisor-omni.md b/docs/plans/SCENARIOS-mysql-advisor-omni.md
new file mode 100644
index 0000000000..6c5c5cbf22
--- /dev/null
+++ b/docs/plans/SCENARIOS-mysql-advisor-omni.md
@@ -0,0 +1,219 @@
+# MySQL Advisor Omni Migration Scenarios
+
+> Goal: Migrate all 87 MySQL advisor rules from ANTLR tree walking to omni AST type switches
+> Verification: `go test -count=1 ./backend/plugin/advisor/mysql/...` passes with identical advice output; `grep -r "GetANTLRAST" backend/plugin/advisor/mysql/` returns zero matches after completion
+> Reference sources: PG omni migration (`backend/plugin/advisor/pg/generic_checker_omni.go`, `utils_omni.go`), omni MySQL AST (`github.com/bytebase/omni/mysql/ast`)
+
+Status: [ ] pending, [x] passing, [~] partial
+
+---
+
+## Phase 1: Framework & Proof
+
+Build the omni rule infrastructure and prove it works with one representative rule from each major category.
+
+### 1.1 Core Framework
+
+- [x] `OmniRule` interface with `OnStatement(node ast.Node)`, `Name()`, `GetAdviceList()`
+- [x] `OmniBaseRule` struct with `Level`, `Title`, `Advice`, `BaseLine`, `StmtText` fields
+- [x] `SetStatement(baseLine int, stmtText string)` sets context for position calculations
+- [x] `AddAdvice()` adds advice with BaseLine offset adjustment
+- [x] `LocToLine(loc ast.Loc)` converts omni byte offset to 1-based line number
+- [x] `RunOmniRules()` dispatcher iterates statements, extracts omni node, dispatches to rules
+- [x] `FindLineByName(name string)` searches identifier in statement text, returns line
+
+### 1.2 Utility Functions
+
+- [x] `omniTableName(ref *ast.TableRef)` extracts table name
+- [x] `omniColumnNames(constraint *ast.Constraint)` extracts column name list from constraint
+- [x] `omniIndexColumns(cols []*ast.IndexColumn)` extracts column names from index column list
+- [x] `omniDataTypeName(dt *ast.DataType)` extracts normalized type name string
+- [x] `omniIsNullable(col *ast.ColumnDef)` checks if column allows NULL
+- [x] `omniHasDefault(col *ast.ColumnDef)` checks if column has DEFAULT
+- [x] `omniIsAutoIncrement(col *ast.ColumnDef)` checks if column is AUTO_INCREMENT
+- [x] `omniColumnComment(col *ast.ColumnDef)` extracts column COMMENT string
+- [x] `omniTableOptionValue(opts []*ast.TableOption, name string)` extracts table option by name
+- [x] `omniConstraintsByType(constraints []*ast.Constraint, typ ast.ConstraintType)` filters constraints
+
+### 1.3 Proof-of-Concept Rules (one per category)
+
+- [x] `rule_table_require_pk` migrated — DDL-table representative (CreateTable + AlterTable + DropTable)
+- [x] `rule_column_no_null` migrated — DDL-column representative (CreateTable + AlterTable column inspection)
+- [x] `rule_index_no_duplicate_column` migrated — DDL-index representative (CreateTable + AlterTable + CreateIndex)
+- [x] `rule_naming_table` migrated — naming representative (CreateTable + AlterTable + RenameTable)
+- [x] `rule_stmt_no_select_all` migrated — DML representative (SelectStmt target list inspection)
+- [x] All 5 proof rules produce identical advice output to ANTLR versions
+
+---
+
+## Phase 2: DDL-Column Rules (21 rules)
+
+### 2.1 Column Type & Attribute Rules
+
+- [x] `rule_column_auto_increment_initial_value` — AUTO_INCREMENT initial value check
+- [x] `rule_column_auto_increment_must_integer` — AUTO_INCREMENT must be integer type
+- [x] `rule_column_auto_increment_must_unsigned` — AUTO_INCREMENT must be UNSIGNED
+- [x] `rule_column_current_time_count_limit` — limit CURRENT_TIMESTAMP columns
+- [x] `rule_column_maximum_character_length` — CHAR length limit
+- [x] `rule_column_maximum_varchar_length` — VARCHAR length limit
+- [x] `rule_column_type_disallow_list` — disallowed column types
+- [x] `rule_table_text_fields_total_length` — total text field length limit
+
+### 2.2 Column Constraint & Default Rules
+
+- [x] `rule_column_no_null` — (already migrated in Phase 1, skip)
+- [x] `rule_column_require_default` — columns must have DEFAULT
+- [x] `rule_column_set_default_for_not_null` — NOT NULL columns must have DEFAULT
+- [x] `rule_column_required` — required columns must exist
+- [x] `rule_column_comment_convention` — column COMMENT convention
+
+### 2.3 Column Modification Rules
+
+- [x] `rule_column_disallow_changing` — disallow column changes
+- [x] `rule_column_disallow_changing_order` — disallow column reordering
+- [x] `rule_column_disallow_changing_type` — disallow column type changes
+- [x] `rule_column_disallow_drop` — disallow column drops
+- [x] `rule_column_disallow_drop_in_index` — disallow dropping indexed columns
+- [x] `rule_column_disallow_set_charset` — disallow column-level charset
+- [x] `rule_column_require_charset` — require column charset
+- [x] `rule_column_require_collation` — require column collation
+
+---
+
+## Phase 3: DDL-Table Rules (11 rules)
+
+### 3.1 Table Structure Rules
+
+- [x] `rule_table_require_pk` — (already migrated in Phase 1, skip)
+- [x] `rule_table_comment_convention` — table COMMENT convention
+- [x] `rule_table_disallow_partition` — disallow partitioning
+- [x] `rule_table_disallow_set_charset` — disallow table-level charset
+- [x] `rule_table_require_charset` — require table charset
+- [x] `rule_table_require_collation` — require table collation
+- [x] `rule_table_limit_size` — table size limit check
+
+### 3.2 Table DDL Policy Rules
+
+- [x] `rule_table_disallow_ddl` — disallow DDL on specific tables
+- [x] `rule_table_disallow_dml` — disallow DML on specific tables
+- [x] `rule_table_drop_naming_convention` — drop table naming convention
+- [x] `rule_use_innodb` — require InnoDB engine
+
+---
+
+## Phase 4: DDL-Index Rules (8 rules)
+
+### 4.1 Index Rules
+
+- [x] `rule_index_no_duplicate_column` — (already migrated in Phase 1, skip)
+- [x] `rule_index_key_number_limit` — max columns per index
+- [x] `rule_index_pk_type` — primary key type restrictions
+- [x] `rule_index_primary_key_type_allowlist` — PK type allowlist
+- [x] `rule_index_total_number_limit` — max indexes per table
+- [x] `rule_index_type_allow_list` — index type allowlist
+- [x] `rule_index_type_no_blob` — disallow BLOB in index
+- [x] `rule_table_no_duplicate_index` — no duplicate indexes
+
+---
+
+## Phase 5: DDL-Constraint & Database & View & Misc (12 rules)
+
+### 5.1 Constraint & Charset Rules
+
+- [x] `rule_table_no_fk` — disallow foreign keys
+- [x] `rule_charset_allowlist` — charset allowlist
+- [x] `rule_collation_allowlist` — collation allowlist
+
+### 5.2 Database, View & System Object Rules
+
+- [x] `rule_database_drop_empty_db` — only drop empty databases
+- [x] `rule_view_disallow_create` — disallow view creation
+- [x] `rule_disallow_procedure` — disallow procedure creation
+- [x] `rule_event_disallow_create` — disallow event creation
+- [x] `rule_function_disallow_create` — disallow function creation
+- [x] `rule_function_disallowed_list` — disallowed function list
+- [x] `rule_table_disallow_trigger` — disallow trigger creation
+- [x] `rule_migration_compatibility` — backward compatibility checks
+- [x] `rule_online_migration` — online migration checks
+
+---
+
+## Phase 6: Naming Rules (7 rules)
+
+### 6.1 Naming Convention Rules
+
+- [x] `rule_naming_table` — (already migrated in Phase 1, skip)
+- [x] `rule_naming_column` — column naming convention
+- [x] `rule_naming_auto_increment_column` — auto-increment column naming
+- [x] `rule_naming_identifier_no_keyword` — no reserved keywords as identifiers
+- [x] `rule_naming_index_convention` — index naming convention
+- [x] `rule_naming_foreign_key_convention` — foreign key naming convention
+- [x] `rule_naming_unique_key_convention` — unique key naming convention
+
+---
+
+## Phase 7: Statement Quality Rules (18 rules)
+
+### 7.1 WHERE Clause Rules
+
+- [x] `rule_stmt_where_requirement_for_select` — SELECT requires WHERE
+- [x] `rule_stmt_where_requirement_for_update_delete` — UPDATE/DELETE requires WHERE
+- [x] `rule_stmt_no_leading_wildcard_like` — no leading wildcard in LIKE
+- [x] `rule_statement_where_no_equal_null` — no `= NULL` (use IS NULL)
+- [x] `rule_statement_where_disallow_using_function` — no functions in WHERE
+- [x] `rule_statement_where_maximum_logical_operator_count` — max logical operators in WHERE
+
+### 7.2 Statement Structure Rules
+
+- [x] `rule_stmt_no_select_all` — (already migrated in Phase 1, skip)
+- [x] `rule_stmt_disallow_limit` — disallow LIMIT in DML
+- [x] `rule_stmt_disallow_order_by` — disallow ORDER BY in DML
+- [x] `rule_stmt_disallow_commit` — disallow COMMIT statement
+- [x] `rule_statement_merge_alter_table` — merge multiple ALTER TABLE
+- [x] `rule_statement_maximum_limit_value` — max LIMIT value
+- [x] `rule_statement_maximum_join_table_count` — max JOIN table count
+- [x] `rule_statement_maximum_statements_in_transaction` — max statements in transaction
+
+### 7.3 Performance & Execution Rules
+
+- [x] `rule_stmt_max_execution_time` — max execution time hint
+- [x] `rule_stmt_require_algorithm_or_lock_options` — require ALGORITHM/LOCK in ALTER
+- [x] `rule_statement_add_column_without_position` — ADD COLUMN without FIRST/AFTER
+- [x] `rule_statement_join_strict_column_attrs` — strict JOIN column attribute matching
+
+---
+
+## Phase 8: DML Rules (8 rules)
+
+### 8.1 INSERT Rules
+
+- [x] `rule_insert_must_specify_column` — INSERT must specify columns
+- [x] `rule_insert_row_limit` — INSERT row count limit
+- [x] `rule_insert_disallow_order_by_rand` — no ORDER BY RAND() in INSERT
+
+### 8.2 DML Safety Rules
+
+- [x] `rule_statement_affected_row_limit` — affected row limit
+- [x] `rule_statement_dml_dry_run` — DML dry run check
+- [x] `rule_statement_select_full_table_scan` — detect full table scans
+- [x] `rule_statement_disallow_using_filesort` — detect filesort usage
+- [x] `rule_statement_disallow_using_temporary` — detect temporary table usage
+
+---
+
+## Phase 9: System Rule & Cleanup
+
+### 9.1 System Rule
+
+- [x] `rule_builtin_prior_backup_check` migrated — backup check before DDL
+- [x] `rule_statement_query_minimum_plan_level` migrated — query plan level check
+
+### 9.2 ANTLR Removal
+
+- [ ] Zero `GetANTLRAST` calls remain in `backend/plugin/advisor/mysql/`
+- [ ] `GenericChecker` ANTLR dispatcher removed or deprecated
+- [ ] `AsANTLRAST()` fallback removed from `OmniAST`
+- [ ] `parseSingleStatementLenient()` removed
+- [ ] `ParseMySQL()` callers migrated or removed
+- [ ] Build passes with no ANTLR imports in MySQL advisor package
+- [x] All tests pass: advisor, schema, parser, integration
diff --git a/go.mod b/go.mod
index 3f203ae26c..e3d1885523 100644
--- a/go.mod
+++ b/go.mod
@@ -34,7 +34,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20260402092952-d5ff8b1753d6
+ github.com/bytebase/omni v0.0.0-20260403030128-870fdbdfbf2e
github.com/caarlos0/env/v11 v11.4.0
github.com/cenkalti/backoff/v5 v5.0.3
github.com/cockroachdb/cockroachdb-parser v0.25.2
diff --git a/go.sum b/go.sum
index d327525adf..7b7c63241f 100644
--- a/go.sum
+++ b/go.sum
@@ -253,8 +253,8 @@ github.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35 h1:SjcZSrUMNj6GZq
github.com/bytebase/gomongo v0.0.0-20260324061529-f15b9a5add35/go.mod h1:jHdAD+tN3bO2Hazme0crgkNiHqkDR/3Igesy1chhjLA=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=
-github.com/bytebase/omni v0.0.0-20260402092952-d5ff8b1753d6 h1:WwcxIkyoV4kugHcebFxDucHCnSsSgGZl04WbkJRV9l4=
-github.com/bytebase/omni v0.0.0-20260402092952-d5ff8b1753d6/go.mod h1:7kUVQrivlkckALafCX5j4W3M95Cg9+Kw69ZukIHKXhQ=
+github.com/bytebase/omni v0.0.0-20260403030128-870fdbdfbf2e h1:Yj4aqHR9cyrC1K7FpTMrfIoIYoyvV+LJu00HrmZFWqE=
+github.com/bytebase/omni v0.0.0-20260403030128-870fdbdfbf2e/go.mod h1:7kUVQrivlkckALafCX5j4W3M95Cg9+Kw69ZukIHKXhQ=
github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50 h1:Kq5wlhZ586Rch39y/IQV9MnrEzHob4iiXBG6zrbvSMU=
github.com/bytebase/parser v0.0.0-20260324092042-1d52e5412f50/go.mod h1:jeak/EfutSOAuWKvrFIT2IZunhWprM7oTFBRgZ9RCxo=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=