private void DeleteByKey <T>(string key, SessionWrapper session) where T : IExpirableWithKey
 {
     session.CreateQuery(SqlUtil.DeleteByKeyStatementDictionary[typeof(T)])
     .SetParameter(SqlUtil.ValueParameterName, key)
     .ExecuteUpdate();
     session.Flush();
 }
Exemplo n.º 2
0
        /// <summary>
        ///     delete entities that implement IInt32Id, by using the value stored in their Id property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session">Sessionwrapper instance to act upon</param>
        /// <param name="ids">Collection of ids to delete</param>
        /// <returns>the number of rows deleted</returns>
        public static long DeleteByInt32Id <T>(this SessionWrapper session, ICollection <int> ids) where T : IInt32Id
        {
            if (!ids.Any())
            {
                return(0);
            }

            string queryString;

            lock (GenerateStatementMutex)
            {
                var typeName = typeof(T);
                if (DeleteByIdCommands.ContainsKey(typeName))
                {
                    queryString = DeleteByIdCommands[typeName];
                }
                else
                {
                    queryString = string.Format("delete from {0} where {1} in (:{2})", typeName.Name.WrapObjectName(),
                                                nameof(IInt32Id.Id).WrapObjectName(), IdParameterName);
                    DeleteByIdCommands[typeName] = queryString;
                }
            }


            var query = session.CreateQuery(queryString);
            int count = 0;

            for (var i = 0; i < ids.Count; i += DeleteBatchSize)
            {
                var batch = ids.Skip(i).Take(DeleteBatchSize).ToList();
                count += query.SetParameterList(IdParameterName, batch).ExecuteUpdate();
            }
            return(count);
        }
Exemplo n.º 3
0
 public LockResourceParams(SessionWrapper session, string resource, TimeSpan timeout)
 {
     Resource        = resource;
     CreatedAt       = session.Storage.UtcNow;
     ExpireAtAsLong  = CreatedAt.Add(timeout).ToUnixDate();
     CreatedAtAsLong = CreatedAt.ToUnixDate();
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Delete entities that implement iexpirablewithid and have expired based on server's system date
        /// </summary>
        /// <typeparam name="T">The type of entity</typeparam>
        /// <param name="session">Sessionwrapper instance to act upon</param>
        /// <returns></returns>
        internal static long DeleteExpirableWithId <T>(this SessionWrapper session) where T : IExpirableWithId

        {
            var ids = session.Query <T>()
                      .Where(i => i.ExpireAt < session.Storage.UtcNow)
                      .Select(i => i.Id)
                      .ToList();

            return(session.DeleteByInt32Id <T>(ids));
        }
        private void SetExpireAt <T>(string key, DateTime?expire, SessionWrapper session) where T : IExpirableWithKey
        {
            var queryString = SqlUtil.SetExpireAtByKeyStatementDictionary[typeof(T)];

            session.CreateQuery(queryString)
            .SetParameter(SqlUtil.ValueParameterName, expire)
            .SetParameter(SqlUtil.IdParameterName, key)
            .ExecuteUpdate();
            session.Flush();
        }
Exemplo n.º 6
0
        public static void DoActionByExpression <T>(this SessionWrapper session, Expression <Func <T, bool> > expr,
                                                    Action <T> action)
            where T : IExpirableWithId
        {
            var entities = session.Query <T>().Where(expr);

            foreach (var entity in entities)
            {
                action(entity);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Delete entities that implement iexpirablewithid and have expired based on server's system date
        /// </summary>
        /// <typeparam name="T">The type of entity</typeparam>
        /// <param name="session">Sessionwrapper instance to act upon</param>
        /// <param name="cutOffDate"></param>
        /// <returns></returns>
        internal static long DeleteExpirableWithId <T>(this SessionWrapper session, DateTime cutOffDate)
            where T : IExpirableWithId

        {
            var ids = session.Query <T>()
                      .Where(i => i.ExpireAt < cutOffDate)
                      .Select(i => i.Id)
                      .ToList();

            return(session.DeleteByInt32Id <T>(ids));
        }
Exemplo n.º 8
0
        /// <summary>
        /// do an upsert into a table
        /// </summary>
        /// <typeparam name="T">The entity type to upsert</typeparam>
        /// <param name="session">a SessionWrapper instance to act upon</param>
        /// <param name="matchFunc">A function that returns a single instance of T</param>
        /// <param name="changeAction">A delegate that changes specified properties of instance of T </param>
        /// <param name="keysetAction">A delegate that sets the primary key properties of instance of T if we have to do an upsert</param>
        public static void UpsertEntity <T>(this SessionWrapper session, Expression <Func <T, bool> > matchFunc,
                                            Action <T> changeAction,
                                            Action <T> keysetAction) where T : new()
        {
            var entity = session.Query <T>().SingleOrDefault(matchFunc);

            if (entity == null)
            {
                entity = new T();
                keysetAction(entity);
                changeAction(entity);
                session.Insert(entity);
            }
            else
            {
                changeAction(entity);
                session.Update(entity);
            }
            session.Flush();
        }
Exemplo n.º 9
0
        /// <summary>
        /// delete entities that implement IInt64Id, by using the value stored in their Id property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session">Sessionwrapper instance to act upon</param>
        /// <param name="id">Collection of ids to delete</param>
        /// <returns>the number of rows deleted</returns>
        public static long DeleteByInt32Id <T>(this SessionWrapper session, ICollection <int> id) where T : IInt32Id
        {
            if (!id.Any())
            {
                return(0);
            }
            string queryString;

            lock (GenerateStatementMutex)
            {
                var typeName = typeof(T);
                if (DeleteByIdCommands.ContainsKey(typeName))
                {
                    queryString = DeleteByIdCommands[typeName];
                }
                else
                {
                    queryString = string.Format("delete from {0} where {1} in (:{2})", typeName.Name.WrapObjectName(),
                                                nameof(IInt32Id.Id).WrapObjectName(), IdParameterName);
                    DeleteByIdCommands[typeName] = queryString;
                }
            }
            return(session.CreateQuery(queryString).SetParameterList(IdParameterName, id).ExecuteUpdate());
        }
Exemplo n.º 10
0
        internal static long DeleteExpirableWithId <T>(SessionWrapper session) where T : IExpirableWithId

        {
            return(session.DeleteExpirableWithId <T>(NumberOfRecordsInSinglePass));
        }
Exemplo n.º 11
0
        internal static long DeleteExpirableWithId <T>(SessionWrapper session) where T : IExpirableWithId

        {
            return(session.DeleteExpirableWithId <T>());
        }