Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ type PlannedOperationError struct {
func (e *PlannedOperationError) Error() string {
return fmt.Sprintf("operation %s is not yet implemented", e.Operation)
}

// UnsupportedOptionError represents an unsupported option in a supported method.
type UnsupportedOptionError struct {
Method string
Option string
}

func (e *UnsupportedOptionError) Error() string {
return fmt.Sprintf("unsupported option '%s' in %s", e.Option, e.Method)
}
81 changes: 79 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/antlr4-go/antlr/v4"
"github.com/bytebase/parser/mongodb"
Expand Down Expand Up @@ -111,6 +112,25 @@ func executeFind(ctx context.Context, client *mongo.Client, database string, op
if op.projection != nil {
opts.SetProjection(op.projection)
}
if op.hint != nil {
opts.SetHint(op.hint)
}
if op.max != nil {
opts.SetMax(op.max)
}
if op.min != nil {
opts.SetMin(op.min)
}

// Apply maxTimeMS using context timeout.
// Note: MongoDB Go driver v2 removed SetMaxTime() from options. The recommended
// replacement is context.WithTimeout(). This is a client-side timeout (includes
// network latency), unlike mongosh's maxTimeMS which is server-side only.
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

cursor, err := collection.Find(ctx, filter, opts)
if err != nil {
Expand Down Expand Up @@ -152,7 +172,19 @@ func executeAggregate(ctx context.Context, client *mongo.Client, database string
pipeline = bson.A{}
}

cursor, err := collection.Aggregate(ctx, pipeline)
opts := options.Aggregate()
if op.hint != nil {
opts.SetHint(op.hint)
}

// Apply maxTimeMS using context timeout (see comment in executeFind for details).
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

cursor, err := collection.Aggregate(ctx, pipeline, opts)
if err != nil {
return nil, fmt.Errorf("aggregate failed: %w", err)
}
Expand Down Expand Up @@ -201,6 +233,22 @@ func executeFindOne(ctx context.Context, client *mongo.Client, database string,
if op.projection != nil {
opts.SetProjection(op.projection)
}
if op.hint != nil {
opts.SetHint(op.hint)
}
if op.max != nil {
opts.SetMax(op.max)
}
if op.min != nil {
opts.SetMin(op.min)
}

// Apply maxTimeMS using context timeout (see comment in executeFind for details).
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

var doc bson.M
err := collection.FindOne(ctx, filter, opts).Decode(&doc)
Expand Down Expand Up @@ -269,7 +317,15 @@ func executeGetCollectionInfos(ctx context.Context, client *mongo.Client, databa
filter = bson.D{}
}

cursor, err := client.Database(database).ListCollections(ctx, filter)
opts := options.ListCollections()
if op.nameOnly != nil {
opts.SetNameOnly(*op.nameOnly)
}
if op.authorizedCollections != nil {
opts.SetAuthorizedCollections(*op.authorizedCollections)
}

cursor, err := client.Database(database).ListCollections(ctx, filter, opts)
if err != nil {
return nil, fmt.Errorf("list collections failed: %w", err)
}
Expand Down Expand Up @@ -353,6 +409,13 @@ func executeCountDocuments(ctx context.Context, client *mongo.Client, database s
opts.SetSkip(*op.skip)
}

// Apply maxTimeMS using context timeout (see comment in executeFind for details).
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

count, err := collection.CountDocuments(ctx, filter, opts)
if err != nil {
return nil, fmt.Errorf("count documents failed: %w", err)
Expand All @@ -368,6 +431,13 @@ func executeCountDocuments(ctx context.Context, client *mongo.Client, database s
func executeEstimatedDocumentCount(ctx context.Context, client *mongo.Client, database string, op *mongoOperation) (*Result, error) {
collection := client.Database(database).Collection(op.collection)

// Apply maxTimeMS using context timeout (see comment in executeFind for details).
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

count, err := collection.EstimatedDocumentCount(ctx)
if err != nil {
return nil, fmt.Errorf("estimated document count failed: %w", err)
Expand All @@ -388,6 +458,13 @@ func executeDistinct(ctx context.Context, client *mongo.Client, database string,
filter = bson.D{}
}

// Apply maxTimeMS using context timeout (see comment in executeFind for details).
if op.maxTimeMS != nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(*op.maxTimeMS)*time.Millisecond)
defer cancel()
}

result := collection.Distinct(ctx, op.distinctField, filter)
if err := result.Err(); err != nil {
return nil, fmt.Errorf("distinct failed: %w", err)
Expand Down
Loading