Skip to content

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

feat(mongodb): implement command-type ACL control (#19170)

h3n4lf4ede7278b17
diff --git a/backend/common/engine.go b/backend/common/engine.go
index 376c3ca771..8225fb4e57 100644
--- a/backend/common/engine.go
+++ b/backend/common/engine.go
@@ -57,13 +57,13 @@ func EngineSupportQueryNewACL(engine storepb.Engine) bool {
storepb.Engine_TIDB,
storepb.Engine_SNOWFLAKE,
storepb.Engine_SPANNER,
- storepb.Engine_BIGQUERY:
+ storepb.Engine_BIGQUERY,
+ storepb.Engine_MONGODB:
return true
case
storepb.Engine_ENGINE_UNSPECIFIED,
storepb.Engine_CASSANDRA,
storepb.Engine_SQLITE,
- storepb.Engine_MONGODB,
storepb.Engine_REDIS,
storepb.Engine_CLICKHOUSE,
storepb.Engine_OCEANBASE,
diff --git a/backend/plugin/parser/mongodb/diagnose.go b/backend/plugin/parser/mongodb/diagnose.go
index 36e46aa2d9..851d7feba6 100644
--- a/backend/plugin/parser/mongodb/diagnose.go
+++ b/backend/plugin/parser/mongodb/diagnose.go
@@ -15,12 +15,12 @@ func init() {
func Diagnose(_ context.Context, _ base.DiagnoseContext, statement string) ([]base.Diagnostic, error) {
diagnostics := []base.Diagnostic{}
- parseResult := ParseMongoShell(statement)
- if parseResult == nil {
+ raw := parseMongoShellRaw(statement)
+ if raw == nil {
return diagnostics, nil
}
- for _, err := range parseResult.Errors {
+ for _, err := range raw.Errors {
if err != nil {
diagnostics = append(diagnostics, base.ConvertSyntaxErrorToDiagnostic(err, statement))
}
diff --git a/backend/plugin/parser/mongodb/mongodb.go b/backend/plugin/parser/mongodb/mongodb.go
index d9ac2d8c53..62baf3d29e 100644
--- a/backend/plugin/parser/mongodb/mongodb.go
+++ b/backend/plugin/parser/mongodb/mongodb.go
@@ -6,83 +6,114 @@ import (
"github.com/bytebase/parser/mongodb"
+ "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
-// ParseResult contains the result of parsing a MongoDB shell script.
-type ParseResult struct {
- Tree mongodb.IProgramContext
- Statements []StatementInfo
- Errors []*base.SyntaxError
+func init() {
+ base.RegisterParseStatementsFunc(storepb.Engine_MONGODB, ParseMongoShell)
}
-// StatementInfo contains information about a parsed statement.
-type StatementInfo struct {
- StartOffset int
- EndOffset int
- StartLine int
- StartColumn int
- EndLine int
- EndColumn int
+// rawParseResult contains the raw result from ANTLR parsing.
+// Used internally by Diagnose and statement_ranges which need raw ANTLR data.
+type rawParseResult struct {
+ Tree mongodb.IProgramContext
+ Errors []*base.SyntaxError
+ Stream *antlr.CommonTokenStream
}
-// ParseMongoShell parses a MongoDB shell script and returns the parse result.
-func ParseMongoShell(statement string) *ParseResult {
+// parseMongoShellRaw performs the raw ANTLR parsing and returns the program tree,
+// token stream, and any syntax errors.
+func parseMongoShellRaw(statement string) *rawParseResult {
inputStream := antlr.NewInputStream(statement)
lexer := mongodb.NewMongoShellLexer(inputStream)
stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)
- parser := mongodb.NewMongoShellParser(stream)
+ p := mongodb.NewMongoShellParser(stream)
- // Set up error listener
lexer.RemoveErrorListeners()
- parser.RemoveErrorListeners()
+ p.RemoveErrorListeners()
errorListener := mongodb.NewMongoShellErrorListener()
lexer.AddErrorListener(errorListener)
- parser.AddErrorListener(errorListener)
+ p.AddErrorListener(errorListener)
- // Parse the input
- tree := parser.Program()
+ tree := p.Program()
- result := &ParseResult{
- Tree: tree,
+ result := &rawParseResult{
+ Tree: tree,
+ Stream: stream,
}
- // Convert errors
for _, err := range errorListener.Errors {
result.Errors = append(result.Errors, &base.SyntaxError{
Position: &storepb.Position{
Line: int32(err.Line),
- Column: int32(err.Column + 1), // Convert to 1-based column
+ Column: int32(err.Column + 1),
},
RawMessage: err.Message,
Message: err.Message,
})
}
- // Extract statement information
- if tree != nil {
- for _, stmt := range tree.AllStatement() {
- if stmt == nil {
- continue
- }
- start := stmt.GetStart()
- stop := stmt.GetStop()
- if start == nil || stop == nil {
- continue
- }
-
- info := StatementInfo{
- StartOffset: start.GetStart(),
- EndOffset: stop.GetStop() + 1, // End is exclusive
- StartLine: start.GetLine(),
- StartColumn: start.GetColumn(),
- EndLine: stop.GetLine(),
- EndColumn: stop.GetColumn() + len(stop.GetText()),
- }
- result.Statements = append(result.Statements, info)
+ return result
+}
+
+// ParseMongoShell parses a MongoDB shell script and returns parsed statements
+// with their ASTs. Conforms to the standard ParseStatementsFunc interface.
+func ParseMongoShell(statement string) ([]base.ParsedStatement, error) {
+ raw := parseMongoShellRaw(statement)
+ if raw.Tree == nil {
+ return nil, nil
+ }
+
+ runes := []rune(statement)
+ var result []base.ParsedStatement
+
+ for _, stmt := range raw.Tree.AllStatement() {
+ if stmt == nil {
+ continue
+ }
+ start := stmt.GetStart()
+ stop := stmt.GetStop()
+ if start == nil || stop == nil {
+ continue
}
+
+ startOffset := start.GetStart()
+ endOffset := min(stop.GetStop()+1, len(runes))
+
+ text := string(runes[startOffset:endOffset])
+ byteStart := len(string(runes[:startOffset]))
+ byteEnd := len(string(runes[:endOffset]))
+
+ ps := base.ParsedStatement{
+ Statement: base.Statement{
+ Text: text,
+ Start: &storepb.Position{
+ Line: int32(start.GetLine()),
+ Column: int32(start.GetColumn() + 1),
+ },
+ End: common.ConvertANTLRTokenToExclusiveEndPosition(
+ int32(stop.GetLine()),
+ int32(stop.GetColumn()),
+ stop.GetText(),
+ ),
+ Range: &storepb.Range{
+ Start: int32(byteStart),
+ End: int32(byteEnd),
+ },
+ },
+ AST: &base.ANTLRAST{
+ StartPosition: &storepb.Position{
+ Line: int32(start.GetLine()),
+ Column: int32(start.GetColumn() + 1),
+ },
+ Tree: stmt,
+ Tokens: raw.Stream,
+ },
+ }
+ result = append(result, ps)
}
- return result
+ return result, nil
}
diff --git a/backend/plugin/parser/mongodb/mongodb_test.go b/backend/plugin/parser/mongodb/mongodb_test.go
index 6374bfd4e7..9617e3c6ce 100644
--- a/backend/plugin/parser/mongodb/mongodb_test.go
+++ b/backend/plugin/parser/mongodb/mongodb_test.go
@@ -11,104 +11,98 @@ import (
func TestParseMongoShell(t *testing.T) {
testCases := []struct {
- description string
- statement string
- wantStatements int
- wantHasErrors bool
- wantStartOffset int
- wantEndOffset int
+ description string
+ statement string
+ wantStatements int
+ wantText string
}{
{
- description: "single find statement",
- statement: `db.collection.find({})`,
- wantStatements: 1,
- wantHasErrors: false,
- wantStartOffset: 0,
- wantEndOffset: 22,
+ description: "single find statement",
+ statement: `db.collection.find({})`,
+ wantStatements: 1,
+ wantText: `db.collection.find({})`,
},
{
- description: "single find with filter",
- statement: `db.users.find({ name: "John" })`,
- wantStatements: 1,
- wantHasErrors: false,
- wantStartOffset: 0,
- wantEndOffset: 31,
+ description: "single find with filter",
+ statement: `db.users.find({ name: "John" })`,
+ wantStatements: 1,
+ wantText: `db.users.find({ name: "John" })`,
},
{
description: "multiple statements",
statement: "db.users.find({});\ndb.products.insertOne({ name: \"test\" })",
wantStatements: 2,
- wantHasErrors: false,
},
{
description: "show databases command",
statement: "show dbs",
wantStatements: 1,
- wantHasErrors: false,
},
{
description: "show collections command",
statement: "show collections",
wantStatements: 1,
- wantHasErrors: false,
},
{
description: "aggregation pipeline",
statement: `db.orders.aggregate([{ $match: { status: "A" } }, { $group: { _id: "$cust_id", total: { $sum: "$amount" } } }])`,
wantStatements: 1,
- wantHasErrors: false,
},
{
description: "bracket notation for collection",
statement: `db["my-collection"].find({})`,
wantStatements: 1,
- wantHasErrors: false,
},
{
description: "method chaining",
statement: `db.users.find({}).sort({ name: 1 }).limit(10)`,
wantStatements: 1,
- wantHasErrors: false,
- },
- {
- description: "syntax error - missing closing paren",
- statement: `db.collection.find({`,
- wantHasErrors: true,
},
{
description: "empty input",
statement: "",
wantStatements: 0,
- wantHasErrors: false,
},
{
description: "whitespace only",
statement: " \n\t ",
wantStatements: 0,
- wantHasErrors: false,
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
- result := ParseMongoShell(tc.statement)
- require.NotNil(t, result)
+ result, err := ParseMongoShell(tc.statement)
+ require.NoError(t, err)
- if !tc.wantHasErrors {
- require.Len(t, result.Statements, tc.wantStatements, "statement count mismatch")
- require.Empty(t, result.Errors, "expected no errors")
- } else {
- require.NotEmpty(t, result.Errors, "expected errors")
+ require.Len(t, result, tc.wantStatements, "statement count mismatch")
+
+ if tc.wantStatements > 0 {
+ // Verify all statements have position and AST info.
+ for i, ps := range result {
+ require.NotNil(t, ps.Start, "statement %d should have Start", i)
+ require.NotNil(t, ps.End, "statement %d should have End", i)
+ require.NotNil(t, ps.Range, "statement %d should have Range", i)
+ require.NotNil(t, ps.AST, "statement %d should have AST", i)
+ require.NotEmpty(t, ps.Text, "statement %d should have text", i)
+ }
}
- if tc.wantStatements > 0 && tc.wantStartOffset != 0 && tc.wantEndOffset != 0 {
- require.Equal(t, tc.wantStartOffset, result.Statements[0].StartOffset)
- require.Equal(t, tc.wantEndOffset, result.Statements[0].EndOffset)
+ if tc.wantText != "" {
+ require.Equal(t, tc.wantText, result[0].Text)
}
})
}
}
+func TestParseMongoShellSyntaxError(t *testing.T) {
+ // Syntax errors are handled by parseMongoShellRaw, used by Diagnose.
+ // ParseMongoShell returns whatever statements the error recovery produces.
+ raw := parseMongoShellRaw(`db.collection.find({`)
+ require.NotNil(t, raw)
+ require.NotEmpty(t, raw.Errors, "expected errors")
+}
+
func TestGetStatementRanges(t *testing.T) {
testCases := []struct {
description string
diff --git a/backend/plugin/parser/mongodb/query_span.go b/backend/plugin/parser/mongodb/query_span.go
new file mode 100644
index 0000000000..00da8cee6e
--- /dev/null
+++ b/backend/plugin/parser/mongodb/query_span.go
@@ -0,0 +1,22 @@
+package mongodb
+
+import (
+ "context"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func init() {
+ base.RegisterGetQuerySpan(storepb.Engine_MONGODB, GetQuerySpan)
+}
+
+// GetQuerySpan returns a QuerySpan for a MongoDB shell statement.
+// It classifies the statement type for access control purposes.
+// MongoDB does not support column-level tracking, so only the QueryType is set.
+func GetQuerySpan(_ context.Context, _ base.GetQuerySpanContext, stmt base.Statement, _ string, _ string, _ bool) (*base.QuerySpan, error) {
+ queryType := GetQueryType(stmt.Text)
+ return &base.QuerySpan{
+ Type: queryType,
+ }, nil
+}
diff --git a/backend/plugin/parser/mongodb/query_span_test.go b/backend/plugin/parser/mongodb/query_span_test.go
new file mode 100644
index 0000000000..55222e0b82
--- /dev/null
+++ b/backend/plugin/parser/mongodb/query_span_test.go
@@ -0,0 +1,133 @@
+package mongodb
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func TestGetQuerySpan(t *testing.T) {
+ tests := []struct {
+ description string
+ statement string
+ wantType base.QueryType
+ }{
+ {
+ description: "find is Select",
+ statement: `db.users.find({})`,
+ wantType: base.Select,
+ },
+ {
+ description: "findOne is Select",
+ statement: `db.users.findOne({name: "John"})`,
+ wantType: base.Select,
+ },
+ {
+ description: "insertOne is DML",
+ statement: `db.users.insertOne({name: "John"})`,
+ wantType: base.DML,
+ },
+ {
+ description: "insertMany is DML",
+ statement: `db.users.insertMany([{name: "A"}, {name: "B"}])`,
+ wantType: base.DML,
+ },
+ {
+ description: "updateOne is DML",
+ statement: `db.users.updateOne({name: "John"}, {$set: {age: 30}})`,
+ wantType: base.DML,
+ },
+ {
+ description: "deleteOne is DML",
+ statement: `db.users.deleteOne({name: "John"})`,
+ wantType: base.DML,
+ },
+ {
+ description: "createCollection is DDL",
+ statement: `db.createCollection("test")`,
+ wantType: base.DDL,
+ },
+ {
+ description: "dropDatabase is DDL",
+ statement: `db.dropDatabase()`,
+ wantType: base.DDL,
+ },
+ {
+ description: "createIndex is DDL",
+ statement: `db.users.createIndex({name: 1})`,
+ wantType: base.DDL,
+ },
+ {
+ description: "drop collection is DDL",
+ statement: `db.users.drop()`,
+ wantType: base.DDL,
+ },
+ {
+ description: "find with explain is Explain",
+ statement: `db.users.find({}).explain()`,
+ wantType: base.Explain,
+ },
+ {
+ description: "show dbs is SelectInfoSchema",
+ statement: `show dbs`,
+ wantType: base.SelectInfoSchema,
+ },
+ {
+ description: "show collections is SelectInfoSchema",
+ statement: `show collections`,
+ wantType: base.SelectInfoSchema,
+ },
+ {
+ description: "getCollectionNames is SelectInfoSchema",
+ statement: `db.getCollectionNames()`,
+ wantType: base.SelectInfoSchema,
+ },
+ {
+ description: "aggregate without $out is Select",
+ statement: `db.users.aggregate([{$match: {age: {$gt: 25}}}])`,
+ wantType: base.Select,
+ },
+ {
+ description: "aggregate with $out is DML",
+ statement: `db.users.aggregate([{$out: "output"}])`,
+ wantType: base.DML,
+ },
+ {
+ description: "aggregate with $merge is DML",
+ statement: `db.users.aggregate([{$merge: {into: "output"}}])`,
+ wantType: base.DML,
+ },
+ {
+ description: "countDocuments is Select",
+ statement: `db.users.countDocuments({})`,
+ wantType: base.Select,
+ },
+ {
+ description: "distinct is Select",
+ statement: `db.users.distinct("name")`,
+ wantType: base.Select,
+ },
+ {
+ description: "stats is SelectInfoSchema",
+ statement: `db.users.stats()`,
+ wantType: base.SelectInfoSchema,
+ },
+ {
+ description: "getIndexes is SelectInfoSchema",
+ statement: `db.users.getIndexes()`,
+ wantType: base.SelectInfoSchema,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.description, func(t *testing.T) {
+ span, err := GetQuerySpan(context.Background(), base.GetQuerySpanContext{}, base.Statement{Text: tc.statement}, "", "", false)
+ require.NoError(t, err)
+ require.NotNil(t, span)
+ require.Equal(t, tc.wantType, span.Type)
+ })
+ }
+}
diff --git a/backend/plugin/parser/mongodb/query_type.go b/backend/plugin/parser/mongodb/query_type.go
new file mode 100644
index 0000000000..e54d280a57
--- /dev/null
+++ b/backend/plugin/parser/mongodb/query_type.go
@@ -0,0 +1,403 @@
+package mongodb
+
+import (
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ "github.com/bytebase/parser/mongodb"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+// GetQueryType parses a MongoDB shell statement and returns its query type.
+func GetQueryType(statement string) base.QueryType {
+ stmts, err := ParseMongoShell(statement)
+ if err != nil || len(stmts) == 0 {
+ return base.DML
+ }
+
+ ast, ok := stmts[0].AST.(*base.ANTLRAST)
+ if !ok || ast.Tree == nil {
+ return base.DML
+ }
+
+ l := &queryTypeListener{
+ result: base.DML,
+ }
+ antlr.ParseTreeWalkerDefault.Walk(l, ast.Tree)
+ return l.result
+}
+
+type queryTypeListener struct {
+ *mongodb.BaseMongoShellParserListener
+
+ result base.QueryType
+}
+
+// Shell commands.
+
+func (l *queryTypeListener) EnterShowDatabases(_ *mongodb.ShowDatabasesContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterShowCollections(_ *mongodb.ShowCollectionsContext) {
+ l.result = base.SelectInfoSchema
+}
+
+// Database methods: info/read-only.
+
+func (l *queryTypeListener) EnterGetCollectionNames(_ *mongodb.GetCollectionNamesContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterGetCollectionInfos(_ *mongodb.GetCollectionInfosContext) {
+ l.result = base.SelectInfoSchema
+}
+
+//nolint:staticcheck // Method name must match ANTLR-generated listener interface.
+func (l *queryTypeListener) EnterDbStats(_ *mongodb.DbStatsContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterServerStatus(_ *mongodb.ServerStatusContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterServerBuildInfo(_ *mongodb.ServerBuildInfoContext) {
+ l.result = base.SelectInfoSchema
+}
+
+//nolint:staticcheck // Method name must match ANTLR-generated listener interface.
+func (l *queryTypeListener) EnterDbVersion(_ *mongodb.DbVersionContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterHostInfo(_ *mongodb.HostInfoContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterListCommands(_ *mongodb.ListCommandsContext) {
+ l.result = base.SelectInfoSchema
+}
+
+func (l *queryTypeListener) EnterGetName(_ *mongodb.GetNameContext) {
+ l.result = base.SelectInfoSchema
+}
+
+// Database methods: DDL.
+
+func (l *queryTypeListener) EnterCreateCollection(_ *mongodb.CreateCollectionContext) {
+ l.result = base.DDL
+}
+
+func (l *queryTypeListener) EnterDropDatabase(_ *mongodb.DropDatabaseContext) {
+ l.result = base.DDL
+}
+
+// Database methods: DML.
+
+func (l *queryTypeListener) EnterGetSiblingDB(_ *mongodb.GetSiblingDBContext) {
+ l.result = base.DML
+}
+
+func (l *queryTypeListener) EnterGetMongo(_ *mongodb.GetMongoContext) {
+ l.result = base.DML
+}
+
+//nolint:staticcheck // Method name must match ANTLR-generated listener interface.
+func (l *queryTypeListener) EnterDbGenericMethod(_ *mongodb.DbGenericMethodContext) {
+ l.result = base.DML
+}
+
+// runCommand / adminCommand: classify by argument text.
+
+func (l *queryTypeListener) EnterRunCommand(ctx *mongodb.RunCommandContext) {
+ l.result = classifyCommandArguments(ctx.Arguments())
+}
+
+func (l *queryTypeListener) EnterAdminCommand(ctx *mongodb.AdminCommandContext) {
+ l.result = classifyCommandArguments(ctx.Arguments())
+}
+
+func classifyCommandArguments(args mongodb.IArgumentsContext) base.QueryType {
+ if args == nil {
+ return base.DML
+ }
+ argText := args.GetText()
+
+ // Read-only query commands.
+ for _, keyword := range []string{"find", "aggregate", "count", "distinct"} {
+ if strings.Contains(argText, keyword) {
+ return base.Select
+ }
+ }
+ // Info/metadata commands.
+ for _, keyword := range []string{
+ "serverStatus", "listCollections", "listIndexes", "listDatabases",
+ "collStats", "dbStats", "hostInfo", "buildInfo", "connectionStatus",
+ } {
+ if strings.Contains(argText, keyword) {
+ return base.SelectInfoSchema
+ }
+ }
+ // DDL commands.
+ for _, keyword := range []string{
+ "create", "drop", "createIndexes", "dropIndexes",
+ "renameCollection", "collMod",
+ } {
+ if strings.Contains(argText, keyword) {
+ return base.DDL
+ }
+ }
+
+ return base.DML
+}
+
+// Collection operations: db.collection.method().
+
+func (l *queryTypeListener) EnterCollectionOperation(ctx *mongodb.CollectionOperationContext) {
+ chain := ctx.MethodChain()
+ if chain == nil {
+ l.result = base.DML
+ return
+ }
+ l.result = classifyMethodChain(chain)
+}
+
+func classifyMethodChain(chain mongodb.IMethodChainContext) base.QueryType {
+ calls := chain.AllMethodCall()
+ if len(calls) == 0 {
+ return base.DML
+ }
+
+ // Check if .explain() appears anywhere in the chain.
+ for _, call := range calls {
+ if call.ExplainMethod() != nil {
+ return base.Explain
+ }
+ }
+
+ // Classify based on the first method call.
+ first := calls[0]
+ return classifyMethodCall(first)
+}
+
+func classifyMethodCall(mc mongodb.IMethodCallContext) base.QueryType {
+ switch {
+ // Read methods -> Select.
+ case mc.FindMethod() != nil,
+ mc.FindOneMethod() != nil,
+ mc.CountDocumentsMethod() != nil,
+ mc.EstimatedDocumentCountMethod() != nil,
+ mc.CountMethod() != nil,
+ mc.DistinctMethod() != nil:
+ return base.Select
+
+ // Aggregate: Select by default, DML if $out or $merge.
+ case mc.AggregateMethod() != nil:
+ return classifyAggregate(mc.AggregateMethod())
+
+ // Write methods -> DML.
+ case mc.InsertOneMethod() != nil,
+ mc.InsertManyMethod() != nil,
+ mc.UpdateOneMethod() != nil,
+ mc.UpdateManyMethod() != nil,
+ mc.DeleteOneMethod() != nil,
+ mc.DeleteManyMethod() != nil,
+ mc.ReplaceOneMethod() != nil,
+ mc.FindOneAndUpdateMethod() != nil,
+ mc.FindOneAndReplaceMethod() != nil,
+ mc.FindOneAndDeleteMethod() != nil:
+ return base.DML
+
+ // DDL methods.
+ case mc.CreateIndexMethod() != nil,
+ mc.CreateIndexesMethod() != nil,
+ mc.DropIndexMethod() != nil,
+ mc.DropIndexesMethod() != nil,
+ mc.DropMethod() != nil,
+ mc.RenameCollectionMethod() != nil:
+ return base.DDL
+
+ // Info methods -> SelectInfoSchema.
+ case mc.GetIndexesMethod() != nil,
+ mc.StatsMethod() != nil,
+ mc.StorageSizeMethod() != nil,
+ mc.TotalIndexSizeMethod() != nil,
+ mc.TotalSizeMethod() != nil,
+ mc.DataSizeMethod() != nil,
+ mc.IsCappedMethod() != nil,
+ mc.ValidateMethod() != nil,
+ mc.LatencyStatsMethod() != nil:
+ return base.SelectInfoSchema
+
+ // Explain.
+ case mc.ExplainMethod() != nil:
+ return base.Explain
+
+ // Generic/unknown method -> DML.
+ case mc.GenericMethod() != nil:
+ return base.DML
+
+ default:
+ return base.DML
+ }
+}
+
+func classifyAggregate(ctx mongodb.IAggregateMethodContext) base.QueryType {
+ if ctx == nil {
+ return base.Select
+ }
+ args := ctx.Arguments()
+ if args != nil {
+ argText := args.GetText()
+ if strings.Contains(argText, "$out") || strings.Contains(argText, "$merge") {
+ return base.DML
+ }
+ }
+ return base.Select
+}
+
+// Bulk operations.
+
+func (l *queryTypeListener) EnterBulkStatement(_ *mongodb.BulkStatementContext) {
+ l.result = base.DML
+}
+
+// RS statements.
+
+func (l *queryTypeListener) EnterRsStatement(ctx *mongodb.RsStatementContext) {
+ id := ctx.Identifier()
+ if id == nil {
+ l.result = base.DML
+ return
+ }
+ name := id.GetText()
+ switch name {
+ case "status", "printReplicationInfo", "printSecondaryReplicationInfo", "help", "conf":
+ l.result = base.SelectInfoSchema
+ default:
+ l.result = base.DML
+ }
+}
+
+// SH statements.
+
+func (l *queryTypeListener) EnterShStatement(ctx *mongodb.ShStatementContext) {
+ id := ctx.Identifier()
+ if id == nil {
+ l.result = base.DML
+ return
+ }
+ name := id.GetText()
+ switch name {
+ case "status", "getBalancerState", "isBalancerRunning",
+ "getShardedDataDistribution", "isConfigShardEnabled",
+ "listShards", "help", "balancerCollectionStatus",
+ "checkMetadataConsistency":
+ l.result = base.SelectInfoSchema
+ default:
+ l.result = base.DML
+ }
+}
+
+// Encryption statements.
+
+func (l *queryTypeListener) EnterKeyVaultStatement(ctx *mongodb.KeyVaultStatementContext) {
+ l.result = classifyEncryptionIdentifiers(ctx.AllIdentifier())
+}
+
+func (l *queryTypeListener) EnterClientEncryptionStatement(ctx *mongodb.ClientEncryptionStatementContext) {
+ l.result = classifyEncryptionIdentifiers(ctx.AllIdentifier())
+}
+
+func classifyEncryptionIdentifiers(identifiers []mongodb.IIdentifierContext) base.QueryType {
+ if len(identifiers) == 0 {
+ return base.DML
+ }
+ // Use the last identifier in the chain.
+ last := identifiers[len(identifiers)-1]
+ name := last.GetText()
+ switch name {
+ case "getKey", "getKeyByAltName", "getKeys", "decrypt", "encrypt", "encryptExpression":
+ return base.SelectInfoSchema
+ default:
+ return base.DML
+ }
+}
+
+// Plan cache statements.
+
+func (l *queryTypeListener) EnterPlanCacheStatement(ctx *mongodb.PlanCacheStatementContext) {
+ identifiers := ctx.AllIdentifier()
+ if len(identifiers) == 0 {
+ l.result = base.DML
+ return
+ }
+ // Check identifiers in the method chain after getPlanCache().
+ for _, id := range identifiers {
+ name := id.GetText()
+ switch name {
+ case "list", "help":
+ l.result = base.SelectInfoSchema
+ return
+ case "clear", "clearPlansByQuery":
+ l.result = base.DML
+ return
+ }
+ }
+ l.result = base.DML
+}
+
+// SP statements.
+
+func (l *queryTypeListener) EnterSpStatement(ctx *mongodb.SpStatementContext) {
+ identifiers := ctx.AllIdentifier()
+ if len(identifiers) == 0 {
+ l.result = base.DML
+ return
+ }
+
+ // Single-identifier form: sp.method()
+ if len(identifiers) == 1 {
+ name := identifiers[0].GetText()
+ switch name {
+ case "listConnections", "listStreamProcessors":
+ l.result = base.SelectInfoSchema
+ default:
+ l.result = base.DML
+ }
+ return
+ }
+
+ // Compound form: sp.X.Y()
+ secondName := identifiers[1].GetText()
+ switch secondName {
+ case "stats", "sample":
+ l.result = base.SelectInfoSchema
+ default:
+ l.result = base.DML
+ }
+}
+
+// Native function calls.
+
+func (l *queryTypeListener) EnterNativeFunctionCall(_ *mongodb.NativeFunctionCallContext) {
+ l.result = base.DML
+}
+
+// Connection statements.
+
+func (l *queryTypeListener) EnterMongoConnection(_ *mongodb.MongoConnectionContext) {
+ l.result = base.DML
+}
+
+func (l *queryTypeListener) EnterConnectCall(_ *mongodb.ConnectCallContext) {
+ l.result = base.DML
+}
+
+//nolint:staticcheck // Method name must match ANTLR-generated listener interface.
+func (l *queryTypeListener) EnterDbGetMongoChain(_ *mongodb.DbGetMongoChainContext) {
+ l.result = base.DML
+}
diff --git a/backend/plugin/parser/mongodb/query_type_test.go b/backend/plugin/parser/mongodb/query_type_test.go
new file mode 100644
index 0000000000..e31a159ce0
--- /dev/null
+++ b/backend/plugin/parser/mongodb/query_type_test.go
@@ -0,0 +1,361 @@
+package mongodb
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func TestGetQueryType(t *testing.T) {
+ tests := []struct {
+ description string
+ statement string
+ want base.QueryType
+ }{
+ // Select commands.
+ {
+ description: "find",
+ statement: `db.users.find({})`,
+ want: base.Select,
+ },
+ {
+ description: "findOne",
+ statement: `db.users.findOne({name: "John"})`,
+ want: base.Select,
+ },
+ {
+ description: "countDocuments",
+ statement: `db.users.countDocuments({})`,
+ want: base.Select,
+ },
+ {
+ description: "estimatedDocumentCount",
+ statement: `db.users.estimatedDocumentCount()`,
+ want: base.Select,
+ },
+ {
+ description: "distinct",
+ statement: `db.users.distinct("name")`,
+ want: base.Select,
+ },
+ {
+ description: "aggregate without $out/$merge",
+ statement: `db.users.aggregate([{$match: {}}])`,
+ want: base.Select,
+ },
+
+ // DML commands.
+ {
+ description: "insertOne",
+ statement: `db.users.insertOne({name: "John"})`,
+ want: base.DML,
+ },
+ {
+ description: "insertMany",
+ statement: `db.users.insertMany([{a: 1}, {a: 2}])`,
+ want: base.DML,
+ },
+ {
+ description: "updateOne",
+ statement: `db.users.updateOne({}, {$set: {a: 1}})`,
+ want: base.DML,
+ },
+ {
+ description: "updateMany",
+ statement: `db.users.updateMany({}, {$set: {a: 1}})`,
+ want: base.DML,
+ },
+ {
+ description: "deleteOne",
+ statement: `db.users.deleteOne({})`,
+ want: base.DML,
+ },
+ {
+ description: "deleteMany",
+ statement: `db.users.deleteMany({})`,
+ want: base.DML,
+ },
+ {
+ description: "replaceOne",
+ statement: `db.users.replaceOne({a: 1}, {a: 2})`,
+ want: base.DML,
+ },
+ {
+ description: "findOneAndUpdate",
+ statement: `db.users.findOneAndUpdate({}, {$set: {a: 1}})`,
+ want: base.DML,
+ },
+ {
+ description: "findOneAndReplace",
+ statement: `db.users.findOneAndReplace({a: 1}, {a: 2})`,
+ want: base.DML,
+ },
+ {
+ description: "findOneAndDelete",
+ statement: `db.users.findOneAndDelete({a: 1})`,
+ want: base.DML,
+ },
+
+ // Ambiguous aggregate with $out / $merge.
+ {
+ description: "aggregate with $out",
+ statement: `db.users.aggregate([{$match: {}}, {$out: "output"}])`,
+ want: base.DML,
+ },
+ {
+ description: "aggregate with $merge",
+ statement: `db.users.aggregate([{$match: {}}, {$merge: {into: "output"}}])`,
+ want: base.DML,
+ },
+
+ // DDL commands.
+ {
+ description: "createCollection",
+ statement: `db.createCollection("test")`,
+ want: base.DDL,
+ },
+ {
+ description: "dropDatabase",
+ statement: `db.dropDatabase()`,
+ want: base.DDL,
+ },
+ {
+ description: "drop collection",
+ statement: `db.users.drop()`,
+ want: base.DDL,
+ },
+ {
+ description: "createIndex",
+ statement: `db.users.createIndex({name: 1})`,
+ want: base.DDL,
+ },
+ {
+ description: "createIndexes",
+ statement: `db.users.createIndexes([{key: {name: 1}}])`,
+ want: base.DDL,
+ },
+ {
+ description: "dropIndex",
+ statement: `db.users.dropIndex("name_1")`,
+ want: base.DDL,
+ },
+ {
+ description: "dropIndexes",
+ statement: `db.users.dropIndexes()`,
+ want: base.DDL,
+ },
+ {
+ description: "renameCollection",
+ statement: `db.users.renameCollection("newName")`,
+ want: base.DDL,
+ },
+
+ // Info commands (SelectInfoSchema).
+ {
+ description: "show dbs",
+ statement: `show dbs`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "show collections",
+ statement: `show collections`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "getCollectionNames",
+ statement: `db.getCollectionNames()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "getCollectionInfos",
+ statement: `db.getCollectionInfos()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "db.stats()",
+ statement: `db.stats()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "collection stats",
+ statement: `db.users.stats()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "serverStatus",
+ statement: `db.serverStatus()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "serverBuildInfo",
+ statement: `db.serverBuildInfo()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "db.version()",
+ statement: `db.version()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "hostInfo",
+ statement: `db.hostInfo()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "listCommands",
+ statement: `db.listCommands()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "getIndexes",
+ statement: `db.users.getIndexes()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "dataSize",
+ statement: `db.users.dataSize()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "storageSize",
+ statement: `db.users.storageSize()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "totalIndexSize",
+ statement: `db.users.totalIndexSize()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "totalSize",
+ statement: `db.users.totalSize()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "isCapped",
+ statement: `db.users.isCapped()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "validate",
+ statement: `db.users.validate()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "latencyStats",
+ statement: `db.users.latencyStats()`,
+ want: base.SelectInfoSchema,
+ },
+
+ // Explain.
+ {
+ description: "find with explain",
+ statement: `db.users.find({}).explain()`,
+ want: base.Explain,
+ },
+ {
+ description: "find with sort and explain",
+ statement: `db.users.find({}).sort({a: 1}).explain()`,
+ want: base.Explain,
+ },
+
+ // Bulk operations.
+ {
+ description: "initializeOrderedBulkOp",
+ statement: `db.users.initializeOrderedBulkOp().insert({a: 1}).execute()`,
+ want: base.DML,
+ },
+
+ // RS statements.
+ {
+ description: "rs.status()",
+ statement: `rs.status()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "rs.initiate()",
+ statement: `rs.initiate()`,
+ want: base.DML,
+ },
+ {
+ description: "rs.help()",
+ statement: `rs.help()`,
+ want: base.SelectInfoSchema,
+ },
+
+ // SH statements.
+ {
+ description: "sh.status()",
+ statement: `sh.status()`,
+ want: base.SelectInfoSchema,
+ },
+ {
+ description: "sh.addShard()",
+ statement: `sh.addShard("host:27017")`,
+ want: base.DML,
+ },
+ {
+ description: "sh.help()",
+ statement: `sh.help()`,
+ want: base.SelectInfoSchema,
+ },
+
+ // runCommand.
+ {
+ description: "runCommand find",
+ statement: `db.runCommand({find: "users"})`,
+ want: base.Select,
+ },
+ {
+ description: "runCommand insert",
+ statement: `db.runCommand({insert: "users", documents: [{a: 1}]})`,
+ want: base.DML,
+ },
+ {
+ description: "runCommand create",
+ statement: `db.runCommand({create: "newColl"})`,
+ want: base.DDL,
+ },
+ {
+ description: "runCommand serverStatus",
+ statement: `db.runCommand({serverStatus: 1})`,
+ want: base.SelectInfoSchema,
+ },
+
+ // Generic/unknown methods.
+ {
+ description: "unknown collection method",
+ statement: `db.users.someUnknownMethod()`,
+ want: base.DML,
+ },
+ {
+ description: "unknown db method",
+ statement: `db.someUnknownDbMethod()`,
+ want: base.DML,
+ },
+
+ // Parse error with partial AST: parser cannot recover "find" from the
+ // incomplete input, so it falls back to the default DML.
+ {
+ description: "incomplete find statement",
+ statement: `db.users.find({`,
+ want: base.DML,
+ },
+
+ // Completely unparseable.
+ {
+ description: "completely unparseable input",
+ statement: `@@@###`,
+ want: base.DML,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.description, func(t *testing.T) {
+ got := GetQueryType(tc.statement)
+ require.Equal(t, tc.want, got, "statement: %s", tc.statement)
+ })
+ }
+}
diff --git a/backend/plugin/parser/mongodb/split.go b/backend/plugin/parser/mongodb/split.go
new file mode 100644
index 0000000000..f5194c256f
--- /dev/null
+++ b/backend/plugin/parser/mongodb/split.go
@@ -0,0 +1,36 @@
+package mongodb
+
+import (
+ "strings"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func init() {
+ base.RegisterSplitterFunc(storepb.Engine_MONGODB, SplitSQL)
+}
+
+// SplitSQL splits the input into multiple MongoDB statements.
+// Unlike SQL-based engines that split on semicolons, MongoDB statements can be
+// separated by newlines without semicolons, so we use the parser to identify
+// statement boundaries.
+func SplitSQL(statement string) ([]base.Statement, error) {
+ stmts, err := ParseMongoShell(statement)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(stmts) == 0 {
+ if len(strings.TrimSpace(statement)) == 0 {
+ return nil, nil
+ }
+ return []base.Statement{{Text: statement}}, nil
+ }
+
+ var result []base.Statement
+ for _, ps := range stmts {
+ result = append(result, ps.Statement)
+ }
+ return result, nil
+}
diff --git a/backend/plugin/parser/mongodb/split_test.go b/backend/plugin/parser/mongodb/split_test.go
new file mode 100644
index 0000000000..b4fba777ea
--- /dev/null
+++ b/backend/plugin/parser/mongodb/split_test.go
@@ -0,0 +1,117 @@
+package mongodb
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestSplitSQL(t *testing.T) {
+ testCases := []struct {
+ description string
+ statement string
+ wantCount int
+ wantTexts []string
+ wantNil bool
+ }{
+ {
+ description: "single statement",
+ statement: `db.users.find({})`,
+ wantCount: 1,
+ wantTexts: []string{`db.users.find({})`},
+ },
+ {
+ description: "two statements with semicolons",
+ statement: `db.users.find({});db.products.find({})`,
+ wantCount: 2,
+ wantTexts: []string{`db.users.find({});`, `db.products.find({})`},
+ },
+ {
+ description: "two statements with newlines",
+ statement: "db.users.find({})\ndb.products.find({})",
+ wantCount: 2,
+ wantTexts: []string{`db.users.find({})`, `db.products.find({})`},
+ },
+ {
+ description: "two statements with semicolon and newline",
+ statement: "db.users.find({});\ndb.products.insertOne({name: \"test\"})",
+ wantCount: 2,
+ wantTexts: []string{"db.users.find({});", `db.products.insertOne({name: "test"})`},
+ },
+ {
+ description: "three statements mixed separators",
+ statement: "show dbs;\ndb.users.find({})\ndb.products.drop()",
+ wantCount: 3,
+ wantTexts: []string{"show dbs;", "db.users.find({})", "db.products.drop()"},
+ },
+ {
+ description: "single statement with trailing semicolon",
+ statement: `db.users.find({});`,
+ wantCount: 1,
+ wantTexts: []string{`db.users.find({});`},
+ },
+ {
+ description: "empty input",
+ statement: "",
+ wantNil: true,
+ },
+ {
+ description: "whitespace only",
+ statement: " \n\t ",
+ wantNil: true,
+ },
+ {
+ description: "show commands",
+ statement: "show dbs\nshow collections",
+ wantCount: 2,
+ wantTexts: []string{"show dbs", "show collections"},
+ },
+ {
+ description: "multiline aggregate is one statement",
+ statement: `db.users.aggregate([
+ { $match: { status: "A" } },
+ { $group: { _id: "$cust_id" } }
+])`,
+ wantCount: 1,
+ },
+ {
+ description: "method chaining is one statement",
+ statement: `db.users.find({}).sort({ name: 1 }).limit(10)`,
+ wantCount: 1,
+ },
+ {
+ description: "bracket notation collection",
+ statement: `db["my-collection"].find({})`,
+ wantCount: 1,
+ wantTexts: []string{`db["my-collection"].find({})`},
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.description, func(t *testing.T) {
+ result, err := SplitSQL(tc.statement)
+ require.NoError(t, err)
+
+ if tc.wantNil {
+ require.Nil(t, result)
+ return
+ }
+
+ require.Len(t, result, tc.wantCount)
+
+ if tc.wantTexts != nil {
+ for i, wantText := range tc.wantTexts {
+ require.Equal(t, wantText, result[i].Text, "statement %d text mismatch", i)
+ }
+ }
+
+ // Verify all statements have position info.
+ for i, stmt := range result {
+ require.NotNil(t, stmt.Start, "statement %d should have Start", i)
+ require.NotNil(t, stmt.End, "statement %d should have End", i)
+ require.NotNil(t, stmt.Range, "statement %d should have Range", i)
+ require.Greater(t, stmt.Range.End, stmt.Range.Start, "statement %d Range.End should be > Range.Start", i)
+ }
+ })
+ }
+}
diff --git a/backend/plugin/parser/mongodb/statement_ranges.go b/backend/plugin/parser/mongodb/statement_ranges.go
index d6c1567113..64fef3cfaa 100644
--- a/backend/plugin/parser/mongodb/statement_ranges.go
+++ b/backend/plugin/parser/mongodb/statement_ranges.go
@@ -15,8 +15,8 @@ func init() {
// GetStatementRanges returns the ranges of statements in the MongoDB shell script.
func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, statement string) ([]base.Range, error) {
- parseResult := ParseMongoShell(statement)
- if parseResult == nil {
+ raw := parseMongoShellRaw(statement)
+ if raw == nil || raw.Tree == nil {
return []base.Range{}, nil
}
@@ -26,13 +26,21 @@ func GetStatementRanges(_ context.Context, _ base.StatementRangeContext, stateme
var ranges []base.Range
- for _, stmt := range parseResult.Statements {
- if stmt.EndOffset <= stmt.StartOffset {
+ for _, stmt := range raw.Tree.AllStatement() {
+ if stmt == nil {
continue
}
+ start := stmt.GetStart()
+ stop := stmt.GetStop()
+ if start == nil || stop == nil {
+ continue
+ }
+
+ startOffset := start.GetStart()
+ endOffset := stop.GetStop() + 1
- startPosition := getPositionByRuneOffset(stmt.StartOffset, runePositions)
- endPosition := getPositionByRuneOffset(stmt.EndOffset, runePositions)
+ startPosition := getPositionByRuneOffset(startOffset, runePositions)
+ endPosition := getPositionByRuneOffset(endOffset, runePositions)
if startPosition == nil || endPosition == nil {
continue
}