Skip to content

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

feat(sql-review, mssql): support drop table naming convention sql review rule for mssql (#7152)

h3n4lb751dd8e9000
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 71a599f346..acf5f12ac2 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -458,6 +458,12 @@ const (
// MSSQLColumnMaximumVarcharLength is an advisor type for MSSQL maximum varchar length.
MSSQLColumnMaximumVarcharLength Type = "bb.plugin.advisor.mssql.column.maximum-varchar-length"
+
+ // MSSQLTableDropNamingConvention is an advisor type for MSSQL table drop with naming convention.
+ MSSQLTableDropNamingConvention Type = "bb.plugin.advisor.mssql.table.drop-naming-convention"
+
+ // MSSQLTableRequirePK is an advisor type for MSSQL table require primary key.
+ MSSQLTableRequirePK Type = "bb.plugin.advisor.mssql.table.require-pk"
)
// Advice is the result of an advisor.
diff --git a/backend/plugin/advisor/mssql/advisor.go b/backend/plugin/advisor/mssql/advisor.go
new file mode 100644
index 0000000000..c60d6c05ea
--- /dev/null
+++ b/backend/plugin/advisor/mssql/advisor.go
@@ -0,0 +1,11 @@
+// Package mssql is the advisor for MSSQL database.
+package mssql
+
+// currentConstraintAction is the action of current constraint.
+type currentConstraintAction int
+
+const (
+ currentConstraintActionNone currentConstraintAction = iota
+ currentConstraintActionAdd
+ currentConstraintActionDrop
+)
diff --git a/backend/plugin/advisor/mssql/advisor_column_maximum_varchar_length.go b/backend/plugin/advisor/mssql/advisor_column_maximum_varchar_length.go
index c600276ef4..4393bc273d 100644
--- a/backend/plugin/advisor/mssql/advisor_column_maximum_varchar_length.go
+++ b/backend/plugin/advisor/mssql/advisor_column_maximum_varchar_length.go
@@ -97,10 +97,7 @@ func (l *columnMaximumVarcharLengthChecker) EnterData_type(ctx *parser.Data_type
currentLength = math.MaxInt32 // 2 ^ 31 - 1
line = ctx.MAX().GetSymbol().GetLine()
} else if ctx.GetExt_type() != nil && ctx.GetScale() != nil && ctx.GetPrec() == nil && ctx.GetInc() == nil {
- normalizedTypeString, err := bbparser.NormalizeTSQLTableNamePart(ctx.GetExt_type())
- if err != nil {
- log.Error("failed to normalize type string", zap.Error(err))
- }
+ normalizedTypeString := bbparser.NormalizeTSQLIdentifier(ctx.GetExt_type())
if _, ok := l.checkTypeString[normalizedTypeString]; !ok {
return
}
@@ -111,10 +108,7 @@ func (l *columnMaximumVarcharLengthChecker) EnterData_type(ctx *parser.Data_type
currentLength = length
line = ctx.GetScale().GetLine()
} else if ctx.GetUnscaled_type() != nil {
- normalizedTypeString, err := bbparser.NormalizeTSQLTableNamePart(ctx.GetUnscaled_type())
- if err != nil {
- log.Error("failed to normalize type string", zap.Error(err))
- }
+ normalizedTypeString := bbparser.NormalizeTSQLIdentifier(ctx.GetUnscaled_type())
if _, ok := l.checkTypeString[normalizedTypeString]; !ok {
return
}
diff --git a/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go
index 4549222719..895445c545 100644
--- a/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go
+++ b/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go
@@ -7,9 +7,7 @@ import (
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/tsql-parser"
"github.com/pkg/errors"
- "go.uber.org/zap"
- "github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/db"
bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
@@ -93,11 +91,7 @@ func (l *namingIdentifierNoKeywordChecker) EnterId_(ctx *parser.Id_Context) {
return
}
- normalizedID, err := bbparser.NormalizeTSQLTableNamePart(ctx)
- if err != nil {
- log.Error("failed to normalize identifier", zap.Error(err))
- return
- }
+ normalizedID := bbparser.NormalizeTSQLIdentifier(ctx)
if bbparser.IsTSQLKeyword(normalizedID, false) {
l.adviceList = append(l.adviceList, advisor.Advice{
Status: l.level,
diff --git a/backend/plugin/advisor/mssql/advisor_naming_table.go b/backend/plugin/advisor/mssql/advisor_naming_table.go
index ebedd313bc..72b5ee3158 100644
--- a/backend/plugin/advisor/mssql/advisor_naming_table.go
+++ b/backend/plugin/advisor/mssql/advisor_naming_table.go
@@ -11,7 +11,6 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/db"
bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
@@ -123,11 +122,7 @@ func (l *namingTableListener) EnterExecute_body(ctx *parser.Execute_bodyContext)
}
v := ctx.Func_proc_name_server_database_schema().Func_proc_name_database_schema().Func_proc_name_schema().GetProcedure()
- normalizedProcedureName, err := bbparser.NormalizeTSQLTableNamePart(v)
- if err != nil {
- log.Error(errors.Wrapf(err, "failed to normalize procedure name").Error())
- return
- }
+ normalizedProcedureName := bbparser.NormalizeTSQLIdentifier(v)
if normalizedProcedureName != "sp_rename" {
return
}
diff --git a/backend/plugin/advisor/mssql/advisor_naming_table_no_keyword.go b/backend/plugin/advisor/mssql/advisor_naming_table_no_keyword.go
index df83c58ee5..e90a0122c4 100644
--- a/backend/plugin/advisor/mssql/advisor_naming_table_no_keyword.go
+++ b/backend/plugin/advisor/mssql/advisor_naming_table_no_keyword.go
@@ -7,9 +7,7 @@ import (
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/tsql-parser"
"github.com/pkg/errors"
- "go.uber.org/zap"
- "github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/db"
bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
@@ -75,10 +73,7 @@ func (l *namingTableNoKeywordChecker) generateAdvice() ([]advisor.Advice, error)
// EnterCreate_table is called when production create_table is entered.
func (l *namingTableNoKeywordChecker) EnterCreate_table(ctx *parser.Create_tableContext) {
tableName := ctx.Table_name().GetTable()
- normalizedTableName, err := bbparser.NormalizeTSQLTableNamePart(tableName)
- if err != nil {
- log.Error("failed to normalize table name", zap.Error(err))
- }
+ normalizedTableName := bbparser.NormalizeTSQLIdentifier(tableName)
if bbparser.IsTSQLKeyword(normalizedTableName, false) {
l.adviceList = append(l.adviceList, advisor.Advice{
Status: l.level,
diff --git a/backend/plugin/advisor/mssql/advisor_table_drop_naming_convention.go b/backend/plugin/advisor/mssql/advisor_table_drop_naming_convention.go
new file mode 100644
index 0000000000..19687be27d
--- /dev/null
+++ b/backend/plugin/advisor/mssql/advisor_table_drop_naming_convention.go
@@ -0,0 +1,99 @@
+// Package mssql is the advisor for MSSQL database.
+package mssql
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/tsql-parser"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+)
+
+var (
+ _ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MSSQL, advisor.MSSQLTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+}
+
+// TableDropNamingConventionAdvisor is the advisor checking for table drop with naming convention..
+type TableDropNamingConventionAdvisor struct {
+}
+
+// Check checks for table drop with naming convention..
+func (*TableDropNamingConventionAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ tree, ok := ctx.AST.(antlr.Tree)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Tree")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ format, _, err := advisor.UnmarshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ listener := &tableDropNamingConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// tableDropNamingConventionChecker is the listener for table drop with naming convention.
+type tableDropNamingConventionChecker struct {
+ *parser.BaseTSqlParserListener
+
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *tableDropNamingConventionChecker) generateAdvice() ([]advisor.Advice, error) {
+ if len(l.adviceList) == 0 {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return l.adviceList, nil
+}
+
+// EnterDrop_table is called when production drop_table is entered.
+func (l *tableDropNamingConventionChecker) EnterDrop_table(ctx *parser.Drop_tableContext) {
+ allTableNames := ctx.AllTable_name()
+ for _, tableName := range allTableNames {
+ table := tableName.GetTable()
+ if table == nil {
+ continue
+ }
+ normalizedTableName := bbparser.NormalizeTSQLIdentifier(table)
+ if !l.format.MatchString(normalizedTableName) {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: l.title,
+ Content: fmt.Sprintf("[%s] mismatches drop table naming convention, naming format should be %q", normalizedTableName, l.format),
+ Line: table.GetStart().GetLine(),
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/mssql/advisor_table_require_pk.go b/backend/plugin/advisor/mssql/advisor_table_require_pk.go
new file mode 100644
index 0000000000..3f8e70984a
--- /dev/null
+++ b/backend/plugin/advisor/mssql/advisor_table_require_pk.go
@@ -0,0 +1,171 @@
+// Package mssql is the advisor for MSSQL database.
+package mssql
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/tsql-parser"
+ "github.com/pkg/errors"
+
+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*TableRequirePkAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MSSQL, advisor.MSSQLTableRequirePK, &TableRequirePkAdvisor{})
+}
+
+// TableRequirePkAdvisor is the advisor checking for table require primary key..
+type TableRequirePkAdvisor struct {
+}
+
+// Check checks for table require primary key..
+func (*TableRequirePkAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ tree, ok := ctx.AST.(antlr.Tree)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Tree")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ listener := &tableRequirePkChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ currentNormalizedTableName: "",
+ currentConstraintAction: currentConstraintActionNone,
+ tableHasPrimaryKey: make(map[string]bool),
+ tableOriginalName: make(map[string]string),
+ tableLine: make(map[string]int),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// tableRequirePkChecker is the listener for table require primary key.
+type tableRequirePkChecker struct {
+ *parser.BaseTSqlParserListener
+
+ level advisor.Status
+ title string
+
+ currentNormalizedTableName string
+ currentConstraintAction currentConstraintAction
+
+ // tableHasPrimaryKey is a map from normalized table name to whether the table has primary key.
+ tableHasPrimaryKey map[string]bool
+ // tableOriginalName is a map from normalized table name to the original table name.
+ tableOriginalName map[string]string
+ // tableLine is a map from normalized table name to the line number of the table.
+ tableLine map[string]int
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *tableRequirePkChecker) generateAdvice() ([]advisor.Advice, error) {
+ for tableName, hasPK := range l.tableHasPrimaryKey {
+ if !hasPK {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.TableNoPK,
+ Title: l.title,
+ Content: fmt.Sprintf("Table %s requires PRIMARY KEY.", l.tableOriginalName[tableName]),
+ Line: l.tableLine[tableName],
+ })
+ }
+ }
+ if len(l.adviceList) == 0 {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+
+ return l.adviceList, nil
+}
+
+// EnterCreate_table will be called when entering "CREATE TABLE" statement.
+func (l *tableRequirePkChecker) EnterCreate_table(ctx *parser.Create_tableContext) {
+ tableName := ctx.Table_name()
+ if tableName == nil {
+ return
+ }
+ normalizedTableName := bbparser.NormalizeTSQLTableName(tableName, "" /* fallbackDatabase */, "dbo" /* fallbackSchema */, false /* caseSensitive */)
+
+ l.tableHasPrimaryKey[normalizedTableName] = false
+ l.tableOriginalName[normalizedTableName] = tableName.GetText()
+ l.tableLine[normalizedTableName] = tableName.GetStart().GetLine()
+
+ l.currentNormalizedTableName = normalizedTableName
+ l.currentConstraintAction = currentConstraintActionAdd
+}
+
+func (l *tableRequirePkChecker) ExitCreate_table(*parser.Create_tableContext) {
+ l.currentNormalizedTableName = ""
+ l.currentConstraintAction = currentConstraintActionNone
+}
+
+// EnterColumn_def_table_constraints will be called when entering "column_def_table_constraints" rule.
+func (l *tableRequirePkChecker) EnterColumn_def_table_constraints(ctx *parser.Column_def_table_constraintsContext) {
+ if l.currentNormalizedTableName == "" {
+ return
+ }
+
+ allColumnDefTableConstraints := ctx.AllColumn_def_table_constraint()
+ for _, columnDefTableConstraint := range allColumnDefTableConstraints {
+ if v := columnDefTableConstraint.Column_definition(); v != nil {
+ allColumnDefinitionElements := v.AllColumn_definition_element()
+ for _, columnDefinitionElement := range allColumnDefinitionElements {
+ if v := columnDefinitionElement.Column_constraint(); v != nil {
+ if v.PRIMARY() != nil {
+ if l.currentConstraintAction == currentConstraintActionAdd {
+ l.tableHasPrimaryKey[l.currentNormalizedTableName] = true
+ }
+ return
+ }
+ }
+ }
+ } else if v := columnDefTableConstraint.Table_constraint(); v != nil {
+ if v.PRIMARY() != nil {
+ if l.currentConstraintAction == currentConstraintActionAdd {
+ l.tableHasPrimaryKey[l.currentNormalizedTableName] = true
+ }
+ return
+ }
+ }
+ }
+}
+
+func (l *tableRequirePkChecker) EnterAlter_table(ctx *parser.Alter_tableContext) {
+ tableName := ctx.Table_name(0)
+ if tableName == nil {
+ return
+ }
+ normalizedTableName := bbparser.NormalizeTSQLTableName(tableName, "" /* fallbackDatabase */, "dbo" /* fallbackSchema */, false /* caseSensitive */)
+ if ctx.ADD() != nil && ctx.Column_def_table_constraints() != nil {
+ l.currentNormalizedTableName = normalizedTableName
+ l.currentConstraintAction = currentConstraintActionAdd
+ } else if ctx.DROP() != nil && ctx.CONSTRAINT() != nil && ctx.GetConstraint() != nil {
+ l.currentNormalizedTableName = normalizedTableName
+ l.currentConstraintAction = currentConstraintActionDrop
+ }
+}
+
+func (l *tableRequirePkChecker) ExitAlter_table(*parser.Alter_tableContext) {
+ l.currentNormalizedTableName = ""
+ l.currentConstraintAction = currentConstraintActionNone
+}
diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.go
index 565fbefe6a..aede69d88f 100644
--- a/backend/plugin/advisor/mssql/mssql_rules_test.go
+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go
@@ -16,6 +16,8 @@ func TestMSSQLRules(t *testing.T) {
advisor.SchemaRuleIdentifierNoKeyword,
advisor.SchemaRuleStatementRequireWhere,
advisor.SchemaRuleColumnMaximumVarcharLength,
+ advisor.SchemaRuleTableDropNamingConvention,
+ advisor.SchemaRuleTableRequirePK,
}
for _, rule := range snowflakeRules {
diff --git a/backend/plugin/advisor/mssql/test/table_drop_naming_convention.yaml b/backend/plugin/advisor/mssql/test/table_drop_naming_convention.yaml
new file mode 100644
index 0000000000..4af9c3e4f9
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/table_drop_naming_convention.yaml
@@ -0,0 +1,24 @@
+- statement: DROP TABLE IF EXISTS [Foo_delete];
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: DROP TABLE Foo;
+ want:
+ - status: WARN
+ code: 603
+ title: table.drop-naming-convention
+ content: '[foo] mismatches drop table naming convention, naming format should be "_delete$"'
+ line: 1
+ details: ""
+- statement: DROP TABLE [Foo], [Bar_delete];
+ want:
+ - status: WARN
+ code: 603
+ title: table.drop-naming-convention
+ content: '[foo] mismatches drop table naming convention, naming format should be "_delete$"'
+ line: 1
+ details: ""
diff --git a/backend/plugin/advisor/mssql/test/table_require_pk.yaml b/backend/plugin/advisor/mssql/test/table_require_pk.yaml
new file mode 100644
index 0000000000..f20a7dcc9b
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/table_require_pk.yaml
@@ -0,0 +1,45 @@
+- statement: |-
+ CREATE TABLE MySchema.MyTable (
+ Id INT NOT NULL,
+ Name VARCHAR(50) NOT NULL
+ );
+ want:
+ - status: WARN
+ code: 601
+ title: table.require-pk
+ content: Table MySchema.MyTable requires PRIMARY KEY.
+ line: 1
+ details: ""
+- statement: |-
+ CREATE TABLE MySchema.MyTable (
+ Id INT NOT NULL PRIMARY KEY,
+ Name VARCHAR(50) NOT NULL
+ );
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: |-
+ CREATE TABLE MySchema.MyTable (
+ Id INT NOT NULL,
+ Name VARCHAR(50) NOT NULL,
+ PRIMARY KEY (Id)
+ );
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: ALTER TABLE MySchema.MyTable ADD CONSTRAINT PK_MyTable PRIMARY KEY (Id);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 0a613b29c0..f686522387 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1288,6 +1288,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return OracleTableRequirePK, nil
case db.Snowflake:
return SnowflakeTableRequirePK, nil
+ case db.MSSQL:
+ return MSSQLTableRequirePK, nil
}
case SchemaRuleTableNoFK:
switch engine {
@@ -1308,6 +1310,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return PostgreSQLTableDropNamingConvention, nil
case db.Snowflake:
return SnowflakeTableDropNamingConvention, nil
+ case db.MSSQL:
+ return MSSQLTableDropNamingConvention, nil
}
case SchemaRuleTableCommentConvention:
switch engine {
diff --git a/backend/plugin/parser/sql/tsql.go b/backend/plugin/parser/sql/tsql.go
index 80cc1571fb..8f112e9ef8 100644
--- a/backend/plugin/parser/sql/tsql.go
+++ b/backend/plugin/parser/sql/tsql.go
@@ -2,12 +2,12 @@
package parser
import (
+ "fmt"
"strings"
"unicode"
"github.com/antlr4-go/antlr/v4"
tsqlparser "github.com/bytebase/tsql-parser"
- "github.com/pkg/errors"
)
// tsqlKeywordsMap is the map of all TSQL keywords.
@@ -539,25 +539,47 @@ func ParseTSQL(statement string) (antlr.Tree, error) {
return tree, nil
}
-// NormalizeTSQLTableNamePart returns the normalized table name part.
+// NormalizeTSQLTableName returns the normalized table name.
+func NormalizeTSQLTableName(ctx tsqlparser.ITable_nameContext, fallbackDatabaseName, fallbackSchemaName string, _ bool) string {
+ database := fallbackDatabaseName
+ schema := fallbackSchemaName
+ table := ""
+ if d := ctx.GetDatabase(); d != nil {
+ if id := NormalizeTSQLIdentifier(d); id != "" {
+ database = id
+ }
+ }
+ if s := ctx.GetSchema(); s != nil {
+ if id := NormalizeTSQLIdentifier(s); id != "" {
+ schema = id
+ }
+ }
+ if t := ctx.GetTable(); t != nil {
+ if id := NormalizeTSQLIdentifier(t); id != "" {
+ table = id
+ }
+ }
+ return fmt.Sprintf("%s.%s.%s", database, schema, table)
+}
+
+// NormalizeTSQLIdentifier returns the normalized identifier.
// https://learn.microsoft.com/zh-cn/sql/relational-databases/databases/database-identifiers?view=sql-server-ver15
// TODO(zp): currently, we returns the lower case of the part, we may need to get the CI/CS from the server/database.
-func NormalizeTSQLTableNamePart(part tsqlparser.IId_Context) (string, error) {
+func NormalizeTSQLIdentifier(part tsqlparser.IId_Context) string {
if part == nil {
- return "", nil
+ return ""
}
text := part.GetText()
if text == "" {
- return "", nil
+ return ""
}
if text[0] == '[' && text[len(text)-1] == ']' {
text = text[1 : len(text)-1]
}
- var sb strings.Builder
+
+ s := ""
for _, r := range text {
- if _, err := sb.WriteRune(unicode.ToLower(r)); err != nil {
- return "", errors.Wrapf(err, "failed to write rune: %q", r)
- }
+ s += string(unicode.ToLower(r))
}
- return sb.String(), nil
+ return s
}
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 0e1f40da01..47ec73a9cf 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -23,6 +23,7 @@ ruleList:
- ORACLE
- OCEANBASE
- SNOWFLAKE
+ - MSSQL
componentList: []
- type: table.no-foreign-key
category: TABLE
@@ -42,6 +43,7 @@ ruleList:
- POSTGRES
- OCEANBASE
- SNOWFLAKE
+ - MSSQL
componentList:
- key: format
payload: