Esempio n. 1
0
        public UnifiedCreateIndexOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BsonDocument         keys    = null;
            CreateIndexOptions   options = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "keys":
                    keys = argument.Value.AsBsonDocument;
                    break;

                case "name":
                    options      = options ?? new CreateIndexOptions();
                    options.Name = argument.Value.AsString;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid CreateIndexOperation argument name: '{argument.Name}'.");
                }
            }

            var createIndexModel = new CreateIndexModel <BsonDocument>(keys, options);

            return(new UnifiedCreateIndexOperation(session, collection, createIndexModel));
        }
        public UnifiedFindOneAndDeleteOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument>        filter  = null;
            FindOneAndDeleteOptions <BsonDocument> options = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "hint":
                    options ??= new FindOneAndDeleteOptions <BsonDocument>();
                    options.Hint = argument.Value;
                    break;

                case "let":
                    options ??= new FindOneAndDeleteOptions <BsonDocument>();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                default:
                    throw new FormatException($"Invalid FindOneAndDeleteOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOneAndDeleteOperation(collection, filter, options));
        }
Esempio n. 3
0
        public UnifiedFindOneAndUpdateOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument>        filter  = null;
            FindOneAndUpdateOptions <BsonDocument> options = null;
            UpdateDefinition <BsonDocument>        update  = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "hint":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.Hint = argument.Value;
                    break;

                case "returnDocument":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), argument.Value.AsString);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "sort":
                    options ??= new FindOneAndUpdateOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "update":
                    switch (argument.Value)
                    {
                    case BsonDocument:
                        update = argument.Value.AsBsonDocument;
                        break;

                    case BsonArray:
                        update = PipelineDefinition <BsonDocument, BsonDocument> .Create(argument.Value.AsBsonArray.Cast <BsonDocument>());

                        break;

                    default:
                        throw new FormatException($"Invalid FindOneAndUpdateOperation update argument: '{argument.Value}'.");
                    }
                    break;

                default:
                    throw new FormatException($"Invalid FindOneAndUpdateOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOneAndUpdateOperation(collection, filter, update, session, options));
        }
Esempio n. 4
0
        public UnifiedEstimatedDocumentCountOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            var options = new EstimatedDocumentCountOptions();

            foreach (var argument in arguments ?? Enumerable.Empty <BsonElement>())
            {
                switch (argument.Name)
                {
                case "comment":
                    options.Comment = argument.Value.AsBsonValue;
                    break;

                case "maxTimeMS":
                    options.MaxTime = TimeSpan.FromMilliseconds(argument.Value.AsInt32);
                    break;

                default:
                    throw new FormatException($"Invalid {nameof(UnifiedEstimatedDocumentCountOperation)} argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedEstimatedDocumentCountOperation(collection, options));
        }
        public UnifiedDistinctOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            string fieldName = null;
            FilterDefinition <BsonDocument> filter = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "fieldName":
                    fieldName = argument.Value.AsString;
                    break;

                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                default:
                    throw new FormatException($"Invalid DistinctOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedDistinctOperation(collection, fieldName, filter));
        }
        public UnifiedInsertManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            List <BsonDocument>  documents = null;
            InsertManyOptions    options   = null;
            IClientSessionHandle session   = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "documents":
                    documents = argument.Value.AsBsonArray.Cast <BsonDocument>().ToList();
                    break;

                case "ordered":
                    options           = options ?? new InsertManyOptions();
                    options.IsOrdered = argument.Value.AsBoolean;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid InsertManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedInsertManyOperation(session, collection, documents, options));
        }
Esempio n. 7
0
        public UnifiedInsertOneOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BsonDocument         document = null;
            InsertOneOptions     options  = null;
            IClientSessionHandle session  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "bypassDocumentValidation":
                    options = options ?? new InsertOneOptions();
                    options.BypassDocumentValidation = argument.Value.AsBoolean;
                    break;

                case "document":
                    document = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    var sessionId = argument.Value.AsString;
                    session = _entityMap.GetSession(sessionId);
                    break;

                default:
                    throw new FormatException($"Invalid InsertOneOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedInsertOneOperation(session, collection, document, options));
        }
