コード例 #1
0
        internal void Save(IDbConnection db)
        {
            foreach (var pair in _settings)
            {
                var name    = pair.Key;
                var setting = pair.Value;
                if (!setting.ExistsInDatabase)
                {
                    DbUtil.Insert(db, new PlayerSettingDto
                    {
                        PlayerId = (int)Player.Account.Id,
                        Setting  = name,
                        Value    = GetString(name, setting.Data)
                    });
                    setting.ExistsInDatabase = true;
                }
                else
                {
                    if (!setting.NeedsToSave)
                    {
                        continue;
                    }

                    var dto = new PlayerSettingDto
                    {
                        PlayerId = (int)Player.Account.Id,
                        Setting  = name,
                        Value    = GetString(name, setting.Data)
                    };

                    DbUtil.BulkUpdate(db,
                                      dto,
                                      statement => statement
                                      .Where(
                                          $"{nameof(PlayerSettingDto.PlayerId):C} = @PlayerId AND {nameof(PlayerSettingDto.Setting):C} = @Setting")
                                      .WithParameters(new { PlayerId = (int)Player.Account.Id, Setting = name }));
                    setting.NeedsToSave = false;
                }
            }
        }
コード例 #2
0
ファイル: Mailbox.cs プロジェクト: Lion1616/ProjectP
        internal void Save(IDbConnection db)
        {
            var deleteMapping = OrmConfiguration
                                .GetDefaultEntityMapping <PlayerMailDto>()
                                .Clone()
                                .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true,
                                                           nameof(PlayerMailDto.IsMailDeleted));

            if (!_mailsToDelete.IsEmpty)
            {
                var idsToRemove = new StringBuilder();
                var firstRun    = true;
                while (_mailsToDelete.TryPop(out var mailToDelete))
                {
                    if (firstRun)
                    {
                        firstRun = false;
                    }
                    else
                    {
                        idsToRemove.Append(',');
                    }
                    idsToRemove.Append(mailToDelete.Id);
                }

                DbUtil.BulkUpdate(db, new PlayerMailDto {
                    IsMailDeleted = true
                }, statement => statement
                                  .Where($"{nameof(PlayerMailDto.Id):C} IN ({idsToRemove})")
                                  .WithEntityMappingOverride(deleteMapping));
            }

            var isNewMapping = OrmConfiguration
                               .GetDefaultEntityMapping <PlayerMailDto>()
                               .Clone()
                               .UpdatePropertiesExcluding(prop => prop.IsExcludedFromUpdates = true,
                                                          nameof(PlayerMailDto.IsMailNew));

            var needsSave = _mails.Values.Where(mail => mail.NeedsToSave).ToArray();
            var isNew     = needsSave.Where(mail => mail.IsNew);
            var isNotNew  = needsSave.Where(mail => !mail.IsNew);

            var enumerable = isNew as Mail[] ?? isNew.ToArray();

            if (enumerable.Any())
            {
                DbUtil.BulkUpdate(db, new PlayerMailDto {
                    IsMailNew = true
                }, statement => statement
                                  .Where($"{nameof(PlayerMailDto.Id):C} IN ({string.Join(",", enumerable.Select(x => x.Id))})")
                                  .WithEntityMappingOverride(isNewMapping));

                foreach (var mail in enumerable)
                {
                    mail.NeedsToSave = false;
                }
            }

            var notNew = isNotNew as Mail[] ?? isNotNew.ToArray();

            if (notNew.Any())
            {
                DbUtil.BulkUpdate(db, new PlayerMailDto {
                    IsMailNew = false
                }, statement => statement
                                  .Where($"{nameof(PlayerMailDto.Id):C} IN ({string.Join(",", notNew.Select(x => x.Id))})")
                                  .WithEntityMappingOverride(isNewMapping));

                foreach (var mail in notNew)
                {
                    mail.NeedsToSave = false;
                }
            }
        }