Skip to content

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

feat(sql-review, mssql): support no keyword sql review rule of mssql (#7135)

h3n4lb309d7bfc2a1
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 9d39f34dbb..6f00c5c267 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -446,6 +446,12 @@ const (
// MSSQLNamingTableConvention is an advisor type for MSSQL table naming convention.
MSSQLNamingTableConvention Type = "bb.plugin.advisor.mssql.naming.table"
+
+ // MSSQLTableNamingNoKeyword is an advisor type for MSSQL table naming convention without keyword.
+ MSSQLTableNamingNoKeyword Type = "bb.plugin.advisor.mssql.naming.table-no-keyword"
+
+ // MSSQLIdentifierNamingNoKeyword is an advisor type for MSSQL identifier naming convention without keyword.
+ MSSQLIdentifierNamingNoKeyword Type = "bb.plugin.advisor.mssql.naming.identifier-no-keyword"
)
// Advice is the result of an advisor.
diff --git a/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go
new file mode 100644
index 0000000000..4549222719
--- /dev/null
+++ b/backend/plugin/advisor/mssql/advisor_naming_identifier_no_keyword.go
@@ -0,0 +1,110 @@
+// 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"
+ "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"
+)
+
+var (
+ _ advisor.Advisor = (*NamingIdentifierNoKeywordAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MSSQL, advisor.MSSQLIdentifierNamingNoKeyword, &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) {
+ 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 := &namingIdentifierNoKeywordChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// namingIdentifierNoKeywordChecker is the listener for identifier naming convention without keyword.
+type namingIdentifierNoKeywordChecker struct {
+ *parser.BaseTSqlParserListener
+
+ level advisor.Status
+ title string
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *namingIdentifierNoKeywordChecker) 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
+}
+
+// EnterId_ is called when production id_ is entered.
+func (l *namingIdentifierNoKeywordChecker) EnterId_(ctx *parser.Id_Context) {
+ if ctx == nil {
+ return
+ }
+
+ parent := ctx.GetParent()
+ switch parent.(type) {
+ case *parser.Column_definitionContext:
+ case *parser.Table_constraintContext:
+ case *parser.Create_schemaContext:
+ case *parser.Create_databaseContext:
+ case *parser.Create_indexContext:
+ case *parser.Table_nameContext:
+ default:
+ return
+ }
+ if ctx.GetText() == "" {
+ return
+ }
+
+ normalizedID, err := bbparser.NormalizeTSQLTableNamePart(ctx)
+ if err != nil {
+ log.Error("failed to normalize identifier", zap.Error(err))
+ return
+ }
+ if bbparser.IsTSQLKeyword(normalizedID, false) {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.NameIsKeywordIdentifier,
+ Title: l.title,
+ Content: fmt.Sprintf("Identifier [%s] is a keyword identifier and should be avoided.", normalizedID),
+ Line: ctx.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mssql/advisor_naming_table.go b/backend/plugin/advisor/mssql/advisor_naming_table.go
index ce740eb7d7..ebedd313bc 100644
--- a/backend/plugin/advisor/mssql/advisor_naming_table.go
+++ b/backend/plugin/advisor/mssql/advisor_naming_table.go
@@ -123,7 +123,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.NormalizedTSqlTableNamePart(v)
+ normalizedProcedureName, err := bbparser.NormalizeTSQLTableNamePart(v)
if err != nil {
log.Error(errors.Wrapf(err, "failed to normalize procedure name").Error())
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
new file mode 100644
index 0000000000..df83c58ee5
--- /dev/null
+++ b/backend/plugin/advisor/mssql/advisor_naming_table_no_keyword.go
@@ -0,0 +1,91 @@
+// 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"
+ "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"
+)
+
+var (
+ _ advisor.Advisor = (*NamingTableNoKeywordAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MSSQL, advisor.MSSQLTableNamingNoKeyword, &NamingTableNoKeywordAdvisor{})
+}
+
+// NamingTableNoKeywordAdvisor is the advisor checking for table naming convention without keyword..
+type NamingTableNoKeywordAdvisor struct {
+}
+
+// Check checks for table naming convention without keyword..
+func (*NamingTableNoKeywordAdvisor) 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 := &namingTableNoKeywordChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// namingTableNoKeywordChecker is the listener for table naming convention without keyword.
+type namingTableNoKeywordChecker struct {
+ *parser.BaseTSqlParserListener
+
+ level advisor.Status
+ title string
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *namingTableNoKeywordChecker) 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
+}
+
+// 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))
+ }
+ if bbparser.IsTSQLKeyword(normalizedTableName, false) {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.NameIsKeywordIdentifier,
+ Title: l.title,
+ Content: fmt.Sprintf("Table name [%s] is a reserved keyword and should be avoided.", normalizedTableName),
+ Line: tableName.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.go
index b7e91bc278..c0b83f5ca6 100644
--- a/backend/plugin/advisor/mssql/mssql_rules_test.go
+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go
@@ -12,9 +12,11 @@ func TestMSSQLRules(t *testing.T) {
snowflakeRules := []advisor.SQLReviewRuleType{
advisor.SchemaRuleStatementNoSelectAll,
advisor.SchemaRuleTableNaming,
+ advisor.SchemaRuleTableNameNoKeyword,
+ advisor.SchemaRuleIdentifierNoKeyword,
}
for _, rule := range snowflakeRules {
- advisor.RunSQLReviewRuleTest(t, rule, db.MSSQL, true /* record */)
+ advisor.RunSQLReviewRuleTest(t, rule, db.MSSQL, false /* record */)
}
}
diff --git a/backend/plugin/advisor/mssql/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/mssql/test/naming_identifier_no_keyword.yaml
new file mode 100644
index 0000000000..a2e83463ea
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/naming_identifier_no_keyword.yaml
@@ -0,0 +1,28 @@
+- statement: |-
+ CREATE TABLE [Fillfactor](Id INT, [Exec] INT);
+ CREATE INDEX [Add] ON [Fillfactor]([Exec]);
+ want:
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: Identifier [exec] is a keyword identifier and should be avoided.
+ line: 1
+ details: ""
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: Identifier [fillfactor] is a keyword identifier and should be avoided.
+ line: 1
+ details: ""
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: Identifier [add] is a keyword identifier and should be avoided.
+ line: 2
+ details: ""
+ - status: WARN
+ code: 308
+ title: naming.identifier.no-keyword
+ content: Identifier [fillfactor] is a keyword identifier and should be avoided.
+ line: 2
+ details: ""
diff --git a/backend/plugin/advisor/mssql/test/naming_table_no_keyword.yaml b/backend/plugin/advisor/mssql/test/naming_table_no_keyword.yaml
new file mode 100644
index 0000000000..a30fa4217c
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/naming_table_no_keyword.yaml
@@ -0,0 +1,24 @@
+- statement: CREATE TABLE [fillfactor] (id INT);
+ want:
+ - status: WARN
+ code: 308
+ title: naming.table.no-keyword
+ content: Table name [fillfactor] is a reserved keyword and should be avoided.
+ line: 1
+ details: ""
+- statement: CREATE TABLE MyDB.MySchema.[fillfactor] (id INT);
+ want:
+ - status: WARN
+ code: 308
+ title: naming.table.no-keyword
+ content: Table name [fillfactor] is a reserved keyword and should be avoided.
+ line: 1
+ details: ""
+- statement: CREATE TABLE Person(id INT);
+ 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 db70ba7801..789d46187d 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1143,6 +1143,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return OracleTableNamingNoKeyword, nil
case db.Snowflake:
return SnowflakeTableNamingNoKeyword, nil
+ case db.MSSQL:
+ return MSSQLTableNamingNoKeyword, nil
}
case SchemaRuleIdentifierNoKeyword:
switch engine {
@@ -1150,6 +1152,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return OracleIdentifierNamingNoKeyword, nil
case db.Snowflake:
return SnowflakeIdentifierNamingNoKeyword, nil
+ case db.MSSQL:
+ return MSSQLIdentifierNamingNoKeyword, nil
}
case SchemaRuleIdentifierCase:
switch engine {
diff --git a/backend/plugin/parser/sql/tsql.go b/backend/plugin/parser/sql/tsql.go
index f954f86b2e..80cc1571fb 100644
--- a/backend/plugin/parser/sql/tsql.go
+++ b/backend/plugin/parser/sql/tsql.go
@@ -10,6 +10,503 @@ import (
"github.com/pkg/errors"
)
+// tsqlKeywordsMap is the map of all TSQL keywords.
+// Crawled from https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-ver16.
+var tsqlKeywordsMap = map[string]bool{
+ "OVER": true,
+ "END-EXEC": true,
+ "ESCAPE": true,
+ "XMLATTRIBUTES": true,
+ "OPTION": true,
+ "SIMILAR": true,
+ "XMLTEXT": true,
+ "CHECK": true,
+ "BETWEEN": true,
+ "XMLTABLE": true,
+ "SQLSTATE": true,
+ "OLD": true,
+ "ROWCOUNT": true,
+ "VARCHAR": true,
+ "PERCENT_RANK": true,
+ "EVERY": true,
+ "ALL": true,
+ "RULE": true,
+ "PIVOT": true,
+ "CONNECTION": true,
+ "CLASS": true,
+ "PAD": true,
+ "UESCAPE": true,
+ "XMLPARSE": true,
+ "BLOB": true,
+ "EXEC": true,
+ "EXCEPTION": true,
+ "ROWGUIDCOL": true,
+ "RESULT": true,
+ "DYNAMIC": true,
+ "TEMPORARY": true,
+ "ASC": true,
+ "XMLITERATE": true,
+ "DETERMINISTIC": true,
+ "START": true,
+ "CURRENT_TRANSFORM_GROUP_FOR_TYPE": true,
+ "MODIFY": true,
+ "STATEMENT": true,
+ "PLAN": true,
+ "XMLFOREST": true,
+ "SQLWARNING": true,
+ "MATCH": true,
+ "CALLED": true,
+ "OPENXML": true,
+ "ROW": true,
+ "CURSOR": true,
+ "INTERVAL": true,
+ "ADA": true,
+ "REPLICATION": true,
+ "CUME_DIST": true,
+ "WITHIN GROUP": true,
+ "CLOSE": true,
+ "UPPER": true,
+ "REVERT": true,
+ "USING": true,
+ "DECIMAL": true,
+ "FULL": true,
+ "USER": true,
+ "TRUNCATE": true,
+ "IMMEDIATE": true,
+ "REGR_SLOPE": true,
+ "SEMANTICSIMILARITYTABLE": true,
+ "PASCAL": true,
+ "SEMANTICSIMILARITYDETAILSTABLE": true,
+ "RETURN": true,
+ "PRIOR": true,
+ "NONCLUSTERED": true,
+ "INTERSECT": true,
+ "ON": true,
+ "TRAILING": true,
+ "DISTINCT": true,
+ "FETCH": true,
+ "PARTITION": true,
+ "AT": true,
+ "TSEQUAL": true,
+ "BREADTH": true,
+ "SECTION": true,
+ "MODIFIES": true,
+ "OPERATION": true,
+ "CONSTRAINTS": true,
+ "ITERATE": true,
+ "AND": true,
+ "XMLVALIDATE": true,
+ "COUNT": true,
+ "ASYMMETRIC": true,
+ "DESCRIPTOR": true,
+ "SCOPE": true,
+ "EXTERNAL": true,
+ "FOR": true,
+ "SELECT": true,
+ "STATE": true,
+ "TERMINATE": true,
+ "EQUALS": true,
+ "XMLPI": true,
+ "ORDINALITY": true,
+ "PRESERVE": true,
+ "PRIMARY": true,
+ "TOP": true,
+ "GOTO": true,
+ "BEGIN": true,
+ "SET": true,
+ "ISOLATION": true,
+ "USAGE": true,
+ "ARRAY": true,
+ "BACKUP": true,
+ "REFERENCES": true,
+ "DEC": true,
+ "DEFERRABLE": true,
+ "WRITE": true,
+ "ROLLBACK": true,
+ "WAITFOR": true,
+ "ROWS": true,
+ "LIKE": true,
+ "ASENSITIVE": true,
+ "XMLCONCAT": true,
+ "USE": true,
+ "DAY": true,
+ "INCLUDE": true,
+ "OCTET_LENGTH": true,
+ "UNKNOWN": true,
+ "OBJECT": true,
+ "SPECIFIC": true,
+ "WORK": true,
+ "SEQUENCE": true,
+ "GRANT": true,
+ "OPENROWSET": true,
+ "LESS": true,
+ "REGR_AVGY": true,
+ "DOUBLE": true,
+ "TRY_CONVERT": true,
+ "HAVING": true,
+ "PROC": true,
+ "CLUSTERED": true,
+ "CHARACTER_LENGTH": true,
+ "GLOBAL": true,
+ "DROP": true,
+ "ELEMENT": true,
+ "IDENTITYCOL": true,
+ "LOCALTIMESTAMP": true,
+ "NOCHECK": true,
+ "OPENQUERY": true,
+ "LAST": true,
+ "LOCATOR": true,
+ "COVAR_POP": true,
+ "DELETE": true,
+ "SYSTEM_USER": true,
+ "DESTRUCTOR": true,
+ "UNDER": true,
+ "WITHIN": true,
+ "WITH": true,
+ "NO": true,
+ "NCLOB": true,
+ "LATERAL": true,
+ "LEVEL": true,
+ "SQLEXCEPTION": true,
+ "INNER": true,
+ "FOREIGN": true,
+ "REGR_INTERCEPT": true,
+ "XMLCOMMENT": true,
+ "FUSION": true,
+ "INTO": true,
+ "NOT": true,
+ "LOWER": true,
+ "INITIALIZE": true,
+ "SUBMULTISET": true,
+ "SCHEMA": true,
+ "PREFIX": true,
+ "TRANSLATION": true,
+ "MEMBER": true,
+ "ACTION": true,
+ "METHOD": true,
+ "EXECUTE": true,
+ "NATIONAL": true,
+ "RIGHT": true,
+ "PERCENTILE_CONT": true,
+ "OVERLAPS": true,
+ "MIN": true,
+ "VARIABLE": true,
+ "PRIVILEGES": true,
+ "DESCRIBE": true,
+ "FILE": true,
+ "KILL": true,
+ "CYCLE": true,
+ "SQLCODE": true,
+ "READS": true,
+ "REGR_SXY": true,
+ "LINENO": true,
+ "INTERSECTION": true,
+ "REGR_SXX": true,
+ "BREAK": true,
+ "CALL": true,
+ "RETURNS": true,
+ "LN": true,
+ "MODULE": true,
+ "DISCONNECT": true,
+ "DATABASE": true,
+ "PERCENTILE_DISC": true,
+ "CURRENT_DATE": true,
+ "RELATIVE": true,
+ "DESTROY": true,
+ "LIMIT": true,
+ "VAR_POP": true,
+ "EACH": true,
+ "END": true,
+ "FALSE": true,
+ "PARAMETERS": true,
+ "IGNORE": true,
+ "GET": true,
+ "FREE": true,
+ "XMLNAMESPACES": true,
+ "ALIAS": true,
+ "REGR_COUNT": true,
+ "CONTAINSTABLE": true,
+ "SCROLL": true,
+ "RESTRICT": true,
+ "ALLOCATE": true,
+ "ONLY": true,
+ "DOMAIN": true,
+ "SMALLINT": true,
+ "DATE": true,
+ "BINARY": true,
+ "VAR_SAMP": true,
+ "TREAT": true,
+ "OPENDATASOURCE": true,
+ "THAN": true,
+ "NUMERIC": true,
+ "COMMIT": true,
+ "EXCEPT": true,
+ "FORTRAN": true,
+ "OFF": true,
+ "TIMEZONE_MINUTE": true,
+ "PREPARE": true,
+ "OUTER": true,
+ "SQL": true,
+ "FULLTEXTTABLE": true,
+ "PRECISION": true,
+ "CROSS": true,
+ "SAVE": true,
+ "LEFT": true,
+ "FILTER": true,
+ "SAVEPOINT": true,
+ "TRANSACTION": true,
+ "WHILE": true,
+ "STRUCTURE": true,
+ "READ": true,
+ "ROLE": true,
+ "VALUE": true,
+ "REVOKE": true,
+ "ZONE": true,
+ "XMLEXISTS": true,
+ "INTEGER": true,
+ "WHERE": true,
+ "SUBSTRING": true,
+ "NAMES": true,
+ "IDENTITY": true,
+ "IN": true,
+ "SEMANTICKEYPHRASETABLE": true,
+ "RELEASE": true,
+ "DATA": true,
+ "AGGREGATE": true,
+ "DENY": true,
+ "TRIM": true,
+ "SESSION": true,
+ "INDICATOR": true,
+ "EXTRACT": true,
+ "NATURAL": true,
+ "DISTRIBUTED": true,
+ "TEXTSIZE": true,
+ "CAST": true,
+ "CARDINALITY": true,
+ "BROWSE": true,
+ "NEW": true,
+ "BOOLEAN": true,
+ "OCCURRENCES_REGEX": true,
+ "XMLSERIALIZE": true,
+ "CURRENT_DEFAULT_TRANSFORM_GROUP": true,
+ "INDEX": true,
+ "INSENSITIVE": true,
+ "CONVERT": true,
+ "SOME": true,
+ "GENERAL": true,
+ "KEY": true,
+ "VARYING": true,
+ "CONTAINS": true,
+ "OUT": true,
+ "MAX": true,
+ "REFERENCING": true,
+ "PREORDER": true,
+ "OR": true,
+ "HOLDLOCK": true,
+ "STDDEV_POP": true,
+ "XMLCAST": true,
+ "INPUT": true,
+ "ASSERTION": true,
+ "DEALLOCATE": true,
+ "XMLDOCUMENT": true,
+ "CHAR": true,
+ "INOUT": true,
+ "DEFERRED": true,
+ "READTEXT": true,
+ "COLLATION": true,
+ "STATIC": true,
+ "FREETEXTTABLE": true,
+ "THEN": true,
+ "GROUPING": true,
+ "SQLERROR": true,
+ "NORMALIZE": true,
+ "BEFORE": true,
+ "SECOND": true,
+ "DBCC": true,
+ "FIRST": true,
+ "ROUTINE": true,
+ "COLUMN": true,
+ "ABSOLUTE": true,
+ "WRITETEXT": true,
+ "IS": true,
+ "NCHAR": true,
+ "CONSTRAINT": true,
+ "ERRLVL": true,
+ "DUMP": true,
+ "TIMEZONE_HOUR": true,
+ "YEAR": true,
+ "OVERLAY": true,
+ "ROLLUP": true,
+ "POSITION": true,
+ "BIT": true,
+ "SYSTEM": true,
+ "REF": true,
+ "CHAR_LENGTH": true,
+ "GROUP": true,
+ "LOAD": true,
+ "IDENTITY_INSERT": true,
+ "TRAN": true,
+ "XMLELEMENT": true,
+ "XMLBINARY": true,
+ "SUBSTRING_REGEX": true,
+ "LABEL": true,
+ "ARE": true,
+ "REGR_R2": true,
+ "SENSITIVE": true,
+ "INITIALLY": true,
+ "CHECKPOINT": true,
+ "DICTIONARY": true,
+ "GO": true,
+ "REAL": true,
+ "IF": true,
+ "HOST": true,
+ "CASCADE": true,
+ "DEPTH": true,
+ "ALTER": true,
+ "TRUE": true,
+ "CURRENT_ROLE": true,
+ "TO": true,
+ "DISK": true,
+ "OUTPUT": true,
+ "STATISTICS": true,
+ "FLOAT": true,
+ "CURRENT_PATH": true,
+ "NONE": true,
+ "VIEW": true,
+ "LARGE": true,
+ "SHUTDOWN": true,
+ "SEARCH": true,
+ "DECLARE": true,
+ "CURRENT_CATALOG": true,
+ "POSTFIX": true,
+ "WITHOUT": true,
+ "XMLQUERY": true,
+ "WINDOW": true,
+ "PERCENT": true,
+ "COMPUTE": true,
+ "STDDEV_SAMP": true,
+ "SYMMETRIC": true,
+ "CONSTRUCTOR": true,
+ "COLLECT": true,
+ "PARAMETER": true,
+ "COALESCE": true,
+ "SETS": true,
+ "CREATE": true,
+ "TABLE": true,
+ "DIAGNOSTICS": true,
+ "CURRENT_SCHEMA": true,
+ "MERGE": true,
+ "AUTHORIZATION": true,
+ "SUM": true,
+ "SPACE": true,
+ "CLOB": true,
+ "CORRESPONDING": true,
+ "CASE": true,
+ "FILLFACTOR": true,
+ "LEADING": true,
+ "COMPLETION": true,
+ "CUBE": true,
+ "UNION": true,
+ "BULK": true,
+ "LOCAL": true,
+ "ADD": true,
+ "DEREF": true,
+ "DESC": true,
+ "OPEN": true,
+ "RESTORE": true,
+ "JOIN": true,
+ "MONTH": true,
+ "POSITION_REGEX": true,
+ "MAP": true,
+ "REGR_SYY": true,
+ "HOUR": true,
+ "VALUES": true,
+ "TRANSLATE_REGEX": true,
+ "FOUND": true,
+ "RANGE": true,
+ "BOTH": true,
+ "INSERT": true,
+ "COVAR_SAMP": true,
+ "CURRENT_USER": true,
+ "AVG": true,
+ "REGR_AVGX": true,
+ "LANGUAGE": true,
+ "SIZE": true,
+ "PROCEDURE": true,
+ "ORDER": true,
+ "CORR": true,
+ "NULL": true,
+ "SPECIFICTYPE": true,
+ "SESSION_USER": true,
+ "RECURSIVE": true,
+ "PUBLIC": true,
+ "AS": true,
+ "RECONFIGURE": true,
+ "CONDITION": true,
+ "TIMESTAMP": true,
+ "OFFSETS": true,
+ "SQLCA": true,
+ "INT": true,
+ "TIME": true,
+ "LIKE_REGEX": true,
+ "RAISERROR": true,
+ "CURRENT_TIME": true,
+ "PARTIAL": true,
+ "ATOMIC": true,
+ "FUNCTION": true,
+ "UPDATE": true,
+ "ADMIN": true,
+ "MOD": true,
+ "EXISTS": true,
+ "SECURITYAUDIT": true,
+ "CONTINUE": true,
+ "UNIQUE": true,
+ "CONNECT": true,
+ "LOCALTIME": true,
+ "COLLATE": true,
+ "CASCADED": true,
+ "UNNEST": true,
+ "ELSE": true,
+ "CURRENT": true,
+ "EXIT": true,
+ "FREETEXT": true,
+ "UPDATETEXT": true,
+ "PATH": true,
+ "XMLAGG": true,
+ "MULTISET": true,
+ "CHARACTER": true,
+ "WHEN": true,
+ "TRIGGER": true,
+ "WIDTH_BUCKET": true,
+ "AFTER": true,
+ "TABLESAMPLE": true,
+ "TRANSLATE": true,
+ "DEFAULT": true,
+ "HOLD": true,
+ "BY": true,
+ "CURRENT_TIMESTAMP": true,
+ "PRINT": true,
+ "BIT_LENGTH": true,
+ "CATALOG": true,
+ "UNPIVOT": true,
+ "WHENEVER": true,
+ "FROM": true,
+ "SETUSER": true,
+ "OF": true,
+ "NULLIF": true,
+ "MINUTE": true,
+ "NEXT": true,
+ "ANY": true,
+}
+
+// IsTSQLKeyword returns true if the given keyword is a TSQL keywords.
+func IsTSQLKeyword(keyword string, caseSensitive bool) bool {
+ if !caseSensitive {
+ keyword = strings.ToUpper(keyword)
+ }
+ return tsqlKeywordsMap[keyword]
+}
+
// ParseTSQL parses the given SQL statement by using antlr4. Returns the AST and token stream if no error.
func ParseTSQL(statement string) (antlr.Tree, error) {
statement = strings.TrimRight(statement, " \t\n\r\f;") + "\n;"
@@ -42,10 +539,10 @@ func ParseTSQL(statement string) (antlr.Tree, error) {
return tree, nil
}
-// NormalizedTSqlTableNamePart returns the normalized table name part.
+// NormalizeTSQLTableNamePart returns the normalized table name part.
// 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 NormalizedTSqlTableNamePart(part tsqlparser.IId_Context) (string, error) {
+func NormalizeTSQLTableNamePart(part tsqlparser.IId_Context) (string, error) {
if part == nil {
return "", nil
}
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index ae8f5a3bed..4bb5167bae 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -321,12 +321,14 @@ ruleList:
engineList:
- ORACLE
- SNOWFLAKE
+ - MSSQL
componentList: []
- type: naming.identifier.no-keyword
category: NAMING
engineList:
- ORACLE
- SNOWFLAKE
+ - MSSQL
componentList: []
- type: naming.identifier.case
category: NAMING