public T BulkUpdate <T>(IEnumerable <T> ts)
        {
            Write();
            DapperPlusActionSet <T> result = connection.BulkUpdate <T>(ts);

            return(result.CurrentItem);
        }
Example #2
0
 protected void UpdateMany(List <T> item)
 {
     DapperPlusManager.Entity <T>().Table(typeof(T).Name);
     using (IDbConnection db = CreateConnection(_connectionString))
     {
         db.BulkUpdate(item);
     }
 }
Example #3
0
        /// <summary>
        /// Cập nhật bản ghi
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>số bản ghi</returns>
        /// createdby ngochtb(02/12/2020)
        public int Update(TEntity entity)
        {
            //// Khởi tạo kết nối với Db:
            //var parameters = MappingDbType(entity);
            //// Thực thi commandText:
            //var rowAffects = _dbConnection.Execute($"Proc_Update{_tableName}", parameters, commandType: CommandType.StoredProcedure);
            //// Trả về kết quả (số bản ghi thêm mới được)
            var rowAffects = _dbConnection.BulkUpdate(entity).Actions.Count();

            return(rowAffects);
        }
Example #4
0
        public static int BulkUpdate <TEntity>(IDbConnection db, TEntity updateData,
                                               Action <IConditionalBulkSqlStatementOptionsBuilder <TEntity> > statementOptions = null)
        {
            // using (Sync.Lock())
            {
                try
                {
                    return(db.BulkUpdate(updateData, statementOptions));
                }
                catch (Exception e)
                {
                    Logger.Error($"DBError, {e}");
                }

                return(-1);
            }
        }
Example #5
0
        /// <summary>
        ///  This saves the holiday in the office.
        /// </summary>
        /// <param name="currentOffice">Current office to be saved.</param>
        /// <param name="holidayDto">List of vacation for the current office.</param>
        /// <returns>return a task for saving the holidays</returns>
        private async Task SaveHolidayOfficeAsync(IDbConnection connection, OFICINAS currentOffice, IEnumerable <HolidayDto> holidayDto)
        {
            Contract.Requires(connection != null, "Connection is not null");
            Contract.Requires(currentOffice != null, "Current office is not null");
            Contract.Requires(holidayDto != null, "HolidayDto is not null");

            IEnumerable <FESTIVOS_OFICINA> holidayOffice = _mapper.Map <IEnumerable <HolidayDto>, IEnumerable <FESTIVOS_OFICINA> >(holidayDto);
            IQueryStore store = _queryStoreFactory.GetQueryStore();

            store.AddParam(QueryType.HolidaysByOffice, currentOffice.CODIGO);
            var  query = store.BuildQuery();
            bool saved = false;

            // First i want fetch the current festivo oficina.
            // we shall insert or merge.
            try
            {
                IEnumerable <FESTIVOS_OFICINA> currentHolidays = await connection.QueryAsync <FESTIVOS_OFICINA>(query);

                if (currentHolidays.Count <FESTIVOS_OFICINA>() == 0)
                {
                    connection.BulkInsert(holidayOffice);
                }
                else
                {
                    // FIXME : check for concurrent optimistic lock.
                    var holidaysToBeInserted = holidayOffice.Except(currentHolidays);
                    connection.BulkInsert <FESTIVOS_OFICINA>(holidaysToBeInserted);
                    var holidaysToBeUpdated = holidayOffice.Intersect(currentHolidays);
                    connection.BulkUpdate <FESTIVOS_OFICINA>(holidaysToBeUpdated);
                }
                saved = true;
            }
            catch (System.Exception e)
            {
                connection.Close();
                connection.Dispose();
                throw new DataLayerException(e.Message, e);
            }
            Contract.Ensures(saved);
        }
        internal void Save(IDbConnection db)
        {
            foreach (var pair in _settings)
            {
                var name    = pair.Key;
                var setting = pair.Value;
                if (!setting.ExistsInDatabase)
                {
                    db.Insert(new PlayerSettingDto
                    {
                        PlayerId = (int)Player.Account.Id,
                        Setting  = name,
                        Value    = GetString(name, setting.Data)
                    });
                    setting.ExistsInDatabase = true;
                }
                else
                {
                    if (!setting.NeedsToSave)
                    {
                        continue;
                    }

                    db.BulkUpdate(new PlayerSettingDto
                    {
                        PlayerId = (int)Player.Account.Id,
                        Setting  = name,
                        Value    = GetString(name, setting.Data)
                    },
                                  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;
                }
            }
        }
Example #7
0
        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);
                }
                db.BulkUpdate(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())
            {
                db.BulkUpdate(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())
            {
                db.BulkUpdate(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;
                }
            }
        }
Example #8
0
 /// <summary>
 ///     An IDbConnection extension method to UPDATE entities in a database table or a view.
 /// </summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="connection">The connection to act on.</param>
 /// <param name="items">items to update.</param>
 /// <param name="selectors">The selection of entities to update.</param>
 /// <returns>A DapperPlusActionSet&lt;T&gt;</returns>
 public static DapperPlusActionSet <T> BulkUpdate <T>(this IDbConnection connection, IEnumerable <T> items, params Func <T, object>[] selectors)
 {
     return(connection.BulkUpdate(null, items, selectors));
 }
Example #9
0
 /// <summary>
 ///     An IDbConnection extension method to UPDATE entities in a database table or a view.
 /// </summary>
 /// <typeparam name="T">Generic type parameter.</typeparam>
 /// <param name="connection">The connection to act on.</param>
 /// <param name="items">items to update.</param>
 /// <returns>A DapperPlusActionSet&lt;T&gt;</returns>
 public static DapperPlusActionSet <T> BulkUpdate <T>(this IDbConnection connection, params T[] items)
 {
     return(connection.BulkUpdate <T>(null, items));
 }