Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: introduce PLSQL parser (#5925)
rebelicea78daf35047a
diff --git a/backend/plugin/parser/sql/plsql.go b/backend/plugin/parser/sql/plsql.gonew file mode 100644index 0000000000..c532f6d044--- /dev/null+++ b/backend/plugin/parser/sql/plsql.go@@ -0,0 +1,73 @@+package parser++import (+ "errors"+ "strconv"+ "strings"++ "github.com/antlr/antlr4/runtime/Go/antlr/v4"++ parser "github.com/bytebase/plsql-parser"+)++// PLSQLErrorListener is a custom error listener for PLSQL parser.+type PLSQLErrorListener struct {+ errors []string+}++// NewPLSQLErrorListener creates a new PLSQLErrorListener.+func NewPLSQLErrorListener() *PLSQLErrorListener {+ return &PLSQLErrorListener{}+}++// SyntaxError returns the errors.+func (l *PLSQLErrorListener) SyntaxError(_ antlr.Recognizer, _ any, line, column int, msg string, _ antlr.RecognitionException) {+ if len(msg) > 1024 {+ msg = msg[:1024]+ }+ l.errors = append(l.errors, "line "+strconv.Itoa(line)+":"+strconv.Itoa(column)+" "+msg)+}++// ReportAmbiguity reports an ambiguity.+func (*PLSQLErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs antlr.ATNConfigSet) {+ antlr.ConsoleErrorListenerINSTANCE.ReportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs)+}++// ReportAttemptingFullContext reports an attempting full context.+func (*PLSQLErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs antlr.ATNConfigSet) {+ antlr.ConsoleErrorListenerINSTANCE.ReportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs)+}++// ReportContextSensitivity reports a context sensitivity.+func (*PLSQLErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs antlr.ATNConfigSet) {+ antlr.ConsoleErrorListenerINSTANCE.ReportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs)+}++// ParsePLSQL parses the given PLSQL.+func ParsePLSQL(sql string) (antlr.Tree, error) {+ lexer := parser.NewPlSqlLexer(antlr.NewInputStream(sql))+ steam := antlr.NewCommonTokenStream(lexer, 0)+ p := parser.NewPlSqlParser(steam)+ p.SetVersion12(true)++ lexerErrorListener := &PLSQLErrorListener{}+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &PLSQLErrorListener{}+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = true+ tree := p.Sql_script()++ if len(lexerErrorListener.errors) > 0 {+ return nil, errors.New(strings.Join(lexerErrorListener.errors, " \n"))+ }++ if len(parserErrorListener.errors) > 0 {+ return nil, errors.New(strings.Join(parserErrorListener.errors, " \n"))+ }++ return tree, nil+}diff --git a/backend/plugin/parser/sql/plsql_test.go b/backend/plugin/parser/sql/plsql_test.gonew file mode 100644index 0000000000..35baa09fe4--- /dev/null+++ b/backend/plugin/parser/sql/plsql_test.go@@ -0,0 +1,34 @@+package parser++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestPLSQLParser(t *testing.T) {+ tests := []struct {+ statement string+ errorMessage string+ }{+ {+ statement: "CREATE TABLE t1 (c1 NUMBER(10,2), c2 VARCHAR2(10));",+ },+ {+ statement: "SELECT * FROM t1;",+ },+ {+ statement: "SELECT * FROM t1 WHERE c1 = ",+ errorMessage: "line 1:26 extraneous input '=' expecting {<EOF>, 'ABORT', 'ABS', 'ABSENT', 'ACCESS', 'ACCESSED', 'ACCOUNT', 'ACL', 'ACOS', 'ACROSS', 'ACTION', 'ACTIONS', 'ACTIVATE', 'ACTIVE', 'ACTIVE_COMPONENT', 'ACTIVE_DATA', 'ACTIVE_FUNCTION', 'ACTIVE_TAG', 'ACTIVITY', 'ADAPTIVE_PLAN', 'ADD', 'ADD_COLUMN', 'ADD_GROUP', 'ADD_MONTHS', 'ADJ_DATE', 'ADMIN', 'ADMINISTER', 'ADMINISTRATOR', 'ADVANCED', 'ADVISE', 'ADVISOR', 'AFD_DISKSTRING', 'AFTER', 'AGENT', 'AGGREGATE', 'A', 'ALIAS', 'ALLOCATE', 'ALLOW', 'ALL_ROWS', 'ALTER', 'ALTERNATE', 'ALWAYS', 'ANALYTIC', 'ANALYZE', 'ANCESTOR', 'ANCILLARY', 'AND_EQUAL', 'ANOMALY', 'ANSI_REARCH', 'ANTIJOIN', 'ANYSCHEMA', 'APPEND', 'APPENDCHILDXML', 'APPEND_VALUES', 'APPLICATION', 'APPLY', 'APPROX_COUNT_DISTINCT', 'ARCHIVAL', 'ARCHIVE', 'ARCHIVED', 'ARCHIVELOG', 'ARRAY', 'ASCII', 'ASCIISTR', 'ASIN', 'ASIS', 'ASSEMBLY', 'ASSIGN', 'ASSOCIATE', 'ASYNC', 'ASYNCHRONOUS', 'ATAN2', 'ATAN', 'AT', 'ATTRIBUTE', 'ATTRIBUTES', 'AUDIT', 'AUTHENTICATED', 'AUTHENTICATION', 'AUTHID', 'AUTHORIZATION', 'AUTOALLOCATE', 'A",+ },+ }++ for _, test := range tests {+ _, err := ParsePLSQL(test.statement)+ if test.errorMessage == "" {+ require.NoError(t, err)+ } else {+ require.EqualError(t, err, test.errorMessage)+ }+ }+}diff --git a/go.mod b/go.modindex 7a4d5030ba..2ddfefa979 100644--- a/go.mod+++ b/go.mod@@ -5,12 +5,14 @@ go 1.20require (cloud.google.com/go/spanner v1.45.0github.com/ClickHouse/clickhouse-go/v2 v2.8.3+ github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1github.com/aws/aws-sdk-go-v2 v1.17.7github.com/aws/aws-sdk-go-v2/config v1.18.19github.com/aws/aws-sdk-go-v2/credentials v1.13.18github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.60github.com/aws/aws-sdk-go-v2/service/s3 v1.31.1github.com/blang/semver/v4 v4.0.0+ github.com/bytebase/plsql-parser v0.0.0-20230508042408-11706c020b1cgithub.com/casbin/casbin/v2 v2.66.2github.com/cenkalti/backoff/v4 v4.2.0github.com/coreos/go-oidc v2.2.1+incompatible@@ -74,7 +76,6 @@ require (github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 // indirectgithub.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirectgithub.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.0.0 // indirect- github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirectgithub.com/apache/arrow/go/v10 v10.0.1 // indirectgithub.com/apache/thrift v0.16.0 // indirectgithub.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirectdiff --git a/go.sum b/go.sumindex 77e441177a..b65c83f039 100644--- a/go.sum+++ b/go.sum@@ -61,8 +61,8 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q=github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=-github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df h1:7RFfzj4SSt6nnvCPbCqijJi1nWCd+TqAT3bYCStRC18=-github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=+github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1 h1:X8MJ0fnN5FPdcGF5Ij2/OW+HgiJrRg3AfHAx1PJtIzM=+github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM=github.com/apache/arrow/go/v10 v10.0.1 h1:n9dERvixoC/1JjDmBcs9FPaEryoANa2sCgVFo6ez9cI=github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0=github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=@@ -135,6 +135,8 @@ github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651 h1:NpoTuRY6Ckj37github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651/go.mod h1:/HsnX0+34LXVz3MxtZoW15GiwEOXz/PrEhutjb3icfE=github.com/bytebase/pg_query_go/v2 v2.0.0-20221123093650-17425d1d18b7 h1:RDN6UnjTq4HR4LKrN8Yav4XfwUoGRjTocvWmzGr6Usc=github.com/bytebase/pg_query_go/v2 v2.0.0-20221123093650-17425d1d18b7/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=+github.com/bytebase/plsql-parser v0.0.0-20230508042408-11706c020b1c h1:QNwsznuwbcZSLyCsjEAYmDerUSPMEhUTEujU8bY/rqY=+github.com/bytebase/plsql-parser v0.0.0-20230508042408-11706c020b1c/go.mod h1:Mxor1eHkrVYgLF5D62T7nMKyZqCovaT4Srt3ru84Sc0=github.com/bytebase/tidb v0.0.0-20230329095902-fe196d3c77d0 h1:h0LzCFbiCKWy/TbnE5amjDf+/j+06chImQwEhe+TbHc=github.com/bytebase/tidb v0.0.0-20230329095902-fe196d3c77d0/go.mod h1:xrVXE+0V54QRGgrD9o+tuZmBDDx8vJpQgrPtRQvodWo=github.com/bytebase/tidb/parser v0.0.0-20230329095902-fe196d3c77d0 h1:Xdq53fvcG9qeUKkxdNbR9B1JLbfeIjhW2N0xxFbaZNg=