Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: diagnose for PostgreSQL/Oracle/Snowflake/SQL Server (#13693)
h3n4ldd10af801afb
diff --git a/backend/api/lsp/fs_handler.go b/backend/api/lsp/fs_handler.goindex e65a2638e7..d9183d15f7 100644--- a/backend/api/lsp/fs_handler.go+++ b/backend/api/lsp/fs_handler.go@@ -78,7 +78,6 @@ func (h *Handler) handleFileSystemRequest(ctx context.Context, conn *jsonrpc2.Coif err := fs.DidChange(¶ms); err != nil {return err}- // Beta: Parse and send diagnostics for MySQL.if h.profile.Mode == common.ReleaseModeDev {uri := params.TextDocument.URIcontent, found := fs.get(uri)diff --git a/backend/plugin/parser/pg/diagnose.go b/backend/plugin/parser/pg/diagnose.gonew file mode 100644index 0000000000..562775810b--- /dev/null+++ b/backend/plugin/parser/pg/diagnose.go@@ -0,0 +1,58 @@+package pg++import (+ "context"++ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/postgresql-parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ base.RegisterDiagnoseFunc(store.Engine_POSTGRES, Diagnose)+ base.RegisterDiagnoseFunc(store.Engine_REDSHIFT, Diagnose)+ base.RegisterDiagnoseFunc(store.Engine_RISINGWAVE, Diagnose)+}++func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ diagnostics := make([]base.Diagnostic, 0)+ syntaxError := parsePostgreSQLStatement(statement)+ if syntaxError != nil {+ diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError))+ }++ return diagnostics, nil+}++// parsePostgreSQLStatement parses the given SQL and returns the ParseResult.+// Use the PostgreSQL parser based on antlr4.+func parsePostgreSQLStatement(sql string) *base.SyntaxError {+ lexer := parser.NewPostgreSQLLexer(antlr.NewInputStream(sql))+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)+ p := parser.NewPostgreSQLParser(stream)+ lexerErrorListener := &base.ParseErrorListener{}+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = false++ _ = p.Root()++ if lexerErrorListener.Err != nil {+ return lexerErrorListener.Err+ }++ if parserErrorListener.Err != nil {+ return parserErrorListener.Err+ }++ return nil+}diff --git a/backend/plugin/parser/plsql/diagnose.go b/backend/plugin/parser/plsql/diagnose.gonew file mode 100644index 0000000000..fe40840697--- /dev/null+++ b/backend/plugin/parser/plsql/diagnose.go@@ -0,0 +1,55 @@+package plsql++import (+ "context"++ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/plsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ base.RegisterDiagnoseFunc(store.Engine_ORACLE, Diagnose)+ base.RegisterDiagnoseFunc(store.Engine_DM, Diagnose)+ base.RegisterDiagnoseFunc(store.Engine_OCEANBASE_ORACLE, Diagnose)+}++func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ diagnostics := make([]base.Diagnostic, 0)+ syntaxError := parsePLSQLStatement(statement)+ if syntaxError != nil {+ diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError))+ }++ return diagnostics, nil+}++// ParsePLSQL parses the given PLSQL.+func parsePLSQLStatement(sql string) *base.SyntaxError {+ lexer := parser.NewPlSqlLexer(antlr.NewInputStream(sql))+ stream := antlr.NewCommonTokenStream(lexer, 0)+ p := parser.NewPlSqlParser(stream)+ p.SetVersion12(true)++ lexerErrorListener := &base.ParseErrorListener{}+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &base.ParseErrorListener{}+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = false+ _ = p.Sql_script()++ if lexerErrorListener.Err != nil {+ return lexerErrorListener.Err+ }++ if parserErrorListener.Err != nil {+ return parserErrorListener.Err+ }+ return nil+}diff --git a/backend/plugin/parser/snowflake/diagnose.go b/backend/plugin/parser/snowflake/diagnose.gonew file mode 100644index 0000000000..0ae934e1cb--- /dev/null+++ b/backend/plugin/parser/snowflake/diagnose.go@@ -0,0 +1,57 @@+package snowflake++import (+ "context"++ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/snowsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ base.RegisterDiagnoseFunc(store.Engine_SNOWFLAKE, Diagnose)+}++func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ diagnostics := make([]base.Diagnostic, 0)+ syntaxError := parseSnowflakeStatement(statement)+ if syntaxError != nil {+ diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError))+ }++ return diagnostics, nil+}++func parseSnowflakeStatement(statement string) *base.SyntaxError {+ inputStream := antlr.NewInputStream(statement)+ lexer := parser.NewSnowflakeLexer(inputStream)+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)++ p := parser.NewSnowflakeParser(stream)+ lexerErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = false++ _ = p.Snowflake_file()++ if lexerErrorListener.Err != nil {+ return lexerErrorListener.Err+ }++ if parserErrorListener.Err != nil {+ return parserErrorListener.Err+ }+ return nil+}diff --git a/backend/plugin/parser/tsql/diagnose.go b/backend/plugin/parser/tsql/diagnose.gonew file mode 100644index 0000000000..e4a42d8f16--- /dev/null+++ b/backend/plugin/parser/tsql/diagnose.go@@ -0,0 +1,57 @@+package tsql++import (+ "context"++ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/tsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/proto/generated-go/store"+)++func init() {+ base.RegisterDiagnoseFunc(store.Engine_MSSQL, Diagnose)+}++func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {+ diagnostics := make([]base.Diagnostic, 0)+ syntaxError := parseTSQLStatement(statement)+ if syntaxError != nil {+ diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(syntaxError))+ }++ return diagnostics, nil+}++func parseTSQLStatement(statement string) *base.SyntaxError {+ inputStream := antlr.NewInputStream(statement)+ lexer := parser.NewTSqlLexer(inputStream)+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)++ p := parser.NewTSqlParser(stream)+ lexerErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = false++ _ = p.Tsql_file()++ if lexerErrorListener.Err != nil {+ return lexerErrorListener.Err+ }++ if parserErrorListener.Err != nil {+ return parserErrorListener.Err+ }+ return nil+}