Skip to content

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

chore: add identifier no keyword rule for mysql (#10708)

boojacke11e333c80fa
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index d6b8356bcc..6c23bac647 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -98,6 +98,9 @@ const (
// MySQLNamingAutoIncrementColumnConvention is an advisor type for MySQL auto-increment naming convention.
MySQLNamingAutoIncrementColumnConvention Type = "bb.plugin.advisor.mysql.naming.auto-increment-column"
+ // MySQLIdentifierNamingNoKeyword is an advisor type for MySQL identifier naming convention without keyword.
+ MySQLIdentifierNamingNoKeyword Type = "bb.plugin.advisor.mysql.naming.identifier-no-keyword"
+
// MySQLColumnRequirement is an advisor type for MySQL column requirement.
MySQLColumnRequirement Type = "bb.plugin.advisor.mysql.column.require"
diff --git a/backend/plugin/advisor/mysql/advisor_disallow_procedure.go b/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
index 6a7c486f7b..eaa0f34a75 100644
--- a/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
+++ b/backend/plugin/advisor/mysql/advisor_disallow_procedure.go
@@ -36,7 +36,7 @@ func (*DisallowProcedureAdvisor) Check(ctx advisor.Context, _ string) ([]advisor
if err != nil {
return nil, err
}
- checker := &tableDisallowProcedureChecker{
+ checker := &disallowProcedureChecker{
level: level,
title: string(ctx.Rule.Type),
}
@@ -57,7 +57,7 @@ func (*DisallowProcedureAdvisor) Check(ctx advisor.Context, _ string) ([]advisor
return checker.adviceList, nil
}
-type tableDisallowProcedureChecker struct {
+type disallowProcedureChecker struct {
*mysql.BaseMySQLParserListener
baseLine int
@@ -67,12 +67,12 @@ type tableDisallowProcedureChecker struct {
text string
}
-func (checker *tableDisallowProcedureChecker) EnterQuery(ctx *mysql.QueryContext) {
+func (checker *disallowProcedureChecker) EnterQuery(ctx *mysql.QueryContext) {
checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
}
// EnterCreateProcedure is to check if the procedure is forbidden.
-func (checker *tableDisallowProcedureChecker) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {
+func (checker *disallowProcedureChecker) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {
code := advisor.Ok
if ctx.ProcedureName() != nil {
code = advisor.DisallowProcedure
diff --git a/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go
new file mode 100644
index 0000000000..99d06c5400
--- /dev/null
+++ b/backend/plugin/advisor/mysql/advisor_naming_identifier_no_keyword.go
@@ -0,0 +1,114 @@
+package mysql
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ "github.com/pkg/errors"
+
+ mysql "github.com/bytebase/mysql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*NamingIdentifierNoKeywordAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLIdentifierNamingNoKeyword, &NamingIdentifierNoKeywordAdvisor{})
+}
+
+// NamingIdentifierNoKeywordAdvisor is the advisor checking for identifier naming convention without keyword.
+type NamingIdentifierNoKeywordAdvisor struct {
+}
+
+// Check checks for identifier naming convention without keyword.
+func (*NamingIdentifierNoKeywordAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to mysql parser result")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &namingIdentifierNoKeywordChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+ for _, stmt := range stmtList {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type namingIdentifierNoKeywordChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+}
+
+// EnterPureIdentifier is called when entering the pureIdentifier production.
+func (checker *namingIdentifierNoKeywordChecker) EnterPureIdentifier(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
+ }
+
+ // Remove backticks as possible.
+ identifier := trimBackTicks(textNode.GetText())
+ advice := checker.checkIdentifier(identifier)
+ if advice != nil {
+ advice.Line = ctx.GetStart().GetLine()
+ checker.adviceList = append(checker.adviceList, *advice)
+ }
+}
+
+// EnterIdentifierKeyword is called when entering the identifierKeyword production.
+func (checker *namingIdentifierNoKeywordChecker) EnterIdentifierKeyword(ctx *mysql.IdentifierKeywordContext) {
+ identifier := ctx.GetText()
+ advice := checker.checkIdentifier(identifier)
+ if advice != nil {
+ advice.Line = ctx.GetStart().GetLine()
+ checker.adviceList = append(checker.adviceList, *advice)
+ }
+}
+
+func (checker *namingIdentifierNoKeywordChecker) checkIdentifier(identifier string) *advisor.Advice {
+ if isKeyword(identifier) {
+ return &advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NameIsKeywordIdentifier,
+ Title: checker.title,
+ Content: fmt.Sprintf("Identifier %q is a keyword and should be avoided", identifier),
+ }
+ }
+
+ return nil
+}
+
+func trimBackTicks(s string) string {
+ if len(s) < 2 {
+ return s
+ }
+ return s[1 : len(s)-1]
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 6ea15adff1..9953296b63 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -24,6 +24,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleIDXNaming,
// advisor.SchemaRuleAutoIncrementColumnNaming enforce the auto_increment column name format.
advisor.SchemaRuleAutoIncrementColumnNaming,
+ // advisor.SchemaRuleIdentifierNoKeyword enforce the identifier no keyword.
+ advisor.SchemaRuleIdentifierNoKeyword,
// advisor.SchemaRuleStatementNoSelectAll disallow 'SELECT *'.
advisor.SchemaRuleStatementNoSelectAll,
diff --git a/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml
new file mode 100644
index 0000000000..04dd176e22
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/naming_identifier_no_keyword.yaml
@@ -0,0 +1,61 @@
+- statement: CREATE TABLE `t`(a int);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: CREATE TABLE interval(id int);
+ want:
+ - status: ERROR
+ code: 201
+ title: Syntax error
+ content: "Syntax error at line 1:13 \nrelated text: CREATE TABLE interval"
+ line: 1
+ column: 13
+ details: ""
+- statement: CREATE TABLE execute(id int)
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: 'Identifier "execute" is a keyword and should be avoided'
+ line: 1
+ details: ""
+- statement: CREATE TABLE `execute`(id int)
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: 'Identifier "execute" is a keyword and should be avoided'
+ line: 1
+ details: ""
+- statement: CREATE TABLE `interval`(id int);
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: 'Identifier "interval" is a keyword and should be avoided'
+ line: 1
+ details: ""
+- statement: |
+ CREATE TABLE t(
+ id int,
+ `current_timestamp` int
+ );
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: 'Identifier "current_timestamp" is a keyword and should be avoided'
+ line: 3
+ details: ""
+- statement: CREATE TABLE `test`.`interval`(id int);
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: 'Identifier "interval" is a keyword and should be avoided'
+ line: 1
+ details: ""
diff --git a/backend/plugin/advisor/mysql/utils.go b/backend/plugin/advisor/mysql/utils.go
index 0b8f6a3819..9971c91132 100644
--- a/backend/plugin/advisor/mysql/utils.go
+++ b/backend/plugin/advisor/mysql/utils.go
@@ -5,6 +5,8 @@ import (
"regexp"
"sort"
"strings"
+
+ parser "github.com/bytebase/mysql-parser"
)
type columnSet map[string]bool
@@ -83,3 +85,14 @@ type tableData struct {
onUpdateCurrentTimeCount int
line int
}
+
+// isKeyword checks if the keyword is a MySQL keyword.
+// TODO: We should check with map instead of linear search.
+func isKeyword(suspect string) bool {
+ for _, item := range parser.Keywords80 {
+ if strings.EqualFold(suspect, item.Keyword) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 3fb1b68a34..5f46e7fbfc 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1111,6 +1111,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
return SnowflakeIdentifierNamingNoKeyword, nil
case storepb.Engine_MSSQL:
return MSSQLIdentifierNamingNoKeyword, nil
+ case storepb.Engine_MYSQL:
+ return MySQLIdentifierNamingNoKeyword, nil
}
case SchemaRuleIdentifierCase:
switch engine {