Esempio n. 8
0
        public UnifiedCreateFindCursorOperation Build(string targetDatabaseId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetDatabaseId);

            IClientSessionHandle session = null;
            BsonDocument         filter  = null;
            var findOptions = new FindOptions <BsonDocument>();

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "batchSize":
                    findOptions.BatchSize = argument.Value.AsInt32;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.ToString());
                    break;

                default:
                    throw new FormatException($"Invalid {nameof(UnifiedCreateFindCursorOperation)} argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCreateFindCursorOperation(session, collection, filter, findOptions));
        }
        public UnifiedCreateChangeStreamOnCollectionOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            ChangeStreamOptions options = null;
            BsonDocumentStagePipelineDefinition <ChangeStreamDocument <BsonDocument>, ChangeStreamDocument <BsonDocument> > pipeline = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "batchSize":
                    options           = options ?? new ChangeStreamOptions();
                    options.BatchSize = argument.Value.AsInt32;
                    break;

                case "pipeline":
                    var stages = argument.Value.AsBsonArray.Cast <BsonDocument>();
                    pipeline = new BsonDocumentStagePipelineDefinition <ChangeStreamDocument <BsonDocument>, ChangeStreamDocument <BsonDocument> >(stages);
                    break;

                default:
                    throw new FormatException($"Invalid CreateChangeStreamOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCreateChangeStreamOnCollectionOperation(collection, pipeline, options));
        }
        public UnifiedCountDocumentsOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter = null;
            CountOptions         options           = null;
            IClientSessionHandle session           = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "comment":
                    options ??= new CountOptions();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid CountDocumentsOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCountDocumentsOperation(collection, filter, options, session));
        }
Esempio n. 11
0
        public UnifiedBulkWriteOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            BulkWriteOptions options = null;
            List <WriteModel <BsonDocument> > requests = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "ordered":
                    options           = options ?? new BulkWriteOptions();
                    options.IsOrdered = argument.Value.AsBoolean;
                    break;

                case "requests":
                    requests = ParseWriteModels(argument.Value.AsBsonArray.Cast <BsonDocument>());
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid BulkWriteOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedBulkWriteOperation(session, collection, requests, options));
        }
        public IUnifiedEntityTestOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            AggregateOptions options = null;
            PipelineDefinition <BsonDocument, BsonDocument> pipeline = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "pipeline":
                    var stages = argument.Value.AsBsonArray.Cast <BsonDocument>();
                    pipeline = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(stages);
                    break;

                default:
                    throw new FormatException($"Invalid AggregateOperation argument name: '{argument.Name}'.");
                }
            }

            if (pipeline.Stages.Last().OperatorName == "$out")
            {
                return(new UnifiedAggregateToCollectionOperation(collection, pipeline, options));
            }
            else
            {
                return(new UnifiedAggregateOnCollectionOperation(collection, pipeline, options));
            }
        }
        public UnifiedUpdateManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            UpdateOptions                   options = null;
            IClientSessionHandle            session = null;
            UpdateDefinition <BsonDocument> update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "update":
                    update = argument.Value.AsBsonDocument;
                    break;

                default:
                    throw new FormatException($"Invalid UpdateManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedUpdateManyOperation(session, collection, filter, update, options));
        }
        public UnifiedReplaceOneOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter = null;
            ReplaceOptions options     = null;
            BsonDocument   replacement = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "replacement":
                    replacement = argument.Value.AsBsonDocument;
                    break;

                case "upsert":
                    options          = options ?? new ReplaceOptions();
                    options.IsUpsert = argument.Value.AsBoolean;
                    break;

                default:
                    throw new FormatException($"Invalid ReplaceOneOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedReplaceOneOperation(collection, filter, replacement, options));
        }
Esempio n. 15
0
        public UnifiedListIndexesOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            var listIndexesOptions       = new ListIndexesOptions();
            IClientSessionHandle session = null;

            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    switch (argument.Name)
                    {
                    case "batchSize":
                        listIndexesOptions.BatchSize = argument.Value.ToInt32();
                        break;

                    case "session":
                        session = _entityMap.GetSession(argument.Value.AsString);
                        break;

                    default:
                        throw new FormatException($"Invalid {nameof(UnifiedListIndexesOperation)} argument name: '{argument.Name}'.");
                    }
                }
            }

            return(new UnifiedListIndexesOperation(collection, listIndexesOptions, session));
        }
Esempio n. 16
0
        public UnifiedUpdateManyOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            UpdateOptions                   options = null;
            IClientSessionHandle            session = null;
            UpdateDefinition <BsonDocument> update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "comment":
                    options ??= new UpdateOptions();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = argument.Value.AsBsonDocument;
                    break;

                case "hint":
                    options ??= new UpdateOptions();
                    options.Hint = argument.Value;
                    break;

                case "let":
                    options ??= new UpdateOptions();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "update":
                    switch (argument.Value)
                    {
                    case BsonDocument:
                        update = argument.Value.AsBsonDocument;
                        break;

                    case BsonArray:
                        update = PipelineDefinition <BsonDocument, BsonDocument> .Create(argument.Value.AsBsonArray.Cast <BsonDocument>());

                        break;

                    default:
                        throw new FormatException($"Invalid BulkWrite Update model update argument: '{argument.Value}'.");
                    }
                    break;

                default:
                    throw new FormatException($"Invalid UpdateManyOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedUpdateManyOperation(session, collection, filter, update, options));
        }
