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: add pg antlr parse result (#8820)
zll600cfb484070baa
diff --git a/backend/plugin/parser/pg/pg.go b/backend/plugin/parser/pg/pg.goindex c5a761e0cd..9d8400ac49 100644--- a/backend/plugin/parser/pg/pg.go+++ b/backend/plugin/parser/pg/pg.go@@ -11,9 +11,14 @@ import ("github.com/bytebase/bytebase/backend/plugin/parser/base")-// ParsePostgreSQL parses the given SQL and returns the AST tree.+type ParseResult struct {+ Tree antlr.Tree+ Tokens *antlr.CommonTokenStream+}++// ParsePostgreSQL parses the given SQL and returns the ParseResult.// Use the PostgreSQL parser based on antlr4.-func ParsePostgreSQL(sql string) (antlr.Tree, error) {+func ParsePostgreSQL(sql string) (*ParseResult, error) {lexer := parser.NewPostgreSQLLexer(antlr.NewInputStream(sql))stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)p := parser.NewPostgreSQLParser(stream)@@ -37,7 +42,12 @@ func ParsePostgreSQL(sql string) (antlr.Tree, error) {return nil, parserErrorListener.Err}- return tree, nil+ result := &ParseResult{+ Tree: tree,+ Tokens: stream,+ }++ return result, nil}// NormalizePostgreSQLAnyName normalizes the given any name.diff --git a/backend/plugin/parser/pg/pg_test.go b/backend/plugin/parser/pg/pg_test.goindex 7604a2cd9a..e2862ad2ed 100644--- a/backend/plugin/parser/pg/pg_test.go+++ b/backend/plugin/parser/pg/pg_test.go@@ -25,8 +25,10 @@ func TestPostgreSQLParser(t *testing.T) {}for _, test := range tests {- tree, err := ParsePostgreSQL(test.statement)- _, _ = tree.(*parser.RootContext)+ res, err := ParsePostgreSQL(test.statement)+ if res != nil {+ _, _ = res.Tree.(*parser.RootContext)+ }if test.errorMessage == "" {require.NoError(t, err)} else {diff --git a/backend/plugin/schema-engine/pg/structurer.go b/backend/plugin/schema-engine/pg/structurer.goindex b98d77d093..9b30bf88f8 100644--- a/backend/plugin/schema-engine/pg/structurer.go+++ b/backend/plugin/schema-engine/pg/structurer.go@@ -771,21 +771,27 @@ type designSchemaGenerator struct {// GetDesignSchema returns the schema string for the design schema.func GetDesignSchema(baselineSchema string, to *v1pb.DatabaseMetadata) (string, error) {toState := convertToDatabaseState(to)- tree, err := pgparser.ParsePostgreSQL(baselineSchema)+ parseResult, err := pgparser.ParsePostgreSQL(baselineSchema)if err != nil {return "", err}+ if parseResult == nil {+ return "", nil+ }+ if parseResult.Tree == nil {+ return "", nil+ }listener := &designSchemaGenerator{lastTokenIndex: 0,to: toState,}- antlr.ParseTreeWalkerDefault.Walk(listener, tree)+ antlr.ParseTreeWalkerDefault.Walk(listener, parseResult.Tree)if listener.err != nil {return "", listener.err}- root, ok := tree.(*postgres.RootContext)+ root, ok := parseResult.Tree.(*postgres.RootContext)if !ok {return "", errors.Errorf("failed to convert to RootContext")}