Beispiel #1
0
        public override Core.UpdateResult Update(T entity, UpdateMode updateMode)
        {
            if (updateMode == UpdateMode.db_implementation)
            {
                var updateDate = NoSQLRepoHelper.DateTimeUtcNow();

                entity.SystemLastUpdateDate = updateDate;

                var filter = Builders <T> .Filter.Eq(s => s.Id, entity.Id);

                ReplaceOneResult res = collection.ReplaceOne(filter, entity, new UpdateOptions {
                    IsUpsert = false
                });

                if (!res.IsAcknowledged)
                {
                    throw new QueryNotAcknowledgedException();
                }

                if (entity.Id == null)
                {
                    throw new ArgumentException("Cannot update an entity with a null field value");
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            return(Core.UpdateResult.updated);
        }
Beispiel #2
0
        public override BulkInsertResult <string> InsertMany(IEnumerable <T> entities, InsertMode insertMode)
        {
            if (insertMode != InsertMode.db_implementation)
            {
                throw new NotImplementedException();
            }

            var insertResult = new BulkInsertResult <string>();

            var creationDate = NoSQLRepoHelper.DateTimeUtcNow();

            foreach (var entity in entities)
            {
                if (AutoGeneratedEntityDate)
                {
                    entity.SystemCreationDate = creationDate;
                }
                if (AutoGeneratedEntityDate)
                {
                    entity.SystemLastUpdateDate = creationDate;
                }

                NoSQLRepoHelper.SetIds(entity);

                var entityToStore = NewtonJsonHelper.CloneJson(entity, DateTimeZoneHandling.Utc);
                localDb[entity.Id] = entityToStore;
                config.ExpireAt(entity.Id, null);
                insertResult[entity.Id] = InsertResult.unknown;
            }
            SaveJSONFile();

            return(insertResult);
        }
        public override UpdateResult Update(T entity, UpdateMode updateMode)
        {
            if (entity.Id == null)
            {
                throw new ArgumentException("Cannot update an entity with a null field value");
            }

            var updateResult = default(UpdateResult);

            if (updateMode == UpdateMode.db_implementation)
            {
                var idDocument = GetInternalCBLId(entity.Id);
                var updateDate = NoSQLRepoHelper.DateTimeUtcNow();

                var documentObjet = database.GetDocument(idDocument);

                documentObjet.Update((IUnsavedRevision newRevision) =>
                {
                    var properties            = newRevision.Properties;
                    properties["update date"] = updateDate;
                    properties["members"]     = entity;
                    return(true);
                });
                updateResult = UpdateResult.updated;
            }
            else
            {
                throw new NotImplementedException();
            }
            return(updateResult);
        }
        public override UpdateResult Update(T entity, UpdateMode updateMode)
        {
            CheckOpenedConnection();

            if (updateMode != UpdateMode.db_implementation)
            {
                throw new NotImplementedException();
            }

            using (var document = database.GetDocument(entity.Id))
            {
                if (document == null)
                {
                    throw new KeyNotFoundNoSQLException();
                }

                using (var mutabledocument = document.ToMutable())
                {
                    // Update update date
                    var date = NoSQLRepoHelper.DateTimeUtcNow();
                    entity.SystemLastUpdateDate = date;

                    SetDocument(entity, mutabledocument);

                    database.Save(mutabledocument);
                    return(UpdateResult.updated);
                }
            }
        }
        public override UpdateResult Update(T entity, UpdateMode updateMode)
        {
            CheckOpenedConnection();

            if (updateMode == UpdateMode.upsert_if_missing_key)
            {
                throw new NotImplementedException();
            }

            // Update update date
            var date          = NoSQLRepoHelper.DateTimeUtcNow();
            var entityToStore = entity;

            entityToStore.SystemLastUpdateDate = date;

            if (!localDb.ContainsKey(entity.Id))
            {
                if (updateMode == UpdateMode.error_if_missing_key)
                {
                    throw new KeyNotFoundNoSQLException("Misssing key '" + entity.Id + "'");
                }
                else if (updateMode == UpdateMode.do_nothing_if_missing_key)
                {
                    return(UpdateResult.not_affected);
                }
            }

            localDb[entity.Id] = entityToStore;

            config.ExpireAt(entity.Id, null);

            SaveJSONFile();

            return(UpdateResult.updated);
        }
        public override InsertResult InsertOne(T entity, InsertMode insertMode)
        {
            CheckOpenedConnection();

            var entitydomain = entity;

            NoSQLRepoHelper.SetIds(entitydomain);

            var updateddate = NoSQLRepoHelper.DateTimeUtcNow();
            var createdDate = NoSQLRepoHelper.DateTimeUtcNow();

            if (!string.IsNullOrEmpty(entity.Id) && localDb.ContainsKey(entity.Id))
            {
                // Document already exists
                switch (insertMode)
                {
                case InsertMode.error_if_key_exists:
                    throw new DupplicateKeyNoSQLException();

                case InsertMode.erase_existing:
                    createdDate = localDb[entity.Id].SystemCreationDate;
                    localDb.Remove(entity.Id);
                    break;

                case InsertMode.do_nothing_if_key_exists:
                    return(InsertResult.not_affected);

                default:
                    break;
                }
            }

            if (AutoGeneratedEntityDate)
            {
                entitydomain.SystemCreationDate   = createdDate;
                entitydomain.SystemLastUpdateDate = updateddate;
            }

            // Clone to not shared reference between App instance and repository persisted value
            // UTC : Ensure to store only utc datetime
            var entityToStore = NewtonJsonHelper.CloneJson(entitydomain, DateTimeZoneHandling.Utc);

            if (localDb.ContainsKey(entity.Id))
            {
                localDb[entity.Id] = entityToStore;
            }
            else
            {
                localDb.Add(entity.Id, entityToStore);
            }

            config.ExpireAt(entity.Id, null);

            SaveJSONFile();

            return(InsertResult.inserted);
        }
        public override InsertResult InsertOne(T entity, InsertMode insertMode)
        {
            CheckOpenedConnection();

            var createdDate = NoSQLRepoHelper.DateTimeUtcNow();
            var updateddate = NoSQLRepoHelper.DateTimeUtcNow();

            MutableDocument mutabledocument;

            if (!string.IsNullOrEmpty(entity.Id))
            {
                // Get an existing document or return a new one if not exists
                var document = database.GetDocument(entity.Id);
                if (document != null)
                {
                    // Document already exists
                    switch (insertMode)
                    {
                    case InsertMode.error_if_key_exists:
                        throw new DupplicateKeyNoSQLException();

                    case InsertMode.erase_existing:
                        createdDate = document.GetDate("SystemCreationDate");
                        database.Delete(document);
                        break;

                    case InsertMode.do_nothing_if_key_exists:
                        return(InsertResult.not_affected);

                    default:
                        break;
                    }
                }

                mutabledocument = new MutableDocument(entity.Id);
            }
            else
            {
                mutabledocument = new MutableDocument();
            }

            // Normally, at this point, the document is deleted from database or an exception occured.
            // We can insert the new document :
            mutabledocument.SetString("collection", CollectionName);

            entity.SystemCreationDate   = createdDate;
            entity.SystemLastUpdateDate = updateddate;
            entity.Id = mutabledocument.Id;

            SetDocument(entity, mutabledocument);

            database.Save(mutabledocument);

            return(InsertResult.inserted);
        }
Beispiel #8
0
        public override InsertResult InsertOne(T entity, InsertMode insertMode)
        {
            if (insertMode == InsertMode.erase_existing && !string.IsNullOrWhiteSpace(entity.Id) && Exist(entity.Id))
            {
                var existingEntity = GetById(entity.Id);

                entity.SystemCreationDate = existingEntity.SystemCreationDate;

                Delete(existingEntity.Id, true);
            }
            try
            {
                try
                {
                    var date = NoSQLRepoHelper.DateTimeUtcNow();
                    if (AutoGeneratedEntityDate)
                    {
                        if (insertMode != InsertMode.erase_existing)
                        {
                            entity.SystemCreationDate = date;
                        }

                        entity.SystemLastUpdateDate = date;
                    }

                    collection.InsertOne(entity);
                    return(InsertResult.inserted);
                }
                catch (AggregateException e)
                {
                    throw e.InnerException;
                }
            }
            catch (MongoWriteException e)
            {
                if (e.WriteError.Category == ServerErrorCategory.DuplicateKey && insertMode == InsertMode.error_if_key_exists)
                {
                    throw new DupplicateKeyNoSQLException("The key '" + entity.Id + "' already exists", e);
                }
                if (e.WriteError.Category == ServerErrorCategory.DuplicateKey && insertMode == InsertMode.do_nothing_if_key_exists)
                {
                    return(InsertResult.not_affected);
                }
                else if (e.WriteError.Category == ServerErrorCategory.DuplicateKey)
                {
                    return(InsertResult.duplicate_key_exception);
                }
                else
                {
                    throw;
                }
            }
        }
Beispiel #9
0
        public override InsertResult InsertOne(T entity, InsertMode keyExistsAction)
        {
            var insertResult = default(InsertResult);

            var date = NoSQLRepoHelper.DateTimeUtcNow();

            if (AutoGeneratedEntityDate)
            {
                entity.SystemCreationDate   = date;
                entity.SystemLastUpdateDate = date;
            }

            NoSQLRepoHelper.SetIds(entity);

            if (localDb.ContainsKey(entity.Id))
            {
                if (keyExistsAction == InsertMode.error_if_key_exists)
                {
                    throw new DupplicateKeyNoSQLException();
                }
                else if (keyExistsAction == InsertMode.do_nothing_if_key_exists)
                {
                    return(InsertResult.not_affected);
                }
                else if (keyExistsAction == InsertMode.erase_existing)
                {
                    entity.SystemCreationDate = localDb[entity.Id].SystemCreationDate; // keep the origin creation date of the entity
                    // Continue execution
                }
                insertResult = InsertResult.updated;
            }
            else
            {
                insertResult = InsertResult.inserted;
            }

            // Clone to not shared reference between App instance and repository persisted value
            // UTC : Ensure to store only utc datetime
            var entityToStore = NewtonJsonHelper.CloneJson(entity, DateTimeZoneHandling.Utc);

            localDb[entity.Id] = entityToStore;
            config.ExpireAt(entity.Id, null);

            SaveJSONFile();
            return(insertResult);
        }
Beispiel #10
0
        public override UpdateResult Update(T entity, UpdateMode updateMode)
        {
            var updateResult = default(UpdateResult);

            if (updateMode == UpdateMode.upsert_if_missing_key)
            {
                throw new NotImplementedException();
            }

            var updateDate = NoSQLRepoHelper.DateTimeUtcNow();

            entity.SystemLastUpdateDate = updateDate;

            if (updateMode == UpdateMode.upsert_if_missing_key)
            {
                NoSQLRepoHelper.SetIds(entity);
                entity.SystemCreationDate = updateDate;
            }

            if (!localDb.ContainsKey(entity.Id))
            {
                if (updateMode == UpdateMode.error_if_missing_key)
                {
                    throw new KeyNotFoundNoSQLException("Misssing key '" + entity.Id + "'");
                }
                else if (updateMode == UpdateMode.do_nothing_if_missing_key)
                {
                    return(UpdateResult.not_affected);
                }

                updateResult = UpdateResult.inserted;
            }

            localDb[entity.Id] = entity;

            SaveJSONFile();
            updateResult = UpdateResult.updated;

            return(updateResult);
        }
        public override InsertResult InsertOne(T entity, InsertMode insertMode)
        {
            var       insertResult          = default(InsertResult);
            bool      documentAlreadyExists = false;
            IDocument documentObjet         = null;

            var date = NoSQLRepoHelper.DateTimeUtcNow();
            IDictionary <string, object> properties;

            if (string.IsNullOrEmpty(entity.Id))
            {
                // No id specified, let CouchBaseLite generate the id and affect it to the entity
                documentObjet = database.CreateDocument();
                entity.Id     = cblGeneratedIdPrefix + documentObjet.Id; // NB: prefix the Id generated by CouchBaseLite to be able to distinguish it with a user provided id
            }
            else
            {
                // Get an existing document or return a new one if not exists
                documentObjet = database.GetDocument(GetInternalCBLId(entity.Id));

                if (documentObjet.CurrentRevisionId != null)
                {
                    // Document already exists
                    if (insertMode == InsertMode.error_if_key_exists)
                    {
                        throw new DupplicateKeyNoSQLException();
                    }
                    else if (insertMode == InsertMode.do_nothing_if_key_exists)
                    {
                        return(InsertResult.not_affected);
                    }

                    documentAlreadyExists = true;
                }
            }

            if (!documentAlreadyExists)
            {
                if (AutoGeneratedEntityDate)
                {
                    entity.SystemCreationDate   = date;
                    entity.SystemLastUpdateDate = date;
                }

                properties = new Dictionary <string, object>()
                {
                    { "creat date", date },
                    { "update date", date },
                    { "collection", this.CollectionName },
                    { "members", entity },
                    { "entityType", entity.GetType().Name } // Store the original actual object class to handle polymorphism
                };

                insertResult = InsertResult.inserted;
            }
            else
            {
                properties = documentObjet.Properties;

                entity.SystemCreationDate   = (DateTime)properties["creat date"];
                entity.SystemLastUpdateDate = date;

                properties["update date"] = entity.SystemLastUpdateDate;
                properties["members"]     = entity;

                insertResult = InsertResult.updated;
            }

            documentObjet.PutProperties(properties);

            return(insertResult);
        }