Esempio n. 17
0
        public UnifiedFindOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            FindOptions <BsonDocument>      options = null;
            IClientSessionHandle            session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "allowDiskUse":
                    options ??= new FindOptions <BsonDocument>();
                    options.AllowDiskUse = argument.Value.AsBoolean;
                    break;

                case "batchSize":
                    options ??= new FindOptions <BsonDocument>();
                    options.BatchSize = argument.Value.AsInt32;
                    break;

                case "comment":
                    options ??= new FindOptions <BsonDocument>();
                    options.Comment = argument.Value;
                    break;

                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "let":
                    options ??= new FindOptions <BsonDocument>();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "limit":
                    options ??= new FindOptions <BsonDocument>();
                    options.Limit = argument.Value.AsInt32;
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                case "sort":
                    options ??= new FindOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                default:
                    throw new FormatException($"Invalid FindOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOperation(collection, filter, session, options));
        }
Esempio n. 18
0
        public IUnifiedEntityTestOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            AggregateOptions options = null;
            PipelineDefinition <BsonDocument, BsonDocument> pipeline = null;
            IClientSessionHandle session = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "batchSize":
                    options ??= new AggregateOptions();
                    options.BatchSize = argument.Value.ToInt32();
                    break;

                case "let":
                    options ??= new AggregateOptions();
                    options.Let = argument.Value.AsBsonDocument;
                    break;

                case "pipeline":
                    var stages = argument.Value.AsBsonArray.Cast <BsonDocument>();
                    pipeline = new BsonDocumentStagePipelineDefinition <BsonDocument, BsonDocument>(stages);
                    break;

                case "session":
                    session = _entityMap.GetSession(argument.Value.AsString);
                    break;

                default:
                    throw new FormatException($"Invalid AggregateOperation argument name: '{argument.Name}'.");
                }
            }

            var lastStageName = pipeline.Stages.LastOrDefault()?.OperatorName;

            if (lastStageName == "$out" || lastStageName == "$merge")
            {
                return(new UnifiedAggregateToCollectionOperation(collection, pipeline, options));
            }
            else
            {
                return(new UnifiedAggregateOnCollectionOperation(collection, pipeline, options, session));
            }
        }
        public UnifiedCountDocumentsOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter = null;
            CountOptions options = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                default:
                    throw new FormatException($"Invalid CountDocumentsOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedCountDocumentsOperation(collection, filter, options));
        }
Esempio n. 20
0
        public UnifiedRenameCollectionOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var    collection = _entityMap.GetCollection(targetCollectionId);
            var    database   = collection.Database;
            var    oldName    = collection.CollectionNamespace.CollectionName;
            string newName    = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "to":
                    newName = argument.Value.AsString;
                    break;

                default:
                    throw new FormatException($"Invalid RenameCollectionOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedRenameCollectionOperation(database, oldName, newName));
        }
Esempio n. 21
0
        public UnifiedFindOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument> filter  = null;
            FindOptions <BsonDocument>      options = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "batchSize":
                    options           = options ?? new FindOptions <BsonDocument>();
                    options.BatchSize = argument.Value.AsInt32;
                    break;

                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "limit":
                    options       = options ?? new FindOptions <BsonDocument>();
                    options.Limit = argument.Value.AsInt32;
                    break;

                case "sort":
                    options      = options ?? new FindOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                default:
                    throw new FormatException($"Invalid FindOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOperation(collection, filter, options));
        }
        public UnifiedFindOneAndUpdateOperation Build(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            FilterDefinition <BsonDocument>        filter  = null;
            FindOneAndUpdateOptions <BsonDocument> options = null;
            UpdateDefinition <BsonDocument>        update  = null;

            foreach (var argument in arguments)
            {
                switch (argument.Name)
                {
                case "filter":
                    filter = new BsonDocumentFilterDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "returnDocument":
                    options = options ?? new FindOneAndUpdateOptions <BsonDocument>();
                    options.ReturnDocument = (ReturnDocument)Enum.Parse(typeof(ReturnDocument), argument.Value.AsString);
                    break;

                case "sort":
                    options      = options ?? new FindOneAndUpdateOptions <BsonDocument>();
                    options.Sort = new BsonDocumentSortDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                case "update":
                    update = new BsonDocumentUpdateDefinition <BsonDocument>(argument.Value.AsBsonDocument);
                    break;

                default:
                    throw new FormatException($"Invalid FindOneAndUpdateOperation argument name: '{argument.Name}'.");
                }
            }

            return(new UnifiedFindOneAndUpdateOperation(collection, filter, update, options));
        }
        public IUnifiedEntityTestOperation BuildCollectionOperation(string targetCollectionId, BsonDocument arguments)
        {
            var collection = _entityMap.GetCollection(targetCollectionId);

            return(Build(collection.Database, collection, arguments));
        }